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

Commit b003319

Browse files
authored
fix svdvals computation (#13)
* fix svdvals computation * type piracy strikes again
1 parent 26c7f53 commit b003319

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/GenericSVD.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function generic_svdfact!(X::AbstractMatrix; sorted=true, thin=true)
2828
end
2929
B,P = bidiagonalize_tall!(X)
3030
U,Vt = unpack(P,thin=thin)
31-
U,S,Vt = svd!(B,U,Vt)
31+
U,S,Vt = svd_bidiag!(B,U,Vt)
3232
# as of Julia v0.7 we need to revert a mysterious transpose here
3333
Vt=Vt'
3434
for i = 1:n
@@ -54,12 +54,8 @@ function generic_svdvals!(X::AbstractMatrix; sorted=true)
5454
X = X'
5555
end
5656
B,P = bidiagonalize_tall!(X)
57-
S = svd!(B)
58-
for i = eachindex(S)
59-
if signbit(S[i])
60-
S[i] = -S[i]
61-
end
62-
end
57+
S = svd_bidiag!(B)
58+
S .= abs.(S)
6359
sorted ? sort!(S,rev=true) : S
6460
end
6561

@@ -83,7 +79,7 @@ end
8379

8480

8581
"""
86-
svd!(B::Bidiagonal [, U, Vt [, ϵ]])
82+
svd_bidiag!(B::Bidiagonal [, U, Vt [, ϵ]])
8783
8884
Compute the SVD of a bidiagonal matrix `B`, via an implicit QR algorithm with shift (known as a Golub-Kahan iterations).
8985
@@ -106,7 +102,7 @@ This proceeds by iteratively finding the lowest strictly-bidiagonal submatrix, i
106102
```
107103
then applying a Golub-Kahan QR iteration.
108104
"""
109-
function svd!(B::Bidiagonal{T}, U=nothing, Vt=nothing, ɛ=eps(T)) where T <: Real
105+
function svd_bidiag!(B::Bidiagonal{T}, U=nothing, Vt=nothing, ɛ=eps(T)) where T <: Real
110106
n = size(B, 1)
111107
if n == 1
112108
@goto done
@@ -156,16 +152,19 @@ function svd!(B::Bidiagonal{T}, U=nothing, Vt=nothing, ɛ=eps(T)) where T <: Rea
156152
shift = abs(s₁-h) < abs(s₂-h) ? s₁ : s₂
157153
# avoid infinite loop
158154
if !all(isfinite.(B))
159-
(U == nothing) && return B.dv+NaN
160-
return SVD(U .+ NaN, B.dv .+ NaN, Vt .+ NaN)
155+
if U === nothing
156+
return B.dv+NaN
157+
else
158+
return SVD(U .+ NaN, B.dv .+ NaN, Vt .+ NaN)
159+
end
161160
end
162161
svd_gk!(B, U, Vt, n₁, n₂, shift)
163162
end
164163
else
165164
throw(ArgumentError("lower bidiagonal version not implemented yet"))
166165
end
167166
@label done
168-
if U == nothing
167+
if U === nothing
169168
return B.dv
170169
else
171170
return SVD(U, B.dv, Vt)

test/quaternions.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ S = svd(X)
1414
Xt = Matrix(X')
1515
St = svd(Xt)
1616
@test isapprox(Matrix(St), Xt, rtol=1e3*eps())
17+
18+
@test svdvals([quat(1) quat(0); quat(0) quat(1)]) == [1.0, 1.0]

0 commit comments

Comments
 (0)