Repository: JuliaLinearAlgebra/GenericLinearAlgebra.jl (julia)

Issue:
svdvals! failing for empty matrices
The singular value decompositon fails for empty matrices with element types not `<: BlasFloat`:

```julia
julia> svdvals!(Float16.(ones(10,0)))
ERROR: DimensionMismatch("length of diagonal vector is 0, length of off-diagonal vector is 0")
Stacktrace:
 [1] Bidiagonal
   @ /home/julia/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/bidiag.jl:11 [inlined]
 [2] Bidiagonal
   @ /home/julia/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/bidiag.jl:17 [inlined]
 [3] Bidiagonal
   @ /home/julia/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/bidiag.jl:75 [inlined]
 [4] bidiagonalize!(A::Matrix{Float16})
   @ GenericLinearAlgebra ~/.julia/packages/GenericLinearAlgebra/JoKjw/src/svd.jl:269
 [5] svdvals!(A::Matrix{Float16}; tol::Float16, debug::Bool)
   @ GenericLinearAlgebra ~/.julia/packages/GenericLinearAlgebra/JoKjw/src/svd.jl:487
 [6] svdvals!(A::Matrix{Float16})
   @ GenericLinearAlgebra ~/.julia/packages/GenericLinearAlgebra/JoKjw/src/svd.jl:487
 [7] top-level scope
   @ REPL[121]:1

julia> svdvals!(BigFloat.(ones(10,0)))
ERROR: DimensionMismatch("length of diagonal vector is 0, length of off-diagonal vector is 0")
Stacktrace:
 [1] Bidiagonal
   @ /home/julia/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/bidiag.jl:11 [inlined]
 [2] Bidiagonal
   @ /home/julia/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/bidiag.jl:17 [inlined]
 [3] Bidiagonal
   @ /home/julia/julia/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/bidiag.jl:75 [inlined]
 [4] bidiagonalize!(A::Matrix{BigFloat})
   @ GenericLinearAlgebra ~/.julia/packages/GenericLinearAlgebra/JoKjw/src/svd.jl:269
 [5] svdvals!(A::Matrix{BigFloat}; tol::BigFloat, debug::Bool)
   @ GenericLinearAlgebra ~/.julia/packages/GenericLinearAlgebra/JoKjw/src/svd.jl:487
 [6] svdvals!(A::Matrix{BigFloat})
   @ GenericLinearAlgebra ~/.julia/packages/GenericLinearAlgebra/JoKjw/src/svd.jl:487
 [7] top-level scope
   @ REPL[122]:1

julia> svdvals!(Float32.(ones(10,0)))
Float32[]
```

Pull request:
Fixes https://github.com/JuliaLinearAlgebra/GenericLinearAlgebra.jl/issues/70

Patch:
diff --git a/src/svd.jl b/src/svd.jl
index 098a982..656ebb0 100644
--- a/src/svd.jl
+++ b/src/svd.jl
@@ -161,6 +161,10 @@ function __svd!(
 ) where {T<:Real}
 
     n = size(B, 1)
+    if n == 0
+        # Exit early in the empty case
+        return nothing
+    end
     n1 = 1
     n2 = n
     d = B.dv


Test patch:
diff --git a/test/svd.jl b/test/svd.jl
index a091957..bf211b4 100644
--- a/test/svd.jl
+++ b/test/svd.jl
@@ -68,6 +68,19 @@ using Test, GenericLinearAlgebra, LinearAlgebra, Quaternions, DoubleFloats
         @test A ≈ U * Diagonal(S) * V'
     end
 
+    @testset "Empty matrices. Issue 70" begin
+        @test svdvals!(Float16.(ones(10, 0))) == Float16[]
+        @test svdvals!(Float16.(ones(0, 10))) == Float16[]
+        U, s, Vt = svd!(Float16.(ones(10, 0)))
+        @test U == Matrix{Float16}(undef, 10, 0)
+        @test s == Float16[]
+        @test Vt == Matrix{Float16}(undef, 0, 0)
+        U, s, Vt = svd!(Float16.(ones(0, 10)))
+        @test U == Matrix{Float16}(undef, 0, 0)
+        @test s == Float16[]
+        @test Vt == Matrix{Float16}(undef, 10, 0)
+    end
+
     @testset "Very small matrices. Issue 79" begin
         A = randn(1, 2)
         FA = svd(A)