Skip to content

Setting values of triangular matrices also sets data above/below the diagonal #203

Closed
@penelopeysm

Description

@penelopeysm

Hello! I'm not entirely sure if this is really an Accessors issue, but calling Accessors.set on an upper / lower triangular matrix causes the values below / above the diagonal to be changed. I'm currently working around this by using a mutating optic as described in https://juliaobjects.github.io/Accessors.jl/stable/examples/custom_macros/, which doesn't have this issue.

julia> using Accessors, LinearAlgebra

julia> x = LowerTriangular(zeros(2, 2))
2×2 LowerTriangular{Float64, Matrix{Float64}}:
 0.0   
 0.0  0.0

julia> x.data
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> x = Accessors.set(x, (@o _[1, 1]), 1.0)
2×2 LowerTriangular{Float64, Matrix{Float64}}:
 1.0   
 0.0  0.0

julia> x = Accessors.set(x, (@o _[2, 1]), 2.0)
2×2 LowerTriangular{Float64, Matrix{Float64}}:
 1.0   
 2.0  0.0

julia> x = Accessors.set(x, (@o _[2, 2]), 3.0)
2×2 LowerTriangular{Float64, Matrix{Float64}}:
 1.0   
 2.0  3.0

julia> x.data
2×2 Matrix{Float64}:
 1.0  5.129e160
 2.0  3.0

Even trying to manually set the value to zero doesn't work:

julia> x = Accessors.set(x, (@o _[1, 2]), 0.0)
2×2 LowerTriangular{Float64, Matrix{Float64}}:
 1.0   
 2.0  3.0

julia> x.data
2×2 Matrix{Float64}:
 1.0  2.17757e-314
 2.0  3.0

Version info:

(ppl) pkg> st
Status `~/ppl/Project.toml`
  [7d9f7c33] Accessors v0.1.42
  [37e2e46d] LinearAlgebra v1.11.0

julia> versioninfo()
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin24.0.0)
  CPU: 10 × Apple M1 Pro
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions