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

Pauli Virtanen pav@iki...
Tue Sep 11 12:15:27 CDT 2012


First a quick note: Please, when posting any benchmark result, always
include the full test code for each case.

11.09.2012 05:43, Hugh Perkins kirjoitti:
>> 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:

You are here bencmarking the underlying BLAS libraries. Matlab comes
with Intel MKL, whereas your Numpy/Scipy is likely linked with ATLAS.

MKL can be faster than ATLAS, but on the other hand you can also link
the Numpy/Scipy combination against MKL (provided you buy a license from

In your Matlab example, the first matrix product is a sparse-dense one,
whereas the second is dense-dense (MKL-fast). That the speeds of the two
operations happen to coincide is likely a coincidence.

In the Numpy case without sparse matrices, the first product is
broadcast-multiplication (faster than a sparse-dense matrix product),
whereas the second product is a dense-dense matrix multiplication.

Here are results on one (slowish) machine:
import numpy as np
n = 10000
k = 100
a = np.random.rand(n)
c = np.random.rand(k,n)
d = c*a
e = np.dot(d, c.T)
%timeit d = c*a
# -> 100 loops, best of 3: 11 ms per loop
%timeit e = np.dot(d, c.T)
# -> 10 loops, best of 3: 538 ms per loop
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.018380 seconds.
tic, e = d*c'; toc
% -> Elapsed time is 0.138673 seconds.

Pauli Virtanen

More information about the SciPy-User mailing list