# [SciPy-user] advice on inner product, element by element

Joseph Anderson J.Anderson@hull.ac...
Mon Jun 30 10:38:56 CDT 2008

```Ah, ha. . . thanks Pauli, this was just the sort of thing I was hoping to see. . . the trouble being I'm still tending to think in terms of loops.

Thanks for the help!!

My best,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr Joseph Anderson
Lecturer in Music

School of Arts and New Media
University of Hull, Scarborough Campus,
Scarborough, North Yorkshire, YO11 3AZ, UK

T: +44.(0)1723.357341 T: +44.(0)1723.357370 F: +44.(0)1723.350815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-----Original Message-----
From: scipy-user-bounces@scipy.org on behalf of Pauli Virtanen
Sent: Sun 06/29/2008 8:27 PM
To: scipy-user@scipy.org
Subject: Re: [SciPy-user] advice on inner product, element by element

Sun, 29 Jun 2008 19:45:48 +0100, Joseph Anderson wrote:
[clip]
> Example code:
>
> # parms
> cs = 4
> fs = 60 * 44100
>
> a = reshape(arange(fs * cs), (fs, cs))
> b = reshape(arange(fs * cs * cs), (fs, cs, cs))
>
> c1 = array([inner(val_a, val_b) for val_a, val_b in zip(a, b)])
> c2 = asarray(map(inner, a, b))

Something like this:

c3 = (a[:,None,:]*b).sum(axis=2)

Note that "None" is the same as "newaxis". (Also note that inner
does an inner product along the last dimension by default, but you
probably knew that.)

Timings:

In [49]: %time c1 = array([inner(val_a, val_b) for val_a, val_b in zip(a, b)])
CPU times: user 1.07 s, sys: 0.04 s, total: 1.10 s
Wall time: 1.34 s

In [51]: %time c2 = asarray(map(inner, a, b))
CPU times: user 0.65 s, sys: 0.01 s, total: 0.66 s
Wall time: 0.82 s

In [53]: %time c3 = (a[:,None,:]*b).sum(axis=2)
CPU times: user 0.07 s, sys: 0.00 s, total: 0.07 s
Wall time: 0.13 s

In [60]: a[:,None,:].shape
Out[60]: (88200, 1, 4)

In [61]: b.shape
Out[61]: (88200, 4, 4)

In [55]: allclose(c1, c2)
Out[55]: True

In [56]: allclose(c1, c3)
Out[56]: True

_______________________________________________
SciPy-user mailing list
SciPy-user@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3669 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20080630/6d8c3be2/attachment-0001.bin
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
Url: http://projects.scipy.org/pipermail/scipy-user/attachments/20080630/6d8c3be2/attachment-0001.pl
```