Skip to content
This repository was archived by the owner on Apr 26, 2021. It is now read-only.

Commit a61c55a

Browse files
authored
Fixes #17, full=true returns full SVD. (#20)
1 parent 07156af commit a61c55a

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/GenericSVD.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@ function svd!(X::AbstractMatrix; full::Bool=false, thin::Union{Bool,Nothing} = n
1313
end
1414
if thin != nothing
1515
@warn "obsolete keyword `thin` in generic svd!"
16-
thinx = thin
17-
else
18-
thinx = !full
16+
full = !thin
1917
end
20-
generic_svdfact!(X; thin=thinx)
18+
generic_svdfact!(X; full=full)
2119
end
2220

2321
LinearAlgebra.svdvals!(X::AbstractMatrix) = generic_svdvals!(X)
2422

25-
function generic_svdfact!(X::AbstractMatrix; sorted=true, thin=true)
23+
function generic_svdfact!(X::AbstractMatrix; sorted=true, full=false)
2624
m,n = size(X)
2725
wide = m < n
2826
if wide
2927
m,n = n,m
3028
X = X'
3129
end
3230
B,P = bidiagonalize_tall!(X)
33-
U,Vt = unpack(P,thin=thin)
31+
U,Vt = unpack(P,full=full)
3432
U,S,Vt = svd_bidiag!(B,U,Vt)
3533
# as of Julia v0.7 we need to revert a mysterious transpose here
3634
Vt=Vt'
@@ -45,8 +43,8 @@ function generic_svdfact!(X::AbstractMatrix; sorted=true, thin=true)
4543
if sorted
4644
Idx = sortperm(S,rev=true)
4745
S = S[Idx]
48-
U = U[:,Idx]
49-
Vt = Vt[Idx,:]
46+
U[:, 1:n] .= U[:,Idx]
47+
Vt[1:n, :] .= Vt[Idx,:]
5048
end
5149
wide ? SVD(Vt',S,U') : SVD(U,S,Vt)
5250
end

src/bidiagonalize.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ function bidiagonalize_tall!(A::Matrix{T}) where T
5454
bidiagonalize_tall!(A, B)
5555
end
5656

57-
function unpack(P::PackedUVt{T};thin=true) where T
57+
function unpack(P::PackedUVt{T};full=false) where T
5858
A = P.A
5959
m,n = size(A)
6060

6161
# U = Q_1 ... Q_n I_{m,n}
62-
w = thin ? n : m
62+
w = full ? m : n
6363
U = Matrix(one(T)*I,m,w) # eye(T,m,w)
6464
for i = n:-1:1
6565
τi = A[i,i]

test/misc.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,33 @@ GenericSVD.svd_zerodiag_col!(B,Vt,1,3)
2929
@test B[3,3] == 0
3030
@test B[2,3] == 0
3131
@test Matrix(B)*Vt B1
32+
33+
34+
35+
# Test of the full keyword
36+
A_43 = big.(reshape(1:12, 4, 3))
37+
38+
U, S, V = svd(A_43, full=true)
39+
@test U'*U Matrix(I, 4, 4)
40+
@test V'*V Matrix(I, 3, 3)
41+
@test U*[diagm(0 => S); zeros(1,3)]*V' A_43
42+
@test issorted(S, rev=true)
43+
44+
U, S, V = svd(A_43, full=false)
45+
@test U'*U Matrix(I, 3, 3)
46+
@test V'*V Matrix(I, 3, 3)
47+
@test U*diagm(0 => S)*V' A_43
48+
@test issorted(S, rev=true)
49+
50+
A_34 = big.(reshape(1:12, 3, 4))
51+
U, S, V = svd(A_34, full=true)
52+
@test U'*U Matrix(I, 3, 3)
53+
@test V'*V Matrix(I, 4, 4)
54+
@test U*[diagm(0 => S) zeros(3,1)]*V' A_34
55+
@test issorted(S, rev=true)
56+
57+
U, S, V = svd(A_34, full=false)
58+
@test U'*U Matrix(I, 3, 3)
59+
@test V'*V Matrix(I, 3, 3)
60+
@test U*diagm(0 => S)*V' A_34
61+
@test issorted(S, rev=true)

0 commit comments

Comments
 (0)