Additional functionality related to this tutorial has been introduced in subsequent versions of Mathematica. For the latest information, see Matrices and Linear Algebra.

Matrix and Tensor Operations

This tutorial reviews the functions that Mathematica provides for building and working with matrices, vectors, and tensors. It focuses on functions that are specific to Mathematica, and uses matrices for many of the examples. However, all the functions are general, and they will also work for vectors and tensors.

Building Matrices

Matrices are represented in Mathematica with lists. They can be entered directly with the { } notation that Mathematica provides for lists. An example of a matrix is shown here; by default a matrix prints with the list notation.
In[1]:=
Click for copyable input
Out[1]=

Mathematica provides a number of ways to build matrices.

Table[f,{i,m},{j,n}]build an m×n matrix where f is a function of i and j that gives the value of ^(th) entry
Array[f,{m,n}]build an m×n matrix whose ^(th) entry is
DiagonalMatrix[list]build a diagonal matrix with the elements of list on the diagonal
IdentityMatrix[n]build an n×n identity matrix
ConstantArray[val,{m,n}]]build an m×n matrix with each element being val
RandomReal[{0,val},{m,n}]]build an m×n matrix with random entries
Normal[SparseArray[{{i1,j1}->v1,{i2,j2}->v2,...},{m,n}]]
build an m×n matrix with nonzero values at positions
The function Table is particularly important to generate matrices, when there is not a built-in function to generate your matrix. The Mathematica documentation contains more information on Table. In this example a 3×3 matrix is built.
In[2]:=
Click for copyable input
Out[2]=
MatrixForm prints the matrix in a two-dimensional form.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
Another way to build matrices in Mathematica is with the Array command. Here it is used to build a 3×3 matrix.
In[4]:=
Click for copyable input
Out[4]=
Mathematica has a number of functions for building certain specialized matrices. This builds the identity matrix.
In[5]:=
Click for copyable input
Out[5]=
Note that the identity matrix that is generated is an integer matrix. If you want to compute with floating-point numbers it can be advantageous to use matrices that contain floating-point entries; this is described in more detail under "Matrix Contents". If you start with an integer matrix, such as the identity matrix generated previously, it can be converted to a floating-point matrix by using the function N. This is shown in the following.
In[6]:=
Click for copyable input
Out[6]=
Here a diagonal matrix is constructed.
In[7]:=
Click for copyable input
Out[7]=
In this example, the elements are put on the first diagonal. MatrixForm is useful for showing the results.
In[8]:=
Click for copyable input
Out[8]//MatrixForm=
This makes a 5×5 matrix; each element is 2.
In[9]:=
Click for copyable input
Out[9]=
Here a 2×4 matrix with pseudorandom real numbers in the range 0 to 5 is built.
In[10]:=
Click for copyable input
Out[10]=
This makes a 3×4 matrix with pseudorandom integers in the range -5 to 5.
In[11]:=
Click for copyable input
Out[11]=
This makes a 3×4 matrix with two nonzero values filled in. SparseArray expressions are described under "Working with Sparse Arrays".
In[12]:=
Click for copyable input
Out[12]=
SparseArray[{},{n,n}]a zero matrix
SparseArray[{i_,i_}->1,{n,n}]an n×n identity matrix
SparseArray[{i_,j_}/;i>=j->1,{n,n}]
a lower-triangular matrix

Constructing special types of matrices with SparseArray.

This sets up a general lower-triangular matrix.
In[13]:=
Click for copyable input
Out[13]//MatrixForm=

Functions that can read in matrices from files are discussed in the section "Import and Export" of matrices.

Special Matrices

Mathematica contains definitions for a number of special matrices.

HilbertMatrix[n]create an n×n Hilbert matrix, with elements given by
HilbertMatrix[{m,n}]create an m×n Hilbert matrix
HankelMatrix[n]create an n×n Hankel matrix with the first column given by 1, 2, ..., n and zeros beneath the main antidiagonal
HankelMatrix[list]create a Hankel matrix with the first column given by list and zeros beneath the main antidiagonal
HankelMatrix[col,row]create a Hankel matrix with the first column given by the list col and the last row given by the list row

Special matrices.

