Accuracy of Computations
Gauss’s Method lends itself to computerization. The code below illustrates. It operates on an matrix named a, doing row combinations using the first row, then the second row, etc.
for(row=1; row<=n-1; row++){
for(row_below=row+1; row_below<=n; row_below++){
multiplier=a[row_below,row]/a[row,row];
for(col=row; col<=n; col++){
a[row_below,col]-=multiplier*a[row,col];
}
}
}
This is in the C language. The for(row=1; row<=n-1; row++){ .. } loop initializes row at and then iterates while row is less than or equal to , each time through incrementing row by one with the ++ operation. The other non-obvious language construct is that the -= in the innermost loop has the effect of a[row_below,col]=-1*multiplier*a[row,col]+a[row_below,col].
While that code is a first take on mechanizing Gauss’s Method, it is naive. For one thing, it assumes that the entry in the row,row position is nonzero. So one way that it needs to be extended is to cover the case where finding a zero in that location leads to a row swap or to the conclusion that the matrix is singular.
We could add some if statements to cover those cases but we will instead consider another way in which this code is naive. It is prone to pitfalls arising from the computer’s reliance on floating point arithmetic.
For example, above we have seen that we must handle a singular system as a separate case. But systems that are nearly singular also require care. Consider this one (the extra digits are in the ninth significant place).
By eye we easily spot the solution , . A computer has more trouble. If it represents real numbers to eight significant places, called single precision, then it will represent the second equation internally as , losing the digits in the ninth place. Instead of reporting the correct solution, this computer will think that the two equations are equal and it will report that the system is singular.
For some intuition about how the computer could come up with something that far off, consider this graph of the system.
We cannot tell the two lines apart; this system is nearly singular in the sense that the two lines are nearly the same line. This gives the system () the property that a small change in an equation can cause a large change in the solution. For instance, changing the to changes the intersection point from to . The solution changes radically depending on the ninth digit, which explains why an eight-place computer has trouble. A problem that is very sensitive to inaccuracy or uncertainties in the input values is ill-conditioned.
The above example gives one way in which a system can be difficult to solve on a computer. It has the advantage that the picture of nearly-equal lines gives a memorable insight into one way for numerical difficulties to happen. Unfortunately this insight isn’t useful when we wish to solve some large system. We typically will not understand the geometry of an arbitrary large system.
There are other ways that a computer’s results may be unreliable, besides that the angle between some of the linear surfaces is small. For example, consider this system (from [Hamming]).
The second equation gives , so and thus both variables have values that are just less than . A computer using two digits represents the system internally in this way (we will do this example in two-digit floating point arithmetic for clarity but inventing a similar one with eight or more digits is easy).
The row reduction step produces a second equation , which this computer rounds to two places as . The computer decides from the second equation that and with that it concludes from the first equation that . The value is close but the is bad— the ratio of the actual answer to the computer’s answer is infinite. In short, another cause of unreliable output is the computer’s reliance on floating point arithmetic when the system-solving code leads to using leading entries that are small.
An experienced programmer may respond by using double precision, which retains sixteen significant digits, or perhaps using some even larger size. This will indeed solve many problems. However, double precision has greater memory requirements and besides we can obviously tweak the above to give the same trouble in the seventeenth digit, so double precision isn’t a panacea. We need a strategy to minimize numerical trouble as well as some guidance about how far we can trust the reported solutions.
A basic improvement on the naive code above is to not determine the factor to use for row combinations by simply taking the entry in the row,row position, but rather to look at all of the entries in the row column below the row,row entry and take one that is likely to give reliable results because it is not too small. This is partial pivoting.
For example, to solve the troublesome system () above we start by looking at both equations for a best entry to use, and take the in the second equation as more likely to give good results. The combination step of gives a first equation of , which the computer will represent as , leading to the conclusion that and, after back-substitution, that , both of which are close to right. We can adapt the code from above to do this.
for(row=1; row<=n-1; row++){
/* find the largest entry in this column (in row max) */
max=row;
for(row_below=row+1; row_below<=n; row_below++){
if (abs(a[row_below,row]) > abs(a[max,row]));
max = row_below;
}
/* swap rows to move that best entry up */
for(col=row; col<=n; col++){
temp=a[row,col];
a[row,col]=a[max,col];
a[max,col]=temp;
}
/* proceed as before */
for(row_below=row+1; row_below<=n; row_below++){
multiplier=a[row_below,row]/a[row,row];
for(col=row; col<=n; col++){
a[row_below,col]-=multiplier*a[row,col];
}
}
}
A full analysis of the best way to implement Gauss’s Method is beyond the scope of this book (see [Wilkinson 1965]), but the method recommended by most experts first finds the best entry among the candidates and then scales it to a number that is less likely to give trouble. This is scaled partial pivoting.
In addition to returning a result that is likely to be reliable, most well-done code will return a conditioning number that describes the factor by which uncertainties in the input numbers could be magnified to become inaccuracies in the results returned (see [Rice]).
The lesson is that just because Gauss’s Method always works in theory, and just because computer code correctly implements that method, doesn’t mean that the answer is reliable. In practice, always use a package where experts have worked hard to counter what can go wrong.
Exercises
Exercise 1 Worked answer
Using two decimal places, add and .
Answer. Scientific notation is convenient for expressing the two-place restriction: . Note that adding the has no effect on the total.
Exercise 2 Worked answer
This intersect-the-lines problem contrasts with the example discussed above.
Illustrate that in this system some small change in the numbers will produce only a small change in the solution by changing the constant in the bottom equation to and solving. Compare it to the solution of the unchanged system.
Answer. The reduction
gives . So for this system a small change in the constant produces only a small change in the solution.
Exercise 3 Worked answer
Consider this system ([Rice]).
Solve it.
Solve it by rounding at each step to four digits.
Answer.
The fully accurate solution is that and .
The four-digit reduction
gives the conclusion that , which is not bad, and that . Of course, this is twenty percent different than the correct answer.
Exercise 4 Worked answer
Rounding inside the computer often has an effect on the result. Assume that your machine has eight significant digits.
Show that the machine will compute as unequal to . Thus, computer arithmetic is not associative.
Compare the computer’s version of and . Is twice the first equation the same as the second?
Answer.
For the first one, first, is and so .
For the other one, first and so .
The first equation is while the second is .
Exercise 5 Worked answer
Ill-conditioning is not only dependent on the matrix of coefficients. This example [Hamming] shows that it can arise from an interaction between the left and right sides of the system. Let be a small real.
Solve the system by hand. Notice that the ’s divide out only because there is an exact cancellation of the integer parts on the right side as well as on the left.
Solve the system by hand, rounding to two decimal places, and with .
Answer.
This calculation
gives a third equation of . Substituting into the second equation gives so and thus . With those, the first equation says that .
As above, scientific notation is convenient to express the restriction on the numbe of digits.
The solution with two digits retained is , , and .
References cited in this section
Hamming
Richard W. Hamming, Introduction to Applied Numerical Analysis, Hemisphere Publishing, 1971.
Wilkinson 1965
The Algebraic Eigenvalue Problem, J. H. Wilkinson, Oxford University Press, 1965
Rice
John R. Rice, Numerical Mathods, Software, and Analysis, second edition, Academic Press, 1993.