Skip to content

Commit 90c8b74

Browse files
committed
Support \ for SVD factorization
1 parent b81044a commit 90c8b74

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/StaticArrays.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using LinearAlgebra
1818
import LinearAlgebra: transpose, adjoint, dot, eigvals, eigen, lyap, tr,
1919
kron, diag, norm, dot, diagm, lu, svd, svdvals,
2020
factorize, ishermitian, issymmetric, isposdef, normalize,
21-
normalize!, Eigen, det, logdet, cross, diff, qr
21+
normalize!, Eigen, det, logdet, cross, diff, qr, \
2222

2323
# import eye for deprecation warnings
2424
@static if isdefined(LinearAlgebra, :eye)

src/svd.jl

+5
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ function _svd(A, full::Val{true})
5555
SVD(U,S,Vt)
5656
end
5757

58+
function \(F::SVD, B::StaticVecOrMat)
59+
sthresh = eps(F.S[1])
60+
Sinv = map(s->s < sthresh ? zero(1/sthresh) : 1/s, F.S)
61+
return F.Vt' * (Diagonal(Sinv) * (F.U'*B))
62+
end

test/svd.jl

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ using StaticArrays, Test, LinearAlgebra
44
m3 = @SMatrix Float64[3 9 4; 6 6 2; 3 7 9]
55
m3c = ComplexF64.(m3)
66
m23 = @SMatrix Float64[3 9 4; 6 6 2]
7+
m_sing = @SMatrix [2.0 3.0 5.0; 4.0 9.0 10.0; 1.0 1.0 1.0]
8+
v = @SVector [1, 2, 3]
79

810
@testset "svd" begin
911
@testinf svdvals(@SMatrix [2 0; 0 0])::StaticVector [2, 0]
@@ -51,5 +53,10 @@ using StaticArrays, Test, LinearAlgebra
5153
@testinf svd(m3c).U isa SMatrix{3,3,ComplexF64}
5254
@testinf svd(m3c).S isa SVector{3,Float64}
5355
@testinf svd(m3c).Vt isa SMatrix{3,3,ComplexF64}
56+
57+
@testinf svd(m3) \ v svd(Matrix(m3)) \ Vector(v)
58+
@testinf svd(m_sing) \ v svd(Matrix(m_sing)) \ Vector(v)
59+
@testinf svd(m3) \ m23' svd(Matrix(m3)) \ Matrix(m23')
60+
@testinf svd(m_sing) \ m23' svd(Matrix(m_sing)) \ Matrix(m23')
5461
end
5562
end

0 commit comments

Comments
 (0)