Here is a 2×4 Hilbert matrix.
In[1]:=
Click for copyable input
Out[1]//MatrixForm=
The elements of the Hankel matrix can be given as a list.
In[2]:=
Click for copyable input
Out[2]//MatrixForm=
The Hankel matrix can be filled with nonzero values by giving the final row. This also allows a nonsquare matrix to be generated. Note that the last element of the first column and the first element of the final row must be identical.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=

Structural Operations

These operations are all related to the structure of matrices. Many of the techniques shown in this section can be applied to Mathematica expressions and are not specific just to matrices.

Getting Pieces of Matrices

Extracting elements, rows, or columns of a matrix is quite straightforward with the Mathematica function Part. Typically Part is entered with notation.

m[[i,j]]the ^(th) entry
m[[i]]the i^(th) row
m[[i;;i]]rows i through j
m[[All,i]]the i^(th) column
m[[All,i;;j]]columns i through j
m[[{i1,...,ir},{j1,...,js}]]the r×s submatrix with elements having row indices and column indices
Tr[m,List]list of the diagonal elements of m

Ways to get pieces of matrices.

Define the following matrix.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
This gets the second element in the first row.
In[3]:=
Click for copyable input
Out[3]=
This gets the third row.
In[4]:=
Click for copyable input
Out[4]=
It can also obtain a column by using All to specify all rows.
In[5]:=
Click for copyable input
Out[5]=
Negative indices are used to refer to the end of the matrix. The following gets the last element of the last row.
In[6]:=
Click for copyable input
Out[6]=
You can get ranges of a matrix using . This gets the second through the fourth rows.
In[7]:=
Click for copyable input
Out[7]=
This gets the second through the fourth columns.
In[10]:=
Click for copyable input
Out[10]=
You can also give a step. This gets every other column.
In[12]:=
Click for copyable input
Out[12]=
The function Tr works on the diagonal elements of a matrix. The one-argument form adds them up.
Click for copyable input
Tr can also take a function, as its second argument, to apply to the diagonal elements. If List is used this returns the diagonal elements.
Click for copyable input

It should be noted that these commands for extracting parts of matrices will work for any Mathematica expression.

Getting Multiple Pieces

It is possible to extract multiple elements by using indices in lists. This is demonstrated with the following sample matrix.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
The following gets the first and third elements of the second row.
In[3]:=
Click for copyable input
Out[3]=
The following gets the second and fourth rows.
In[4]:=
Click for copyable input
Out[4]=
The following gets the first and third elements of the second and fourth rows.
In[5]:=
Click for copyable input
Out[5]=

Setting Pieces of Matrices

Setting elements, rows, and columns so that a matrix is updated is quite straightforward using the Mathematica function Part on the left-hand side of an assignment.

m={{a11,a12,...},{a21,a22,...},...}assign m to be a matrix
m[[i,j]]=vreset element to be v
m[[i]]=vreset all elements in row i to be v
m[[i]]={v1,v2,...}reset elements in row i to be
m[[All,j]]=vreset all elements in column j to be v
m[[All,j]]={v1,v2,...}reset elements in column j to be

Resetting parts of matrices.

Here is a 5×5 matrix.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=

To update parts of a matrix you can use Part on the left-hand side of an assignment.

This sets the third element of the third row.
In[3]:=
Click for copyable input
Out[4]//MatrixForm=
This sets the second row to have a particular value.
In[5]:=
Click for copyable input
Out[6]//MatrixForm=
This sets the second column.
In[7]:=
Click for copyable input
Out[8]//MatrixForm=
It is also possible to use negative indices to count from the end of the matrix. The following example sets the last element of the last row.
In[9]:=
Click for copyable input
Out[10]//MatrixForm=
You can also use the range syntax for setting pieces of the matrix. This sets every element in every other row to be .
In[10]:=
Click for copyable input
Out[11]//MatrixForm=

Setting Multiple Pieces

