Skip to content

Commit 2a97b67

Browse files
Roger-luojohanmon
authored andcommitted
allow creating an undef Diagonal (JuliaLang#38282)
1 parent 2b831e4 commit 2a97b67

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ AbstractMatrix{T}(D::Diagonal) where {T} = Diagonal{T}(D)
6363
Matrix(D::Diagonal) = diagm(0 => D.diag)
6464
Array(D::Diagonal) = Matrix(D)
6565

66+
"""
67+
Diagonal{T}(undef, n)
68+
69+
Construct an uninitialized `Diagonal{T}` of length `n`. See `undef`.
70+
"""
71+
Diagonal{T}(::UndefInitializer, n::Integer) where T = Diagonal(Vector{T}(undef, n))
72+
6673
# For D<:Diagonal, similar(D[, neweltype]) should yield a Diagonal matrix.
6774
# On the other hand, similar(D, [neweltype,] shape...) should yield a sparse matrix.
6875
# The first method below effects the former, and the second the latter.

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,10 @@ end
744744
@test dot(zeros(Int32, 0), Diagonal(zeros(Int, 0)), zeros(Int16, 0)) === 0
745745
end
746746

747+
@testset "Diagonal(undef)" begin
748+
d = Diagonal{Float32}(undef, 2)
749+
@test length(d.diag) == 2
750+
end
747751

748752
@testset "permutedims (#39447)" begin
749753
for D in (Diagonal(zeros(5)), Diagonal(zeros(5) .+ 1im), Diagonal([[1,2],[3,4]]))

0 commit comments

Comments
 (0)