[LinearAlgebra] Re: Matrix Dot Product

*To*: mathgroup at smc.vnet.net*Subject*: [mg52227] [LinearAlgebra] Re: Matrix Dot Product*From*: Paul Abbott <paul at physics.uwa.edu.au>*Date*: Mon, 15 Nov 2004 03:17:37 -0500 (EST)*Organization*: The University of Western Australia*References*: <cn4lbm$13s$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

In article <cn4lbm$13s$1 at smc.vnet.net>, "MacDonald, Calum \(MAT\)" <C.A.MacDonald at gcal.ac.uk> wrote: > I was wondering if someone could please help me with a command for > calculating the dot product of two (NxN) matrices. > > For example, for two (2x2) matrices, A and B, we define the dot product > as: > > A(1,1)*B(1,1) + A(2,1)*B(2,1) + A(1,2)*B(1,2) + A(2,2)*B(2,2) > > i.e. we multiply corresponding entries of the matrices and sum these > values. This is, of course, not Mathematica's Dot product. > It is easy to write this in a loop but for large matrices the > calculation is rather slow. > > Is there a Mathematica function that I can call that will allow me to do > this faster? As others have pointed out, you can use Dot after applying Flatten to each matrix. A faster solution is to use direct "element by element" multiplication and then apply Total. For example, the following code (borrowed from Bob Hanlon) works in any number of dimensions: dim={2,3}; A=Array[a,dim]; B=Array[b,dim]; Total[A B, Length[dim]] Cheers, Paul -- Paul Abbott Phone: +61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) 35 Stirling Highway Crawley WA 6009 mailto:paul at physics.uwa.edu.au AUSTRALIA http://physics.uwa.edu.au/~paul