# Preface

Understanding matrices is crucial for almost all applications, especially for computer modeling. Indeed, a laptop or smartphone monitor is the most common example of a matrix filled with pixels. A wide range of applications includes the numerical solution to a set of linear algebraic equations. All numerical algorithms for solving differential equations are based on the reduction of solutions to algebraic matrix problems. Every matrix can be considered as an array or vectors whose entries are algebraic entries. A matrix is the next generalization of a vector. In this section, you will learn how to define matrices with Mathematica as well as some other manipulation tools.

Introduction to Linear Algebra with Mathematica

# How to define Matrices

Matrices are simultaneously a very ancient and a highly relevant mathematical concept. The origin of mathematical matrices has a long history. References to matrices and systems of equations can be found in Chinese manuscripts dating back to around 200 B.C. "Matrix" is the Latin word for womb. The term "matrix" in combinatorics was introduced in 1850 by the British mathematician James Joseph Sylvester (1814--1897), who also coined many mathematical terms or used them in "new or unusual ways" mathematically, such as graphs, discriminant, annihilators, canonical forms, minor, nullity, and many others.

A matrix (plural matrices) is a rectangular array of numbers, functions, or any symbols. It can be written as

${\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right] \qquad \mbox{or} \qquad {\bf A} = \left( \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right) .$

We denote this array by a single letter A (usually a capital boldfaced letter) or by $$\left( a_{i,j} \right)$$ or $$\left[ a_{i,j} \right] ,$$ depending on what notation (parenthesis or brackets) is in use. The symbol $$a_{i,j} ,$$ or sometimes $$a_{ij} ,$$ in the ith row and jth column is called the $$\left( i, \, j \right)$$ entry. We say that A has m rows and n columns, and that it is an $$m \times n$$ matrix. We also refer to A as a matrix of size $$m \times n .$$

Any $$m \times n$$ matrix can be considered as an array of $$n$$ columns

${\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right] = \left[ \left( \begin{array}{c} a_{1,1} \\ a_{2,1} \\ \vdots \\ a_{m,1} \end{array} \right) , \ \left( \begin{array}{c} a_{1,2} \\ a_{2,2} \\ \vdots \\ a_{m,2} \end{array} \right) , \ \cdots \left( \begin{array}{c} a_{1,n} \\ a_{2,n} \\ \vdots \\ a_{m,n} \end{array} \right) \right] = \left[ {\bf c}_1 , {\bf c}_2 , \ldots {\bf c}_n \right] ,$
or as a collection of $$m$$ rows
${\bf A} = \left[ \begin{array}{cccc} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{array} \right] = \left( \begin{array}{cccc} \langle a_{1,1} , a_{1,2} , \cdots , a_{1,n} \rangle \\ \langle a_{2,1} , a_{2,2} , \cdots , a_{2,n} \rangle \\ \vdots \\ \langle a_{m,1} , a_{m,2} , \cdots , a_{m,n} \rangle \end{array} \right) = \left[ \begin{array}{c} {\bf r}_1 \\ {\bf r}_2 \\ \vdots \\ {\bf r}_m \end{array} \right] .$
Here the column vector $${\bf c}_i = \langle a_{1,i} , a_{2,i} , \ldots , a_{m,i} \rangle^T$$ in ith row contains entries of matrix A in ith column. Correspondingly, the row vector $${\bf r}_j = \langle a_{j,1} , a_{j,2} , \ldots , a_{j,n} \rangle$$ in jth column contains entries of matrix A in jth row.

Before we can discuss arithmetic operations for matrices, we have to define equality for matrices. Two matrices are equal if they have the same size and their corresponding elements are equal. A matrix with elements that are all 0’s is called a zero or null matrix. A null matrix usually is indicated as 0.

Another very important type of matrices are square matrices that have the same number of rows as columns. In particular, a square matrix having all elements equal to zero except those on the principal diagonal is called a diagonal matrix.

Constructing Matrices

