Skip to content

Commit 550d534

Browse files
authored
Fix bug in converting Zeros matrix to Diagonal (#389)
* Fix bug in converting Zeros matrix to Diagonal * Update runtests.jl * only overload diagonal/convert for ≤v1.11-
1 parent b0dbf4b commit 550d534

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/FillArrays.jl

+7-5
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,13 @@ for (Typ, funcs, func) in ((:AbstractZeros, :zeros, :zero), (:AbstractOnes, :one
554554
end
555555
end
556556

557-
# temporary patch. should be a PR(#48895) to LinearAlgebra
558-
Diagonal{T}(A::AbstractFillMatrix) where T = Diagonal{T}(diag(A))
559-
function convert(::Type{T}, A::AbstractFillMatrix) where T<:Diagonal
560-
checksquare(A)
561-
isdiag(A) ? T(A) : throw(InexactError(:convert, T, A))
557+
if VERSION < v"1.11-"
558+
# temporary patch. should be a PR(#48895) to LinearAlgebra
559+
Diagonal{T}(A::AbstractFillMatrix) where T = Diagonal{T}(diag(A))
560+
function convert(::Type{T}, A::AbstractFillMatrix) where T<:Diagonal
561+
checksquare(A)
562+
isdiag(A) ? T(diag(A)) : throw(InexactError(:convert, T, A))
563+
end
562564
end
563565

564566
Base.StepRangeLen(F::AbstractFillVector{T}) where T = StepRangeLen(getindex_value(F), zero(T), length(F))

test/runtests.jl

+7
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,13 @@ end
29772977
@test tril(Z, 2) === Z
29782978
end
29792979

2980+
@testset "Diagonal conversion (#389)" begin
2981+
@test convert(Diagonal{Int, Vector{Int}}, Zeros(5,5)) isa Diagonal{Int,Vector{Int}}
2982+
@test convert(Diagonal{Int, Vector{Int}}, Zeros(5,5)) == zeros(5,5)
2983+
@test Diagonal{Int}(Zeros(5,5)) Diagonal(Zeros{Int}(5))
2984+
@test Diagonal{Int}(Ones(5,5)) Diagonal(Ones{Int}(5))
2985+
end
2986+
29802987
@testset "sqrt/cbrt" begin
29812988
F = Fill(4, 4, 4)
29822989
A = Array(F)

0 commit comments

Comments
 (0)