It is possible to set multiple elements by using indices in lists. This is demonstrated with the following sample matrix.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
The following sets the first and third elements of the second row.
In[3]:=
Click for copyable input
Out[4]//MatrixForm=
If the right-hand side of the assignment is a list that matches the number of elements being assigned, the assignment is done element by element. Thus, the following gives two different values for the first and third elements of the second row.
In[5]:=
Click for copyable input
Out[6]//MatrixForm=
The following sets the second and fourth rows.
In[7]:=
Click for copyable input
Out[8]//MatrixForm=
The following gives two different values for the second and fourth rows.
In[9]:=
Click for copyable input
Out[10]//MatrixForm=
The following sets the first and third elements of the second and fourth rows.
In[11]:=
Click for copyable input
Out[13]//MatrixForm=
The following sets the first and third elements of the second and fourth rows with different values.
In[14]:=
Click for copyable input
Out[15]//MatrixForm=

Extracting Submatrices

The range syntax is useful to extract a submatrix.

m[[i0;;i1,j0;;j1]]extract the submatrix with rows through and columns through
m[[i0;;i1]]extract the submatrix with rows through
m[[All,j0;;j1]]extract the submatrix with columns through

Extracting submatrices.

In[1]:=
Click for copyable input
Out[2]//MatrixForm=
This extracts the submatrix from to .
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
This extracts the submatrix of rows 1 to 3.
In[4]:=
Click for copyable input
Out[4]//MatrixForm=
This extracts the submatrix of columns 2 to 4.
In[5]:=
Click for copyable input
Out[5]//MatrixForm=

You can use negative indices to count from the end.

This returns the matrix with the first and last columns dropped.
In[6]:=
Click for copyable input
Out[6]//MatrixForm=

Deleting Rows and Columns

If you want to delete rows or columns you can use Drop.

Drop[m,{i0,i1}]delete rows through
Drop[m,{},{j0,j1}]delete columns through
Drop[m,{i0,i1},{j0,j1}]delete rows through and columns through

Deleting rows and columns.

In[1]:=
Click for copyable input
Out[2]//MatrixForm=
This drops rows 2 through 4.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
This drops columns 2 through 4.
In[4]:=
Click for copyable input
Out[4]//MatrixForm=
In this example rows 2 and 3, and columns 1, 2, and 3 are all dropped.
In[5]:=
Click for copyable input
Out[5]//MatrixForm=

Inserting Rows and Columns

If you want to insert a row, you can use Insert.

Insert[m,r,i]insert row r into matrix m at position i

Inserting a row.

In[1]:=
Click for copyable input
Out[2]//MatrixForm=
This inserts a row before row 3.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
If you want to insert a column, you must transpose the matrix, insert the column as a row, and then transpose the matrix back. This inserts a column before column 5.
In[4]:=
Click for copyable input
Out[4]//MatrixForm=

Extending Matrices

You can increase the size of a matrix by padding it with PadLeft and PadRight.
In[1]:=
Click for copyable input
PadLeft adds the elements to the beginning of the matrix.
In[2]:=
Click for copyable input
Out[2]//MatrixForm=
PadRight adds the elements to the end of the matrix.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
One important use of the padding functions is to replicate and tile a matrix. In this example the input matrix is extended to have two versions in every row and three in every column.
In[4]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]//MatrixForm=
PadLeft and PadRight have a number of extra features and are completely general so that they work for arbitrary rank tensors. These are described in the Mathematica documentation.

Transpose

Transposition of elements is a general matrix operation.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
Transpose swaps elements at specific indices.
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]//MatrixForm=
If you wish to compute the conjugate transpose of a matrix, use the function ConjugateTranspose.
In[5]:=
Click for copyable input
Out[6]=
If a matrix is equal to its conjugate transpose, it is said to be Hermitian.
In[7]:=
Click for copyable input
Out[7]=
You can also test with the function HermitianMatrixQ.
In[8]:=
Click for copyable input
Out[8]=

Rotating Elements

Another structural operation is to rotate elements within an index. This can be done with the functions RotateLeft and RotateRight.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
This rotates left by one step in the first level, which operates on the rows.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
This rotates left by one step in the second level, which operates on the columns.
In[4]:=
Click for copyable input
Out[4]//MatrixForm=
This rotates the columns in the opposite direction.
In[5]:=
Click for copyable input
Out[5]//MatrixForm=

Testing Matrices

Mathematica provides a number of functions for testing matrices and extracting size information.

