*Mathematica*. For the latest information, see Matrices and Linear Algebra.

# Matrix and Tensor Operations

Building Matrices | Visualization of Matrices |

Structural Operations | Import and Export of Matrices |

Element-wise Operations | Matrix Multiplication |

Vectors and Tensors | Matrix Permutations |

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

*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]:= |

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 entry |

Array[f,{m,n}] | build an m×n matrix whose 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[{{i_{1},j_{1}}->v_{1},{i_{2},j_{2}}->v_{2},…},{m,n}]] | |

build an m×n matrix with nonzero values at positions |

*Mathematica*documentation contains more information on Table. In this example a 3×3 matrix is built.

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]//MatrixForm= | |

*Mathematica*is with the Array command. Here it is used to build a 3×3 matrix.

In[4]:= |

Out[4]= |

*Mathematica*has a number of functions for building certain specialized matrices. This builds the identity matrix.

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]//MatrixForm= | |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

In[12]:= |

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.

In[13]:= |

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 |

In[1]:= |

Out[1]//MatrixForm= | |

In[2]:= |

Out[2]//MatrixForm= | |

In[3]:= |

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 entry |

m[[i]] | the i row |

m[[i;;i]] | rows i through j |

m[[All,i]] | the i column |

m[[All,i;;j]] | columns i through j |

m[[{i_{1},…,i_{r}},{j_{1},…,j_{s}}]] | 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.

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[10]:= |

Out[10]= |

In[12]:= |

Out[12]= |

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

#### Getting Multiple Pieces

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

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={{a_{11},a_{12},…},{a_{21},a_{22},…},…} | assign m to be a matrix |

m[[i,j]]=v | reset element to be v |

m[[i]]=v | reset all elements in row i to be v |

m[[i]]={v_{1},v_{2},…} | reset elements in row i to be |

m[[All,j]]=v | reset all elements in column j to be v |

m[[All,j]]={v_{1},v_{2},…} | reset elements in column j to be |

In[1]:= |

Out[2]//MatrixForm= | |

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

In[3]:= |

Out[4]//MatrixForm= | |

In[5]:= |

Out[6]//MatrixForm= | |

In[7]:= |

Out[8]//MatrixForm= | |

In[9]:= |

Out[10]//MatrixForm= | |

In[10]:= |

Out[11]//MatrixForm= | |

#### Setting Multiple Pieces

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[4]//MatrixForm= | |

In[5]:= |

Out[6]//MatrixForm= | |

In[7]:= |

Out[8]//MatrixForm= | |

In[9]:= |

Out[10]//MatrixForm= | |

In[11]:= |

Out[13]//MatrixForm= | |

In[14]:= |

Out[15]//MatrixForm= | |

### Extracting Submatrices

The range syntax is useful to extract a submatrix.

m[[i_{0};;i_{1},j_{0};;j_{1}]] | extract the submatrix with rows through and columns through |

m[[i_{0};;i_{1}]] | extract the submatrix with rows through |

m[[All,j_{0};;j_{1}]] | extract the submatrix with columns through |

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]//MatrixForm= | |

In[4]:= |

Out[4]//MatrixForm= | |

In[5]:= |

Out[5]//MatrixForm= | |

You can use negative indices to count from the end.

In[6]:= |

Out[6]//MatrixForm= | |

### Deleting Rows and Columns

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

Drop[m,{i_{0},i_{1}}] | delete rows through |

Drop[m,{},{j_{0},j_{1}}] | delete columns through |

Drop[m,{i_{0},i_{1}},{j_{0},j_{1}}] | delete rows through and columns through |

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]//MatrixForm= | |

In[4]:= |

Out[4]//MatrixForm= | |

In[5]:= |

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 |

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]//MatrixForm= | |

In[4]:= |

Out[4]//MatrixForm= | |

### Extending Matrices

In[1]:= |

In[2]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]//MatrixForm= | |

In[4]:= |

Out[5]= |

In[6]:= |

Out[6]//MatrixForm= | |

*Mathematica*documentation.

### Transpose

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]//MatrixForm= | |

In[5]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

### Rotating Elements

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]//MatrixForm= | |

In[4]:= |

Out[4]//MatrixForm= | |

In[5]:= |

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 |

m_{i}==m_{j} | compare elements of two matrices for equality |

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

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[8]= |

In[9]:= |

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[m_{1},m_{2}] | concatenate and |

Append[m,r] | insert row r at the end of m |

Prepend[m,r] | insert row r at the beginning of m |

In[1]:= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

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

## Element-wise Operations

*Mathematica*. First, build a matrix of floating-point numbers.

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[13]= |

### Listability

In[1]:= |

In[3]:= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

### Map

In[1]:= |

In[2]:= |

Out[2]= |

In[3]:= |

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.

In[1]:= |

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[n_{1},n_{2}] | create the list |

Range[n_{1},n_{2},dn] | create the list |

Functions for generating vectors.

*Mathematica*function Range. This generates a vector of integers.

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

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.

In[10]:= |

Out[10]= |

In[11]:= |

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 |

t_{i}==t_{j} | compare elements of two tensors for equality |

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

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

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

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]//MatrixForm= | |

### Plotting Matrices

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

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

MaxPlotPoints | 200 | maximum size of matrix to display |

AspectRatio | 1 | scaling for the final image shape |

ColorRules | Automatic | coloring for each element |

ColorFunction | Automatic | function for coloring each element |

Mesh | False | whether to draw a mesh |

MeshStyle | GrayLevel[1] | the style of the mesh |

Options of MatrixPlot.

In[4]:= |

Out[7]= |

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

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

*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]:= |

Out[1]= |

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

*Mathematica*with the Import command using a format of Table; the result is a matrix.

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[6]:= |

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

*Mathematica*with the function Dot, typically entered with a dot shorthand syntax.

In[1]:= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

In[9]:= |

Out[10]= |

In[11]:= |

Out[11]= |

In[12]:= |

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.

In[13]:= |

Out[14]//MatrixForm= | |

In[15]:= |

Out[16]//MatrixForm= | |

In[17]:= |

Out[17]//MatrixForm= | |

In[18]:= |

Out[18]= |

### Outer Product

*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= | |

In[4]:= |

Out[4]//MatrixForm= | |

#### Visualization of the Outer Product

In[1]:= |

In[2]:= |

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.

In[1]:= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

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]]=m | apply the inverse of a permutation to the rows of a matrix |

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

Applying permutations to matrices.

In[1]:= |

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".)

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[6]//MatrixForm= | |

In[7]:= |

Out[8]//MatrixForm= | |

In[9]:= |

Out[9]= |

### Permutation Matrices

In[1]:= |

Out[2]//MatrixForm= | |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[7]//MatrixForm= | |

In[8]:= |

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.