Preface
"Matrix" is the Latin word for womb. The origin of mathematical matrices has a long history. 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.
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 // MatrixForm
ReplacePart[expr,i->new] yields an expression in which the i-th part of expr is replaced by new.
Diagonal Matrices
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: Consider the 4×5 matrix
%// MatrixForm
To see diagonal elements, we type:
Diagonal[A]
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 only thing
that is important to understand for this 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.
The commands A[[2,1]] and A[[1]] are used to have Mathematica output certain matrix elements.
The second to last command just asks Mathematica if the two matrices that we generated are the same, which, of course, they are not.
The command MatrixQ[matrix] gives True if it is a matrix, otherwise -- False
Mathematica uses two operations for multiplication of matrices: asterisk (*) and dot (.). The asterisk command can be applied only when two matrices have the same dimensions; in this case the output is the matrix containing corresponding products of corresponding entry. For example, we multiply two 2×3 matrices:
% // MatrixForm
(A*B) // TraditionalForm
B = {{4,5},{6,7},{-8,-9}}
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:
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.
A square matrix whose transpose is equal to its negative is called a skew-symmetric matrix; that is, A is skew-symmetric if
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 complex conjugate:
Adjoint Matrices
If we take a transpose of the complex conjugate of m × n matrix A, we get the n × m matrix, called the adjoint matrix ofA, which is denoted by \( {\bf A}^{\ast} = \overline{{\bf A}^{\mathrm T}} = \left( \overline{\bf A} \right)^{\mathrm T} . \)
% //TraditionalForm
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} . \)
Trace of a Matrix
The trace of a matrix is the sum of its diagonal elements:
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:
\( \begin{pmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{pmatrix} \)
\( \begin{pmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{pmatrix} \)
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,
Adding distinct size matrices
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:
n = Length[A[[1]]]; m = Length[B[[1]]];
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[[1]]];
m = Length[B[[1]]];
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:
B = {{a, b}, {c, d}, {e, f}};
AddSets[A, B]
Example
B = {{a, b}, {c, d}, {e, f}};
AddSets0[A, B]
0}, {4 + a, 5 + b, 6, 0}, {4 + c, 5 + d, 6, 0}, {4 + e, 5 + f, 6, 0}}
- Keskin, A.U., Ordinary Differential Equations for Engineers: with MATLAB Solutions, Springer; 1st ed. 2019. ISBN-13: 978-3030069995 ISBN-10: 3030069990
Glossary