[PYTHON MATRIX-SIG] default axes
Carlos Fonseca
fonseca@gaivota.demon.co.uk
Thu, 15 Aug 1996 19:39:10 +0100 (BST)
On 15 Aug 1996, Janne Sinkkonen wrote:
>
> If add.reduce() would operate on -1, then a dot like proposed in 1)
> would be equivalent to add.reduce(a*b) which is not too difficult to
> write, thus that kind of dot() would be more redundant than 3). On the
> other hand, Konrad had a good argument against reducing -1 (python's
> standard reduce reduces axis 0), so I'm not sure anymore about that.
I really think dot(a,b) should behave _exactly_ like
add.reduce(a*b,axis=-1)
The big difference is that, unlike the expression above, there would be no
need to allocate memory to store a*b, just the final result.
If, for example,
a.shape=(m,n)
b.shape=(n,p)
then
dot(a[...,NewAxis],b,axis=-2).shape = (m,p) -> matrix multiplication
whithout having to allocate an array of shape=(m,n,p), as add.reduce
would.
In general, by adding axes as needed and broadcasting dimensions, one can
perform anything from "vectorized" dot products to "vectorized" array
multiplication of arbitrary rank. I proposed this to the list one or two
months ago, but then never got back to it.
Clearly, dot() should allow the default axis to be specified. Note that
*one* axis seems to be enough: for a dot product of row vectors, all axes
must be aligned. For "intermediate" rank multiplication, some axes must be
aligned (assume the outer), while correctly adding the necessary
additional axes will align the inner.
Full rank multiplication of two arrays could be written:
dot(a[...,(n-1)*(NewAxis,)],b,axes=-n)
where n is the number of dimensions of b, and assuming the last dimension
of a matches the first of b.
> How about two functions? dot() would be like 3), and then there could
> be 'vdot()' (vector dot) like 1)? vdot() would eliminate the commonly
> needed add.reduce(a,b,-1), thus add.reduce() could reduce over 0 by
> default.
I believe this would be the way to go. Not that it really matters, but I
would call your vdot() just dot(), and give matrix multiplication another
name (mtimes(), mmult(), mprod(), whatever). All cases of matrix
multiplication are possible with [v]dot(), yet I agree that adding axes by
hand could be done better in a function built on top of dot().
Konrad> But evidently dot() needs two optional axis arguments to cover
all cases.
>
> I agree.
> --
> Janne
See above.
Carlos
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================