0

I have a sparse_csr matrix c and d:

a=np.array([[1,0,1],[0,0,1]])
b=np.array([[1,1,1],[1,0,1],[0,0,0]])

c=sparse.csr_matrix(a)
d=sparse.csr_matrix(b)

I am iteratively multiplying c and d like this:

for j in range(3):
    c_new = d.dot(c.T).T
    c = c_new

I observed the type(c) is <class 'scipy.sparse.csc.csc_matrix'>. This slows down to do matrix computation for large matrix inside every iteration. Is there any way to speed up the process keeping the "csr" format.

Mohan Timilsina
  • 490
  • 6
  • 25
  • So you are doing `(d * d * d * c.T).T`; repeated or chained matrix products, not iterative ones. Matrix multiplication is a strong point for sparse matrices - for large, really sparse ones. For small relatively dense ones it will slower than dense array `dot`. – hpaulj Mar 15 '18 at 18:34
  • 1
    Do you want just 3 repetitions, or some variable number. It looks a bit like a matrix power calculation. `d**3 * c.T`; https://stackoverflow.com/questions/28702416/matrix-power-for-sparse-matrix-in-python – hpaulj Mar 15 '18 at 18:40
  • For repeated dense matrix product see https://stackoverflow.com/q/48801371/901925 – hpaulj Mar 18 '18 at 03:35

0 Answers0