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

Tony Stillfjord tony@maths.lth...
Tue Sep 11 04:30:04 CDT 2012

```Oh. Of course. Sorry about that, I forgot about the density argument to
sparse.rand
and didn't check my results.

That certainly explains why it was so fast...and since d can't be expected
to be sparse
the sparse approach is kind of dumb.

Tony

On Tue, Sep 11, 2012 at 11:10 AM, Hugh Perkins <hughperkins@gmail.com>wrote:

> > '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
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120911/5f758162/attachment.html
```