Skip to content

Commit 4a71f79

Browse files
authored
Test if SVD problem is tall or wide based on uplo field instead (#80)
istriu function since it's misleading for matrices where one of the dimnension sizes is just one.
1 parent 68b746b commit 4a71f79

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
77
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
88
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
99

10+
[compat]
11+
julia = "1.3"
12+
1013
[extras]
1114
Quaternions = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
1215
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1316

1417
[targets]
1518
test = ["Quaternions", "Test"]
16-
17-
[compat]
18-
julia = "1.3"

src/svd.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919

2020
function svdIter!(B::Bidiagonal{T}, n1, n2, shift, U = nothing, Vᴴ = nothing) where T<:Real
2121

22-
if istriu(B)
22+
if B.uplo === 'U'
2323

2424
d = B.dv
2525
e = B.ev
@@ -71,7 +71,7 @@ end
7171
# See LAWN 3
7272
function svdDemmelKahan!(B::Bidiagonal{T}, n1, n2, U = nothing, Vᴴ = nothing) where T<:Real
7373

74-
if istriu(B)
74+
if B.uplo === 'U'
7575

7676
d = B.dv
7777
e = B.ev
@@ -137,7 +137,7 @@ function __svd!(B::Bidiagonal{T}, U = nothing, Vᴴ = nothing; tol = 100eps(T),
137137
fudge = n
138138
thresh = tol*σ⁻
139139

140-
if istriu(B)
140+
if B.uplo === 'U'
141141
while true
142142

143143
# Search for biggest index for non-zero off diagonal value in e
@@ -252,7 +252,7 @@ function bidiagonalize!(A::AbstractMatrix)
252252

253253
if m >= n
254254
# tall case: upper bidiagonal
255-
for i = 1:min(m, n)
255+
for i in 1:min(m, n)
256256
x = view(A, i:m, i)
257257
τi = LinearAlgebra.reflector!(x)
258258
push!(τl, τi)
@@ -271,7 +271,7 @@ function bidiagonalize!(A::AbstractMatrix)
271271
return BidiagonalFactorization{eltype(bd),typeof(bd.dv),typeof(A),typeof(τl)}(bd, A, τl, τr)
272272
else
273273
# wide case: lower bidiagonal
274-
for i = 1:min(m, n)
274+
for i in 1:min(m, n)
275275
x = view(A, i, i:n)
276276
conj!(x)
277277
τi = LinearAlgebra.reflector!(x)
@@ -299,7 +299,7 @@ function Base.getproperty(F::BidiagonalFactorization, s::Symbol)
299299
R = getfield(F, :reflectors)
300300
τl = getfield(F, :τl)
301301
τr = getfield(F, :τr)
302-
if istriu(BD)
302+
if BD.uplo === 'U'
303303
if s === :leftQ
304304
return LinearAlgebra.QRPackedQ(R, τl)
305305
elseif s === :rightQ
@@ -559,13 +559,13 @@ function LinearAlgebra.svd!(A::StridedMatrix{T};
559559
# |x x x x x| |O O O| | x x| |O O O O O|
560560

561561
_B = BF.bidiagonal
562-
B = istriu(_B) ? _B : Bidiagonal(_B.dv, _B.ev, :U)
562+
B = _B.uplo === 'U' ? _B : Bidiagonal(_B.dv, _B.ev, :U)
563563

564564
# Compute the SVD of the bidiagonal matrix B
565565
F = _svd!(B, tol = tol, debug = debug)
566566

567567
# Form the matrices U and Vᴴ by combining the singular vector matrices of the bidiagonal SVD with the Householder reflectors from the bidiagonal factorization.
568-
if istriu(_B)
568+
if _B.uplo === 'U'
569569
U = Matrix{T}(I, m, full ? m : n)
570570
U[1:n,1:n] = F.U
571571
lmul!(BF.leftQ, U)

test/svd.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,15 @@ using Test, GenericLinearAlgebra, LinearAlgebra, Quaternions
5959
@test A U*Diagonal(S)*V'
6060
end
6161

62+
@testset "Very small matrices. Issue 79" begin
63+
A = randn(1, 2)
64+
FA = svd(A)
65+
FAb = svd(big.(A))
66+
FAtb = svd(big.(A'))
67+
@test FA.S Float64.(FAb.S) Float64.(FAtb.S)
68+
@test abs.(FA.U'*Float64.(FAb.U)) I
69+
@test abs.(FA.U'*Float64.(FAtb.V)) I
70+
@test abs.(FA.V'*Float64.(FAb.V)) I
71+
@test abs.(FA.V'*Float64.(FAtb.U)) I
72+
end
6273
end

0 commit comments

Comments
 (0)