MatrixQ[expr]give True if expr has the form of a matrix, and False otherwise
Dimensions[expr]a list of the dimensions of a vector or matrix
mi==mjcompare elements of two matrices for equality

Functions for testing the structure of vectors, matrices, and arrays.

If you want to test that an expression is a matrix, you can use the predicate MatrixQ.
In[1]:=
Click for copyable input
Out[1]=
The integer 5 is not a matrix.
In[2]:=
Click for copyable input
Out[2]=
A matrix has to have the same lengths in every dimension.
In[3]:=
Click for copyable input
Out[3]=
MatrixQ takes an optional second argument that specifies a test to apply to every element. In this example every element is tested to see if it is an integer.
In[4]:=
Click for copyable input
Out[4]=
In this example every element must be an integer greater than 1.
In[5]:=
Click for copyable input
Out[5]=
The command Dimensions is useful for extracting size information.
In[6]:=
Click for copyable input
Out[6]=
To compare if the elements of two matrices are equal, it is possible to use Equal, typically entered using a shortcut notation. For example, comparing a matrix with itself returns True.
In[7]:=
Click for copyable input
Out[8]=
Equal uses the value of numbers so it can be used to compare integer and real values.
In[9]:=
Click for copyable input
Out[9]=

It should be noted that Equal works on any Mathematica expression. If you want to compare two matrices for equality using properties of the matrix as a whole, it may be better to compare matrix norms. These are discussed in a later section.

Further Structural Operations

This section discusses some further structural operations that are useful for working with matrices.

Flatten[m]flatten out nested lists in m
Flatten[m,n]flatten out nested lists in m to level n
Partition[m,n]partition m into sublists of length n
Join[m1,m2]concatenate and
Append[m,r]insert row r at the end of m
Prepend[m,r]insert row r at the beginning of m
This generates a sample matrix for demonstration.
In[1]:=
Click for copyable input
This flattens the matrix into a vector.
In[2]:=
Click for copyable input
Out[2]=
Here the vector is partitioned back into a matrix with rows of length 4 using Partition.
In[3]:=
Click for copyable input
Out[3]=
Matrices can be joined together with the function Join. This inserts the new matrix as new rows.
In[4]:=
Click for copyable input
Out[4]=
Alternatively, you can join the new matrix as new columns.
In[5]:=
Click for copyable input
Out[5]=
A new row can be inserted at the end of a matrix with Append.
In[6]:=
Click for copyable input
Out[6]=

It should be noted that this can also be done with Insert; see the section "Inserting Rows and Columns".

Element-wise Operations

If you want to operate on the elements of a matrix, you can do this easily with Mathematica. First, build a matrix of floating-point numbers.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
Arithmetic operations applied to a matrix thread down to work on each element. Thus, if 5 is added to the matrix, the result is equivalent to adding 5 to each element.
In[3]:=
Click for copyable input
Out[3]=
Here, every element of the matrix is doubled.
In[4]:=
Click for copyable input
Out[4]=
Here, every element of the matrix is squared.
In[5]:=
Click for copyable input
Out[5]=
If one matrix is divided by another, the division is done element by element. If the dimensions of the two matrices do not agree, there is an error.
In[6]:=
Click for copyable input
Out[6]=
To apply the Sin function to every element, you apply Sin to the entire matrix.
In[7]:=
Click for copyable input
Out[7]=
If both of the arguments of an operation are matrices, the operation is carried out on corresponding elements.
In[8]:=
Click for copyable input
Out[8]=
Note that the dimensions of the two matrices have to be compatible.
In[9]:=
Click for copyable input
Out[9]=
If one of the arguments is a matrix and the other is a vector, the operation is carried out between rows of the matrix and elements of the vector.
In[10]:=
Click for copyable input
Out[10]=
Note that the multiplication of two matrices using the operator Times (typically this is entered by placing two arguments together) produces a matrix with elements that are products of the corresponding elements of the matrices. If you want to carry out matrix multiplication, this can be done with the function Dot and is described in the section "Matrix Multiplication". An example of element-wise multiplication is shown in the following example.
In[11]:=
Click for copyable input
Out[13]=

Listability

