Skip to content

Commit edbe5df

Browse files
authored
Refactor code to avoid out of bounds operation after deflation (#122)
1 parent 8414b44 commit edbe5df

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/svd.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ function __svd!(
167167
e = B.ev
168168
iteration = 0
169169

170-
# See LAWN3 page 6 and 22
171-
σ⁻ = estimate_σ⁻!(d, e, n1, n2, tol)
172-
fudge = n
173-
thresh = tol * σ⁻
170+
thresh = tol * zero(first(d))
174171

175172
if B.uplo === 'U'
176173
while true
@@ -199,6 +196,7 @@ function __svd!(
199196
break
200197
end
201198
end
199+
@debug "Active submatrix" iteration n1 n2 d[n1] d[n2] e[n1] e[n2 - 1]
202200

203201
# Deflation check. See LAWN3 p21
204202
# The Demmel-Kahan iteration moves the zero to the end and produces a
@@ -210,6 +208,9 @@ function __svd!(
210208

211209
# We have now moved a zero to the end so the problem is one smaller
212210
n2 -= 1
211+
212+
# We'll start over to find the relevant submatrix after deflation
213+
@goto top
213214
end
214215
end
215216

@@ -225,7 +226,7 @@ function __svd!(
225226
fudge = n2 - n1 + 1
226227
thresh = tol * σ⁻
227228

228-
@debug "__svd!" iteration n1 n2 d[n1] d[n2] e[n1] e[n2-1] thresh
229+
@debug "estimated quantities" σ⁻ σ⁺ fudge thresh
229230

230231
if fudge * tol * σ⁻ <= eps(σ⁺)
231232
svdDemmelKahan!(B, n1, n2, U, Vᴴ)

test/svd.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,9 @@ using Test, GenericLinearAlgebra, LinearAlgebra, Quaternions, DoubleFloats
248248
@test F.U == I
249249
@test F.Vt == I
250250
end
251+
252+
@testset "Issue 121" begin
253+
@test svdvals(BigFloat[0 0; 1 -1]) [sqrt(2), 0]
254+
@test svdvals(BigFloat[1 0 0; 0 0 0; 0 1 -1]) [sqrt(2), 1, 0]
255+
end
251256
end

0 commit comments

Comments
 (0)