Appendix C - Review of Linear Algebra and R - 2006 - Numerical Methods in Biomed
Appendix C - Review of Linear Algebra and R - 2006 - Numerical Methods in Biomed
In MATLAB, elements in a row are separated by commas, while rows are separated
by semicolons or by line breaks. Thus,
>> B=[1,3,5;2,0,4;1,5,6]
B =
1 3 5
2 0 4
1 5 6
517
518 APPENDIX C: REVIEW OF LINEAR ALGEBRA
The trace of a matrix is the sum of the elements on the main diagonal of a square
matrix. The MATLAB command for calculating the trace is
>> trace(B)
ans = 7
Matrix addition and subtraction can be performed if matrices have the same number
of rows and the same number of columns.
Matrix multiplication between A and B is allowed if A has the same number of
columns as the number of rows of B. When this condition is satisfied, then A and B
are said to be conformable. The multiplication of the matrix A, that has (m × n)
elements, with matrix B, of (n × p) elements, results in a matrix with m rows and p
columns that has an order of (m × p). For example,
Matrix multiplication is not commutative, that is, AB ≠ BA. It should be noted that to
an element-by-element product of two matrices is denoted by the (.*) operator:
The transpose of the matrix A, AT, is defined as an interchange of the rows and
columns of A, and is denoted by A' in MATLAB. If A = A T , then A is called a
symmetric matrix.
An identity, or unit matrix, is one whose diagonal elements are all unity and the rest
of the elements are zero. An identity matrix is always symmetric and square. Thus, a
(3 × 3) order identity matrix is given by:
⎡1 0 0⎤
I = ⎢0 1 0⎥
⎢ ⎥
⎢⎣0 0 1⎥⎦
The inverse of a matrix, A-1, is that matrix which when multiplied by the matrix A
gives the identity matrix,
AA -1 = I
A matrix has an inverse if it is square and nonsingular, or equivalently, if the
determinant of the matrix is non-zero. To check if a matrix B is nonsingular, the
determinant of the matrix can be readily computed with a MATLAB command, as
shown below:
>> B=[1,3,5;2,0,4;1,5,6]
B =
1 3 5
2 0 4
1 5 6
>> det(B)
ans =
6
520 APPENDIX C: REVIEW OF LINEAR ALGEBRA
( AB) −1 = B −1A −1
The rank of a matrix A is defined as the order of the largest nonsingular square
matrix within A. Consider the (m × n) matrix, where n ≥ m. Then, the rank of the
largest square submatrix within A is m, provided the (m × m) submatrix is
nonsingular, that is, the determinant of the (m × m) submatrix is nonzero. The
MATLAB command rank(A) gives the rank of the matrix:
>> A=[1,2,1,0;1,0,0,1;2,1,0,1]
A =
1 2 1 0
1 0 0 1
2 1 0 1
>> rank(A)
ans =
3
Each square matrix has its characteristic scalar properties, called the eigenvalues, and
corresponding vectors, called the eigenvectors. The matrix A is said to have
eigenvalue λ and eigenvector x if and only if:
Ax = λ x
To find λ and the corresponding vector x requires the solution of the homogeneous
set of equations shown above. The homogeneous problem above has nontrivial
solutions if the determinant of ( A − λ I), also called the characteristic matrix, equals
zero. For example, for a (2 × 2) matrix, A, shown below, the values of λ can be found
readily:
C.2 MATRIX FACTORIZATION 521
⎡1 0⎤
A=⎢ ⎥
⎣1 1⎦
1− λ 0
A − λI = = (1 − λ )2 = 0 ⇒ λ1 = 1, λ2 = 1
1 1− λ
In MATLAB, eig(A) finds the values of λ. The output vector of eigenvalues can be
named, as m=eig(A).
The command [Z,D]=eig(A) generates a diagonal matrix D of eigenvalues and a full
matrix Z whose columns contain the corresponding eigenvectors:
>> B
B =
1 3 5
2 0 4
1 5 6
>> m=eig(B)
m =
9.6540
-0.2596
-2.3944
>> [Z,D]=eig(B)
Z =
0.5587 0.8176 0.0067
0.4136 0.3791 -0.8595
0.7189 -0.4334 0.5111
D =
9.6540 0 0
0 -0.2596 0
0 0 -2.3944
A lower triangular matrix L with 1s on the diagonals can be constructed from the
multipliers used in Gaussian elimination (reviewed in Chapter 4). The elimination
process transforms the original matrix A into an upper triangular matrix U. The lower
triangular matrix L is formed by placing the negatives of the multipliers in the
appropriate positions, as illustrated below.
LU factorization from basic Gaussian elimination (no pivoting)
Input
A n-by-n matrix to be factored
n dimension of A
Initialize
L=I n-by-n identity matrix
U=A
Operate
For k = 1 to n-1
For i = k + 1 to n each row of matrix U after the kth row
m(i, k ) = −U (i, k ) / U ( k , k )
For j = k to n transform row i
U (i , j ) = U (i , j ) + m (i , k ) * U ( k , j )
End
L (i , k ) = − m (i , k ) update L matrix
End
End
L lower triangular matrix
U upper triangular matrix
Direct LU factorization
There are two alternative approaches to Gaussian elimination for finding the LU
factorization of matrix A: Doolittle factorization, wherein the diagonal elements of
matrix L are 1s; and Cholesky factorization for symmetric matrix A, wherein the
upper triangular matrix U is the transpose of the lower triangular matrix L.
Doolittle factorization
For a three-by-three matrix A, the problem is to find matrices L and U so that
L U = A
⎡1 0 0⎤ ⎡ u11 u12 u13 ⎤ ⎡ a11 a12 a13 ⎤
⎢l 1 0⎥ ⎢ 0 u22 u23 ⎥ = ⎢ a21 a22 a23 ⎥
⎢ 21 ⎥⎢ ⎥ ⎢ ⎥
⎢⎣ l31 l32 1⎥⎦ ⎢⎣ 0 0 u33 ⎥⎦ ⎢⎣ a31 a32 a33 ⎥⎦
C.2 MATRIX FACTORIZATION 523
One begins by finding u11 = a11 and then solving for the remaining elements in the
first row of U and the first column of L. At the second stage, we find u22 and then the
remainder of the second row of U and the second column of L. This procedure is
continued till all the elements of U and L are determined. A general algorithm is
summarized below: in what follows, the MATLAB colon (:) notation is used to
represent a row or column.
Input
A n-by-n matrix to be factorized
n dimension of A
Initialize
U = zeros(n) initialize U to n-by-n zero matrix
L = I(n) initialize L to identity matrix
Operate
For k = 1 to n
U ( k , k ) = A( k , k ) − L( k ,1: k − 1) * U (1: k − 1, j )
For j = k + 1 to n
U ( k , j ) = A( k , j ) − L( k ,1: k − 1) * U (1: k − 1, j )
L( j, k ) = ( A( j, k ) − L( j ,1: k − 1) * U (1: k − 1, k )) / U ( k , k )
End
End
Cholesky LU factorization
If the matrix A is symmetric, there is a convenient form of LU factorization called
the Cholesky factorization. In this method, the upper triangular matrix U is the
transpose of the lower triangular matrix L; that is, A = LLT. The basic steps for the
Cholesky factorization are summarized below:
Input
A symmetric n-by-n matrix ( A = AT )
Initialize
L=I(n) n-by-n identity matrix
Operate
For k = 1 to n
x = L( k ,1: k − 1) columns 1 to k-1 of the kth row of L
L( k , k ) = A( k , k ) − xx T
For j = k + 1 to n
y = L( j,1: k − 1) columns 1 to k-1 of the jth row of L
524 APPENDIX C: REVIEW OF LINEAR ALGEBRA
End
End
Transpose
U=LT
U =
4.0000 12.0000 8.0000 4.0000
0 3.0000 23.0000 8.0000
0 0 3.0000 20.0000
0 0 0 18.5556
The MATLAB function for the Cholesky factorization is chol. This function uses
only the diagonal and upper triangular part of A, as demonstrated below. Note that
the Cholesky factorization only works on positive definite matrices: A symmetric
matrix A is positive definite if xTAx > 0; all eigenvalues of A are positive and all
diagonal elements are positive. In general, A is positive definite if it is nonsingular,
diagonally dominant ( aii > ∑ aij ), and each diagonal element is positive
j ≠i
>> chol(A)
ans =
1.0000 4.0000 5.0000
0 2.0000 10.0000
0 0 1.7321
This concludes our brief review of selected topics from linear algebra. The interested
student is encouraged to read more on the subject in any of the numerous texts
available on linear algebra.