1
votes

I'm using Python, Numpy and Scipy packages to do matrix computations. I am attempting to perform the calculation X.transpose() * W * X where X is a 2x3 dense matrix and W is a sparse diagonal matrix. (Very simplified example below)

import numpy
import scipy.sparse as sp

X = numpy.array([[1, 1, 1],[2, 2, 2]])

W = sp.spdiags([1, 2], [0], 2, 2).tocsr()

I need to find the product of the Dense Matrix X.transpose and sparse matrix W.

The one method that I know of within scipy does not accept a sparse matrix on the right hand side.

>>> sp.csr_matrix.dot(X.transpose(), W)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method dot() must be called with csr_matrix instance as first argument (got ndarray instance instead)

Is there a way to multiply a sparse and dense matrix where the sparse matrix is the term on the right within scipy? If not, what is the best way to do this without turning my W into a dense matrix?

1

1 Answers

1
votes

Matrix multiplication is associative, so you can always first compute W * X:

>>> X.T.dot(W.dot(X))
array([[9, 9, 9],
       [9, 9, 9],
       [9, 9, 9]])

If you really have to compute X.T * W, the first dense, the second sparse, you can let the sparse matrix __mul__ method take care of it for you:

>>> X.T * W
array([[1, 4],
       [1, 4],
       [1, 4]])

Actually, for your use case, if you use np.matrix instead of np.array, your particular operation becomes surprisingly neat to code:

>>> Y = np.matrix(X)
>>> Y.T * W * Y
matrix([[9, 9, 9],
        [9, 9, 9],
        [9, 9, 9]])