Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cumsum and friends don't support tuples #26690

Closed
ssfrr opened this issue Apr 3, 2018 · 6 comments · Fixed by #34654
Closed

cumsum and friends don't support tuples #26690

ssfrr opened this issue Apr 3, 2018 · 6 comments · Fixed by #34654

Comments

@ssfrr
Copy link
Contributor

ssfrr commented Apr 3, 2018

cumsum(x), cumprod(x) and diff(x) (maybe others?) don't have methods defined for tuples. vec also doesn't have a tuple method so it can't be used to convert (though if cumsum(::Tuple) were implemented I'd expect it to also return a tuple)

@stevengj
Copy link
Member

stevengj commented Apr 3, 2018

You can use collect to convert a tuple to an array, e.g. cumsum(collect(x)) where x is a tuple.

See also #25766.

@KristofferC
Copy link
Member

Tuples are not AbstractArrays so perhaps better to use StaticArrays.jl for this?

@mbauman
Copy link
Member

mbauman commented Apr 3, 2018

On #25766 cumsum((1,2,3)) returns an Array [1,3,6], so that's not quite what I'd want here.

I've thought before that (1, cumprod(front(size(A)))...) would be helpful just for pseudocode in expressing stride computations. That's a case where you want to use and get back tuples… and you want a highly optimized tuple-specific implementation to make it fast.

@bramtayl
Copy link
Contributor

bramtayl commented Apr 3, 2018

Are all the items in the tuple the same type? If they aren't and the tuple is short you might be better of going with recursion? I could make an accumulate_unrolled function in RecurUnrolled

@ssfrr
Copy link
Contributor Author

ssfrr commented Apr 3, 2018

@stevengj thanks for that suggestion, collect is definitely more concise than what I did (using an array comprehension)

@KristofferC

Tuples are not AbstractArrays so perhaps better to use StaticArrays.jl for this?

For me this came up in the context of wanting to do something like foo(xs...) = cumsum(xs), and wanting a tuple output, so it would be more convenient to stay in tuple-land.

@tkf
Copy link
Member

tkf commented Feb 4, 2020

I implemented accumulate(op, xs::Tuple) in #34654.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants