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

Slicing DenseAxisArrays #287

Closed
IainNZ opened this issue Oct 17, 2014 · 25 comments · Fixed by #2524
Closed

Slicing DenseAxisArrays #287

IainNZ opened this issue Oct 17, 2014 · 25 comments · Fixed by #2524
Labels
Category: Containers Related to the Containers submodule Type: Bug

Comments

@IainNZ
Copy link
Collaborator

IainNZ commented Oct 17, 2014

m = Model()
@defVar(m, x[0:5], Bin)
solve(m)
getValue(x[0])  # Works
getValue(x[0:2])
#BoundsError()
#while loading In[15], in expression starting on line 1
#
# in copy! at array.jl:52
# in getindex at array.jl:265
# in getindex at no file
@mlubin
Copy link
Member

mlubin commented Oct 17, 2014

I think we don't transform the ranges, would be a good idea to do so...

mlubin added a commit that referenced this issue Oct 19, 2014
@mlubin
Copy link
Member

mlubin commented Oct 19, 2014

So this is working for JuMPArrays now. For JuMPDicts it's not immediately obvious what slice indexing for a dictionary should mean and how we can disambiguate it from using ranges as dictionary keys themselves.

mlubin added a commit that referenced this issue Oct 19, 2014
@mlubin
Copy link
Member

mlubin commented Oct 19, 2014

Actually that fix doesn't work because it breaks x[:], which is automatically translated by the compiler to x[1:length(x)]. Not sure how we can work around this...

@mlubin
Copy link
Member

mlubin commented Oct 19, 2014

Which also reveals that we have no test coverage for the x[:] syntax.

@joehuchette
Copy link
Contributor

Would adding a method with getindex(x::JuMPArray, c::Colon) work?

@mlubin
Copy link
Member

mlubin commented Oct 19, 2014

I don't think : is currently passed through as a Colon.

@joehuchette
Copy link
Contributor

Yeah, this is weird:

julia> x = rand(3,3);

julia> @which x[:]
getindex(A::Array{T,N},I::UnitRange{Int64}) at array.jl:262

@joehuchette
Copy link
Contributor

Maybe worth opening an issue? Wanting to dispatch on Colon seems legitimate.

@IainNZ
Copy link
Collaborator Author

IainNZ commented Oct 19, 2014

Yikes

@IainNZ
Copy link
Collaborator Author

IainNZ commented Oct 19, 2014

https://groups.google.com/forum/#!topic/julia-dev/NOF6MA6tb9Y
I wonder if subarrays can do it somehow?

@IainNZ
Copy link
Collaborator Author

IainNZ commented Oct 19, 2014

Ah I gues not, or rather, : does what they want anyway. I found the x[-1] behaviour a bit surprising!

julia> x = sub(A,2:3,2:3)
2x2 SubArray{Float64,2,Array{Float64,2},(UnitRange{Int64},UnitRange{Int64})}:
 0.0604057  0.953382
 0.0455991  0.919803

julia> x[:]
4-element Array{Float64,1}:
 0.0604057
 0.0455991
 0.953382
 0.919803

julia> x[1]
0.06040567704545796

julia> x[-1]
0.33475154171736476

@mlubin
Copy link
Member

mlubin commented Oct 19, 2014

@timholy, are there plans to dispatch on Colon for indexing? I vaguely remember a discussion on this.

@timholy
Copy link
Contributor

timholy commented Oct 19, 2014

Yes, it seems likely to happen, but I wouldn't call it certain. There would be quite a few new methods needed for any object that supports indexing.

@joehuchette
Copy link
Contributor

Looks like colon isn't desugared at parse time anymore (?):

julia> dump(:(a[1,:]))
Expr
  head: Symbol ref
  args: Array(Any,(3,))
    1: Symbol a
    2: Int64 1
    3: Symbol :
  typ: Any

@mlubin
Copy link
Member

mlubin commented Jan 15, 2015

This has always been a compile-time transformation.

@mlubin mlubin added this to the 0.11 milestone Sep 29, 2015
@mlubin mlubin modified the milestones: 0.12, 0.11 Dec 1, 2015
@mlubin mlubin modified the milestones: 0.13, 0.12 Feb 21, 2016
@mlubin mlubin removed this from the 0.13 milestone Apr 29, 2016
@mlubin mlubin added the Category: Containers Related to the Containers submodule label Jan 8, 2017
@mlubin
Copy link
Member

mlubin commented Jun 4, 2018

The remaining issue, if any, is:

julia> m = Model();

julia> @variable(m, x[0:5]);

julia> x[0:2]
ERROR: KeyError: key 0:2 not found

@mlubin mlubin changed the title How does indexing work again? Slicing DenseAxisArrays Feb 25, 2019
@rapus95
Copy link

rapus95 commented Feb 18, 2020

Are there any news on this? Why is slicing not supported in the first place?

@odow
Copy link
Member

odow commented Feb 18, 2020

We're likely missing an appropriate getindex method. I don't think anyone has looked in detail about this recently.

@mlubin
Copy link
Member

mlubin commented Feb 18, 2020

AxisArrays has this functionality in some form. You may be interested in https://github.com/rdeits/AxisArrayVariables.jl (which would need to be updated for the latest JuMP).

@rapus95
Copy link

rapus95 commented Feb 20, 2020

How about sneakily passing it through?

JuMP.Containers.lookup_index(i, lookup::Dict) = isa(i, Colon) ? Colon() : isa(i, AbstractRange) ? i : lookup[i]

@mlubin
Copy link
Member

mlubin commented Feb 20, 2020

What's the interpretation of indexing by 5:7 when the index set is 1:2:9? "passing it through" would be the equivalent of collect(1:2:9)[5:7] (BoundsError).

See the discussion at JuliaArrays/AxisArrays.jl#84 for more context.

@rapus95
Copy link

rapus95 commented Feb 20, 2020

Better: if T::AbstractUnitRange then just pass through

@mlubin
Copy link
Member

mlubin commented Feb 20, 2020

collect(5:7)[5:7] also yields a BoundsError.

@rapus95
Copy link

rapus95 commented Feb 20, 2020

Is there any (dirty) workaround for making that

@variable(m, x[1:DN,1:DN,1:NTC,-36:T,1:NivelesC] >= 0, Int)
[constraints]
[optimize!]
JuMP.value.(x)[:, :, :, 3:40,:]

work easily?

@NLaws
Copy link

NLaws commented Jun 26, 2020

+1 on this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Containers Related to the Containers submodule Type: Bug
Development

Successfully merging a pull request may close this issue.

7 participants