Reputation: 2569
I have a large bidimensional ndarray
, A
and I want to compute the SVD retrieving largest eigenvalue and associated eigenvector pair. Looking at NumPy docs it seems that NumPy can compute the complete SVD only (numpy.linalg.svd
), while SciPy has method that does exactly what I need (scipy.sparse.linalg.svds
), but with sparse matrices and I don't want to perform a conversion of A
, since it would require additional computational time.
Until now, I have used SciPy svds
directly on the A
however the documentation discourages to pass ndarray
s to these methods.
Is there a way to perform this task with a method that accepts ndarray
objects?
Upvotes: 2
Views: 2491
Reputation: 231335
If svds
works with your dense A
array, then continue to use it. You don't need to convert it to anything. svds
does all the adaptation that it needs.
It's documentation says
A : {sparse matrix, LinearOperator} Array to compute the SVD on, of shape (M, N)
But what is a LinearOperator
? It is a wrapper around something that can perform a matrix product. For a dense array A.dot
qualifies.
Look at the code for svds
. The first thing is does is A = np.asarray(A)
if A
isn't already a Linear Operator or sparse matrix. Then it grabs A.dot
and (hemetianA).dot
and makes a new LinearOperator.
There's nothing special about a sparse matrix in this function. All that matters is having a compatible matrix product.
Look at these times:
In [358]: A=np.eye(10)
In [359]: Alg=splg.aslinearoperator(A)
In [360]: Am=sparse.csr_matrix(A)
In [361]: timeit splg.svds(A)
1000 loops, best of 3: 541 µs per loop
In [362]: timeit splg.svds(Alg)
1000 loops, best of 3: 964 µs per loop
In [363]: timeit splg.svds(Am)
1000 loops, best of 3: 939 µs per loop
Direct use of A
is fastest. The conversions don't help, even when they are outside of the timing loop.
Upvotes: 4