Velocity Reviews > Is numarray matrixmultiply supposed to transpose the second argument?

# Is numarray matrixmultiply supposed to transpose the second argument?

Raoul
Guest
Posts: n/a

 07-19-2004
It appears that matrixmultiply(A,B) has a side-effect of transposing the
second argument (B).
Is this supposed to happen?
Is there anyway to prevent it from transposing?

This shows what happens:

#! /usr/bin/env python
from numarray import *

A=array([[350],
[370],
[510],
[490],
[500]])

B=array([[35,37,51,49,50]])

print "A and B before matmul:"
print A
print B

AB=matrixmultiply(A,B)

print "AxB, A, B, after matmul:"
print AB
print A
print B

print"Apparantly, the second argument of matrixmultiply(A,B) is always
transposed."
------------ And now a word from our sponsor ----------------------
For a quality mail server, try SurgeMail, easy to install,
fast, efficient and reliable. Run a million users on a standard
PC running NT or Unix without running out of power, use the best!

Christopher T King
Guest
Posts: n/a

 07-19-2004
On Mon, 19 Jul 2004, Raoul wrote:

> A=array([[350],
> [370],
> [510],
> [490],
> [500]])
>
> B=array([[35,37,51,49,50]])
>
> print "A and B before matmul:"
> print A
> print B
>
> AB=matrixmultiply(A,B)
>
> print "AxB, A, B, after matmul:"
> print AB
> print A
> print B

This works for me (B before == B after). Which version of numarray are
you using?

Raoul Meuldijk
Guest
Posts: n/a

 07-19-2004

>
> This works for me (B before == B after). Which version of numarray are
> you using?
>

It's version 1.0, on Python 2.3+.
I do have one lengthy example where the second argument was not changed
(on one matmul, all the others in that example did transpose), but was
not able to reproduce that in a small example; so it seems not to be
consistent.

For completeness, here's my output, as you can see the row-vector B has
changed into a column-vector.

A and B before matmul:
[[350]
[370]
[510]
[490]
[500]]
[[35 37 51 49 50]]
AxB, A, B, after matmul:
[[12250 12950 17850 17150 17500]
[12950 13690 18870 18130 18500]
[17850 18870 26010 24990 25500]
[17150 18130 24990 24010 24500]
[17500 18500 25500 24500 25000]]
[[350]
[370]
[510]
[490]
[500]]
[[35]
[37]
[51]
[49]
[50]]
Apparantly, the second argument of matrixmultiply(A,B) is always transposed.

Robert Kern
Guest
Posts: n/a

 07-19-2004
Raoul wrote:
> It appears that matrixmultiply(A,B) has a side-effect of transposing the
> second argument (B).

I can verify this on Mac OSX with the latest CVS of numarray. Please
post a bug report to the tracker.

http://sourceforge.net/tracker/?atid...69&func=browse

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Christopher T King
Guest
Posts: n/a

 07-19-2004
On Mon, 19 Jul 2004, Raoul Meuldijk wrote:

> > This works for me (B before == B after). Which version of numarray are
> > you using?

>
> It's version 1.0, on Python 2.3+.
> I do have one lengthy example where the second argument was not changed
> (on one matmul, all the others in that example did transpose), but was
> not able to reproduce that in a small example; so it seems not to be
> consistent.

Okay, I was using 0.9. Upgrading to 1.0 confirms the error. This is a
bug in numarray, but it has already been fixed in CVS. As a temporary
fix, use the original Python definition (ripped from numarraycore.py):

def dot(array1, array2):
return ufunc.innerproduct(array1, swapaxes(array2, -1, -2))

matrixmultiply = dot

This will fix both matrixmultiply and dot, as they are both affected by
the bug.

Raoul Meuldijk
Guest
Posts: n/a

 07-20-2004
Robert Kern wrote:
> Raoul wrote:
>
>> It appears that matrixmultiply(A,B) has a side-effect of transposing the
>> second argument (B).

>
>
> I can verify this on Mac OSX with the latest CVS of numarray. Please
> post a bug report to the tracker.
>
> http://sourceforge.net/tracker/?atid...69&func=browse

I have reported it, thanks.

Raoul Meuldijk
Guest
Posts: n/a

 07-20-2004

> Okay, I was using 0.9. Upgrading to 1.0 confirms the error. This is a
> bug in numarray, but it has already been fixed in CVS. As a temporary
> fix, use the original Python definition (ripped from numarraycore.py):
>
> def dot(array1, array2):
> return ufunc.innerproduct(array1, swapaxes(array2, -1, -2))
>
> matrixmultiply = dot
>
> This will fix both matrixmultiply and dot, as they are both affected by
> the bug.
>

Raoul