Documentation MathematicaBuilt-in Functions Advanced DocumentationLinear AlgebraMatrix and Tensor Operations
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.
This demonstrates matrix multiplication of a matrix with itself.
This multiplies a matrix with a vector.
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 23 matrix is multiplied by a 32 matrix.
This will result in a 2x2 matrix.
This generates a 3x3 matrix.
If the dimensions do not match, an error is generated.
Dot can be used to multiply vectors of equal length; the result will be a scalar.
Multiplication of a matrix by a vector works equivalently. This multiples a 23 matrix by a length 3 vector.
This multiples the length 3 vector by a 32 matrix.
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 234 tensor is defined.
Now, a 421 tensor is defined.
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.
The result is a 2321 tensor.
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.
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.
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.
This shows how Outer joins each point to each other point.
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.
This is the scalar product.
This is the equivalent operation using Inner.
Now Power is used instead of Times.