[SciPy-user] Re: [SciPy-dev] matrixmultiply()
Robert Kern
rkern at ucsd.edu
Fri Apr 8 19:32:16 EDT 2005
David Grant wrote:
> Can anyone tell me why the * operation on matrices is for element-wise
> multiplcation and not matrix multiplication for Numeric/numpy arrays?
Not everyone is using Numeric for linear algebra on matrices.
Element-wise multiplication is the most consistent interpretation for *
in this general-purpose context.
Sine you *are* doing linear algebra on matrices, you can use Matrix
objects, which do define the * operator to be matrix multiplication.
In [8]:A = mat([[0,1],[2,3]])
In [9]:B = mat([[5,6],[7,8]])
In [10]:A*B
Out[10]:
Matrix([[ 7, 8],
[31, 36]])
In [11]:dot(A, B)
Out[11]:
array([[ 7, 8],
[31, 36]])
> Or at least tell me a nice way to do A*B*C*D*E*F, where the * denotes
> matrix multiplication. I once wrote a function where I passed those to a
> function as arrary of arrays, so mult([A,B,C,D,E,F]) and the function
> multiplied them all together. But I can't remember where I put this
> function, and I was wondering if there was an easier way.
Alternatively, you can do a bit of magic with this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122
In [25]:d = Infix(dot)
In [26]:a = array([[0,1],[2,3]])
In [27]:b = array([[5,6],[7,8]])
In [28]:dot(a,b)
Out[28]:
array([[ 7, 8],
[31, 36]])
In [29]:a |d| b
Out[29]:
array([[ 7, 8],
[31, 36]])
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the SciPy-User
mailing list