Preface


This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0340. It is primarily for students who have some experience using Mathematica. If you have never used Mathematica before and would like to learn more of the basics for this computer algebra system, it is strongly recommended looking at the APMA 0330 tutorial. As a friendly reminder, don'r forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in regular fonts. This means that you can copy and paste all comamnds into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts to your needs for learning how to use the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Return to Part I of the course APMA0340

How to define Matrices


"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, who also coined many mathematical terms or used them in a "new or unusual ways" mathematically such as graph, discreminant, annihilator, canonical form, minor, nullity, and many others.

In Mathematica, defining vectors and matrices is done by typing every row in curly brackets: Say we define a \( 2\times 3 \) matrix (with two rows and three columns) as

A ={{1,2,3},{-1,3,0}}
However, to see the traditional form of the matrix on the screen, one needs to add a corresponding command, either TraditionalForm or MatrixForm. These special commands tells Mathematica that output should be displayed with the elements of list arranged in a regular array:
A ={{1,2,3},{-1,3,0}} // MatrixForm
Out[1]= \( \begin{pmatrix} 1&2&3 \\ -1&3&0 \end{pmatrix} \)

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

ReplacePart[{{a, 2, 3}, {2, 3, 1}}, {2, 2} -> x]
Out[3]= \( \begin{pmatrix} a&2&3 \\ 2&x&1 \end{pmatrix} \)

Diagonal[M]
gives the list of elements on the leading diagonal of the matrix M.

Diagonal[M,k]
gives the elements on the k-th diagonal of M. To see diagonal elements, we type:
Diagonal[A]
Out[2]= {1 , 3}
Diagonal[A,1]
Out[3]= {2 , 0}
Diagonal[A,-1]
Out[3]= {-1}
Mathematica allows us not only to check diagonal elements but also to constract the diagonal matrix. The following two examples are self-explanatory.
DiagonalMatrix[{2, 3}, 1] // MatrixForm
out[9]/MatrixForm= \( \begin{pmatrix} 0&2&0 \\ 0&0&3 \\ 0&0&0 \end{pmatrix} \)
DiagonalMatrix[{2, 3}, -1]
Out[8]= \( \begin{pmatrix} 0&0&0 \\ 2&0&0 \\ 0&3&0 \end{pmatrix} \)
Dimensions[A]
Out[3]= {2, 3}     (* 2 is number of rows, 3 is number of columns *)
A[[2,1]]                (* entry in second row, first column *)
Out[4]= -1
A[[1]]               (* first row of the matrix A *)
Out[5]= {1,2,3}
Now we define another matrix whose entries are functins:
M ={{Cos[2 x], Sin[2 x]},{Sin[x],-Cos[x]}}
Out[2]= \( \begin{pmatrix} Cos[2 x]& Sin[2 x] \\ Sin[x]& -Cos[x] \end{pmatrix} \)
Dimensions[M]
Out[6]= {2,2}
A == M             (* to check whether these matrices are equal *)
Out[7]= False
A// MatrixForm      (* to see the matrix A in standard matrix form *)
Out[8]=
\( \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 *)
Out[9]= True

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 {} like is done 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 MatrixQ command gives True if matrix is a matrix, otherwise -- False.

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

reflect A over its main diagonal (which runs from top-left to bottom-right) to obtain \( {\bf A}^{\mathrm T} \)
write 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} . \]

A square matrix A is called symmetric if \( {\bf A} = {\bf A}^{\mathrm T} . \) 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 the matrix A * )
Out[5]= \( \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} . \]

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

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

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

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} . \)

A:={{8,-I},{1,2*I}}
Out[5]= \( \begin{pmatrix} 8 & -{\bf i} \\ 1 &2\,{\bf i} \end{pmatrix} \)
ConjugateTranspose[A]
Out[4] = \( \begin{pmatrix} 8 & 1 \\ {\bf i} & -2\,{\bf i} \end{pmatrix} \)

The trace of a matrix is the sum of its diagonal elements:

A := {{0, 1}, {-1, 0}}
Out[2]= \( \begin{pmatrix} 0 & 1 \\ -1&0 \end{pmatrix} \)
Tr[A]
Out[2]= 0

 

Mathematica makes no distinction between vectors and matrices. For example, all n element column vectors are treated as n by one 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.

First, we need build a zero matrix and the identity matrix:

IdentityMatrix[3]//MatrixForm
Out[3]//MatrixForm=
\( \begin{pmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{pmatrix} \)
IdentityMatrix[3]//TraditionalForm
Out[4]//MatrixForm=
\( \begin{pmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{pmatrix} \)
DiagonalMatrix[list]gives a matrix with the elements of 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.

 

 

How to define Vectors

How to define Matrices

Basic Operations with Matrices

Linear Systems of Equations

Determinants and Inverse Matrices

Special Matrices

Eigenvalues and Eigenvectors

Generalized Eigenvectors

Diagonalization Procedure

Sylvester Formula

The Resolvent Method

Spectral Decomposition Method

Positive Matrices

Roots

Miscellaneous

 

Return to Mathematica page

Return to the main page (APMA0330)
Return to the Part 1 Matrix Algebra
Return to the Part 2 Linear Systems of Equations
Return to the Part 3 Linear Systems of Ordinary Differential Equations
Return to the Part 4 Non-linear Systems of Ordinary Differential Equations
Return to the Part 5 Numerical Methods
Return to the Part 6 Fourier Series
Return to the Part 7 Partial Differential Equations