-
-
Notifications
You must be signed in to change notification settings - Fork 396
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
Fixing axes(::JuMPArray)
#1623
Comments
It can't be known from the type information, anyway... We could just do a runtime lookup for every However, we could actually avoid doing the runtime lookup for concrete-eltyped index collections, and only do it for nonconcrete-eltyped index collections. Those |
Okay, just thinking out loud here, but another potential solution is to define: struct FallbackIndex <: Integer
i::Int
end and overload This approach doesn't have the same user-facing problems of the other approach, but it might be harder to implement/more brittle, since it requires defining a new |
AxisArrays has an identical issue: JuliaArrays/AxisArrays.jl#84. My initial reaction to the proposal to mix different index types like: julia> y[3, 2]
y[3,blue] is a strong negative. This is confusing and enables very subtle bugs in user code, and also has the weird edge cases like |
I like the |
Closing because at some point, this got fixed. julia> @variable(model, y[2:5, [:green, :blue]])
2-dimensional DenseAxisArray{VariableRef,2,...} with index sets:
Dimension 1, 2:5
Dimension 2, Symbol[:green, :blue]
And data, a 4×2 Array{VariableRef,2}:
y[2,green] y[2,blue]
y[3,green] y[3,blue]
y[4,green] y[4,blue]
y[5,green] y[5,blue]
julia> axes(y)
(2:5, Symbol[:green, :blue]) |
While tackling #1455, it became apparent that the current indexing behavior of
JuMPArray
does not conform to the assumptions made by theAbstractArray
interface. This will make it quite difficult forJuMPArray
to inherit some pretty fundamental features (e.g. broadcast) and compose with otherAbstractArray
types/operations.Here are some of Base's indexing assumptions:
See JuliaLang/julia#29062 and the Discourse discussion linked therein for more details.
AFAICT, there is no possible way to define
axes(::JuMPArray)
that a) satisfies all the above assumptions and b) doesn't change user-facingJuMPArray
behavior. Right now,axes(::JuMPArray)
is overloaded to respect theA[axes(A)...] == A
assumption.My attempt at resolving this without changing user-facing behavior involved defining an
AxisRange <: AbstractUnitRange
type wrapper, but that ended up being a no-go since Base'sR<:AbstractUnitRange
code assumes that an instancer::R
can be indexed with some multiple ofoneunit(eltype(R))
(which doesn't make sense when e.g.eltype(R) === Symbol
).Another way forward is to allow an integer indexing fallback for non-integer
JuMPArray
axes, e.g.This approach resolves the underlying issue, but has its own problems. It changes user-facing behavior in some non-trivial ways; previously disallowed indexing would now be allowed, and visa versa. I predict that the main breakage in user code would be when the user specifies an axis to be indexed by a collection with non-concrete eltype, e.g.
Under this approach, indexing into
x
above is ambiguous, e.g. it can't be known whetherx[3, 2]
should returnx[3, green]
orx[5, green]
. It'd probably have to be made an error to construct something likex
at all, documenting a user-facing rule like "Given the@variable(m, x[inds...])
,all(isconcretetype(eltype(i)) for i in inds)
must betrue
"...gross!Anybody have any better ideas?
Honestly, it seems like the "right" way to fix this is to just refactor Base's interface, but the ship has sailed on that until Julia 2.0...
The text was updated successfully, but these errors were encountered: