Documentation  / Mathematica / Built-in Functions  / Advanced Documentation / Linear Algebra / Matrix and Tensor Operations /

 

Matrix Multiplication

Matrix multiplication (also called dot or inner product) is carried out in Mathematica with the function Dot, typically entered with a dot short-hand syntax.

In[1]:=

This demonstrates matrix multiplication of a matrix with itself.

In[2]:=

Out[2]=

This multiplies a matrix with a vector.

In[3]:=

Out[3]=

The matrix product can be computed with two matrices of different sizes so long as they are compatible. For matrices this means that to multiply a matrix by a matrix, it is required that is equal to . Here, a 2Cross3 matrix is multiplied by a 3Cross2 matrix.

In[4]:=

This will result in a 2x2 matrix.

In[6]:=

Out[6]=

This generates a 3x3 matrix.

In[7]:=

Out[7]=

If the dimensions do not match, an error is generated.

In[8]:=

Out[8]=

Dot can be used to multiply vectors of equal length; the result will be a scalar.

In[9]:=

Out[10]=

Multiplication of a matrix by a vector works equivalently. This multiples a 2Cross3 matrix by a length 3 vector.

In[11]:=

Out[11]=

This multiples the length 3 vector by a 3Cross2 matrix.

In[12]:=

Out[12]=

The definition of matrix multiplication is such that the product of two matrices and , where , is given as below.

The definition generalizes, so that the product of two arbitrary rank tensors and is as follows.

Thus applying Dot to a rank tensor and a rank tensor results in a rank tensor. An example is shown below. First, a 2Cross3Cross4 tensor is defined.

In[13]:=

Out[14]//MatrixForm=

Now, a 4Cross2Cross1 tensor is defined.

In[15]:=

Out[16]//MatrixForm=

This multiplies tensor1 by tensor2. They are compatible because the length of the innermost index of tensor1 equals the length of the outermost index of tensor2.

In[17]:=

Out[17]//MatrixForm=

The result is a 2Cross3Cross2Cross1 tensor.

In[18]:=

Out[18]=

Outer Product

The outer product is a way to build a higher rank tensor from those of lower rank. Mathematica provides this functionality with the function Outer. One use of this is to combine two vectors to form a matrix as an outer product.

In[1]:=

Out[3]//MatrixForm=

The function that is used to combine corresponding elements is given as the first argument. It can be an unknown function as in the following example.

In[4]:=

Out[4]//MatrixForm=

Visualization of the Outer Product

One way to visualize the operation of Outer is demonstrated in this example. First, a list of points is created.

In[1]:=

This shows how Outer joins each point to each other point.

In[2]:=

More information on Outer is available in The Mathematica Book.

Generalized Inner Product

Matrix multiplication is a fundamental operation of linear algebra computation. Consequently, Mathematica provides Dot as a dedicated function, which is heavily optimized. However, a generalization of matrix multiplication is provided by Inner. This allows the two operations that are used to form the product to be specified.

Here are two vectors.

In[1]:=

This is the scalar product.

In[3]:=

Out[3]=

This is the equivalent operation using Inner.

In[4]:=

Out[4]=

Now Power is used instead of Times.

In[5]:=

Out[5]=