If you want to apply your own function to each element in a matrix, you may do this by giving your function the attribute Listable. This function squares each element and divides the result by 3.
In[1]:=
Click for copyable input
In[3]:=
Click for copyable input
In[4]:=
Click for copyable input
Out[4]=
Of course, the function will still work with symbolic matrices.
In[5]:=
Click for copyable input
Out[5]=

Map

Instead of using listability you can use Map to apply a function to every element in a matrix.
In[1]:=
Click for copyable input
This says that the function should be applied to every element in the matrix.
In[2]:=
Click for copyable input
Out[2]=
Here, a function that squares its argument and divides the result by 3 is applied to every element in the matrix.
In[3]:=
Click for copyable input
Out[3]=

Vectors and Tensors

In addition to supporting matrices, Mathematica supports vectors and tensors. All of these are built from lists. As described in "Introduction to Linear Algebra in Mathematica", Mathematica uses the term tensor to refer to generalized matrices. All the operations for building matrices can be generalized to work for vectors and tensors. Mathematica vectors have one level of list.

Here a vector is constructed.
In[1]:=
Click for copyable input
Out[1]=

In addition, there are a number of functions that generate vectors.

Table[f,{i,n}]build a length-n vector by evaluating f with
Array[a,n]build a length-n vector of the form
Range[n]create the list
Range[n1,n2]create the list
Range[n1,n2,dn]create the list

Functions for generating vectors.

One very efficient way to generate a vector is to use the Mathematica function Range. This generates a vector of integers.
In[2]:=
Click for copyable input
Out[2]=
This generates a vector of reals, starting with 1., incrementing in 0.1, and ending at 4.
In[3]:=
Click for copyable input
Out[3]=
Table can also be used to programmatically construct a vector; this can be useful if you want to execute a function to determine the elements of the vector.
In[4]:=
Click for copyable input
Out[4]=
Though it is often more efficient to use listable operations.
In[5]:=
Click for copyable input
Out[5]=
The operations and functions discussed in previous sections have equivalent versions for vectors.
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[9]=

It should be noted that Mathematica has no concept of a row or a column vector; a vector has a single index that can be used to reference an element of the vector.

Tensors can also be built using the Table command. Here, three iterators are used to generate a 2×3×4 tensor.
In[10]:=
Click for copyable input
Out[10]=
You can extract an element by using Part with three indices.
In[11]:=
Click for copyable input
Out[11]=

Testing Vectors and Tensors

Mathematica provides a number of functions for testing vectors and tensors and extracting size information.

VectorQ[expr]give True if expr has the form of a vector, and False otherwise
MatrixQ[expr]give True if expr has the form of a matrix, and False otherwise
ArrayQ[t,n]test whether t is a tensor of rank n
Dimensions[expr]a list of the dimensions of a vector or matrix
ArrayDepth[t]find the rank of a tensor
ti==tjcompare elements of two tensors for equality

Functions for testing the structure of vectors, matrices, and arrays.

If you want to test that an expression is a matrix you can use the predicate MatrixQ.
In[1]:=
Click for copyable input
Out[1]=
In addition to MatrixQ, VectorQ and ArrayQ are useful for testing vectors and tensors.
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
ArrayQ can also take a rank argument to test the depth of the array.
In[4]:=
Click for copyable input
Out[4]=
ArrayQ also takes a third argument that tests each element. In this example the result is False because not all the elements are NumberQ.
In[5]:=
Click for copyable input
Out[5]=
The command Dimensions is useful for extracting size information.
In[6]:=
Click for copyable input
Out[6]=
Dimensions returns a list of length 2 when the input is a matrix, stating that two indices are used to reference any element in the matrix. Another way to test the number of indices required to reference elements is with ArrayDepth. In this example the result is 2.
In[7]:=
Click for copyable input
Out[7]=
To compare if the elements of two tensors are equal it is possible to use Equal, typically entered using a shortcut notation. For example, comparing a tensor with itself returns True.
In[8]:=
Click for copyable input
Out[9]=
Equal uses the value of numbers, so it can be used to compare integer and real values.
In[10]:=
Click for copyable input
Out[10]=
If the structure of two objects does not match they are not equal.
In[11]:=
Click for copyable input
Out[11]=

