Skip to content

Commit

Permalink
Return rhs in ldiv! for SVD and add a test (JuliaLang#41083)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
  • Loading branch information
andreasnoack and stevengj authored Jun 5, 2021
1 parent e0165b4 commit d9d59a9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stdlib/LinearAlgebra/src/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ svdvals(S::SVD{<:Any,T}) where {T} = (S.S)::Vector{T}
function ldiv!(A::SVD{T}, B::StridedVecOrMat) where T
m, n = size(A)
k = searchsortedlast(A.S, eps(real(T))*A.S[1], rev=true)
return mul!(view(B, 1:n, :), view(A.Vt, 1:k, :)', view(A.S, 1:k) .\ (view(A.U, :, 1:k)' * _cut_B(B, 1:m)))
mul!(view(B, 1:n, :), view(A.Vt, 1:k, :)', view(A.S, 1:k) .\ (view(A.U, :, 1:k)' * _cut_B(B, 1:m)))
return B
end

function inv(F::SVD{T}) where T
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ end
@test Uc * diagm(0=>Sc) * transpose(V) complex.(A) rtol=1e-3
end

@testset "Issue 40944. ldiV!(SVD) should update rhs" begin
F = svd(randn(2, 2))
b = randn(2)
x = ldiv!(F, b)
@test x === b
end

@testset "adjoint of SVD" begin
n = 5
B = randn(5, 2)
Expand Down

0 comments on commit d9d59a9

Please sign in to comment.