Mathematica offers several ways for constructing 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 the i,j entry Array[f,{m,n}] Build an m×n matrix whose i,j entry is f[i,j] ConstantArray[a,{m,n}] Build an m×n matrix with all entries equal to a DiagonalMatrix[list] Generate a diagonal matrix with the elements of list on the diagonal IdentityMatrix[n] Generate an n×n identity matrix Normal[SparseArray[{{i1,j1}->v1,{i2,j2}->v2,…},{m,n}]] Make a matrix with nonzero values vkk at positions {ik,jk} RandomReal[1, {m,n}] Make a m×n matrix with random coordinates

In addition, Mathematica offers matrices with different random distributions together with RandomVariate.

Nevertheless, it is most common to define vectors and matrices by typing every row in curly brackets: For example, let's define a 2×3 matrix (with two rows and three columns) as

A ={{1,2,3},{-1,3,0}}
{{1, 2, 3}, {-1, 3, 0}}
To see the traditional form of the matrix on the screen, one needs to add a corresponding command, either TraditionalForm or MatrixForm.
A ={{1,2,3},{-1,3,0}} // MatrixForm
Out= $$\begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix}$$
or
TraditionalForm[{{1, 2, 3}, {-1, 3, 0}}]
$$\begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix}$$
As you can see, MatrixForm and TraditionalForm are interchangeable as they perform the same operation and are for display only. The output is a single object (image in this case), rather than a matrix with individual components. This means that individual components, such as the second element of the second row, cannot be called upon once either MatrixForm or TraditionalForm is used. As a result, these options are not suitable for matrix operations. For instance, if you want to multiply A with its transpose or extract an element from A, Mathematica will not perform these operations:
A.Transpose[A]
$$\begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix} . \mbox{Transpose} \left[ \begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix} \right]$$
and
A[[{2, 2}]]
Part: Part {2,2} of $$\begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix}$$ does not exist.
There are two ways to avoid frozen matrices (from TraditionalForm or MatrixForm) that are kept by Mathematica in traditional form as a single unit (or image). The motivation for this is so that the matrix operations can be performed on the matrices we define. The first option is to define a matrix on one line of code and then display it in a convenient form on a completely separate line of code.
A ={{1,2,3},{-1,3,0}}
A // MatrixForm
Another option is to use one line for all code (matrix definition and use of MatrixForm or TraditionalForm) but define the entire matrix within parentheses.
Out= $$\begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix}$$

ReplacePart[expr,i->new] yields an expression in which the i-th part of expr is replaced by new.

ReplacePart[{{a, 2, 3}, {2, 3, 1}}, {2, 2} -> x]
$$\begin{pmatrix} a&2&3 \\ 2&x&1 \end{pmatrix}$$

Diagonal Matrices

There are several commands with which you can define diagonal matrices. The basic command is of course DiagonalMatrix[L]. When you have a list of values, L, you can build a square diagonal matrix with entries from L along its diagonal. All entries outside the main diagonal are zeroes. Other "diagonals" of a rectangular or square matrix extend from upper left to lower right; the main diagonal starts in the upper left-hand corner.

The command Diagonal[M] gives the list of elements on the leading diagonal of matrix M.
The command Diagonal[M,k] gives the elements on the k-th diagonal of matrix M.

Example 1: Consider the 4×5 matrix

A = {{1, 2, 3, 4, 5}, {-1, -2, -3,-4,-5}, {31,32,33,34,35},{41,42,43,44,45}}
%// MatrixForm
$$\begin{pmatrix} 1&2&3&4&5 \\ -1&-2&-3&-4&-5 \\ 31&32&33&34&35 \\ 41&42&43&44&45 \end{pmatrix}$$
Recall that using MatrixForm for the direct definition of matrix A will prohibit any operations with elements of the matrix. Therefore, we first define matrix A and only after that we visualize it with MatrixForm or TraditionalForm. For instance, you can determine a particular element of the matrix
A[[2, 3]]
-3
However, if you define matrix B as
B = {{1, 2, 3, 4, 5}, {-1, -2, -3, -4, -5}, {31, 32, 33, 34, 35}, {41, 42, 43, 44, 45}} // TraditionalForm
and then try to determine its element in position (2,3), Mathematica will not be able to provide you the answer:
B[[2, 3]]
Part 2 of B does not exist

