Skip to content

Commit 27a78e0

Browse files
authored
Fix SVD of matrices with zero sizes (#125)
1 parent 7d68683 commit 27a78e0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/svd.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ function __svd!(
161161
) where {T<:Real}
162162

163163
n = size(B, 1)
164+
if n == 0
165+
# Exit early in the empty case
166+
return nothing
167+
end
164168
n1 = 1
165169
n2 = n
166170
d = B.dv

test/svd.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ using Test, GenericLinearAlgebra, LinearAlgebra, Quaternions, DoubleFloats
6868
@test A U * Diagonal(S) * V'
6969
end
7070

71+
@testset "Empty matrices. Issue 70" begin
72+
@test svdvals!(Float16.(ones(10, 0))) == Float16[]
73+
@test svdvals!(Float16.(ones(0, 10))) == Float16[]
74+
U, s, Vt = svd!(Float16.(ones(10, 0)))
75+
@test U == Matrix{Float16}(undef, 10, 0)
76+
@test s == Float16[]
77+
@test Vt == Matrix{Float16}(undef, 0, 0)
78+
U, s, Vt = svd!(Float16.(ones(0, 10)))
79+
@test U == Matrix{Float16}(undef, 0, 0)
80+
@test s == Float16[]
81+
@test Vt == Matrix{Float16}(undef, 10, 0)
82+
end
83+
7184
@testset "Very small matrices. Issue 79" begin
7285
A = randn(1, 2)
7386
FA = svd(A)

0 commit comments

Comments
 (0)