Eigenvalues and Eigenvectors of large sparse matrix in Python Scipy - python

My Python code generates upper triangle of a matrix in coordinate (COO) format (row,col,mat_element) and stores non-zero elements only to decrease memory usage. However, it is sparse symmetric matrix and I wish to obtain it's eigenvalues and eigenvectors using 'eigsh' from 'scipy.sparse.linalg'. How can I do this?
I would prefer to convert COO to CSR matrix directly and give it as an input for 'eigsh'. But if that is not possible I would like to make the matrix symmetric first and then convert it to CSR format.
COO and CSR matrix wiki:
https://en.wikipedia.org/wiki/Sparse_matrix

Related

CSC vs. CSR matrix vector multiplication

I have a python program that solves iterative methods for linear problem where the input data matrix is sparse.
My question is that which of the two compressed scipy.sparse formats, csc or csr, performs a faster matrix vector multiplication A#x and why? In other words, if I want to multiply a matrix from the left side to a vector is it more efficient to have it compressed in csc or csr format?

Diagonal of sparse 4D matrix

This is question is the same as this, but for a sparse matrix (scipy.sparse). The solution given to the linked question used indexing schemes that are incompatible with sparse matrices.
For context I am constructing a Jacobian for a large discretized PDE, so the B matrix in this case contains various relevant partial terms while A will be the complete Jacobian I need to invert for a Newton's method approximation. On a large grid A will be far too large to fit in memory, so I want to use sparse matrices.
I would like to construct an array with the following structure:
A[i,j,i,j,] = B[i,j] with all other entries 0: A[i,j,l,k]=0 # (i,j) =\= (l,k)
I.e. if I have the B matrix constructed how can I create the matrix A, preferably in a vectorized manner.
Explicitly, let B = [[1,2],[3,4]]
Then:
A[1,1,:,:]=[[1,0],[0,0]]
A[1,2,:,:]=[[0,2],[0,0]]
A[2,1,:,:]=[[0,0],[3,0]]
A[2,2,:,:]=[[0,0],[0,4]]

How to obtain correct L and U matrices from LU decomposition of a sparse matrix A, without using scipy.sparse.linalg.splu()?

I have noticed that scipy.sparse.linalg.splu() does not allow me to decompose a sparse matrix A into the correct L and U matrix that I can call separately. The command ''merely'' allows me to decompose the matrix and reconstruct it later on using the permutation matrix. However, for my code I need to decompose a sparse matrix A into a sparse matrix L and U and then be able to call the L and U matrices separately (without permutation matrices etc.). This does not work when using the scipy.sparse.linalg.splu() command. I could use scipy.linalg.lu() but I cannot apply this to a matrix A in sparse format. Are there any other methods out there for obtaining the correct L and U decomposition matrices from a sparse matrix A? Thanks in advance.

Finding largest eigenvalue in sparse matrix

I'm using numpy and scipy. I have a large sparse matrix and I want to find the largest eigenvalue of the sparse matrix. How can I do that?
I use scipy.sparse.linalg.eigsh for symmetric sparse matrices passing which="LM":
eigvals, eigvecs = eigsh(A, k=10, which='LM', sigma=1.)
but you should definitely read the documentation.

Determine sparsity of sparse matrix ( Lil matrix )

I have a large sparse matrix, implemented as a lil sparse matrix from sci-py. I just want a statistic for how sparse the matrix is once populated. Is there a method to find out this?
m.nnz is the number of nonzero elements in the matrix m, you can use m.size to get the total number of elements.

Categories