To see diagonal elements, we type:

(A = {{1, 2, 3, 4, 5}, {-1, -2, -3, -4, -5}, {31, 32, 33, 34, 35}, {41, 42, 43, 44, 45}}) // MatrixForm
Diagonal[A]
{1, -2, 33, 44}
As you see, Mathematica provides the main diagonal, starting at the upper left corner. Other diagonal elements are obtained by including a particular shift from the main diagonal:
Diagonal[A,1]
{2, -3, 34, 45}
Diagonal[A,2]
{3, -4, 35}
To shift down from the main diagonal, just type a negative integer:
Diagonal[A,-1]
{-1, 32, 43}
Mathematica allows us not only to check diagonal elements but also to construct the diagonal matrix. The following two examples are self-explanatory.
DiagonalMatrix[{2, 3}, 1] // MatrixForm
$$\begin{pmatrix} 0&2&0 \\ 0&0&3 \\ 0&0&0 \end{pmatrix}$$
DiagonalMatrix[{2, 3}, -1] // MatrixForm
$$\begin{pmatrix} 0&0&0 \\ 2&0&0 \\ 0&3&0 \end{pmatrix}$$
■

Basic Commands

These introductory commands are very easy to use. The first two command lines define the matrices, A and M that we will be analyzing. The most important thing to understand is that to create a matrix with multiple rows, you need to separate each row and surround it with {}, as shown in the example above.
The Dimensions command tells you the dimensions for each matrix.

Dimensions[A]
{2, 3}     (* 2 is number of rows, 3 is number of columns *)

The commands A[[2,1]] and A[] are used to have Mathematica output certain matrix elements.
A[[2,1]]                (* entry in second row, first column *)
-1
A[]               (* first row of the matrix A *)
{1,2,3}
Now we define another matrix whose entries are functions:
(M ={{Cos[2 x], Sin[2 x]},{Sin[x],-Cos[x]}}) // MatrixForm
$$\begin{pmatrix} \mbox{Cos}[2 x]& \mbox{Sin}[2 x] \\ \mbox{Sin}[x]& -\mbox{Cos}[x] \end{pmatrix}$$
Dimensions[M]
{2,2}

The second to last command just asks Mathematica if the two matrices that we generated are the same, which, of course, they are not.
A == M             (* to check whether these matrices are equal *)
False
A // MatrixForm      (* to see the matrix A in standard matrix form *)
$$\begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix}$$

The command MatrixQ[matrix] gives True if it is a matrix, otherwise -- False

MatrixQ[A]             (* to check whether it is list of lists *)
True
Two m×n matrices $${\bf A} = \left[ a_{i,j} \right]$$ and $${\bf B} = \left[ b_{i,j} \right]$$ having the same dimensions can be added or subtracted
${\bf A} \pm {\bf B} = \left[ a_{i,j} \pm b_{i,j} \right] , \qquad i = 1,2, \ldots , m , \quad j=1,2,\ldots , n ,$
Mathematica uses the standard commands "+" and "-" to add or subtract two matrices of the same dimensions. Remember that you cannot add or subtract matrices of distinct dimensions, and Mathematica will not allow you to perform such operations. However, it is possible to enlarge the lowest size by appending zeroes and then add/subtract the matrices.

Transposition of Matrices

