# 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 Wolfram Language provides for building and working with matrices, vectors, and tensors. It focuses on functions that are specific to Wolfram Language, 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

Wolfram Language 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 i,j ^{th} entry |

Array[f,{m,n}] | build an m×n matrix whose i,j ^{th} entry is f[i,j] |

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 ν_{k} at positions {i_{k},j_{k}} |

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.

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

### Special Matrices

Wolfram Language contains definitions for a number of special matrices.

HilbertMatrix[n] | create an n×n Hilbert matrix, with elements given by 1/(i+j-1) |

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 |

## Structural Operations

These operations are all related to the structure of matrices. Many of the techniques shown in this section can be applied to Wolfram Language 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 Wolfram Language function Part. Typically Part is entered with [[ ]] notation.

m[[i,j]] | the i,j ^{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[[{i_{1},…,i_{r}},{j_{1},…,j_{s}}]] | the r×s submatrix with elements having row indices i_{k} and column indices j_{k} |

Tr[m,List] | list of the diagonal elements of m |

Ways to get pieces of matrices.

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

#### Getting Multiple Pieces

### Setting Pieces of Matrices

Setting elements, rows, and columns so that a matrix is updated is quite straightforward using the Wolfram Language 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 {i,j} 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 {v_{1},v_{2},…} |

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 {v_{1},v_{2},…} |

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

#### Setting Multiple Pieces

### 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 i_{0} through i_{1} and columns j_{0} through j_{1} |

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

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

You can use negative indices to count from the end.

### Deleting Rows and Columns

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

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

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

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

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

### Extending Matrices

### Transpose

### Rotating Elements

### Testing Matrices

Wolfram Language 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.

It should be noted that Equal works on any Wolfram Language 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 m_{1} and m_{2} |

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

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

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

## Element-wise Operations

### Listability

### Map

## Vectors and Tensors

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

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

Table[f,{i,n}] | build a length‐n vector by evaluating f with i=1,2,… ,n |

Array[a,n] | build a length‐n vector of the form {a[1],a[2],…} |

Range[n] | create the list {1,2,3,… ,n} |

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

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

Functions for generating vectors.

It should be noted that Wolfram Language 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.

### Testing Vectors and Tensors

Wolfram Language 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.

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

### Plotting Matrices

MatrixPlot has a number of graphics options for controlling the appearance of the plot. Many of these are the typical options of Wolfram Language 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.

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

Wolfram Language 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 Wolfram Language, you might want to use some of the functions that work with Wolfram Language expressions in files. These are discussed under "Expression Input and Output".

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

_{1}×n

_{1}matrix by a m

_{2}×n

_{2}matrix, it is required that n

_{1}is equal to m

_{2}. Here, a 2×3 matrix is multiplied by a 3×2 matrix.

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.

### Outer Product

#### Visualization of the Outer Product

### Generalized Inner Product

Matrix multiplication is a fundamental operation of linear algebra computation. Consequently, Wolfram Language 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.

## 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 Wolfram Language 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.

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

### Permutation Matrices

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