Question

I have a sparse matrix A and a column vector a,

In[1]: A
Out[1]: <256x256 sparse matrix of type '<type 'numpy.float64'>'
with 512 stored elements (blocksize = 2x2) in Block Sparse Row format>

In[2]: len(a)
Out[2]: 70

I would to write a submatrix. The code I wrote for this is:

Sub = A.tolil()[a,:][:,a]

The time needed to solve the problem is quite long. I woul like to know if is possible to modify the code in order to make the process faster.

Was it helpful?

Solution

csr matrices have fast row slicing; csc matrices have fast column slicing. Conversions between most sparse types are well optimized, so I would suggest trying to use the appropriate type for the kind of slice to be performed.

Sub = A.tocsr()[a,:].tocsc()[:,a]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top