JuliaLang/LinearAlgebra.jl

[LinearAlgebra] `zero(::ArrayWrapper)` should forward to the parent

オープン

#1,050 opened on 2024/01/23

 (15 件のコメント) (1 件のリアクション) (0 人の担当者)Julia (62 件のフォーク)github user discovery
arraysgood first issue

Repository metrics

Stars
 (77 個のスター)
PR merge metrics
 (平均マージ 8d 17h) (30d で 5 merged PRs)

説明

So for the various array wrapper types like Symmetric, Hermitian, and Adjoint, we currently don't have a specialized zero method, but instead fall back to the AbstractArray definition which works in terms of similar. That means that e.g. this gives a MMatrix:

julia> using StaticArrays

julia> zero(Symmetric(SA[1 2; 3 4]))
2×2 Symmetric{Int64, MMatrix{2, 2, Int64, 4}} with indices SOneTo(2)×SOneTo(2):
 0  0
 0  0

I think though for these wrapper types, we should be able to define zero (and maybe some other functions?) like

for func in [:zero, #=Any others?=#]
    for Wrapper in [:Symmetric, :Hermitian, :Adjoint, #=Any others?=#]
       @eval Base.:($func)(M::$Wrapper) = $Wrapper($func(parent(M)))
    end
end

to avoid falling back to similar where possible, and make sure that custom methods like zero(::SMatrix) end up getting called instead.


Edit (May 18, 2026)

StaticArrays.jl has addressed some of these missing methods itself, and https://github.com/JuliaLang/LinearAlgebra.jl/pull/1592 addressed a subset of the missing methods, but I still think there's more.

struct TestWrap{T} <: AbstractMatrix{T}
    parent::Matrix{T}
end
Base.size(A::TestWrap) = size(A.parent)
Base.getindex(A::TestWrap, i, j) = A.parent[i,j]
Base.zero(A::TestWrap) = TestWrap(zero(A.parent))
  • zero(::Union{Hermitian, Symmetric}) https://github.com/JuliaLang/LinearAlgebra.jl/pull/1592
  • zero(::Union{Adjoint, Transpose, UpperTriangular, LowerTriangular, UnitUpperTriangular, UnitLowerTriangular, UpperHessenberg})
  • one(::Union{Hermitian, Symmetric, Adjoint, Transpose, UpperTriangular, LowerTriangular, UnitUpperTriangular, UnitLowerTriangular, UpperHessenberg})
  • copy(::Union{Hermitian, Symmetric, Adjoint, Transpose, UpperTriangular, LowerTriangular, UnitUpperTriangular, UnitLowerTriangular, UpperHessenberg})

There's probably more but those are the ones I could find so far. Please suggest more examples if you find them.

コントリビューターガイド