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

Make cumsum(A) compute a summed-area table #26412

Open
yurivish opened this issue Mar 10, 2018 · 8 comments
Open

Make cumsum(A) compute a summed-area table #26412

yurivish opened this issue Mar 10, 2018 · 8 comments
Labels
domain:arrays [a, r, r, a, y, s] good first issue Indicates a good issue for first-time contributors to Julia

Comments

@yurivish
Copy link
Contributor

yurivish commented Mar 10, 2018

The idea has been discussed in a few places (e.g. #20041 (comment)) to make cumsum(A) return a summed-area table for multidimensional arrays.

The 1D case is already a summed-area table, since that's just the cumulative sum along the one dimension.

Happily, the appropriate deprecation for cumsum — deprecating it for AbstractArrays with no dims argument — is already in place, so this is just a tracking issue for the idea.

cc: @simonbyrne

@mbauman mbauman added domain:arrays [a, r, r, a, y, s] good first issue Indicates a good issue for first-time contributors to Julia labels Mar 10, 2018
@mbauman
Copy link
Sponsor Member

mbauman commented Mar 10, 2018

More than just cumsumcumprod, accumulate and maybe diff should be considered at the same time, too. Any others?

@simonbyrne
Copy link
Contributor

We should at least support it when dims is passed a tuple.

@bhvieira
Copy link

bhvieira commented May 31, 2018

@yurivish Something like this?

A = [31 2;12 26;13 17]

import Base.cumsum

function cumsum(A::AbstractArray, dims::Tuple{Int})
    out = copy(A)
    for i in dims
        cumsum!(out, out, i)
    end
    return out
end

cumsum(A, (1,2))
#=
3×2 Array{Int64,2}:
 31   33
 43   71
 56  101
=#

#Expected behavior is consistent
cumsum(A, (1,2)) == cumsum(cumsum(A, 1), 2)
#true

#Compatibility with the basic definition is guaranteed
cumsum(A, 1) == cumsum(A, (1,))
#true
cumsum(A, 2) == cumsum(A, (2,))
#true

It works even for subsets of 1:ndims(A) just as well

A = cat(3, A, A, A)
cumsum(A, (1,2))
#=3×2×3 Array{Int64,3}:
[:, :, 1] =
 31   33
 43   71
 56  101

[:, :, 2] =
 31   33
 43   71
 56  101

[:, :, 3] =
 31   33
 43   71
 56  101=#

I can work this into a PR. Just need some time to get acquainted to JuliaLang PR policies, so devs can assign me this if you want.

@StefanKarpinski
Copy link
Sponsor Member

GitHub only allows assignment to members of an organization, but you can certainly work on this if you like. Keep in mind that one will want to compute the summed area table in an order which is cache-friendly (i.e. compute along columns first for dense arrays).

@martinholters
Copy link
Member

I think that definition of cumsum is nice because

  • it generalizes well to accumulate because it defines precisely what should happen for a non-commutative function argument
  • it even has a defined behavior for dimensions occurring more than once in dims.

@StefanKarpinski
Copy link
Sponsor Member

Yes, it does seem like a nice definition. If I'm understanding correctly, it's compatible with computing a summed area table as well, assuming that the default is cumsum(A, dims=1:ndims(A)). With perfectly commutative arithmetic, the order of summation doesn't matter so we can optimize it to choose the best order for summing the dimensions, but now that I'm thinking about it based on this observation you're doing the same set of summation operations no matter what so maybe that doesn't actually matter.

@ChamanAgrawal
Copy link

Hi, If the order of summation doesn't matter then I think simply calculating the cumulative sum will be good, right? If nobody is working on this I would like to contribute.
P.S.: I am new to Julia(motivated by the concept of this lang) so any beginners advice?

@StefanKarpinski
Copy link
Sponsor Member

No, I would say just try it and ask for help on the #my-first-pr channel on julialang.slack.com if you need help with anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] good first issue Indicates a good issue for first-time contributors to Julia
Projects
None yet
Development

No branches or pull requests

7 participants