[SciPy-User] Help on matrix multiplication with spare result

Fabrice Silva silva at lma.cnrs-mrs.fr
Tue May 12 15:53:51 EDT 2015


Le mercredi 13 mai 2015, Ningping Wang a écrit :
> I have two dense matrices U (10000x50) and V(50x10000), and one sparse
> matrix A(10000x10000). Each element in A is either 1 or 0 and only 1%
> elements are non-zero. I hope to find A*(UV) and I write A*(U.dot(V) in
> SciPy, noting that '*' is element-wise multiplication. Here SciPy will
> calculate a dense matrix UV first, then filter the result using A. But UV
> is dense and large (10000x10000) so it's very slow.
> 
> Because I only need a few elements of UV indicated by A, it should save a
> lot of time if only necessary elements are calculated. Is there a way to
> instruct scipy to do this?
> 
> BTW, I used Matlab to solve this problem before and Matlab is smart enough
> to find what I'm trying to do and works efficiently. In Matlab I write like
> this: A.*(U*V).

You may then be more comfortable using indexing on nonzeros values of A
Assuming A is some scipy sparse matrix:

from itertools import izip, count
Atmp = A.tocoo()
res = np.zeros(Atmp.nnz)
for ind, row, col in izip(count(), Atmp.row, Atmp.col):
	res[ind] = np.sum(U[row, :] * V[:, col])
res = scipy.sparse.coo_matrix((res, (Atmp.row, Atmp.col)))

should (not tested) compute it cheaply.

-- 
Fabrice




More information about the SciPy-User mailing list