-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Comments
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 |
Closing because this is expected behavior. |
@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. |
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): |
Sounds good. Thanks |
It would be nice to be able to execute
Base.sum
over a variable defined with aSparseAxisArray
in a more straightforward fashion (perhaps extendingBase.sum
in the utils.jl file?).For example:
Given a variable defined as:
You could sum over it doing:
However, it would be nicer to be able to remove the second
for
:(which can't be done due to a
UndefVarError
)The text was updated successfully, but these errors were encountered: