2.1. Row Reduction (Gaussian Elimination)#
Last revised on August 25, 2025
References:
[Chasnov, 2012] Section 3.1, Gaussian Elimination.
[Sauer, 2022] Section 2.1.1, Naive Gaussian Elimination.
[Burden et al., 2016] Section 6.1, Linear Systems of Equations.
[Dionne, 2023] Section 4.1, Gaussian Elimination with Backward Substitution. This includes partial pivoting, as seen here in the section Partial Pivoting.
[Chenney and Kincaid, 2013] Section 2.1, Naive Gaussian Elimination.
[Trefethen and Bau, 2022] Lecture 20, Gaussian Elimination. (This starts directly with the LU factorization approach as in section Solving Ax = b with LU factorization — that is not a bad idea!).
[Dahlquist and Björck, 2009] Section 7.2.1, Triangular Systems and Gaussian Elimination.
2.1.1. Introduction#
This section introduces a basic algorithm for solving a system of \(n\) linear equations in \(n\) unknowns, based on the strategy of row-reduction or Gaussian elimination. You might have seen this strategy in a linear algebra course, but if not, do not worry: this chapter introduces everything from the beginning, not assuming any previous knowledge of linear algebra beyond matrix notation.
This section also takes the opportunity to introduce some general tactics for getting from mathematical facts to precise algorithms and then to code which uses these algorithms.
2.1.2. Strategy for getting from mathematical facts to a good algorithm and then to its implentation in [Julia] code#
Here I take the opportunity to illustrate some useful strategies for getting from mathematical facts and ideas to good algorithms and working code for solving a numerical problem. The pattern we will see here, and often later, is:
2.1.2.1. Step 1. Get a basic algorithm:#
Start with mathematical facts (like the equations \(\sum_{j=1}^n a_{ij}x_j = b_i\)).
Solve to get an equation for each unknown — or for an updated aproximation of each unknown — in terms of other quantitities.
Specify an order of evaluation in which all the quantities at right are evaluated earlier.
In this, it is often best to start with a verbal description before specifying the details in more precise and detailed mathematical form.
2.1.2.2. Step 2. Refine to get a more robust algorithm:#
Identify cases that can lead to failure due to division by zero and such, and revise to avoid them.
Avoid inaccuracy due to problems like severe rounding error. One rule of thumb is that anywhere that a zero value is a fatal flaw (in particular, division by zero), a very small value is also a hazard when rounding error is present. So avoid very small denominators. (We will soon examine this through the phenomenon of loss of significance and it extreme case catastrophic cancellation.)
2.1.2.3. Step 2. Refine to get a more robust algorithm:#
Identify cases that can lead to failure due to division by zero and such, and revise to avoid them.
Avoid inaccuracy due to problems like severe rounding error. One rule of thumb is that anywhere that a zero value is a fatal flaw (in particular, division by zero), a very small value is also a hazard when rounding error is present. So avoid very small denominators. (The section on Machine numbers, rounding error and Error Propagation examines this through the phenomenon of loss of significance.)
2.1.2.4. Step 3. Refine to get a more efficient algorithm#
For example,
Avoid repeated evaluation of exactly the same quantity.
Avoid redundant calculations, such as ones whose value can be determnied in advance; for example, values that can be shown in advance to be zero.
Compare and choose between alternative algorithms.
2.1.3. Row Reduction, a.k.a. Gaussian Elimination#
We start by considering the most basic algorithm, based on ideas seen in a linear algebra course.
The problem is best stated as a collection of equations for individual numerical values:
Given coefficients \(a_{i,j} 1 \leq i \leq n,\, 1 \leq j \leq n\) and right-hand side values \(b_i,\, 1 \leq i \leq n\), solve for the \(n\) unknowns \(x_j,\, 1 \leq j \leq n\) in the equations
In verbal form, the basic strategy of row reduction or Gaussian elimination is this:
Choose one equation and use it to eliminate one chosen unknown from all the other equations, leaving that chosen equation plus \(n-1\) equations in \(n-1\) unknowns.
Repeat recursively, at each stage using one of the remaining equations to eliminate one of the remaining unknowns from all the other equations.
This gives a final equation in just one unknown, preceeded by an equation in that unknown plus one other, and so on: solve them in this order, from last to first.
2.1.3.1. Determining those choices, to produce a first algorithm: “Naive Row Reduction”#
A precise algorithm must include rules specifying all the choices indicated above. The simplest “naive” choice, which works in most but not all cases, is to eliminate from the top to bottom and left to right:
Use the first equation to eliminate the first unknown from all other equations.
Repeat recursively, at each stage using the first remaining equation to eliminate the first remaining unknown. Thus, at step \(k\), equation \(k\) is used to eliminate unknown \(x_k\).
This gives one equation in just the last unknown \(x_n\); another equation in the last two unknowns \(x_{n-1}\) and \(x_n\), and so on: solve them in this reverse order, evaluating the unknowns from last to first.
This usually works, but can fail because at some stage the (updated) \(k\)-th equation might not include the \(k\)-th unknown: that is, its coefficient might be zero, leading to division by zero.
We will refine the algorithm to deal with that in the later section Partial Pivoting.
2.1.4. The general case of solving \(Ax = b\)#
The problem of solving \(Ax = b\) in general, when all you know is that \(A\) is an \(n \times n\) matrix and \(b\) is an \(n\)-vector, can in most cases be handled well by using standard software rather than by writing your own code. Here is an example in Julia, solving
A = [4.0 2.0 7.0; 3.0 5.0 -6.0; 1.0 -3.0 2.0]
println("A =\n$(A)")
b = [2.0; 3.0; 4.0]
println("b = $(b)")
A =
[4.0 2.0 7.0; 3.0 5.0 -6.0; 1.0 -3.0 2.0]
b = [2.0, 3.0, 4.0]
Remark 2.1 (On Julia)
See the notes on Arrays in Notes on the Julia Language.
Julia mimics Matlab’s notation for “dividing from the left”: the solution of \(Ax = b\) is \(x = A^{-1} b\) and given by
A\b; it is not \(b A^{-1}\) which is what you get from the usual “divide from the right” notation ofb/A.
x = A\b;
println("Julia says that the solution of Ax=b is x=$(x)")
Julia says that the solution of Ax=b is x=[1.8116883116883116, -1.0324675324675323, -0.45454545454545453]
Check the residual \(b - Ax\), a measure of backward error:
r = b-A*x;
println()
println("As a check, the residual is")
println(" r = b - Ax = $(r)")
println("and its infinity (or 'maximum') norm is")
println(" ||r|| = $(maximum(abs.(r)))")
As a check, the residual is
r = b - Ax = [0.0, 0.0, 8.881784197001252e-16]
and its infinity (or 'maximum') norm is
||r|| = 8.881784197001252e-16
Remark 2.2 (Not quite zero values and rounding)
Some values here that you might hope to be zero are instead very small non-zero numbers, with exponent \(10^{-16}\), due to rounding error in computer arithmetic. For details on this (like why “-16” in particular) see Machine Numbers, Rounding Error and Error Propagation.
2.1.5. The naive row reduction algorithm, in pseudo-code#
Here the elements of the transformed matrix and vector after step \(k\) are named \(a_{i,j}^{(k)}\) and \(b_{k}^{(k)}\), so that the original values are \(a_{i,j}^{(0)} = a_{i,j}\) and \(b_{i}^{(0)} = b_{i}\).
The name \(l_{i,k}\) is given to the multiple of row \(k\) that is subtracted from row \(i\) at step \(k\). This naming might seem redundant, but it becomes very useful later, in the section on LU factorization.
Algorithm 2.1 (naive row reduction)
for k from 1 to n-1 \(\qquad\) Step k: get zeros in column k below row k:
\(\quad\) for i from k+1 to n
\(\qquad\) Evaluate the multiple of row k to subtract from row i:
\(\quad\quad l_{i,k} = a_{i,k}^{(k-1)}/a_{k,k}^{(k-1)}\) \(\qquad\) If \(a_{k,k}^{(k-1)} \neq 0\)!
\(\qquad\) Subtract \((l_{i,k}\) times row k) from row i in matrix A …:
\(\quad\quad\) for j from 1 to n
\(\quad\quad\quad a_{i,j}^{(k)} = a_{i,j}^{(k-1)} - l_{i,k} a_{k,j}^{(k-1)}\)
\(\quad\quad\) end
\(\qquad\) … and at right, subtract \((l_{i,k}\) times \(b_k)\) from \(b_i\):
\(\quad\quad b_i^{(k)} = b_i^{(k-1)} - l_{i,k} b_{k}^{(k-1)}\)
\(\quad\) end
The rows before \(i=k\) are unchanged, so they are ommited from the update;
however, in a situation where we need to complete the definitions of \(A^{(k)}\) and \(b^{(k)}\) we would also need the following inside the for k loop:
Algorithm 2.2 (Inserting the zeros below the main diagonal)
\(\quad\) for i from 1 to k
\(\quad\quad\) for j from 1 to n
\(\quad\quad\quad a_{i,j}^{(k)} = a_{i,j}^{(k-1)}\)
\(\quad\quad\) end
\(\quad\quad b_i^{(k)} = b_i^{(k-1)}\)
\(\quad\) end
However, the algorithm will usually be implemented by overwriting the previous values in an array with new ones, and then this part is redundant.
The next improvement in efficiency: the updates in the first \(k\) columns at step \(k\) give zero values (that is the key idea of the algorithm!),
so there is no need to compute or store those zeros,
and thus the only calculations needed in the above for j from 1 to n loop are covered by for j from k+1 to n.
Thus from now on we use only the latter—except when, for demonstration purposes, we need those zeros.
Thus, the standard algorithm looks like this:
Algorithm 2.3 (basic row-reduction)
for k from 1 to n-1 \(\qquad\) Step k: Get zeros in column k below row k:
\(\quad\) for i from k+1 to n \(\qquad\) Update only the rows that change: from k+1 on:
\(\qquad\) Evaluate the multiple of row k to subtract from row i:
\(\quad\quad l_{i,k} = a_{i,k}^{(k-1)}/a_{k,k}^{(k-1)}\) \(\qquad\) If \(a_{k,k}^{(k-1)} \neq 0\)!
\(\qquad\) Subtract \((l_{i,k}\) times row k) from row i in matrix A, in the columns that are not automaticaly zero:
\(\quad\quad\) for j from k+1 to n
\(\quad\quad\quad a_{i,j}^{(k)} = a_{i,j}^{(k-1)} - l_{i,k} a_{k,j}^{(k-1)}\)
\(\quad\quad\) end
\(\qquad\) and at right, subtract \((l_{i,k}\) times \(b_k)\) from \(b_i\):
\(\quad\quad b_i^{(k)} = b_i^{(k-1)} - l_{i,k} b_{k}^{(k-1)}\)
\(\quad\) end
2.1.6. The naive row reduction elimination algorithm, in Julia#
Conversion to actual Julia code is now quite straightforward; there is litle more to be done than:
Change the way that indices are described, from \(b_i\) to
b[i]and from \(a_{i,j}\) toA[i,j].Use case consistently in array names, since the quirk in mathematical notation of using upper-case letters for matrix names but lower case letters for their elements is gone! In these notes, matrix names will be upper-case and vector names will be lower-case (even when a vector is considered as 1-column matrix).
Rather than create a new array for each matrix \(A^{(0)}\), \(A^{(1)}\), etc. and each vector \(b^{(0)}\), \(b^{(1)}\), we overwite each in the same array.
for k in 1:n
for i in k+1:n
L[i,k] = A[i,k] / A[k,k]
for j in k+1:n
A[i,j] -= L[i,k] * A[k,j]
end
b[i] -= L[i,k] * b[k]
end
end
To demonstrate this, some additions are needed:
Putting this algorithm into a function.
Getting the value \(n\) needed for the loop, using the fact that it is the length of vector
b.Creating the array \(L\).
Copying the input arrays
Aandbinto new ones,Uandc, so that the original arrays are not changed. That is, when the row reduction is completed,Ucontains \(A^{(n-1)}\) andccontains \(b^{(n-1)}\).
Also, for some demonstrations, the zero values below the main diagonal of U are inserted, though usually they would not be needed.
function row_reduce(A, b)
# To avoid modifying the matrix and vector specified as input,
# they are copied to new arrays, with the function copy().
# Warning: it does not work to say "U = A" and "c = b";
# this makes these names synonyms, referring to the same stored data.
U = copy(A) # not "U=A", which makes U and A synonyms
c = copy(b)
n = length(b)
L = zeros(n, n)
for k in 1:n-1
for i in k+1:n
# compute all the L values for column k:
L[i,k] = U[i,k] / U[k,k] # Beware the case where U[k,k] is 0
for j in k+1:n
U[i,j] -= L[i,k] * U[k,j]
end
# Put in the zeros below the main diagonal in column k of U;
# this is not important for calculations,
# since those elements of U are not used in backward substitution,
# but it helps for displaying results and for checking the results via residuals.
U[i,k] = 0.
c[i] -= L[i,k] * c[k]
end
end
for i in 2:n
for j in 1:i-1
U[i,j] = 0.
end
end
return (U, c)
end;
Here is a helper function for displaying matrices.
Since it will be used in several sections, it is also in the module NumericalMethods, along with the above function;
see Module NumericalMethods.
Remark 2.3 (Julia Modules)
Modules and their usage are introduced in the notes on Using modules and packages. As explained there, these two functions can be obtained with
include("NumericalMethods.jl")
using .NumericalMethods:row_reduce
using .NumericalMethods:print_matrix
Details about creating your own modules will be given in creating modules once those notes are written; meanwhile, examining Module NumericalMethods could help: the actual module definition file is just the Julia code from there with the interspersed explanatory comments removed.
Some helper functions to cleanup output
function print_matrix(A)
# A helper function to "pretty print" matrices
(rows, cols) = size(A)
print("[ ")
for row in 1:rows
if row > 1
print(" ")
end
for col in 1:cols
print(A[row,col], " ")
end
if row < rows;
println()
else
println("]")
end
end
end;
# A shorthand for rounding off to n significant digits.
import Base: round
round(x, n::Integer) = round(x, sigdigits=n);
println("A is")
print_matrix(A)
println("b = $(b)")
A is
[ 4.0 2.0 7.0
3.0 5.0 -6.0
1.0 -3.0 2.0 ]
b = [2.0, 3.0, 4.0]
(U, c) =row_reduce(A, b);
println("Row reduction gives")
println("U =")
print_matrix(U)
println("c = $(c)")
Row reduction gives
U =
[ 4.0 2.0 7.0
0.0 3.5 -11.25
0.0 0.0 -11.0 ]
c = [2.0, 1.5, 5.0]
Let’s take advantage of the fact that we have used Julia’s built-in linear algebra command b\A to get a very accurate approximation of the solution \(x\) to \(Ax=b\); this should also solve \(Ux=c\), so check the backward error, a.k.a. the residual:
r = c - U * x
println("The residual (backward error) r = c-Ux is $(round.(r,4))," *
" with maximum norm $(round(maximum(abs.(r)),4))")
The residual (backward error) r = c-Ux is [0.0, -2.22e-16, 0.0], with maximum norm 2.22e-16
Remark 2.4 (Array slicing in Julia)
Many operations in linear algebra can be expressed more concisely using array slicing and vectorization as described in Notes on the Julia Language allowing the loops above to be expressed as
for k in 1:n
L[k+1:end,end] = A[k+1:end,k] / A[k,k]
A[k+1:end,k+1:end] -= L[k+1:end,k] * A[[k],k+1:end]
b[k+1:end] -= L[k+1:end,k] * b[k]
end
end
Remark 2.5 (Matrix slicing in Julia)
One subtlety here, as mentioned in the notes on slicing:
that row slice at the end of line 3 has to be done as A[[k],k+1:end] with brackets around the row index,
in order to make it a 1-row matrix;
using A[k,k+1:end] instead would give a vector, and then the product would fail.
I will break my usual guideline of non-repetition by redefining row_reduce,
since this is just a restatement of exactly the same algorithm with different Julia notation.
While I am about it, I add a demo_mode, for display of intermediate results.
function row_reduce(A, b; demo_mode=false)
# To avoid modifying the matrix and vector specified as input,
# they are copied to new arrays, with the method .copy()
# Warning: it does not work to say "U = A" and "c = b";
# this makes these names synonyms, referring to the same stored data.
# This version vectorizes the inner loops, and all of the "i, j" loop for updating U.
U = copy(A) # not "U=A", which makes U and A synonyms
c = copy(b)
n = length(b)
L = zeros(n, n)
for k in 1:n-1
if demo_mode; println("Step $k:"); end
# compute all the L values for column k:
L[k+1:end,k] = U[k+1:end,k] / U[k,k] # Beware the case where U[k,k] is 0
if demo_mode
println("The multipliers in column $(k) are $(L[k+1:end,k])")
end
U[k+1:end,k+1:end] -= L[k+1:end,k] * U[[k],k+1:end]
c[k+1:end] -= L[k+1:end,k] * c[k]
# Insert the below-diagonal zeros in column k;
# this is not important for calculations,
# since those elements of U are not used in backward substitution,
# but it helps for displaying results and for checking the results via residuals.
U[k+1:end,k] .= 0.0
if demo_mode
println("The updated matrix is")
print_matrix(U)
println("The updated right-hand side is $c")
end
end
return (U, c)
end;
Repeating the above testing:
(U, c) =row_reduce(A, b,demo_mode=true);
println("Row reduction gives U=")
print_matrix(U)
println("and right-hand side $c")
Step 1:
The multipliers in column 1 are [0.75, 0.25]
The updated matrix is
[ 4.0 2.0 7.0
0.0 3.5 -11.25
0.0 -3.5 0.25 ]
The updated right-hand side is [2.0, 1.5, 3.5]
Step 2:
The multipliers in column 2 are [-1.0]
The updated matrix is
[ 4.0 2.0 7.0
0.0 3.5 -11.25
0.0 0.0 -11.0 ]
The updated right-hand side is [2.0, 1.5, 5.0]
Row reduction gives U=
[ 4.0 2.0 7.0
0.0 3.5 -11.25
0.0 0.0 -11.0 ]
and right-hand side [2.0, 1.5, 5.0]
2.1.7. Backward substitution with an upper triangular matrix#
The transformed equations have the form
and can be solved from bottom up, starting with \(x_n = c_n/u_{n,n}\).
All but the last equation can be written as
and so solved as
This procedure is backward substitution, giving the algorithm
Algorithm 2.4 (Backward substitution)
\(x_n = c_n/u_{n,n}\)
for i from n-1 down to 1
\(\displaystyle \quad x_i = \frac{c_i - \sum_{j=i+1}^{n} u_{i,j} x_j}{u_{i,i}}\)
end
This works so long as none of the main diagonal terms \(u_{i,i}\) is zero, because when done in this order, everything on the right hand side is known by the time it is evaluated.
For future reference, note that the elements \(u_{k,k}\) that must be non-zero here, the ones on the main diagonal of \(U\), are the same as the elements \(a_{k,k}^{(k)}\) that must be non-zero in the row reduction stage above, because after stage \(k\), the elements of row \(k\) do not change any more: \(a_{k,k}^{(k)} = a_{k,k}^{(n-1)} = u_{k,k}\).
Remark 2.6 (Summing in Julia)
For an \(n\)-element single-index array v, the sum of its elements \(\sum_{i=1}^{n} v_i\) is given by sum(v).
Thus \(\sum_{i=a}^{b} v_i\), the sum over a subset of indices \([a,b]\), is given by sum(v[a:b]).
2.1.7.1. The backward substitution algorithm in Julia#
With all the above Julia details, the core code for backward substitution is:
x[end] = c[end]/U[end,end]
for i in n-1:-1:1
x[i] = (c[i] - U[i,i+1:end] * x[i+1:end])) / U[i,i]
end
Observation 2.1
Note that the backward substitution algorithm and its Julia coding have a nice mathematical advantage over the row reduction algorithm above: the precise mathematical statement of the algorithm does not need any intermediate quantities distinguished by superscripts \({}^{(k)}\) and correspondingly, all variables in the code have fixed meanings, rather than changing at each step.
In other words, all uses of the equal sign are mathematically correct as equations!
This can be advantageous in creating algorithms and code that is more understandable and more readily verified to be correct, and is an aspect of the functional programming approach. We will soon go part way to that functional ideal, by rephrasing row-reduction in a form where all variables have clear, fixed meanings, corresponding to the natural mathematical description of the process: the method of LU factorization introduced in Solving Ax = b with LU factorization.
As a final demonstration, we put this second version of the code into a complete working Julia function and test it:
function backward_substitution(U, c;demo_mode=false)
n = length(c)
x = zeros(n)
x[end] = c[end]/U[end,end]
if demo_mode
println("x_$n = $(x[n])")
end
for i in n-1:-1:1
x[i] = ( c[i] - sum(U[i,i+1:end] .* x[i+1:end]) ) / U[i,i]
if demo_mode
println("x_$i = $(x[i])")
end
end
return x
end;
which, as usual, is also available via
using .NumericalMethods:backward_substitution
x = backward_substitution(U, c,demo_mode=true)
println(" ")
println("x = $x")
r = b - A*x
println("The residual b - Ax = $(round.(r,4)), with maximum norm $(round(maximum(abs.(r)),4)))")
x_3 = -0.45454545454545453
x_2 = -1.0324675324675323
x_1 = 1.8116883116883116
x = [1.8116883116883116, -1.0324675324675323, -0.45454545454545453]
The residual b - Ax = [0.0, 0.0, 8.882e-16], with maximum norm 8.882e-16)
Since one is often just interested in the solution given by the two steps of row reduction and then backward substitution, they can be combined in a single function by composition:
solve_linear_system(A, b) = backward_substitution(row_reduce(A, b)...);
Remark 2.7 (The Julia “splat” operation)
The splat notation “…” takes the item to its left (here the tuple (U, c) returned by row_reduce) and unpacks it into separate items [here U and c, as needed for input to backwardsubstitution.
x = solve_linear_system(A, b)
println("x = $x")
x = [1.8116883116883116, -1.0324675324675323, -0.45454545454545453]
2.1.8. Two code testing hacks: starting from a known solution, and using randomly generated examples#
An often useful strategy in developing and testing code is to create a test case with a known solution; another is to use random numbers to avoid accidently using a test case that in unusually easy.
Remark 2.8 (function rand! from module Random)
The preferred style is to have all
importandusingstatements near the top, but since this is the first time we’ve heard of moduleRandomI did not want it to be mentioned mysteriously above.The exclamation point in
rand!indicates that the function modifies its input. This will be discussed in Functions part 2 when that section gets written.
using Random: rand!
n = length(b)
x_random = zeros(n)
rand!(x_random) # fill with random values, uniform in [0, 1)
(x_random *= 2.0) .-= 1.0 # double and then subtract 1: now uniform in [-1, 1)
println("x_random = $x_random")
x_random = [0.09041138353921396, -0.5531448066870839, 0.4495100318153302]
Create a right-hand side b that automatically makes x_random the correct solution:
b_random = A * x_random;
println("A is");print_matrix(A)
println("\nbrandom is $b_random")
(U,c_random) =row_reduce(A,b_random)
println("\nU is");print_matrix(U)
println("\nResidualc_random - U*x_random = $(round.(c_random - U*x_random,4))")
x_computed = backward_substitution(U,c_random)
println("\nx_computed is $(round.(x_computed,12))")
r = b_random - A*x_computed
println("\nResidual b_random-A*x_computed is $(round.(r,4))")
println("\nBackward error is $(round(maximum(abs.(r)),4))")
x_error =x_random - x_computed
println("\nError x_random - x_computed is $(round.(x_error,4))")
println("\nAbsolute error |x_random-x_computed| is $(round(maximum(abs.(x_error)),4))")
A is
[ 4.0 2.0 7.0
3.0 5.0 -6.0
1.0 -3.0 2.0 ]
brandom is [2.40192614349, -5.1915500737097595, 2.648865867231126]
U is
[ 4.0 2.0 7.0
0.0 3.5 -11.25
0.0 0.0 -11.0 ]
Residualc_random - U*x_random = [0.0, 0.0, 0.0]
x_computed is [0.0904113835392, -0.553144806687, 0.449510031815]
Residual b_random-A*x_computed is [4.441e-16, 0.0, -4.441e-16]
Backward error is 4.441e-16
Error x_random - x_computed is [0.0, 1.11e-16, 0.0]
Absolute error |x_random-x_computed| is 1.11e-16
2.1.9. What can go wrong? Some examples#
Example 2.1 (An obvious division by zero problem)
Consider the system of two equations
It is easy to see that this has the solution \(x_1 = x_2 = 1\); in fact it is already in “reduced form”. However when put into matrix form
the above algorithm fails, because the fist pivot element \(a_{11}\) is zero:
A1 = [0.0 1.0 ; 1.0 1.0]
b1 = [1.0 ; 1.0]
println("A1 is)"); print_matrix(A1)
println("b1 is $(b1)")
A1 is)
[ 0.0 1.0
1.0 1.0 ]
b1 is [1.0, 1.0]
(U1, c1) =row_reduce(A1, b1)
x1 = backward_substitution(U1, c1)
println("U1 is");print_matrix(U1)
print("c1 is $c1")
print("x1 is $x1")
U1 is
[ 0.0 1.0
0.0 -Inf ]
c1 is [1.0, -Inf]x1 is [NaN, NaN]
Remark 2.9 (IEEE fake numbers Inf and NaN)
Inf, meaning “infinity”, is a special value given as the result of calculations like division by zero. Surprisingly, it can have a sign!NaN, meaning “Not a Number”, is a special value given as the result of a calculation like0/0.
Example 2.2 (A less obvious division by zero problem)
Next consider this system
The solution is \(x_1 = x_2 = x_3 = 1\), and this time none of th diagonal elements is zero, so it is not so obvious that a division by zero problem will occur, but:
A2 = [1.0 1.0 1.0 ; 1.0 1.0 2.0 ; 1.0 2.0 2.]
b2 = [3.0 ; 4.0 ; 5.]
println("A2 is");print_matrix(A2)
println("b2 is $b2")
A2 is
[ 1.0 1.0 1.0
1.0 1.0 2.0
1.0 2.0 2.0 ]
b2 is [3.0, 4.0, 5.0]
(U2, c2) =row_reduce(A2, b2)
x2 = backward_substitution(U2, c2)
println("U2 is");print_matrix(U2)
println("c2 is $c2")
println("x2 is $x2")
U2 is
[ 1.0 1.0 1.0
0.0 0.0 1.0
0.0 0.0 -Inf ]
c2 is [3.0, 1.0, -Inf]
x2 is [NaN, NaN, NaN]
What happens here is that the first stage subtracts the first row from each of the others …
A2[2,:] -= A2[1,:]
b2[2] -= b2[1]
A2[3,:] -= A2[1,:]
b2[3] -= b2[1];
… and the new matrix has the same problem as above at the next stage:
println("Now A2 is");print_matrix(A2)
println("and b2 is $(b2)")
Now A2 is
[ 1.0 1.0 1.0
0.0 0.0 1.0
0.0 1.0 1.0 ]
and b2 is [3.0, 1.0, 2.0]
Thus, the second and third equations are
with the same problem as in Example 2.1.
Example 2.3 (Problems caused by inexact arithmetic: “divison by almost zero”)
The equations
again have the solution \(x_1 = x_2 = 1\), and the only division that happens in the above algorithm for row reduction is by that pivot element \(a_{11} = 1, \neq 0\), so with exact arithmetic, all would be well. But:
A3 = [1.0 1e16 ; 1.0 1.0]
b3 = [1.0 + 1e16 ; 2.0]
println("A3 is");print_matrix(A3)
println("b3 is $b3")
A3 is
[ 1.0 1.0e16
1.0 1.0 ]
b3 is [1.0e16, 2.0]
(U3, c3) =row_reduce(A3, b3)
x3 = backward_substitution(U3, c3)
println("U3 is");print_matrix(U3)
println("c3 is $c3")
println("x3 is $x3")
U3 is
[ 1.0 1.0e16
0.0 -1.0e16 ]
c3 is [1.0e16, -9.999999999999998e15]
x3 is [2.0, 0.9999999999999998]
This gets \(x_2 = 1\) fairly accurately, but \(x_1\) is completely wrong!
One hint is that \(b_1\), which should be \(1 + 10^{16} = 1000000000000001\), is instead just given as \(10^{16}\).
On the other hand, all is well with less large values, like \(10^{15}\):
A3a = [1.0 1e15 ; 1.0 1.0]
b3a = [1.0 + 1e15 ; 2.0]
println("A3a is");print_matrix(A3a)
println("b3a is $b3a")
A3a is
[ 1.0 1.0e15
1.0 1.0 ]
b3a is [1.000000000000001e15, 2.0]
(U3a, c3a) =row_reduce(A3a, b3a)
x3a = backward_substitution(U3a, c3a)
println("U3a is");print_matrix(U3a)
println("c3a is $c3a")
println("x3a is $x3a")
U3a is
[ 1.0 1.0e15
0.0 -9.99999999999999e14 ]
c3a is [1.000000000000001e15, -9.99999999999999e14]
x3a is [1.0, 1.0]
Example 2.4 (Avoiding small denominators)
The first equation in Example 2.3 can be divided by \(10^{16}\) to get an equivalent system with the same problem:
Now the problem is more obvious: this system differs from the system in Example 2.1 just by a tiny change of \(10^{-16}\) in that pivot elements \(a_{11}\), and the problem is division by a value very close to zero.
A4 = [1e-16 1.0 ; 1.0 1.0]
b4 = [1.0 + 1e-16 ; 2.0]
println("A4 is");print_matrix(A4)
println("b4 is $b4")
A4 is
[ 1.0e-16 1.0
1.0 1.0 ]
b4 is [1.0, 2.0]
(U4, c4) =row_reduce(A4, b4)
x4 = backward_substitution(U4, c4)
println("U4 is");print_matrix(U4)
println("c4 is $c4")
println("x4 is $x4")
U4 is
[ 1.0e-16 1.0
0.0 -1.0e16 ]
c4 is [1.0, -9.999999999999998e15]
x4 is [2.220446049250313, 0.9999999999999998]
One might think that there is no such small denominator in Example 2.3, but what counts for being “small” is magnitude relative to other values — 1 is very small compared to \(10^{16}\).
To understand these problems more (and how to avoid them) we will explore Machine Numbers, Rounding Error and Error Propagation in the next section.
2.1.10. When naive row reduction is safe: diagonal dominance#
There are several important cases when we can guarantee that these problem do not occur. One obvious case is when the matrix \(A\) is diagonal and non-singular (so with all non-zero elements); then it is already row-reduced and with all denominators in backward substitution being non-zero.
A useful measure of being “close to diagonal” is diagonal dominance:
Definition 2.1 (Strict Diagonal Dominance)
A matrix \(A\) is row-wise strictly diagonally dominant, sometimes abbreviated as just strictly diagonally dominant or SDD, if
Loosely, each main diagonal “dominates” in size over all other elements in its row.
Definition 2.2 (Column-wise Strict Diagonal Dominance)
If instead
(so that each main diagonal element “dominates its column”) the matrix is called column-wise strictly diagonally dominant.
Note that this is the same as saying that the transpose \(A^T\) is SDD.
Remark 2.10
Aside: If only the corresponding non-strict inequality holds, the matrix is called diagonally dominant.
Theorem 2.1
For any strictly diagonally dominant matrix \(A\), each of the intermediate matrices \(A^{(k)}\) given by the naive Gaussan elimination algorithm is also strictly diagonally dominant, and so the final upper triangular matrix \(U\) is. In particular, all the diagonal elements \(a_{i,i}^{(k)}\) and \(u_{i,i}\) are non-zero, so no division by zero occurs in any of these algorithms, including the backward substitution solving for \(x\) in \(Ux = c\).
The corresponding fact also true if the matrix is column-wise strictly diagonally dominant: that property is also preserved at each stage in naive row reduction.
Thus in each case the diagonal elements — the elements divided by in both row reduction and backward substitution — are in some sense safely away from zero. We will have more to say about this in the sections on Partial Pivoting and Solving Ax = b with LU factorization
For a column-wise SDD matrix, more is true: at stage \(k\), the diagonal dominance says that the pivot element on the diagonal, \(a_{k,k}^{(k-1)}\), is larger (in magnitude) than any of the elements \(a_{i,k}^{(k-1)}\) below it, so the multipliers \(l_{i,k}\) have
As we will see when we look at the effects of rounding error in Machine Numbers, Rounding Error and Error Propagation and Error bounds for linear algebra, condition numbers, matrix norms, etc., keeping intermediate values small is generally good for accuracy, so this is a nice feature.
Remark 2.11 (Positive definite matrices)
Another class of matrices for which naive row-reduction works well is positive definite matrices which arise in any important situations; that property is in some sense more natural than diagonal dominance. However that topic will be left for later.
2.1.11. Exercises#
Exercise 2.1
Solve \(A \vec{x} = \vec b\) with
using naive row-reduction , computing all intermediate values as decimal approximations rounded to four significant digits – no fractions! Call this approximate solution \(\vec{x}_1\).
Then compute the residual \(\vec{r}_1 = \vec{b} - A \vec{x}_1\) and its norm \(\| \vec{r}_1 \|_\infty\). This should be done with high accuracy (not rounding to four decimal places) and could be done using a Julia notebook as a “calculator”.
Next, use Julia software to compute the condition number (see Definition 2.8)of \(A\), \(\kappa(A) = \kappa_\infty(A)\), (see section Error bounds for linear algebra, condition numbers, matrix norms, etc.), and use this to get a bound on the absolute error in each of the above approximations.
Finally some “cheating”: use Julia software to compute the “exact” solution and use this to compute the actual absolute error; compare this to the bound computed above.