I have been using with JuMP’s container types as I am developing my JuMP extension and have two observations that I think would make for 2 relatively simple additions.
Observation 1: According to the extension of Base.keys(::IndexAnyCartesian, d::SparseAxisArray)
here JuMP.jl/SparseAxisArray.jl at master · jump-dev/JuMP.jl · GitHub, Base.keys(d)
should be defined but it is not:
using JuMP; const JuMPC = JuMP.Containers
JuMPC.@container(a[i = 1:2, j = 1:2; i + j <= 3], 2)
keys(a)
ERROR: MethodError: no method matching size(::JuMP.Containers.SparseAxisArray{Int64,2,Tuple{Int64,Int64}})
Closest candidates are:
size(::AbstractArray{T,N}, ::Any) where {T, N} at abstractarray.jl:38
size(::BitArray{1}) at bitarray.jl:77
size(::BitArray{1}, ::Integer) at bitarray.jl:81
...
Stacktrace:
[1] axes at .\abstractarray.jl:75 [inlined]
[2] keys(::JuMP.Containers.SparseAxisArray{Int64,2,Tuple{Int64,Int64}}) at .\abstractarray.jl:101
[3] top-level scope at REPL[24]:1
However, could it not just be defined as Base.keys(d::SparseAxisArray) = keys(d.data)
? This would be nice to enable Base.keys
as a uniform method for accessing the keys of Array
s, DenseAxisArray
s, and SparseAxisArray
s which is convenient in writing methods that need to iterate using the keys of a container.
Observation 2: I noticed that DenseAxisArray
s can be iterated over in a for loop (as enabled by Base.iterate
, but comprehension does not work for DenseAxisArray
s:
using JuMP; const JuMPC = JuMP.Containers
JuMPC.@container(b[i = 2:3, j = 1:2], 2)
[i for i in b]
ERROR: MethodError: no method matching similar(::Type{Array{Int64,2}}, ::Tuple{UnitRange{Int64},Base.OneTo{Int64}})
Closest candidates are:
similar(::AbstractArray{T,N} where N, ::Tuple) where T at abstractarray.jl:627
similar(::Type{T<:AbstractArray}, ::Union{Integer, AbstractUnitRange}...) where T<:AbstractArray at abstractarray.jl:670
similar(::Type{T<:AbstractArray}, ::Tuple{Vararg{Int64,N}} where N) where T<:AbstractArray at abstractarray.jl:672
...
Stacktrace:
[1] _array_for(::Type{Int64}, ::JuMP.Containers.DenseAxisArray{Int64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}, ::Base.HasShape{2}) at .\array.jl:598
[2] collect(::Base.Generator{JuMP.Containers.DenseAxisArray{Int64,2,Tuple{UnitRange{Int64},Base.OneTo{Int64}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}},getfield(Main, Symbol("##35#36"))}) at .\array.jl:0
[3] top-level scope at REPL[25]:1
Again, this would be a convenient tool for creating containers based on an existing DenseAxisArray
.