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

eat e.antero.tammi@gmail....
Mon Sep 10 17:58:11 CDT 2012

```Hi,

On Mon, Sep 10, 2012 at 7:34 PM, Hugh Perkins <hughperkins@gmail.com> wrote:

> > > How to do efficiently do dot(dot( A.T, diag(d) ), A ) ?
> >
> > dot( A.T * d , A )
>
> This is very good!
>
> Still, the second multiplication looks like it is doing a full
> brute-force matrix multiplication:
>
> >>> tic(); d = c.T * a; toc()
> Elapsed time: 0.00560903549194
> >>> tic(); e = dot( d, c ); toc()
> Elapsed time: 0.110434055328
>
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? Anyway, here
is a simple demonstration to show that dot product is fairly efficient
In []: m, n= 500000, 5
In []: A, d= randn(m, n), randn(m)
In []: %timeit A.T* d
10 loops, best of 3: 34.4 ms per loop
In []: mps= m* n/ .0344 # multiplications per second
In []: c= A.T* d
In []: %timeit dot(c, A)
10 loops, best of 3: 68 ms per loop
In []: masps= m* n** 2/ .068 # multiplications and summations per second
In []: masps/ mps
Out[]: 2.529411764705882

In []: allclose(dot(c, A), dot(A.T* d, A))
Out[]: True
In []: sys.version
Out[]: '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
In []: np.version.version
Out[]: '1.6.0'

Thus multiplication and summation with dot product is some 2.5 times faster
than simple multiplication.

My 2 cents,
-eat

> _______________________________________________
> 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/e5fffcd8/attachment.html
```