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

Tony Stillfjord tony@maths.lth...
Tue Sep 11 03:08:50 CDT 2012

```I'm not sure what's going on here, but when I run your MATLAB-code on my
system
(copy-pasted) I get results more in the line of

Elapsed time is 0.007203 seconds.
Elapsed time is 0.031520 seconds.

i.e. much worse than you report but still better than the Python ones. With
scipy I get
4.1 ms vs. 60 ms.

'Sparsifying' the code improves things on my end:

In [73]: n = 10000
In [74]: k = 100
In [75]: r = rand(n)
In [76]: a = scipy.sparse.spdiags(r, 0, n, n)
In [77]: c = scipy.sparse.rand(k,n)
In [78]: d = c.dot(a)
In [79]: e = d.dot(c.T)
In [80]: %timeit d = c.dot(a)
1000 loops, best of 3: 1.52 ms per loop
In [81]: %timeit e = d.dot(c.T)
1000 loops, best of 3: 1.12 ms per loop

Tony

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

> > It's not so clear what kind of improvements you are looking for. Do you
> > perhaps expect that there exist some magic to ignore half of the
> > computations with dot product, when the result is symmetric?
>
> Here's the same test in matlab:
>
> >> n = 10000;
> >> k = 100;
> >> a = spdiags(rand(n,1),0,n,n);
> >> c = rand(k,n);
> >> tic, d = c*a; toc
> Elapsed time is 0.007769 seconds.
> >> tic, d = d*c'; toc
> Elapsed time is 0.007782 seconds.
>
> (vs, in scipy:
>
> >>> tic(); d = c.T * a; toc()
> Elapsed time: 0.00560903549194
> >>> tic(); e = dot( d, c ); toc()
> Elapsed time: 0.110434055328
> )
> _______________________________________________
> 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/731094c5/attachment.html
```