Visualization of Matrices

This section reviews the functions that are available for formatting and plotting matrices.

MatrixForm[mat]print a matrix with the elements arranged in a two-dimensional array
MatrixPlot[mat]show the structural pattern of mat

Formatting Matrices

Matrices can be formatted with the function MatrixForm.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
MatrixForm also works for vectors and higher-rank tensors; the braces can help you to understand the grouping.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=

Plotting Matrices

A convenient way to plot matrices is with the function MatrixPlot.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
In[3]:=
Click for copyable input
Out[3]=

MatrixPlot has a number of graphics options for controlling the appearance of the plot. Many of these are the typical options of Mathematica DensityGraphics objects. It also has a special option, MaxPlotPoints, that controls the maximum size for the display of a matrix. Above this size the matrix is downsampled. Some of the important options are summarized in the following.

option name
default value
MaxPlotPoints200maximum size of matrix to display
AspectRatio1scaling for the final image shape
ColorRulesAutomaticcoloring for each element
ColorFunctionAutomaticfunction for coloring each element
MeshFalsewhether to draw a mesh
MeshStyleGrayLevel[1]the style of the mesh

Options of MatrixPlot.

One useful feature of MatrixPlot is that it can plot very large matrices. Here a very large sparse matrix is generated.
In[4]:=
Click for copyable input
Out[7]=
You can still plot this out with MatrixPlot. Here the number of points that are plotted is reduced to 100.
In[8]:=
Click for copyable input
Out[8]=

By default, no mesh is drawn around the elements; this can be enabled with the Mesh option. Typically you would not want to draw the mesh if the matrix is very large. When the mesh is drawn, it is sometimes useful to change the style of the mesh with the MeshStyle option.
In[9]:=
Click for copyable input
Out[9]=
You can change the coloring with the ColorRules option. This example plots zero as white and everything else black.
In[10]:=
Click for copyable input
Out[10]=
You can also use the ColorFunction option to add color.
In[11]:=
Click for copyable input
Out[11]=

Import and Export of Matrices

This section reviews the functions that are available for importing and exporting matrices.

Import[file,format]import data in the specified format from a file
Export[file,mat,format]export matrix to a file, converting it to the specified format

Mathematica provides a number of different tools for input/output. If you want to save your data in a file so that later you or a colleague can continue to work with it in Mathematica, you might want to use some of the functions that work with Mathematica expressions in files. These are discussed under "Expression Input and Output".

If you want to work with matrices from a source external to Mathematica using specific data formats, the functions Import and Export are useful. The Import function supports a variety of different formats, some of which are relevant to matrices.
In[1]:=
Click for copyable input
Out[1]=
The Table format will read tabular data into Mathematica. This format is not specific to matrices and can process different types of information such as numbers, dates, and currencies. However, this is often a simple way to read in a matrix. This example displays a sample data file.
Now the data file is read into Mathematica with the Import command using a format of Table; the result is a matrix.
In[3]:=
Click for copyable input
Out[3]=
Export can be used to write out a matrix in some particular format. Here it uses the Table format.
In[4]:=
Click for copyable input
Out[4]=
This example shows how to write a matrix into a CSV format. This could be read into another application such as a spreadsheet.
In[6]:=
Click for copyable input
Out[6]=

There are other matrix formats. For example, Harwell-Boeing, used for sparse matrices, and Matrix Market, used for both sparse and dense matrices. These are discussed under "Import and Export of Sparse Matrices". In addition the MAT matrix format and the FITS astronomical data format can also be useful as import or export formats.

Matrix Multiplication

