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

Error in addition of symmetric NamedDimsArray and UniformScaling #212

Open
mjp98 opened this issue Nov 16, 2022 · 0 comments · May be fixed by #213
Open

Error in addition of symmetric NamedDimsArray and UniformScaling #212

mjp98 opened this issue Nov 16, 2022 · 0 comments · May be fixed by #213

Comments

@mjp98
Copy link
Member

mjp98 commented Nov 16, 2022

In Julia 1.8.2

julia> using AxisKeys, NamedDims, LinearAlgebra

julia> A = Symmetric(rand(2,2))
2×2 Symmetric{Float64, Matrix{Float64}}:
 0.993234  0.122844
 0.122844  0.697353

julia> K = NamedDimsArray(A, rows=[:a,:b], cols=[:c, :d])
2-dimensional NamedDimsArray(KeyedArray(...)) with keys:
   rows  2-element Vector{Symbol}
   cols  2-element Vector{Symbol}
And data, 2×2 Symmetric{Float64, Matrix{Float64}}:
        (:c)       (:d)
  (:a)   0.993234   0.122844
  (:b)   0.122844   0.697353

julia> K + I
ERROR: ArgumentError: Cannot set a non-diagonal index in a symmetric matrix
Stacktrace:
 [1] setindex!
   @ /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/symmetric.jl:227 [inlined]
 [2] setindex!
   @ ~/.julia/packages/AxisKeys/DiXQB/src/struct.jl:132 [inlined]
 [3] setindex!
   @ ~/.julia/packages/NamedDims/pu9Bn/src/wrapper_array.jl:185 [inlined]
 [4] copyto_unaliased!(deststyle::IndexCartesian, dest::NamedDimsArray{(:rows, :cols), Float64, 2, KeyedArray{Float64, 2, Symmetric{Float64, Matrix{Float64}}, Tuple{Vector{Symbol}, Vector{Symbol}}}}, srcstyle::IndexCartesian, src::NamedDimsArray{(:rows, :cols), Float64, 2, KeyedArray{Float64, 2, Symmetric{Float64, Matrix{Float64}}, Tuple{Vector{Symbol}, Vector{Symbol}}}})
   @ Base ./abstractarray.jl:1052
 [5] copyto!
   @ ./abstractarray.jl:1018 [inlined]
 [6] copy_oftype(A::NamedDimsArray{(:rows, :cols), Float64, 2, KeyedArray{Float64, 2, Symmetric{Float64, Matrix{Float64}}, Tuple{Vector{Symbol}, Vector{Symbol}}}}, #unused#::Type{Float64})
   @ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/LinearAlgebra.jl:377
 [7] +(A::NamedDimsArray{(:rows, :cols), Float64, 2, KeyedArray{Float64, 2, Symmetric{Float64, Matrix{Float64}}, Tuple{Vector{Symbol}, Vector{Symbol}}}}, J::UniformScaling{Bool})
   @ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/uniformscaling.jl:216
 [8] top-level scope
   @ REPL[8]:1

It seems like LinearAlgebra.copyto_oftype should be extended for NamedDimsArray, deferring to LinearAlgebra.copyto_oftype for the underlying data. e.g. this seems to be sufficient (open issue mcabbott/AxisKeys.jl#133 for KeyedArray)

julia> function LinearAlgebra.copy_oftype(A::KeyedArray, ::Type{T}) where {T}
           return KeyedArray(LinearAlgebra.copy_oftype(A.data, T), axiskeys(A))
       end

julia> function LinearAlgebra.copy_oftype(A::NamedDimsArray, ::Type{T}) where {T}
           return NamedDimsArray(LinearAlgebra.copy_oftype(A.data, T); named_axiskeys(A)...)
       end

julia> K + I
2-dimensional NamedDimsArray(KeyedArray(...)) with keys:
   rows  2-element Vector{Symbol}
   cols  2-element Vector{Symbol}
And data, 2×2 Symmetric{Float64, Matrix{Float64}}:
        (:c)       (:d)
  (:a)   1.99323    0.122844
  (:b)   0.122844   1.69735
@mjp98 mjp98 changed the title Error in LinearAlgebra.copy_oftype on addition of symmetric NamedDimsArray and UniformScaling Error in addition of symmetric NamedDimsArray and UniformScaling Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant