In this section, you will learn how to execute the basic arithmetic operations (addition, subtraction, and multiplication) with matrices as well as some other matrix manipulation tools.
▣
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 first course APMA0330
Return to the main page for the second course APMA0340
Return to Part I of the course APMA0340
Introduction to Linear Algebra with Mathematica
Recall that we will denote matrices with upper case letters in bold font while vectors are denoted by lower case letters. It is customary to denote the numbers that make up the matrix, called entries, of the matrix, as the lower case version of the letter names the matrix, with two subscripts. For example,
with A being a rectangular m×n matrix while B being a square matrix. The notation (A)_{ij} is also common, depending on the setting.
The subscripts denote the position of the entry. The entry 𝑎_{i,j} or just 𝑎_{ij} occupies the i-th row and j-th column of the matrix A. Two matrices A and B are equal if they have the same dimensions and 𝑎_{ij} = b_{ij} for every i and j.
A square matrix is a matrix that has the same number of rows as columns; that is, and n × n matrix for some positive integer n. When n = 0, we get just one entry. If A is a square matrix, the entries 𝑎_{11}, 𝑎_{22}, … , 𝑎_{nn} make up the main diagonal of A, The trace of a square matrix is the sum of the entries on the main diagonal.
A square matrix is a diagonal matrix if the only non-zero entries of A are on the main diagonal. A square matrix is upper (lower) triangular if the only non-zero entries are above (below) of on the main diagonal. A matrix that consists of either a single row or a single column is called a vector (more precisely, either a row-vector or column-vector).
Arithmetic operations
We define four arithmetic operations on matrices: Matrix addition or subtraction, scalar multiplication, and matrix multiplication. Matrix division is considered in the next section.
Matrix addition/subtraction. In order to add or subtract matrices, the matrices must have the same dimensions, and then one simply adds/subtracts the corresponding entries. For example,
Scalar Multiplication
A scalar multiplication means multiplying a matrix by a number and is accomplished by multiplying every entry in the matrix by the scalar. So
We don't know exactly who invented nor when the multiplication of matrices was invented. However, we do know that the work of 1812 by Jacques Philippe Marie Binet (1786--1856) contains the definition of the product of matrices. Let A [𝑎_{i,j}] be a m × n matrix and B = [b_{i,j}] be a n × k matrix. Its product, a \( {\bf C} = {\bf A}\,{\bf B} \) is a
m × k matrix, in which the n entries across the rows of A are multiplied with the n entries down the columns of B:
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:
The dot product can be performed only when the number of rows m in the first factor is the same as the number of columns m of the second factor. Their product is denoted by A.B and it is defined by
For example, we take 2×3 matrix A and multiply it by 3×2 matrix B:
A = {{1, 2, 3}, {-1, -2, -3}}
B = {{4,5},{6,7},{-8,-9}}
Then their product will be a 2×2 matrix:
(A.B) // MatrixForm
\( \begin{pmatrix} -8&-8 \\ 8&8 \end{pmatrix} \)
With regular arithmetic commands "/" for division and "*" multiplication, Mathematica performs these operations separately for each entry. So for two given matrices (divisor should not contain a zero entry) of the same size
Now we start working with matrices. A period “.” can also be used for matrix multiplication between one matrix and a vector or two matrices. It is important to note that when doing matrix multiplication an (m x n) matrix can only be multiplied by an (n x s) where m, n, and s are whole numbers, producing an (m x s) matrix.
Mathematica knows what vector should be used when a matrix is multiplied by a vector. For example,
A={{16,-9},{-12,13}};
v={1,2};
A.v
Out[3]= {-2,14}
v.A
Out[4]= {-8, 17}
This example shows that when a matrix is multiplied by a vector from the right (this also means that a matrix is operated on a vector as a transformation),
Mathematica treats it as a column-vector. When the vector is multiplied by a matrix from the right, Mathematica treats the same vector as a
row-vector.
However, we can specify either row-vector or column-vector and multiply by a
matrix from left or right:
v={2,1,-1} (* row-vector *)
u = {{2}, {1}, {-1}} (* column-vector *)
Products:
A.v
Out[17]= {1, 1} (* row *)
A.u
Out[18]= {{1}, {1}} (* column *)
Now we generate a special matrix, called the zero matrix, whose entries are
all zeroes.
Z23 = Table[0, {i, 2}, {j, 3}] (* generates a list of zero values *)
Out[1]= {{0, 0, 0}, {0, 0, 0}}
This matrix can be added (or subtracted from) to any matrix of the same dimensions (in our case, it is 2 × 3 matrix). We can verify this by showing that A + Z23 is equal to A:
A + Z23 == A
Out[2]= True
A == M (* when A ≠ M *)
Out[6]= False
Example 1:
Write the vector \( {\bf a}=2{\bf i}+3{\bf j}-4{\bf k} \) as the sum of two vectors, one parallel, and one
perpendicular to the vector \( {\bf b}=2{\bf i}-{\bf j}-3{\bf k} .\)
We find the parallel vector:
a = {2, 3, -4}
b = {2, -1, -3}
madB2 = (b[[1]])^2 + (b[[2]])^2 + (b[[3]])^2
Aparallel = a.b/madB2*b
Out[4]= {13/7, -(13/14), -(39/14)}
To find the perpendicular vector, we type:
Aperpendicular = a - Aparallel
Out[5]= {1/7, 55/14, -(17/14)}
Another more straight forward way:
Aparallel = (a.b)*b/(b.b)
To verify the answer, we type:
Aparallel + Aperpendicular -a
Out[9]= 0
Example 2:
Let a = [1,3,-4] and b = [-1,1,-2] be two vectors. Find \( {\bf a}\cdot ({\bf a}\times {\bf b}), \)
where \( {\bf a}\cdot {\bf b} \) is dot product, and \( {\bf a}\times {\bf b} \) is cross product of two vectors. Input variables are vectors a and b. Function will calculate the cross product of a and b, and then
calculate the dot product of that value and a. Notice that any input values for a and b will
result in zero sum.
a={1,3,-4}
b={-1,1,-2}
c=a.Cross[a,b]
{0,0,0}
{-1,1,-2}
0
Trace of a Matrix
The trace of a matrix is the sum of its diagonal elements.
As a simple introductory example, consider 2×2 matrix of zero trace:
The set ℳ_{m,n} of all m × n matrices under the field of either real or complex numbers is a vector space of dimension m · n.
In order to determine how close two matrices are, and in order to define the convergence of sequences of matrices, a special concept of matrix norm is employed, with notation \( \| {\bf A} \| . \) A norm
is a function from a real or complex vector space to the nonnegative real numbers that satisfies the following conditions:
Positivity: ‖A‖ ≥ 0
‖A‖ = 0 iff A = 0.
Homogeneity: ‖kA‖ = |k| ‖A‖ for arbitrary scalar k.
Triangle inequality: ‖A + B‖ ≤ ‖A‖ + ‖B‖.
Since the set of all matrices admits the operation of multiplication in addition to the basic operation of addition (which is included in the definition of vector spaces), it is natural to require that matrix norm satisfies the special property:
‖A · B‖ ≤ ‖A‖ · ‖B‖.
Once a norm is defined, it is the most natural way of measure distance between two matrices A and B as d(A, B) = ‖A − B‖ = ‖B − A‖. However, not all distance functions have a corresponding norm. For example, a trivial distance that has no equivalent norm is d(A, A) = 0 and d(A, B) = 1 if A ≠ B.
The norm of a matrix may be thought of as its magnitude or length because it is a nonnegative number.
Their definitions are summarized below for an \( m \times n \) matrix A, to which corresponds a self-adjoint (m+n)×(m+n) matrix B:
This matrix norm is called the operator norm or induced norm.
The term "induced" refers to the fact that the definition of a norm for vectors such as A x and x is what enables the definition above of a matrix norm.
This definition of matrix norm is not computationally friendly, so we use other options. The most important norms are as follow
✼
The operator norm corresponding to the p-norm for vectors, p ≥ 1, is:
Theorem 5:
Let ‖ ‖ be any matrix norm, and let matrix
I + B is singular, where
I is the identity matrix.
Then ‖B‖ ≥ 1 for every matrix norm.
Mathematica has a special command for evaluating norms:
Norm[A] = Norm[A,2] for evaluating the Euclidean norm of the matrix A; Norm[A,1] for evaluating the 1-norm; Norm[A, Infinity] for evaluating the ∞-norm; Norm[A, "Frobenius"] for evaluating the Frobenius norm.
A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Norm[A]
Sqrt[3/2 (95 + Sqrt[8881])]
N[%]
16.8481
Example 3:
Evaluate the norms of the matrix
\( {\bf A} = \left[ \begin{array}{cc} \phantom{-}1 & -7 & 4 \\ -2 & -3 & 1\end{array} \right] . \)
The absolute column sums of A are \( 1 + | -2 | =3 \) , \( |-7| + | -3 | =10 , \) and \( 4+1 =5 . \)
The larger of these is 10 and therefore \( \| {\bf A} \|_1 = 10 . \)
Norm[A, 1]
10
The absolute row sums of A are \( 1 + | -7 | + 4 =12 \) and
\( | -2 | + |-3| + 1 = 6 ; \) therefore, \( \| {\bf A} \|_{\infty} = 12 . \)
Norm[Transpose[A], 1]
12
The Euclidean norm of A
is the largest singular value. So we calculate
This matrix\( {\bf A}\,{\bf A}^{\ast} \) has two eigenvalues
\( 40 \pm \sqrt{1205} . \) Hence, the Euclidean norm of the matrix A is
\( \sqrt{40 + \sqrt{1205}} \approx 8.64367 . \)
These matrices S and M have the same eigenvalues. Therefore, we found the Euclidean (operator) norm of A to be approximately 16.8481. Mathematica knows this norm:
Norm[A]
Sqrt[3/2 (95 + Sqrt[8881])]
The spectral radius of A is the largest eigenvalue:
To find 1-norm of A, we add elements in every column; it turns out that the last column has the largest entries, so
\[
\| {\bf A} \|_1 = 3+6+9=18.
\]
If we add entries in every row, then the last row contains the largest values and we get
\[
\| {\bf A} \|_{\infty} = 7+8+9=24.
\]
■
Return to Mathematica page
Return to the main page (APMA0340)
Return to the Part 1 Matrix Algebra
Return to the Part 2 Linear Systems of Ordinary Differential Equations
Return to the Part 3 Non-linear Systems of Ordinary Differential Equations
Return to the Part 4 Numerical Methods
Return to the Part 5 Fourier Series
Return to the Part 6 Partial Differential Equations
Return to the Part 7 Special Functions