Search code examples
scipysparse-matrix

Sparse matrices in SciPy defined by a function


Is it possible to define a sparse matrix in scipy from a function rather than the laying out all the possible values? In the doc's I see that a sparse matrix can be created by

There are seven available sparse matrix types:
 csc_matrix: Compressed Sparse Column format
 csr_matrix: Compressed Sparse Row format
 bsr_matrix: Block Sparse Row format
 lil_matrix: List of Lists format
 dok_matrix: Dictionary of Keys format
 coo_matrix: COOrdinate format (aka IJV, triplet format)
 dia_matrix: DIAgonal format

All of these force you to specify the matrix beforehand, which takes up memory. Is there a way I can simply supply a function to calculate (i,j) when needed? The end goal is to calculate the few largest eigenvectors of the matrix through something like a Lanczos method.


Solution

  • Short answer is "no", but it's pretty easy i think to roll your own matrix-like object. If you are using eigsh to get your answer, (which appears to be an implementation of the Lanczos algorithm.), then your matrix-like requires a matvec(x) method, which may or may not be easy.

    I realize this is not a complete answer, but I hope this sets you on your way.