From a1d0fbd8e9b0e0960e1b040b196499004f636d48 Mon Sep 17 00:00:00 2001 From: Carsten Bauer Date: Mon, 2 Mar 2020 06:51:43 +0100 Subject: [PATCH] Fix inverse of SVD of complex matrix (#34872) Closes #34866 --- stdlib/LinearAlgebra/src/svd.jl | 2 +- stdlib/LinearAlgebra/test/svd.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/svd.jl b/stdlib/LinearAlgebra/src/svd.jl index 2a49512eeb2ec6..61e12a1a5945f4 100644 --- a/stdlib/LinearAlgebra/src/svd.jl +++ b/stdlib/LinearAlgebra/src/svd.jl @@ -229,7 +229,7 @@ function inv(F::SVD{T}) where T @inbounds for i in eachindex(F.S) iszero(F.S[i]) && throw(SingularException(i)) end - k = searchsortedlast(F.S, eps(T)*F.S[1], rev=true) + k = searchsortedlast(F.S, eps(real(T))*F.S[1], rev=true) @views (F.S[1:k] .\ F.Vt[1:k, :])' * F.U[:,1:k]' end diff --git a/stdlib/LinearAlgebra/test/svd.jl b/stdlib/LinearAlgebra/test/svd.jl index 4b475237e5b5e6..c7586c89a77aec 100644 --- a/stdlib/LinearAlgebra/test/svd.jl +++ b/stdlib/LinearAlgebra/test/svd.jl @@ -35,6 +35,7 @@ using LinearAlgebra: BlasComplex, BlasFloat, BlasReal, QRPivoted @test inv(svd([1 2; 3 4])) ≈ [-2.0 1.0; 1.5 -0.5] @test inv(svd([1 0 1; 0 1 0])) ≈ [0.5 0.0; 0.0 1.0; 0.5 0.0] @test_throws SingularException inv(svd([0 0; 0 0])) + @test inv(svd([1+2im 3+4im; 5+6im 7+8im])) ≈ [-0.5 + 0.4375im 0.25 - 0.1875im; 0.375 - 0.3125im -0.125 + 0.0625im] end n = 10