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

sum over a SparseAxisArray #2428

Closed
hdavid16 opened this issue Jan 12, 2021 · 5 comments
Closed

sum over a SparseAxisArray #2428

hdavid16 opened this issue Jan 12, 2021 · 5 comments

Comments

@hdavid16
Copy link
Contributor

hdavid16 commented Jan 12, 2021

It would be nice to be able to execute Base.sum over a variable defined with a SparseAxisArray in a more straightforward fashion (perhaps extending Base.sum in the utils.jl file?).

For example:

Given a variable defined as:

@variable(model, x[i=1:2, j=i:2])

You could sum over it doing:

sum(x[i,j] for i = 1:2 for j = i:2)

However, it would be nicer to be able to remove the second for:

sum(x[i,j] for i = 1:2, j = i:2)

(which can't be done due to a UndefVarError)

@odow
Copy link
Member

odow commented Jan 12, 2021

This is base Julia, not JuMP:

julia> y = rand(2, 2)
2×2 Array{Float64,2}:
 0.408406  0.337018
 0.188717  0.273776

julia> sum(y[i,j] for i = 1:2, j = i:2)
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at REPL[102]:1

If you have triangular indexing, you must specify multiple for.

@odow
Copy link
Member

odow commented Jan 12, 2021

Closing because this is expected behavior.

@odow odow closed this as completed Jan 12, 2021
@hdavid16
Copy link
Contributor Author

hdavid16 commented Jan 13, 2021

@odow Yes. I know it's base Julia. It is just feature that would be nice in the context of JuMP since it is common to sum over a variable that has sparse structure in math programming. So I wasn't sure if it was best to post this here or in Julia. I posted it here since JuMP uses these sparse structures nicely when declaring variables or constraints.

@odow
Copy link
Member

odow commented Jan 13, 2021

I don't think we want to depart from the Julia syntax, because this will just cause more confusion. JuMP's macros rewrite expressions for performance, not to add subtly different functionality.

You could open an issue in Julia. Here's an example which gets at what is happening:

julia> f() = sum(i + j for i=1:2, j=i:2)
f (generic function with 1 method)

julia> @code_lowered f()
CodeInfo(
1#414 = %new(Main.:(var"#414#415"))%2 = #414%3 = 1:2%4 = Main.i:2%5 = Base.product(%3, %4)
│   %6 = Base.Generator(%2, %5)
│   %7 = Main.sum(%6)
└──      return %7
)

julia> f()
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] f() at ./REPL[127]:1
 [2] top-level scope at REPL[129]:1

Relevant issues (might be more, I didn't check thoroughly):
JuliaLang/julia#4867
JuliaLang/julia#15384

@hdavid16
Copy link
Contributor Author

Sounds good. Thanks

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

No branches or pull requests

2 participants