MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

[LinearAlgebra] Re: Matrix Dot Product

  • To: mathgroup at
  • Subject: [mg52227] [LinearAlgebra] Re: Matrix Dot Product
  • From: Paul Abbott <paul at>
  • Date: Mon, 15 Nov 2004 03:17:37 -0500 (EST)
  • Organization: The University of Western Australia
  • References: <cn4lbm$13s$>
  • Sender: owner-wri-mathgroup at

In article <cn4lbm$13s$1 at>,
 "MacDonald, Calum \(MAT\)" <C.A.MacDonald at> 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:



  Total[A B, Length[dim]]


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 

  • Prev by Date: Re: Re: Counting Runs
  • Next by Date: Re: Re: Counting Runs
  • Previous by thread: Clearing local variables and Memory Leaks
  • Next by thread: [DSolve] Re: newbie question DSolve (revisited again)