Dot

Mathematica computes the dot product of two vectors exactly as we do in Linear Algebra. In the next cell we get the Dot product of two 3-D vectors.

ClearAll["Global`*"] ; {a, b, c} . {x, y, z}

a x + b y + c z

In the next cell we try to compute the Dot product of things that aren't litsts, and we see it doesn't work.

Dot[h[a, b, c], h[x, y, z]]

h[a, b, c] . h[x, y, z]

One of the advantages of Inner is that it can do what we tried to do in the previous cell as we see with the next cell.  

Inner[Times, h[a, b, c], h[x, y, z], Plus]

a x + b y + c z

Below we see that matrix multiplication is also implemented using Dot.

The Dot product of a vector and a matrix

We can compute the Dot product of a vector and a matrix.  In the next cell we get the Dot product of (vector A) with each column of (matrix B).

A = {a1, b1, c1} ; B = {{x1, x2}, {y1, y2}, {z1, z2}} ; A . B A . B === Map[A . #&, Transpose[B]]

{a1 x1 + b1 y1 + c1 z1, a1 x2 + b1 y2 + c1 z2}

True

However, in the next example we get the Dot product of each row of (matrix A) with (vector B).

A = {{x1, x2, x3}, {y1, y2, y3}} ; B = {a1, b1, c1} ; A . B A . B === Map[# . B&, A]

{a1 x1 + b1 x2 + c1 x3, a1 y1 + b1 y2 + c1 y3}

True

The Dot product of two matrices

When Mathematica computes the Dot product of matrices you essentially get the product of the matrices as defined in Linear Algebra. Hence if A is a (m × n) matrix and B is a (n × j) matrix then Dot[A, B] is a (m   j) matrix. A generic example is given in the next cell.

A = {{a1, a2}, {b1, b2}} ; B = {{x1, x2, x3}, {y1, y2, y3}} ; A . B

{{a1 x1 + a2 y1, a1 x2 + a2 y2, a1 x3 + a2 y3}, {b1 x1 + b2 y1, b1 x2 + b2 y2, b1 x3 + b2 y3}}

The Dot product of two tensors

Mathematica can compute the Dot product of tensors with compatible dimensions. In the next cell the Dot product of tensors A and B is computed.

A = {{{a1, a2}, {b1, b2}}, {{c1, c2}, {d1, d2}}} ; B = {   {{w1, w2, w3}, {x1, x2, x3}}, {{y1, y2, y3}, {z1, z2, z3}}   } ; A . B

{{{{a1 w1 + a2 y1, a1 w2 + a2 y2, a1 w3 + a2 y3}, {a1 x1 + a2 z1, a1 x2 + a2 z2, a1 x3 + a2 z3 ... , {{d1 w1 + d2 y1, d1 w2 + d2 y2, d1 w3 + d2 y3}, {d1 x1 + d2 z1, d1 x2 + d2 z2, d1 x3 + d2 z3}}}}

The next cell helps explain how parts of the dot product above are computed.

A . B === {A[[1]] . B, A[[2]] . B}

True

In the cell above A[[1]] is a matrix, but B is a tensor. The next cell shows how parts of (A[[1]] . B) are computed. The parts of (A[[2]] . B) are computed in a similar manner. All the Dot products below involve vectors and matrices and are computed as explained above.

A[[1]] . B === { {A[[1, 1]] . First[Transpose[B]], A[[1, 1]] . Last[Transpose[B]]},  {A[[1, 2]] . First[Transpose[B]], A[[1, 2]] . Last[Transpose[B]]} }

True

The Dot product of three or more arguments

Mathematica can also compute Dot[A1, A2, A3,  ... ]  provided  (A1, A2, A3, ...) have suitable dimensions.
Dot[A1, A2, A3 ]  can be considered a concise way or writing Dot[A1, Dot[A2, A3] ] or  
Dot[Dot[A1, A2], A3]].  An example of the Dot product of three arguments is given in the next cell.

A1 = {a, b, c} ; B1 = {{x1, x1}, {y1, y2}, {z3, z3}} ; C1 = {2, 3} ; Dot[A1, B1, C1]

2 (a x1 + b y1 + c z3) + 3 (a x1 + b y2 + c z3)


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page