[SciPy-User] How to efficiently do dot(dot( A.T, diag(d) ), A ) ?

Hugh Perkins hughperkins@gmail....
Tue Sep 11 04:10:25 CDT 2012


> 'Sparsifying' the code improves things on my end:

It did for me too :-)

But then I noticed that sparse.rand produces a matrix with no elements:

>>> from scipy import sparse
>>> sparse.rand(2,2)
<2x2 sparse matrix of type '<type 'numpy.float64'>'
	with 0 stored elements in COOrdinate format>
>>> sparse.rand(2,2).todense()
matrix([[ 0.,  0.],
        [ 0.,  0.]])

You can make a sparse rand matrix from a dense one:

c = sparse.coo_matrix(scipy.rand(n,k))

This gives worse timings than a non-sparse c for me:

>>> c = rand(n,k);
>>> tic(); d = c.T * a; toc()
Elapsed time: 0.0610790252686
>>> tic(); e = dot( d, c ); toc()
Elapsed time: 0.239707946777

>>> import scipy.sparse as sparse
>>> c = sparse.coo_matrix(scipy.rand(n,k))
>>> tic(); d = c.T * a; toc()
Elapsed time: 0.137360095978
>>> tic(); e = dot( d, c ); toc()
Elapsed time: 1.74925804138


More information about the SciPy-User mailing list