CSC vs. CSR matrix vector multiplication - python

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?

Related

How do I do this calculation most efficiently with scipy sparse matrices?

Suppose I have the following:
A complex-valued sparse matrix Q (and its conjugate transpose is Q^H)
A complex-valued diagonal matrix D
A real-valued sparse vector v
and I wish to compute as efficiently as possible, using scipy sparse matrices, the product
Q # (D # (Q^H # v)),
where the #s are matrix-vector multiplication and the bracketed order is chosen to avoid matrix-matrix multiplication. I am new to use of sparse matrices, and the available types confuse me. How would I best compute this in scipy? In particular, I'm wondering:
Is it inefficient to mix matrix types? (e.g. would it be inefficient if Q were a CSC matrix and v were a CSR matrix - is there any general requirement for them all to be the same?)
For each of Q, D, and v, which scipy sparse representation would be best to do efficient matrix-vector multiplication?
If I were to port the code to run on my GPU using cupy, are there any additional factors I'd need to consider for efficiency?

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]]

Eigenvalues and Eigenvectors of large sparse matrix in Python Scipy

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

sparse circulant construction python

scipy.linalg.circulant provides a nice way of turning a (1-D) numpy array x into a circulant matrix C. Is there any stock way of doing so that preserves sparse representation if x is a scipy.sparse.csr_matrix matrix?

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