There is a special operation that transfers columns into rows and vice versa: it is called transposition. The transpose of a matrix was introduced in 1858 by the British mathematician Arthur Cayley (1821--1895). The transpose of a m × n matrix A is an n × m matrix AT (also denoted as $${\bf A}'$$ or $${\bf A}^t$$ ) created by any one of the following equivalent actions:

reflects A over its main diagonal (which runs from top-left to bottom-right);
writes the rows of A as the columns of $${\bf A}^{\mathrm T}$$

Formally, the i-th row, j-th column element of AT is the j-th row, i-th column element of A:

$\left[ {\bf A}^{\mathrm T} \right]_{ij} = \left[ {\bf A} \right]_{ji} .$

Let A and B be $$m \times n$$ matrices and c be a scalar. Then we have the following properties for transpose matrices:

1. $$\left( {\bf A}^{\mathrm T} \right)^{\mathrm T} = {\bf A}$$
2. $$\left( {\bf A} + {\bf B} \right)^{\mathrm T} = {\bf A}^{\mathrm T} + {\bf B}^{\mathrm T}$$
3. $$\left( {\bf A} \, {\bf B} \right)^{\mathrm T} = {\bf B}^{\mathrm T} \, {\bf A}^{\mathrm T}$$
4. $$\left( c \, {\bf B} \right)^{\mathrm T} = c\,{\bf B}^{\mathrm T}$$
5. $${\bf A}\, {\bf A}^{\mathrm T}$$ is a symmetric matrix.

Transpose[A]                  (* interchange rows and columns in matrix A * )
Out= $$\begin{pmatrix} 1&-1 \\ 2&3 \\ 3&0 \end{pmatrix}$$

A square matrix whose transpose is equal to its negative is called a skew-symmetric matrix; that is, A is skew-symmetric if

${\bf A}^{\mathrm T} = - {\bf A} .$

Complex entries

Let A be a m × n matrix with real or complex entries (they could be numbers or functions or other entities). Its complex conjugate, denoted by $$\overline{\bf A} ,$$ is again a m × n matrix, which is formed by taking the complex conjugate of each entry. Mathematica has a specific command to calculate the complex conjugate:

A:={{8,-I},{1,2*I}}
Out= $$\begin{pmatrix} 8 & -{\bf i} \\ 1 &2\,{\bf i} \end{pmatrix}$$
Conjugate[A]                  (* calculate complex conjugate of matrix A * )
Out= $$\begin{pmatrix} 8 & {\bf i} \\ 1 &-2\,{\bf i} \end{pmatrix}$$

If we take a transpose of the complex conjugate of m × n matrix A, we get the n × m matrix, called the adjoint matrix of A, which is denoted by $${\bf A}^{\ast} = \overline{{\bf A}^{\mathrm T}} = \left( \overline{\bf A} \right)^{\mathrm T} .$$

A square matrix A is called symmetric if $${\bf A} = {\bf A}^{\mathrm T} .$$ A square matrix A is called self-adjoint (or Hermitian) if it coincides with its transposed and complex conjugate:
${\bf A}^{\ast} = {\bf A}^{\mathrm H} = \overline{{\bf A}^{\mathrm T}} = \overline{\bf A}^{\mathrm H} = {\bf A} \qquad\mbox{or} \qquad a_{i,j} = \overline{a_{j,i}} , \quad i,j=1,2,\ldots ,n ,$
where the conjugate transpose is denoted A* or AH, AT is the transpose matrix, and $$\overline{z} = a - {\bf j}b$$ is complex conjugate of z = 𝑎 + jb.
An example of self-adjoint matrix gives the Pauli matrix, named after the Austrian (and later American / Swiss) physicist Wolfgang Ernst Pauli (1900-1958):
sigma2 = {{0, -I}, {I,0}}
$$\begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}$$
Adding an arbitrary real-valued 2×2 matrix to the Pauli matrix, we obtain another self-adjoint matrix.

A square complex matrix whose transpose is equal to the matrix with every entry replaced by its complex conjugate (denoted here with an overline) is called a self-adjoint matrix or a Hermitian matrix (equivalent to the matrix being equal to its conjugate transpose); that is, A is self-adjoint or Hermitian if $${\bf A} = {\bf A}^{\ast} .$$

Out= $$\begin{pmatrix} 8 & -{\bf i} \\ 1 &2\,{\bf i} \end{pmatrix}$$
ConjugateTranspose[A]                  (* calculate adjoint of matrix A    * )
Out = $$\begin{pmatrix} 8 & 1 \\ {\bf i} & -2\,{\bf i} \end{pmatrix}$$
Therefore, $${\bf A} \ne {\bf A}^{\ast} ,$$ and matrix A is not self-adjoint.

