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

DenseAxisArray with Symbol key does not support setting #3151

Closed
odow opened this issue Dec 14, 2022 · 1 comment · Fixed by #3152
Closed

DenseAxisArray with Symbol key does not support setting #3151

odow opened this issue Dec 14, 2022 · 1 comment · Fixed by #3152
Labels
Category: Containers Related to the Containers submodule Type: Bug

Comments

@odow
Copy link
Member

odow commented Dec 14, 2022

Reported on discourse: https://discourse.julialang.org/t/denseaxisarray-not-working-as-expected-with-single-dimensional/91646

julia> D = Containers.DenseAxisArray(zeros(3), [:a, :b, :c])
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, [:a, :b, :c]
And data, a 3-element Vector{Float64}:
 0.0
 0.0
 0.0

julia> E = Containers.DenseAxisArray(ones(3), [:a, :b, :c])
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, [:a, :b, :c]
And data, a 3-element Vector{Float64}:
 1.0
 1.0
 1.0

julia> I = [:a, :b]
2-element Vector{Symbol}:
 :a
 :b

julia> D[I] = E[I]
ERROR: MethodError: no method matching Int64(::Symbol)
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::BigInt) where T<:Union{Int128, Int16, Int32, Int64, Int8} at gmp.jl:356
  (::Type{T})(::Base.TwicePrecision) where T<:Number at twiceprecision.jl:243
  ...
Stacktrace:
 [1] (::Base.var"#105#106")(x::Vector{Symbol})
   @ Base ./abstractarray.jl:99
 [2] _tuple_any(::Base.var"#105#106", ::Bool, ::Vector{Symbol})
   @ Base ./tuple.jl:497
 [3] _tuple_any(f::Function, t::Tuple{Vector{Symbol}})
   @ Base ./tuple.jl:494
 [4] has_offset_axes(A::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}})
   @ Base ./abstractarray.jl:99
 [5] require_one_based_indexing(A::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}})
   @ Base ./abstractarray.jl:103
 [6] setindex!
   @ ./array.jl:851 [inlined]
 [7] setindex!(A::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}}, v::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}}, idx::Vector{Symbol})
   @ JuMP.Containers ~/.julia/packages/JuMP/puvTM/src/Containers/DenseAxisArray.jl:344
 [8] top-level scope
   @ REPL[13]:1

julia> D[I] = 0.0
ERROR: ArgumentError: indexed assignment with a single value to many locations is not supported; perhaps use broadcasting `.=` instead?
Stacktrace:
 [1] setindex_shape_check(::Float64, ::Int64)
   @ Base ./indices.jl:261
 [2] _unsafe_setindex!(#unused#::IndexLinear, A::Vector{Float64}, x::Float64, I::Vector{Int64})
   @ Base ./multidimensional.jl:902
 [3] _setindex!
   @ ./multidimensional.jl:893 [inlined]
 [4] setindex!
   @ ./abstractarray.jl:1267 [inlined]
 [5] setindex!(A::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}}, v::Float64, idx::Vector{Symbol})
   @ JuMP.Containers ~/.julia/packages/JuMP/puvTM/src/Containers/DenseAxisArray.jl:344
 [6] top-level scope
   @ REPL[14]:1

julia> D[I] .= 0.0
ERROR: MethodError: no method matching one(::Symbol)
Closest candidates are:
  one(::Union{Type{T}, T}) where T<:AbstractString at strings/basic.jl:262
  one(::Union{Type{P}, P}) where P<:Dates.Period at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:54
  one(::LinearAlgebra.Tridiagonal{T, V} where V<:AbstractVector{T}) where T at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/special.jl:307
  ...
Stacktrace:
  [1] isone(x::Symbol)
    @ Base ./number.jl:60
  [2] (::JuMP.Containers.var"#9#11"{Vector{Symbol}})(i::Int64)
    @ JuMP.Containers ~/.julia/packages/JuMP/puvTM/src/Containers/DenseAxisArray.jl:323
  [3] _any
    @ ./reduce.jl:876 [inlined]
  [4] #any#702
    @ ./reducedim.jl:883 [inlined]
  [5] any
    @ ./reducedim.jl:883 [inlined]
  [6] to_index(A::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}}, idx::Vector{Symbol})
    @ JuMP.Containers ~/.julia/packages/JuMP/puvTM/src/Containers/DenseAxisArray.jl:323
  [7] to_indices
    @ ./indices.jl:333 [inlined]
  [8] to_indices
    @ ./indices.jl:325 [inlined]
  [9] view
    @ ./subarray.jl:176 [inlined]
 [10] maybeview
    @ ./views.jl:133 [inlined]
 [11] dotview(::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}}, ::Vector{Symbol})
    @ Base.Broadcast ./broadcast.jl:1212
 [12] top-level scope
    @ REPL[15]:1
@odow odow added Type: Bug Category: Containers Related to the Containers submodule labels Dec 14, 2022
@odow
Copy link
Member Author

odow commented Dec 14, 2022

Getting this right isn't straightforward. It's actually related to #2998

julia> A = Containers.DenseAxisArray(zeros(2), 2:3)
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, 2:3
And data, a 2-element Vector{Float64}:
 0.0
 0.0

julia> A[3:3] .= 0.5
1-element view(::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, 3:3) with eltype Float64:
 0.5

julia> A
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, 2:3
And data, a 2-element Vector{Float64}:
 0.0
 0.5

julia> B = Containers.DenseAxisArray(zeros(2), [:a, :b])
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, [:a, :b]
And data, a 2-element Vector{Float64}:
 0.0
 0.0

julia> B[[:b]] .= 0.5
ERROR: MethodError: no method matching checkindex(::Type{Bool}, ::Vector{Symbol}, ::Vector{Symbol})
Closest candidates are:
  checkindex(::Type{Bool}, ::AbstractUnitRange, ::AbstractArray) at abstractarray.jl:682
  checkindex(::Type{Bool}, ::AbstractUnitRange, ::Any) at abstractarray.jl:671
  checkindex(::Type{Bool}, ::Tuple, ::Base.LogicalIndex) at multidimensional.jl:792
  ...
Stacktrace:
 [1] checkbounds
   @ ./abstractarray.jl:601 [inlined]
 [2] checkbounds
   @ ./abstractarray.jl:616 [inlined]
 [3] view
   @ ./subarray.jl:177 [inlined]
 [4] maybeview
   @ ./views.jl:133 [inlined]
 [5] dotview(::JuMP.Containers.DenseAxisArray{Float64, 1, Tuple{Vector{Symbol}}, Tuple{JuMP.Containers._AxisLookup{Dict{Symbol, Int64}}}}, ::Vector{Symbol})
   @ Base.Broadcast ./broadcast.jl:1212
 [6] top-level scope
   @ REPL[35]:1

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.

1 participant