[Numpy-discussion] Howto vectorise a dot product ?
Tom K.
tpk@kraussfamily....
Wed Jun 10 22:58:09 CDT 2009
bruno Piguet wrote:
>
> Can someone point me to a doc on dot product vectorisation ?
>
As I posted in the past you can try this one liner:
" numpy.array(map(numpy.dot, a, b))
that works for matrix multiply if a, b are (n, 3, 3). "
This would also work if a is (n, 3, 3) and b is (n, 3, 1)
URL
http://www.nabble.com/Array-of-matrices---Inverse-and-dot-td21666949.html#a21670624
Maybe write a loop of length 3?
For example, taking a linear combination of columns,
out = np.zeros(a.shape[:-1], a.dtype)
for i in range(a.shape[-1]):
out += a[..., i]*b[..., i, :]
Now we can vectorize that loop - here's one with no loop that expands the
right array to a larger intermediate array via broadcasting, does
elementwise multiplication, and then sums along the appropriate dimension:
a=np.random.randn(3,2,2)
b=np.random.randn(3,2,1)
(a[..., np.newaxis]*b[..., np.newaxis, :, :]).sum(len(a.shape)-1)
I think I would still prefer a C implementation ... this isn't terribly
readable or optimal (maybe the numeric police can help me out here....!
Help!)
- Tom
--
View this message in context: http://www.nabble.com/Howto-vectorise-a-dot-product---tp23949253p23974947.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.
More information about the Numpy-discussion
mailing list