Building zero or diagonal matrices

Mathematica makes no distinction between vectors and matrices. For example, all n element column vectors are treated as n×1 matrices. This means that we can create a composition of row vectors in a column vector or vice versa.

If you wish to avoid building your matrix from curly brackets, Mathematica allows you to specify the size of a matrix through its toolbar. Navigate to Insert on the toolbar. Then click Table/Matrix -> New. A window will now appear allowing you to specify the size of your matrix. Under Make select Matrix(List of lists). Then specify the number of rows and columns you wish to input and click ok. Your specified matrix will now appear on your notebook for you to input information.

Suppose we need to build a zero matrix or the identity matrix:

IdentityMatrix//MatrixForm
Out//MatrixForm=
$$\begin{pmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{pmatrix}$$
Out//MatrixForm=
$$\begin{pmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{pmatrix}$$
DiagonalMatrix[list] gives a matrix with the elements of the list on the leading diagonal, and 0 elsewhere. Therefore, the identity matrix of dimensions $$3 \times 3$$ can be defined also as
DiagonalMatrix[{1,1,1}]

To construct an $$n \times n$$ zero square matrix, use the command Table[Table[0,{n}],{n}], where n specifies the dimension of the matrix.

For example,
Table[Table[0, {3}], {3}]
{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
or
Table[0, {3}, {3}]
{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

According to definition, we can add (or subtract) two matrices only when they are of the same size by adding (or subtracting) corresponding entries. However, sometimes we need to add two matrices of distinct sizes. It is natural to extend matrices to the largest of dimensions and fill extra entries by zeroes. It turns out that Mathematica accommodates such an operation, but you need to write a special subroutine in Wolfram language. To add two matrices (or sets of vectors) of different size by appending additional zeros to smallest vectors, we use the following script:

AddSets[A_?MatrixQ, B_?MatrixQ] := Module[{A1, B1, n, m}, A1 = A; B1 = B;
n = Length[A[]]; m = Length[B[]];
Which[n > m, B1 = Map[PadRight[#, n] &, B, 1], n < m,
A1 = Map[PadRight[#, m] &, A, 1], True,(*do nothing*)];
sum1 = Map[PadRight[#, Max[n, m] + 1] &,
Flatten[Table[A1[[i]] + B1[[j]], {i, 1, Length[A1]}, {j, 1, Length[B1]}],
1], 1]; Return[sum1[[All, 1 ;; Length[Part[sum1, 1]] - 1]]];]

Note: The inputs A_ and B_ represent the input variables. However, we use A_?MatrixQ and B_?MatrixQ to tell Mathematica to verify that these input variables are matrices, not arbitrary inputs.
The same code but appending zero to the right of every vector:

Module[{A1, B1, n, m },
A1 = A; B1 = B;
n = Length[A[]];
m = Length[B[]];
Which[n > m, B1 = Map[PadRight[#, n] &, B, 1],
n < m, A1 = Map[PadRight[#, m] &, A, 1],
True, (* do nothing *)];
sum1 = Map[PadRight[#, Max[n, m] + 1] &,
Flatten[Table[
A1[[i]] + B1[[j]], {i, 1, Length[A1]}, {j, 1, Length[B1]}], 1],
1];
Return[sum1];
]

For instance, to add two sets of vectors, we apply:

A = {{1, 2, 3}, {4, 5, 6}};
B = {{a, b}, {c, d}, {e, f}};
Out= {{1 + a, 2 + b, 3}, {1 + c, 2 + d, 3}, {1 + e, 2 + f, 3}, {4 + a, 5 + b, 6}, {4 + c, 5 + d, 6}, {4 + e, 5 + f, 6}}

Example

A = {{1, 2, 3}, {4, 5, 6}};
B = {{a, b}, {c, d}, {e, f}};
Out= {{1 + a, 2 + b, 3, 0}, {1 + c, 2 + d, 3, 0}, {1 + e, 2 + f, 3,
0}, {4 + a, 5 + b, 6, 0}, {4 + c, 5 + d, 6, 0}, {4 + e, 5 + f, 6, 0}}