From: izak m. <iza...@ya...> - 2006-11-08 13:54:33
|
Hi Sorry if this is an obvious question, but what is the easiest way to multiply matrices in numpy? Suppose I want to do A=B*C*D. The ' * ' operator apparently does element wise multiplication, as does the 'multiply' ufunc. All I could find was the numeric function 'matrix_multiply, but this only takes two arguments. Thanks in advance! Izak --------------------------------- Sponsored Link Degrees online in as fast as 1 Yr - MBA, Bachelor's, Master's, Associate - Click now to apply |
From: Keith G. <kwg...@gm...> - 2006-11-08 14:33:15
|
On 11/8/06, izak marais <iza...@ya...> wrote: > Sorry if this is an obvious question, but what is the easiest way to > multiply matrices in numpy? Suppose I want to do A=B*C*D. The ' * ' operator > apparently does element wise multiplication, as does the 'multiply' ufunc. > All I could find was the numeric function 'matrix_multiply, but this only > takes two arguments. If B and C and D are matrices, then '*' is matrix multiplication. |
From: Charles R H. <cha...@gm...> - 2006-11-08 14:49:52
|
On 11/8/06, Keith Goodman <kwg...@gm...> wrote: > > On 11/8/06, izak marais <iza...@ya...> wrote: > > > Sorry if this is an obvious question, but what is the easiest way to > > multiply matrices in numpy? Suppose I want to do A=B*C*D. The ' * ' > operator > > apparently does element wise multiplication, as does the 'multiply' > ufunc. > > All I could find was the numeric function 'matrix_multiply, but this > only > > takes two arguments. Same with the operator *, it takes two arguments but is in infix order, i.e., left side and right side. If B and C and D are matrices, then '*' is matrix multiplication. And if they are arrays: A = dot(B,dot(C,D)) Python has a dearth of recognized operators which makes this necessary once '*' is used for elementwise multiplication, it's a long standing complaint. You can use matrices in numpy, in which case '*' is used for matrix multiplication like in matlab, but I think it would be better to get used to using arrays as they are the numpy core. Chuck |
From: Stefan v. d. W. <st...@su...> - 2006-11-08 14:41:12
|
On Wed, Nov 08, 2006 at 05:54:17AM -0800, izak marais wrote: > Hi >=20 > Sorry if this is an obvious question, but what is the easiest way to mu= ltiply > matrices in numpy? Suppose I want to do A=3DB*C*D. The ' * ' operator a= pparently > does element wise multiplication, as does the 'multiply' ufunc. All I c= ould > find was the numeric function 'matrix_multiply, but this only takes two > arguments. You can also do N.dot(B, N.dot(C,D)). Cheers St=E9fan |
From: Roberto De A. <ro...@de...> - 2006-11-08 14:45:35
|
On 11/8/06, Keith Goodman <kwg...@gm...> wrote: > > Sorry if this is an obvious question, but what is the easiest way to > > multiply matrices in numpy? Suppose I want to do A=B*C*D. > If B and C and D are matrices, then '*' is matrix multiplication. I think the difference between arrays and matrices is not clear for him (he's clearly multiplying arrays). Izak, you should first convert you arrays to matrices using ``numpy.matrix``. --Rob |
From: Albert S. <as...@di...> - 2006-11-08 14:47:52
|
> Izak, you should first convert you arrays to matrices using ``numpy.matrix``. or numpy.asmatrix() > > --Rob > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion |
From: Sven S. <sve...@gm...> - 2006-11-08 14:52:25
|
izak marais schrieb: > Hi > > Sorry if this is an obvious question, but what is the easiest way to > multiply matrices in numpy? Suppose I want to do A=B*C*D. The ' * ' > operator apparently does element wise multiplication, as does the > 'multiply' ufunc. All I could find was the numeric function > 'matrix_multiply, but this only takes two arguments. > > Thanks in advance! > Izak There are (at least) two ways: You can use 'dot', possibly nested. or you can convert your arrays into the matrix subclass, for which '*' is matrix multiplication. I.e. mat(B)*mat(C)*mat(D) does what you want. If you "only" deal with algebra-style matrices (2d-arrays), consider using the matrix subclass as much as you can. E.g. use the functions in numpy.matlib to build your inputs. -sven |
From: Joris De R. <jo...@st...> - 2006-11-08 15:01:57
|
[im]: Sorry if this is an obvious question, but what is the easiest way to multiply matrices in numpy? Suppose I want to do A=B*C*D. The ' * ' operator apparently does element wise multiplication, as does the 'multiply' ufunc. [im] All I could find was the numeric function 'matrix_multiply, but this only takes two arguments. Have a look at the examples "dot()" and "mat()" in the Numpy Example List. http://www.scipy.org/Numpy_Example_List J. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm |
From: Bill B. <wb...@gm...> - 2006-11-08 23:40:18
|
Also have a look at the section on Arrays vs Matrices in the Numpy for Matlab users page. That particular section has nothing to do with Matlab, really. http://www.scipy.org/NumPy_for_Matlab_Users Most of the suggestions and comments made here are already on that page. --bb On 11/8/06, Joris De Ridder <jo...@st...> wrote: > > [im]: Sorry if this is an obvious question, but what is the easiest way to multiply matrices in numpy? Suppose I want to do A=B*C*D. The ' * ' operator apparently does element wise multiplication, as does the 'multiply' ufunc. > [im] All I could find was the numeric function 'matrix_multiply, but this only takes two arguments. > > Have a look at the examples "dot()" and "mat()" in the Numpy Example List. > http://www.scipy.org/Numpy_Example_List > > J. > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Johannes L. <a.u...@gm...> - 2006-11-08 16:16:31
|
Hi, in extension to the previous answers, I'd like to say that it is strongly preferable to use dot(A,dot(B,C)) or dot(dot(A,B),C) instead of A*B*C. The reason is that with dot(), you can control of which operation is performed first, which can *massively* influence the time needed, depending on the involved matrices. A*B*C will always be evaluated left-to-right (if I remember correctly). Johannes |
From: Sven S. <sve...@gm...> - 2006-11-08 22:00:35
|
Johannes Loehnert schrieb: > Hi, > > in extension to the previous answers, I'd like to say that it is strongly > preferable to use dot(A,dot(B,C)) or dot(dot(A,B),C) instead of A*B*C. > > The reason is that with dot(), you can control of which operation is performed > first, which can *massively* influence the time needed, depending on the > involved matrices. A*B*C will always be evaluated left-to-right (if I > remember correctly). > Well what about A*(B*C)? |
From: Johannes L. <a.u...@gm...> - 2006-11-09 08:01:05
|
> Well what about A*(B*C)? Point taken... Johannes |