@@ -326,20 +326,26 @@ function _svds(X; nsv::Int = 6, ritzvec::Bool = true, tol::Float64 = 0.0, maxite
326
326
# ind = [1:2:ncv;]
327
327
# sval = abs.(ex[1][ind])
328
328
329
- svals = sqrt .(max .(zero (real (otype)), real .(ex[1 ])))
329
+ realex1 = real .(ex[1 ])
330
+ threshold = max (eps (real (otype))* realex1[1 ], eps (real (otype)))
331
+ firstzero = findfirst (v -> v <= threshold, realex1)
332
+ r = firstzero === nothing ? nsv : firstzero- 1 # rank of the decomposition
333
+ realex1[r+ 1 : end ] .= zero (real (otype))
334
+ svals = sqrt .(realex1)
335
+
330
336
if ritzvec
331
337
# calculating singular vectors
332
338
# left_sv = sqrt(2) * ex[2][ 1:size(X,1), ind ] .* sign.(ex[1][ind]')
333
339
if size (X, 1 ) >= size (X, 2 )
334
340
V = ex[2 ]
335
341
# We cannot assume that X*V is a Matrix even though V is. This is not
336
342
# the case for e.g. LinearMaps.jl so we convert to Matrix explicitly
337
- U = Array (qr (rmul! (convert (Matrix, X* V), Diagonal (inv .(svals) ))). Q)
343
+ U = Array (qr (rmul! (convert (Matrix, X* V), Diagonal ([ inv .(svals[ 1 : r]); ones (nsv - r)] ))). Q)
338
344
else
339
345
U = ex[2 ]
340
346
# We cannot assume that X'U is a Matrix even though U is. This is not
341
347
# the case for e.g. LinearMaps.jl so we convert to Matrix explicitly
342
- V = Array (qr (rmul! (convert (Matrix, X' U), Diagonal (inv .(svals) ))). Q)
348
+ V = Array (qr (rmul! (convert (Matrix, X' U), Diagonal ([ inv .(svals[ 1 : r]); ones (nsv - r)] ))). Q)
343
349
end
344
350
345
351
# right_sv = sqrt(2) * ex[2][ size(X,1)+1:end, ind ]
0 commit comments