I was working on implementing a solver for sparse undetermined systems in Python (discussed here) and I was trying to rebuild the nullspace function that uses the standard numpy svd function (numpy.linalg.svd
) in the SciPy cookbook using the scipy.sparse version of svd (scipy.sparse.linalg.svds
) but it outputs different left and right singular vectors for the examples I ran - including the matrices:
[[1,1,0,0,0],[0,0,1,1,0],[1,1,1,1,1]]
[[1,0,1],[1,1,0],[0,1,1]]
Why do these two solvers produce two different svd outputs for the matrices above? What can I do to ensure the same output?
Edit
Here's an example: table is a csc_matrix
such that
table.todense() = matrix([[1,1,0,0,0],[0,0,1,1,0],[1,1,1,1,1]],dtype=int64)
So, the following code outputs
numpy.linalg.svd(table.todense()) =
[[ -3.64512933e-01 7.07106781e-01 -6.05912800e-01]
[ -3.64512933e-01 -7.07106781e-01 -6.05912800e-01]
[ -8.56890100e-01 2.32635116e-16 5.15499134e-01]]
-----------------------------------------------------
[ 2.58873755 1.41421356 0.54629468]
-----------------------------------------------------
[[ -4.7181e-01 -4.7181e-01 -4.7181e-01 -4.7181e-01 -3.3101e-01]
[5e-01 5e-01 -5e-01 -5e-01 6.16450329e-17]
[-1.655e-01 -1.655e-01 -1.655e-01 -1.655e-01 9.436e-01]
[5e-01 -5e-01 -5e-01 5e-01 -1.77302319e-16]
[-5e-01 5e-01 -5e-01 5e-01 2.22044605e-16]]
And the following
scipy.sparse.linalg.svds(table,k=2)=
[[ 7.07106781e-01, -3.64512933e-01],
[ -7.07106781e-01, -3.64512933e-01],
[ 2.73756255e-18, -8.56890100e-01]]
-------------------------------------
[ 1.41421356, 2.58873755]
-------------------------------------
[[ 5e-01, 5e-01, -5e-01, -5e-01, 1.93574904e-18],
[ -4.71814e-01, -4.71814e-01, -4.71814e-01, -4.71814e-01, -3.31006e-01]]
Note that there are quite a few values that overlap between the two solutions. Also, the scipy.sparse.linalg.svds
function doesn't allow k to be greater than or equal to min(table.shape)
, which is why I chose k=2.
singular values
(s
) is the main thing they promise. The k largests
values match. SVD is not unique. en.wikipedia.org/wiki/… – SpeakARPACK
implementation, whichsvds
also cites. MATLAB has bothsvd
andsvds
. Also fa.bianp.net/blog/2012/singular-value-decomposition-in-scipy – Speak