Skip to content

Commit 7c0ecd6

Browse files
authored
avoid division by zero in svd(::Diagonal) (#1150)
Fixes #1149
1 parent 39a7e3c commit 7c0ecd6

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/diagonal.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,8 @@ function svd(D::Diagonal{T}) where {T<:Number}
10241024
Vt = copy(U)
10251025
for i in 1:length(d)
10261026
j = piv[i]
1027-
U[j,i] = d[j] / S[i]
1028-
Vt[i,j] = one(Td)
1027+
U[j,i] = iszero(d[j]) ? oneunit(Td) : d[j] / S[i]
1028+
Vt[i,j] = oneunit(Td)
10291029
end
10301030
return SVD(U, S, Vt)
10311031
end

test/diagonal.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ Random.seed!(1)
463463
end
464464
end
465465

466-
@testset "svd (#11120/#11247)" begin
466+
@testset "svd (#11120/#11247/#1149)" begin
467+
D[1] = 0
467468
U, s, V = svd(D)
468469
@test (U*Diagonal(s))*V' D
469470
@test svdvals(D) == s

0 commit comments

Comments
 (0)