[Numpy-discussion] tensor dot ?
Travis Oliphant
oliphant.travis at ieee.org
Mon Aug 28 23:03:29 EDT 2006
Simon Burton wrote:
>>>> numpy.dot.__doc__
>>>>
> matrixproduct(a,b)
> Returns the dot product of a and b for arrays of floating point types.
> Like the generic numpy equivalent the product sum is over
> the last dimension of a and the second-to-last dimension of b.
> NB: The first argument is not conjugated.
>
> Does numpy support summing over arbitrary dimensions,
> as in tensor calculus ?
>
> I could cook up something that uses transpose and dot, but it's
> reasonably tricky i think :)
>
I've just added tensordot to NumPy (adapted and enhanced from
numarray). It allows you to sum over an arbitrary number of axes. It
uses a 2-d dot-product internally as that is optimized if you have a
fast blas installed.
Example:
If a.shape is (3,4,5)
and b.shape is (4,3,2)
Then
tensordot(a, b, axes=([1,0],[0,1]))
returns a (5,2) array which is equivalent to the code:
c = zeros((5,2))
for i in range(5):
for j in range(2):
for k in range(3):
for l in range(4):
c[i,j] += a[k,l,i]*b[l,k,j]
-Travis
More information about the NumPy-Discussion
mailing list