[Numpy-discussion] dot operations on multidimensional arrays
Carsten Rostgaard
Carsten.Rostgaard at fysik.dtu.dk
Thu Nov 23 02:05:57 CST 2006
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
More information about the Numpy-discussion
mailing list