Matrix multiplication (also called dot or inner product) is carried out in Mathematica with the function Dot, typically entered with a dot shorthand syntax.
In[1]:=
Click for copyable input
This demonstrates matrix multiplication of a matrix with itself.
In[2]:=
Click for copyable input
Out[2]=
This multiplies a matrix with a vector.
In[3]:=
Click for copyable input
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 2×3 matrix is multiplied by a 3×2 matrix.
In[4]:=
Click for copyable input
This will result in a 2×2 matrix.
In[6]:=
Click for copyable input
Out[6]=
This generates a 3×3 matrix.
In[7]:=
Click for copyable input
Out[7]=
If the dimensions do not match, an error is generated.
In[8]:=
Click for copyable input
Out[8]=
Dot can be used to multiply vectors of equal length; the result will be a scalar.
In[9]:=
Click for copyable input
Out[10]=
Multiplication of a matrix by a vector works equivalently. This multiples a 2×3 matrix by a length 3 vector.
In[11]:=
Click for copyable input
Out[11]=
This multiplies the length 3 vector by a 3×2 matrix.
In[12]:=
Click for copyable input
Out[12]=

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

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 next.

First, a 2×3×4 tensor is defined.
In[13]:=
Click for copyable input
Out[14]//MatrixForm=
Now, a 4×2×1 tensor is defined.
In[15]:=
Click for copyable input
Out[16]//MatrixForm=
This multiplies by . They are compatible because the length of the innermost index of equals the length of the outermost index of .
In[17]:=
Click for copyable input
Out[17]//MatrixForm=
The result is a 2×3×2×1 tensor.
In[18]:=
Click for copyable input
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]:=
Click for copyable input
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]:=
Click for copyable input
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]:=
Click for copyable input
This shows how Outer joins each point to each other point.
In[2]:=
Click for copyable input
Out[2]=

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]:=
Click for copyable input
This is the scalar product.
In[3]:=
Click for copyable input
Out[3]=
This is the equivalent operation using Inner.
In[4]:=
Click for copyable input
Out[4]=
Now Power is used instead of Times.
In[5]:=
Click for copyable input
Out[5]=

Matrix Permutations

Many matrix techniques rely on ordering a matrix in particular ways. For example, some techniques try to order the matrix to put elements on the diagonal, while others try to group certain elements into dense blocks. The Mathematica function Part is very well suited to applying permutations to the rows and columns of a matrix.

m[[perm]]apply a permutation to the rows of a matrix
m[[All,perm]]apply a permutation to the columns of a matrix
m[[perm,perm]]apply a permutation to the rows and columns of a matrix
m[[perm]]=mapply the inverse of a permutation to the rows of a matrix
m[[All,perm]]=mapply the inverse of a permutation to the columns of a matrix

Applying permutations to matrices.

This generates a random matrix.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=

Now the matrix will be reordered so that the rows are ordered by increasing size of 2-norm. (Norms are discussed under "Matrix Computations: Norms".)

First, the norm of each row is computed.
In[3]:=
Click for copyable input
Out[3]=
This computes the permutation that puts the smallest numbers first.
In[4]:=
Click for copyable input
Out[4]=
This applies the ordering to the rows of the matrix; the result has rows ordered by increasing size of 2-norm.
In[5]:=
Click for copyable input
Out[6]//MatrixForm=
Now the inverse permutation is applied by using part assignment. Note that this modifies the matrix held by the symbol . Part assignment is described previously.
In[7]:=
Click for copyable input
Out[8]//MatrixForm=
If the inverse permutation is applied to the permuted matrix, the original matrix is restored.
In[9]:=
Click for copyable input
Out[9]=

Permutation Matrices

Another way to work with permutations is to compute a matrix that applies the permutation by multiplication. For example, this builds a 4×4 matrix.
In[1]:=
Click for copyable input
Out[2]//MatrixForm=
This computes the permutation necessary to order the rows according to ascending 2-norm.
In[3]:=
Click for copyable input
Out[3]=
The permutation matrix is typically a sparse matrix, which is discussed under "Working with Sparse Arrays". This input generates a sparse identity matrix.
In[4]:=
Click for copyable input
Out[4]=
If the permutation is applied to the identity matrix, a permutation matrix is generated.
In[5]:=
Click for copyable input
Out[5]=
This applies the permutation. Note that the rows with smallest norms are at the top.
In[6]:=
Click for copyable input
Out[7]//MatrixForm=
This applies the inverse permutation.
In[8]:=
Click for copyable input
Out[8]//MatrixForm=

Typically it is faster to use the Mathematica function Part to apply a permutation, but sometimes it is convenient to work with a permutation matrix.

New to Mathematica? Find your learning path »
Have a question? Ask support »