Skip to content

Commit d733a53

Browse files
committed
Move SVD into its own file for clarity
1 parent e5ec77e commit d733a53

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

src/StaticArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ include("sqrtm.jl")
9696
include("cholesky.jl")
9797
include("deque.jl")
9898
include("io.jl")
99+
include("svd.jl")
99100

100101
include("FixedSizeArrays.jl")
101102
include("ImmutableArrays.jl")

src/linalg.jl

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -340,40 +340,3 @@ end
340340
# TODO
341341

342342
#--------------------------------------------------
343-
# SVD
344-
# We need our own SVD factorization struct, as Base.LinAlg.SVD assumes
345-
# Base.Vector for `S`, and that the `U` and `Vt` have the same
346-
struct SVD{T,TU,TS,TVt} <: Factorization{T}
347-
U::TU
348-
S::TS
349-
Vt::TVt
350-
end
351-
SVD(U::AbstractArray{T}, S::AbstractVector, Vt::AbstractArray{T}) where {T} = SVD{T,typeof(U),typeof(S),typeof(Vt)}(U, S, Vt)
352-
353-
getindex(::SVD, ::Symbol) = error("In order to avoid type instability, StaticArrays.SVD doesn't support indexing the output of svdfact with a symbol. Instead, you can access the fields of the factorization directly as f.U, f.S, and f.Vt")
354-
355-
# Return the "diagonal size" of a matrix - the minimum of the two dimensions
356-
@generated function diagsize(A::StaticMatrix{N,M}) where {N,M}
357-
:($(min(N,M)))
358-
end
359-
360-
function svdvals!(A::StaticMatrix)
361-
sv = svdvals!(Matrix(A))
362-
similar_type(A, eltype(sv), Size(diagsize(A)))(sv)
363-
end
364-
365-
function svdfact(A::StaticMatrix)
366-
# "Thin" SVD only for now.
367-
f = svdfact(Matrix(A))
368-
U = similar_type(A, eltype(f.U), Size(Size(A)[1], diagsize(A)))(f.U)
369-
S = similar_type(A, Size(diagsize(A)))(f.S)
370-
Vt = similar_type(A, eltype(f.Vt), Size(diagsize(A), Size(A)[2]))(f.Vt)
371-
SVD(U,S,Vt)
372-
end
373-
374-
function svd(A::StaticMatrix)
375-
# Need our own version of `svd()`, as `Base` passes the `thin` argument
376-
# which makes the resulting dimensions uninferrable.
377-
f = svdfact(A)
378-
(f.U, f.S, f.Vt')
379-
end

src/svd.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Singular Value Decomposition
2+
3+
# We need our own SVD factorization struct, as Base.LinAlg.SVD assumes
4+
# Base.Vector for `S`, and that the `U` and `Vt` have the same
5+
struct SVD{T,TU,TS,TVt} <: Factorization{T}
6+
U::TU
7+
S::TS
8+
Vt::TVt
9+
end
10+
SVD(U::AbstractArray{T}, S::AbstractVector, Vt::AbstractArray{T}) where {T} = SVD{T,typeof(U),typeof(S),typeof(Vt)}(U, S, Vt)
11+
12+
getindex(::SVD, ::Symbol) = error("In order to avoid type instability, StaticArrays.SVD doesn't support indexing the output of svdfact with a symbol. Instead, you can access the fields of the factorization directly as f.U, f.S, and f.Vt")
13+
14+
# Return the "diagonal size" of a matrix - the minimum of the two dimensions
15+
@generated function diagsize(A::StaticMatrix{N,M}) where {N,M}
16+
:($(min(N,M)))
17+
end
18+
19+
function svdvals!(A::StaticMatrix)
20+
sv = svdvals!(Matrix(A))
21+
similar_type(A, eltype(sv), Size(diagsize(A)))(sv)
22+
end
23+
24+
function svdfact(A::StaticMatrix)
25+
# "Thin" SVD only for now.
26+
f = svdfact(Matrix(A))
27+
U = similar_type(A, eltype(f.U), Size(Size(A)[1], diagsize(A)))(f.U)
28+
S = similar_type(A, Size(diagsize(A)))(f.S)
29+
Vt = similar_type(A, eltype(f.Vt), Size(diagsize(A), Size(A)[2]))(f.Vt)
30+
SVD(U,S,Vt)
31+
end
32+
33+
function svd(A::StaticMatrix)
34+
# Need our own version of `svd()`, as `Base` passes the `thin` argument
35+
# which makes the resulting dimensions uninferrable.
36+
f = svdfact(A)
37+
(f.U, f.S, f.Vt')
38+
end

0 commit comments

Comments
 (0)