3

To my understanding, numpy.sparse.csr_sparse.dot(other) does multiply other to my sparse matrix from the right:

A = numpy.sparse.csr_sparse(something)
B = numpy.matrix(something)
C = A.dot(B)                     # C = A*B

How do I commute the two matrices to get B*A without losing the benefits of saving my matrix as a sparse one (i.e. .todense() etc.)?

Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • 1
    Is there any reason why you can't save `B` as a sparse matrix as well? Trying `sparse.csr_matrix.dot(B, A)` says that first argument must be sparse, but `sparse.csr_matrix.dot(sparse.csr_matrix(B), A)` works, if it's possible for your case. – askewchan Oct 30 '13 at 13:07
  • Also, be aware of [this danger](http://stackoverflow.com/a/14204859/1730674). – askewchan Oct 30 '13 at 13:15

1 Answers1

2

A little refresher of matrix multiplication properties:

D = B * A
D.T = A.T * B.T
D = (A.T * B.T).T

Which then leads to the obvious:

D = A.T.dot(B.T).T

Note that transposition of CSR and CSC matrices is very fast, as it simply changes the shape and the type (from CSR to CSC, or from CSC to CSR), leaving the internal data unchanged.

Jaime
  • 65,696
  • 17
  • 124
  • 159
  • 2
    Yes, I know those properties. My fear was that transposing a column-order-sparse matrix back and forth would mess things up terribly... – Nils Werner Oct 30 '13 at 17:33