Skip to content

Commit

Permalink
Return rhs in ldiv! for SVD and add a test (#41083)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
(cherry picked from commit d9d59a9)
  • Loading branch information
andreasnoack authored and fredrikekre committed Jun 14, 2021
1 parent 6d74a23 commit 6855182
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion stdlib/LinearAlgebra/src/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ svdvals(S::SVD{<:Any,T}) where {T} = (S.S)::Vector{T}

# SVD least squares
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)
view(A.Vt,1:k,:)' * (view(A.S,1:k) .\ (view(A.U,:,1:k)' * B))
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 @@ -201,4 +201,11 @@ 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

end # module TestSVD

0 comments on commit 6855182

Please sign in to comment.