# [Numpy-discussion] dot operations on multidimensional arrays

Wed Dec 6 00:42:47 CST 2006

```Try numpy.tensordot

-----Original Message-----
From: numpy-discussion-bounces at scipy.org
[mailto:numpy-discussion-bounces at scipy.org] On Behalf Of Carsten
Rostgaard
Sent: Thursday, November 23, 2006 10:06
To: numpy-discussion at scipy.org
Subject: [Numpy-discussion] dot operations on multidimensional arrays

Hi!
I am trying to use the "dot" method on multi-(more than 2)-dimensional
arrays.

Specifically I do
>> y = dot(a, b)
where a is a 2D array and b is a 3D array.

using numpy I get the the help:
"
dot(...)
dot(a,v) returns matrix-multiplication between a and b.
The product-sum is over the last dimension of a and the
second-to-last dimension of b.
"
I then expect that
>> y[i, j, k] = sum(a[i, :] * b[j, :, k])
which is actually what I get.

The question is then:
1) Is there any way to change the axis for which the product-sum is
performed. This can of course be done by a swapaxis before and after the
operation, but this makes the array non-contiguous, in which case the
dot operation often makes bugs (at least in Numeric).
2) For complicated reasons we still use Numeric in our software package,
and in this, "dot" behaves very strangely.
According to the Numeric help:
"
dot(a, b)
dot(a,b) returns matrix-multiplication between a and b.  The
product-sum
is over the last dimension of a and the second-to-last dimension of
b.
"
so I would have expected again that y[i, j, k] = sum(a[i, :] * b[j, :,
k]), and the dimensions actually fit, i.e.
y.shape = (a.shape[0], b.shape[0], b.shape[2]), but only some rows of
the result has these values!! Does anyone know what Numeric.dot(a, b)
actually does when b has more than two dimensions?

I use the following test script:
---------------------BEGIN SCRIPT-----------------------
import Numeric as num
# import numpy as num

# make 'random' input arrays
a = num.zeros((2, 5))
b = num.zeros((3, 5, 4))
a.flat[:] = num.arange(len(a.flat)) - 3
b.flat[:] = num.arange(len(b.flat)) + 5

# built-in dot product
y1 = num.dot(a, b)

# manual dot product
y2 = num.zeros((a.shape[0], b.shape[0], b.shape[2]))
for i in range(a.shape[0]):
for j in range(b.shape[0]):
for k in range(b.shape[2]):
y2[i, j, k] = num.sum(a[i,:] * b[j, :, k])

# test for consistency
print y1 == y2
---------------------END SCRIPT------------------------
with the result:
[[[1 1 1 1]
[0 0 0 0]
[0 0 0 0]]
[[1 1 1 1]
[0 0 0 0]
[0 0 0 0]]]

thanks a lot,
Carsten Rostgaard
Carsten.Rostgaard at fysik.dtu.dk

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion
```