Professional Documents
Culture Documents
(part 1)
Computational Techniques II
Contents
1 System of linear equations
2 Gaussian elimination
3 Gauss-Jordan elimination
4 Pivoting strategies
4.1 Partial pivoting . . . .
4.2 Complete pivoting . .
4.3 Keeping track of swaps
4.3.1 Method 1 . . .
4.3.2 Method 2 . . .
9
9
11
12
12
13
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
14
18
8 LU
8.1
8.2
8.3
8.4
18
19
19
20
20
decompositions
Application . . . . . . .
Algorithm . . . . . . . .
Ecient implementation
Some theory . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
9 QR
9.1
9.2
9.3
9.4
decomposition
Householders transformation
Using Householder for QR . .
Ecient implementation . . .
Application to least squares .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
21
22
22
23
27
3x + y + 2z
2x + 3z
=
=
15
2
2
3
2
3 1
x
5
1 2 y = 15 .
0 3
z
2
Thus, here
2 3 1
A = 3 1 2 ,
2 0 3
x
x = y ,
z
5
b = 15 .
2
If the rhs vector b is 0, then we call the system homogeneous, else we call it
non-homogeneous.
Any value of x for which Ax = b, is called a solution of the system. A system
of linear equations has either exactly one solution, or innitely many solutions
or no solution at all. In the last case we call the system inconsistent, otherwise
it is called consistent.
Gaussian elimination
This is the matrix version of a method for solving a system of linear equations
that we learn at high school.
Example: Consider the following system of three linear equations, which we
call 1 , 1 and 1 .
1 :
1 :
1 :
x y
2x +5y
x +2y
+z
z
3z
=
2
=
9
= 4
In high school we used to solve this by eliminating the unknowns one by one
until only one remained. Then we could solve for that one. Once we know one
of the unknowns we substitute this value in the equations to get a system with
only two unknowns. Then we eliminated one of these, and so on.
Example: Let us rst eliminate x from the last two equations by subtracting
multiples of the rst equation from them. Here are the resulting 3 equations,
which we call 2 , 2 and 2 .
2 =
2 =
2 =
1 :
1 21 :
1 1 :
y
7y
3y
+z
3z
4z
=
=
=
+z
3z
19 z
7
=
=
=
2
5
6
y
7y
2
5
57
7
Now the third equation involves only one unknown, z. So we can solve for z.
The second equation involves both z and y. So once we have solved for z from
the third equation we can use the second equation to solve for y. The rst
equation (which involves x as well as y and z) can then be used for solving x.
This approach is called back substitution. From the third equation:
z = 3,
then from the second equation
y=
1
(5 + 3z) = 2,
7
1
2
1
1
1
2
9
5 1
2 3 4
It is obtained by appending the rhs after the matrix. We draw a vertical line to
keep the rhs separate.
While doing Gaussian elimination we work with only the augmented matrices.
To move from one augmented matrix to the next we need certain kind of operation that we call an F -move. Before each F -move we have to choose some
nonzero entry in the matrix. This is called the pivot for that step. The position
of the pivot may change from step to step. The row (or column) that contains
the pivot is called the pivotal row (or column).
Forward elimination (F): In this move we add multiples of the pivotal
row to the rows below it so that the entries in the pivotal column below
the pivot become zero. In the example below, we need to add 5 times the
rst row to the second row, and 2 times the rst row to the third row.
1
2
1
-1
5
2
1
2
9
1
3 4
1
7
3
-1
0
0
1 2
4 19
1 0
This operation only aects the rows below the pivotal row. The pivotal
row itself, as well as the rows above it remain unchanged.
5
pivotal
row
2 2 3
7
4 5 6 15
1 1 1
3
The entry in the pivotal row sharing same column with 15 is 7. The entry in
the pivotal column sharing the same row with 15 is 4. The pivot is 2. So 15
will change to
74
= 1.
15
2
times
The symbol means shift the pivot diagonally downward by one place.
If the pivot turns out to be zero at any step, then stop and output Failure.
Otherwise, you will end up with an augmented system
A b
where A will be an upper triangular matrix with nonzero diagonal entries. Now
you have to apply back substitution to solve for xn , ..., x1 in this order as follows.
n
1
bk
akj xj ,
xk =
akk
j=k+1
for k = n, n 1, ..., 2, 1.
It might help to write down the back substitution part explicitly for the 3 3
case where the unknowns are x, y and z.
= 3 /33
b a
y = (2 a23 z)/22
b
a
1 a13 z a12 y)/11
x = (b
a
z
1 1
1
2
2
5 1
9
1
2 3 4
1 1
0
7
0
0
1
3
-19/7
2
5
57/7
1
0
0
2
1
1
7 3
5
3 4 6
Now we have an upper triangular system, i.e., a system where the coecient
matrix (the part to the left of the vertical divider in the augmented matrix) is
upper triangular. We shall now apply back substitution to solve for z, then y,
and then x as follows.
57/7
z=
= 3,
19/7
7
and
y=
5+33
1
(5 + 3z) =
= 2,
7
7
and nally,
x = 2 + y z = 2 + 2 3 = 1.
The following diagram shows the order of the Gaussian elimination operations
for a 4 4 matrix. The gray regions hold the nonzero entries. The white regions
are all zeroes. The little triangles to the left mark the rows that are going to be
aected by the next move. The position of the pivot is shown as a circle.
F
Gauss-Jordan elimination
1
2
1
2
1
1
5 1
9
2 3 4
2
1 1
1
2/5 1 1/5 9/5
1
2
3 4
1 1
1
2
4/3 1/3
0 2/3
B
2
9 5/3 13/3
5 1
0 31/3
1
2 -3 4
1
2 -3
4
8
Here we have subtracted 1 times the last row from the second row, and added
3
1
3 times the last row to the rst row.
In Gauss-Jordan elimination of a n n system we start with the pivot at the
(1, 1)-th position. Then we perform the following operations.
S, F, B, , , S, F, B, , S, B.
n1
times
As before the symbol means shift the pivot diagonally downward by one
place.
2
1 1
1
1 1
1
2
(S,F )
2
0 7 3
5 1
9
5
1
2 3 4
0
3 4 6
(S, F, B)
19/7
1 0
4/7
1 0 0 1
(S,B)
5/7
3/7
0 1 0 2
0 1
0 0 1 3
0 0 -19/7 57/7
Pivoting strategies
We have already mentioned the three basic steps: B,F, and S. All these steps
involve division by the pivot. So the pivot needs to be nonzero. However, it may
happen that while performing the elimination the pivot turns out to be zero.
Then we need to swap rows and/or columns to bring a nonzero number in the
position of the pivot. This is called pivoting. Pivoting is useful even when the
pivot is nonzero, but is very small in absolute value. This is because, division
by numbers new zero introduces large errors in the computer output. We shall
discuss two pivoting strategies: partial pivoting, and complete pivoting.
4.1
Partial pivoting
Partial pivoting involves swapping rows. Suppose that at some step in the
Gaussian or Gauss-Jordan algorithm you have the following augmented matrix,
9
1
2 3
6
2 9 4 3
5
1 2 2
To perform partial pivoting look at the pivot as well as all the entries below the
pivot in the pivotal column. These are shown bold. Locate among these the
entry with the maximum absolute value. Here it is 5. Swap the pivotal row
with with the row containing this entry. In this example we swap row 1 with
row 3 to get
-5
1 2 2
2 9 4 3
6
1
2 3
Note that the position of the pivot (shown by the box) has remained unchanged,
though the pivot itself has changed from 1 to 5.
In partial pivoting you look for a candidate pivot from among the current pivot
and the entries below it:
Partial pivoting
2
0
0
10 1
1 2
3 1
10
3 4
10 1 3
2 13 8
2
1
Pp
2 0
5
0
10 1
3 1
1 2
1
5
2
3
10
3 4
Pp
5 10 1 3
9
2 13 8
3
5
9
We have called the partial pivoting step as Pp . So now we have four dierent
kinds of steps: S, F, B and Pp .
10
times
and then we perform back substitution as before. If the pivot turns out to be
zero at any step (i.e., all the entries at and below the pivotal position are zero
then the system is singular, and the pivotal variable can be set to any value.
For Gauss-Jordan elimination we do
Pp , S, F, B, , , Pp , S, F, B, , S, B.
n1
4.2
times
Complete pivoting
In complete pivoting we swap rows and/or columns. Suppose the pivot is at the
(i, i)-th entry. Then we look at all the (r, s)-th entries where r, s i.
Complete pivoting
1
2
3
6
3 2
1 5
4
1 4 10
Locate the entry with the maximum absolute value among the bold entries. If
this is the (r, s)-th entry then swap i-th row of the augmented matrix with r-th
row, and the i-column with the s-th column. Note that the order of row and
column swaps does not matter. In this example we swap row 2 with row 3, and
column 2 with column 3 to get
6
1
2 3
4 -4 1 10
3
1 2 5
Recall that the columns of the matrix correspond to the variables of the equations. So swapping the columns also involves reordering the variables. A simple
way to keep track of the order of the variables is to write the variables above
the columns. If we call the variables as x, y, z in the last example then we shall
write:
11
x
1
3
4
y
2
2
1
z
3
6
1 5
4 10
x
1
4
3
z
2
-4
1
y
3
6
1 10
2 5
times
4.3
4.3.1
times
There are two ways to keep track of the column swaps. Both uses an extra integer array. In the rst version we keep an integer array colPerm[0],...,colPerm[n-1],
say. Initially colPerm[i] stores i for all i. This means all the columns are in
their right places to start with. Every time we swap two columns we also swap
the corresponding entries of the colPerm array.
Thus, at the end colPerm[i] stores the index of the variable at the i-th position
now.
We need to nd its inverse permutation, to know where a particular variable is.
Remember that if i a permutation, then its inverse permutation is 1 where
i 1 ((i)) = i.
So we can dene a new array called, say, invColPerm[0,...,n-1] and ll it up
as
for(i=0;in;i++) invColPerm[colPerm[i]] = i;
Now the solution for the i-th variable can be found in a[invColPerm[i]][n].
12
4.3.2
Method 2
This is a conceptually simpler, though computationally more demanding solution. Here we keep an integer array colIndex[0,...,n-1]. Instead of referring
to the (i, j)-th entry of A as a[i][j] we shall use a[i][colIndex[j]]. Initially,
colIndex[i] will store i for all i = 0, ..., n 1. So a[i][colIndex[j]] will be
the same as a[i][j]. When we need to swap two columns we shall instead
only swap the corresponding elements of colIndex. The following example will
make things clear.
Example: Let n = 3. After we swap columns 0 and 1, we have
colIndex[0] = 1, colIndex[1] = 0, colIndex[2] = 2.
So the (0, 1)-th entry of the new matrix is now a[0][colIndex[1]], which is
a[0][0], as it should be.
If we perform the entire Gauss-Jordan algorithm like this, then there is no need
to change anything at the end. The last c9olumn of the nal augmented matrix
will automatically store the required solution in the correct order!
Exercise 2:Suppose that we want to solve multiple systems with a common coecient
matrix but dierent r.h.s. vectors and unknown vectors.
Ax1 = b1
Ax2 = b2
.
.
.
Axk = bk .
This is the same as solving the matrix equation:
AX = B,
13
where the bi s are the columns of B, and the xi s are the columns of X. Here we can
apply Gauss/Gauss-Jordan elimination with the augmented matrix:
[A | B].
Write a program to implement Gauss-Jordan elimination (with complete pivoting) in
this set up.
Notice that we are using four basic actions: dividing a row by a number, subtracting multiple of one row from another, swapping two rows, and swapping
two columns. Of these the rst three are row operations and can be done by
left multiplication with elementary matrices. The last operation is a column
operation, and hence done by right multiplication. If we accumulate all the left
and right marices then at the end of Gauss-Jordan elimination we are left with
LAR = I.
So A = L1 R1 , or
A1 = RL.
Convince yourself that left multiplying by R is same as applying the column
swaps to the rows in the reverse order.
From now on we shall assume that R = I for ease of exposition. The results
should be easily modiable for general R.
Here is another interesting point. After p steps the matrix is of the form
Ip
O
P b1
Q b2
When does total pivoting fail? When Q = O. Thus the system is inconsistent
if b2 = 0. If b2 = 0 then the general solution is y arbitrary and x = b1 P y.
Another interesting point is
Q = A22 A21 A1 A12 .
11
This is because if all the accumulated left multiplier matrix is L then it is of
the form
Upp V
L=
.
W
I
Do you see why the lower right hand block is I? Now we have
LA =
Upp
W
V
I
A11
A21
14
A12
A22
Ip
O
P
Q
So
Q = W A12 + A22 .
To nd W we note that W A11 + A21 = O, or
W = A21 A1 .
11
So combining everything we have
Q = A22 A21 A1 A12 .
11
an easy proof by just performing the block matrix multiplication. This result
is useful because we often have to compute the rhs in statistics. It is called the
conditional covariance matrix.
1
Example: Suppose that we want to nd the inverse of the matrix 2
1
Then we append an identity matrix of the same
to get the augmented matrix
1 1
1 1 0
2
5 1 0 1
1
2 3 0 0
1 1
5 1 .
2 3
0
0 .
1
1 1
1 1 0 0
1 1
1
1 0 0
(S,F )
2
5 1 0 1 0 0
7 3 2 1 0
1
2 3 0 0 1
0
3 4 1 0 1
After a set of S, F and B steps we arrive at
5/7
4/7
1 0
-3/7 -2/7
0 1
0 0 -19/7 -1/7
15
1/7
1/7
-3/7
0
0 ,
1
1 0
0 1
0 0
Thus, the required inverse is
6.1
0
0
1
91/133
7/133
4/19
-35/133
28/133
-3/19
1/19
3/19
-7/19
91/133
7/133
4/19
-35/133
1/19
28/133
3/19
-3/19
-7/19
Ecient implementation
1 0
0 1
0 0
1
0
0
Notice that the rst column is now
have
1 0
0 1
0 0
After the third step we have
1
0
0
0
0 .
1
0
0 .
1
0
0 .
1
0 0
1 0
0 1
The s in the right hand half now constitute the inverse matrix. Notice that
at each step there are exactly as many s as there are entries in the original
matrix. Clearly, all that we really need to store are the s, because the othr
entries are anyway constants irrespective of the matrix being being inverted.
It should not be dicult to check that the following algorithm computes the s:
16
aip
aip apj
i, j = p
app
aip /app i = p
apj
app
=
=
apj /app j = p
1/app .
aij
Then in the above algorithm the i-th column of A is needed only to compute the
i-column of A1 . So we can overwrite the i-th column of A with the i-th column
of A1 to save space. This is an example of an in place algorithm, i.e., where
the output gradually overwrites the input. If we need to perform row/column
swaps, then the situation is very similar. To see this consider the same example
once again. We start out as
1 0 0
0 1 0 .
0 0 1
After one sweep we have
1
0
0
1
0
0
The next sweep will produce
1
0
0
0
1
0
0
1
0
0
0 .
1
third rows:
0 0
0 1 .
1 0
0
0
1
1 0 0
0 1 0
0 0 1
Here the right hand half stores the inverse of the row-swapped version of the
original matrix, i.e., (LA)1 , where L is the row-swap matrix. Now (LA)1 =
A1 L1 . Hence
A1 = (LA)1 L.
17
When L is multiplied from the right, it swaps the columns in the reverse order.
Thus we need to keep track of the swaps and apply them in the reverse order
to the inverse. Remember that a row swap becomes a column swap and vice
versa.
Project 3:Implement the in-place version of the Gauss-Jordan inversion algorithm using complete pivoting.
LU decompositions
18
8.1
Application
8.2
Algorithm
aij =
lik ukj .
k=1
Now, lik = 0 if k > i, and ukj = 0 if k < j. So the above sum is eectively
min{i,j}
aij =
lik ukj .
k=1
2
3
4
5
L=
U=
= one
8
10
11
= zero
12
13
19
= aij
=
1
lii
aij
j1
k=1 lik ukj
i1
k=1 lik ukj
(i j)
(i < j).
Exercise 4:What should we do if for some i we have lii = 0? Does this necessarily
mean that LU decomposition does not exist in this case?
8.3
Ecient implementation
Notice that L and U have nonzero elements at dierent locations. The only
place where both has nonzero elements is the diagonal, where U has only 1s.
So we do not need to explicitly store the diagonal entries of U. This lets us store
L and U in a single n n matrix.
Also, observe that aij for i < j is required to compute only uij . Similarly aij for
i j is required to compute only lij . Thus, once uij is computed (for i < j) we
can throw away aij . Similarly, for the case i j. This suggests that we overwrite
A with L and R. Here is how the algorithm overwrites A :
U
L
A
Snapshot of A during Crouts decomposition
8.4
Some theory
Exercise 5:Show that if all the leading principal minors are nonzero then all the lii s
will be nonzero. In fact, if i is the smallest number such that the i-th leading principal
minor is zero, then i is also the smallest number with lii = 0. [Hint: If A = LU and
you partition A as
Aii B
,
A=
C
D
20
where Aii is i i, then what is the LU decomposition of Aii ? Now apply the formula
for determinant of partitioned matrix to show that
lii = |Aii |/|Ai1,i1 |.
Exercise 6:Use the above exercises to characterize all square matrices having LU
decomposition.
QR decomposition
Theorem
For any n by p matrix A with n p we have an n by n orthogonal matrix Q
and an n by p upper triangular matrix R such that
A = QR.
This decomposition is the matrix version of Gram-Schmidt Orthogonalization
(GSO). To see this we rst consider the case where A is full column rank (i.e.,
the columns are independent.) Call the columns
a1 , ..., ap .
Apply GSO to get an orthonormal set of vectors
q1 , ..., qp
given by
i1
qj ai )
qi = unit(ai
j=1
computed in the order i = 1, ..., p. Here unit(v) denotes the unit vector
unit(v) = v/ v ,
for any nonzero v. If v = 0, then unit(v) is not dened.
Notice that ai is a linear combination of q1 , ..., qi only and not of qi+1 , ..., qp . So
we can write
qi = r1i a1 + r2i a2 + + rii ai .
Dene the matrix R using the rij s.
21
i1
If A is not full column rank then some (ai j=1 qj ai ) will be zero, hence we
cannot apply unit to it. But then we can take qi equal to any unit norm vector
orthogonal to q1 , ..., qi1 and set rii = 0.
However, GSO is not the best way to compute QR decomposition of a matrix. This is because in the unit steps you have to divide by the norms which
may be too small. The standard way to implement it is using Householders
transformation.
9.1
Householders transformation
Exercise 7:Show that this A is orthogonal and it sends x to y and vice versa.
9.2
The idea is to shave the columns of X one by one by multiplying with Householder matrices. For any non zero vector u dene Hu as the Householder matrix
22
that reduces u to
u
0
.
.
.
v=
u1
u2
where both u1 , u2 are vectors (u2 being k 1). Consider the n n matrix
I
0
.
H=
0 Hu2
Show that H is orthogonal and compute Hu. We shall say that H shaves the last k 1
elements of u.
Let the rst column of X be a. Let H1 shave its lower n 1 entries. Consider
the second column b of H1 A. Let H2 shave o its lower n 2 entries. Next let c
denote the third column of H2 H1 X, and so on. Proceeding in this way, we get
H1 , H2 , ..., Hp all of which are orthogonal Householder matrices. Dene
Q = (H1 H2 Hp )
and R = Q X to get a QR decomposition.
Exercise 11:Carry this out for the following 5 4 case.
2
6
6
6
6
4
9.3
1
1
1
7
2
2
3
2
2
8
3
2
5
1
5
4
4
0
3
4
3
7
7
7
7
5
Ecient implementation
u1
u2
u6
u4
u3
u5
diagonal entries
of R
Output of ecient QR decomposition
n, p, A
i = 0, ..., p 1
i
A.
The easiest part to expand here is the input part. The only subtlety is to
remember to allocate an extra row in A.
n, p
An+1p
A
n p
The input part
Now comes the tricky step: the shaving. There are two parts here. First, we
have to compute the u vector, and save it in the A matrix. Second, we have to
multiply the later columns by Hu .
25
i.
j = i + 1, ..., p 1
j
How to shave
The owchart below is quite technical in nature. It tells you how to compute
the u vector, and save it in the A matrix. It also saves the diagonal element of
R in the extra row of A.
n1
2
j=i+1 aji
normsq1 =
normsq1 + a2
ii
norm =
normsq1 + a2
ii
norm
normDif f
ani
26
mult = 2
n1
k=i aki akj
k = i, ..., n 1
Updating column j
9.4
Q
or
R1
O
x = b,
x = Q b,
x=
c1
c2
and
Ox = c2 .
The rst system is always consistent and can be solved by back-substitution.
The second system is trivial and inconsistent unless c2 = 0.
1
Exercise 13:Show that x = R1 c1 is the least square solution of the original system.
Notice that you use back-substituting to nd this x and not direct inversion of R1 .
28