Skip to content

Commit 9ce30f2

Browse files
jishnubdkarrasch
andauthored
Forward axes to the parent for a Diagonal (#50514)
Given that [quite a few](https://juliahub.com/ui/Search?q=axes%5Ba-zA-Z%5C%28%5C%29%3A%5D%2BDiagonal&type=code&r=true) packages define `axes(d::Diagonal{T, CustomVector{T}})` as `ax = parent(d); (ax,ax)`, perhaps it makes sense to have this defined in `LinearAlgebra`. After this, ```julia julia> using StaticArrays, StructArrays, LinearAlgebra julia> D = Diagonal(StructArray{Complex{Int}}((SA[1,2], SA[1,2]))) 2×2 Diagonal{Complex{Int64}, StructVector{Complex{Int64}, @NamedTuple{re::SVector{2, Int64}, im::SVector{2, Int64}}, Int64}} with indices SOneTo(2)×SOneTo(2): 1+1im ⋅ ⋅ 2+2im julia> axes(D) (SOneTo(2), SOneTo(2)) ``` The static sizes are preserved. --------- Co-authored-by: Daniel Karrasch <[email protected]>
1 parent 155f4d2 commit 9ce30f2

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/diagonal.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ end
150150
r
151151
end
152152

153+
axes(D::Diagonal) = (ax = axes(D.diag, 1); (ax, ax))
154+
153155
@inline function Base.isstored(D::Diagonal, i::Int, j::Int)
154156
@boundscheck checkbounds(D, i, j)
155157
if i == j

test/diagonal.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ Random.seed!(1)
460460
end
461461
end
462462

463+
@testset "axes" begin
464+
v = OffsetArray(1:3)
465+
D = Diagonal(v)
466+
@test axes(D) isa NTuple{2,typeof(axes(v,1))}
467+
end
468+
463469
@testset "rdiv! (#40887)" begin
464470
@test rdiv!(Matrix(Diagonal([2.0, 3.0])), Diagonal(2:3)) == Diagonal([1.0, 1.0])
465471
@test rdiv!(fill(3.0, 3, 3), 3.0I(3)) == ones(3,3)

0 commit comments

Comments
 (0)