## Solving Systems Of Linear Equations

### July 20, 2010

In today’s exercise we continue the examination of matrix operations that we began previously. Our goal is to be able to solve a system of equations; along the way we will see two methods for decomposing matrices.

We begin with some terminology. All the matrices we will be looking at are square, meaning that they have the same number of rows and columns. A lower-triangular matrix *L* has all entries *L*_{ij} = 0 for *i* < *j*; thus, all entries above the main northwest-to-southeast diagonal are zero. An upper-triangular matrix *U* has all entries *U*_{ij} = 0 for *i* > *j*; thus, all entries below the main northwest-to-southeast diagonal are zero. A lower- or upper-triangular matrix is unit lower- or upper-triangular if all the entries along the main diagonal are 1. A permutation matrix *P* has exactly one 1 in each row and column and 0 elsewhere; it is called a permutation matrix because multiplying a vector *X* by a permutation matrix has the effect of permuting the elements of *X*. The identity matrix *I* has 1 in each entry along the main diagonal and 0 elsewhere. A matrix *M* is singular if it has no inverse, that is, there is no matrix *M*^{-1} such that *M* *M*^{-1} = *I*.

An LU decomposition of a matrix *A* finds two matrices *L*, which is unit lower-triangular, and *U*, which is upper-triangular, such that *A* = *L U*. The algorithm is called *Gaussian elimination*, and works from top to bottom. First, multiples of the first equation are subtracted from the other equations so that the first variable is removed from those equations. Then multiples of the second equation are subtracted from the remaining equations so that the second variable is removed from those equations. Then the third equation, and the fourth, and so on, until all the equations have been processed and the matrix is in upper-triangular form. Here is an example of the LU decomposition of matrix *A* into its factors *L* × *U*:

There are two problems with LU decomposition: First, the algorithm leads to a divide-by-zero error on singular matrices. Second, it is prone to numerical instability for small divisors. The solution is to rearrange, or permute, the equations so that the pivot element is always the largest remaining element, greatly reducing the likelihood of numerical instability.

An improved decomposition is the LUP decomposition, which finds for an input matrix *A* three matrices *L*, *U*, and a permutation matrix *P* such that *P A* = *L U*. Rather than actually moving equations, the permutation matrix records the rearrangements. For example, here is the LUP decomposition of the matrix *A* given by *P* × *A* = *L* × *U*:

Given the LUP decomposition, it is simple to solve a system of linear equations. Forward substitution solves the lower-triangular system by calculating the first variable, which is part of an equation with one unknown, then substitutes that into the second equation, reducing it from two unknowns to one unknown, and so on. Then back substitution runs backward, calculating the final values of the variables in the original matrix. Here’s an example, where we wish to solve for the vector *X* given *A X* = *B*:

The LUP decomposition *P A* = *L U* is

,

the result of forward substitution *L Y* = *P B* is

, giving ,

and the result of the back substitution *U X* = *Y* is

, giving ,

which is the solution.

Your task is to write functions that perform LU-decomposition and LUP-decomposition and solve systems of linear equations. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Since I’m currently at a conference I don’t have as much time to devote to this as I’d like, but here’s my implementation of LU decomposition:

If I have some time before Friday I’ll see if I can work on the rest.

Also, you might want to fix the examples in the exercise, since they contain some errors (e.g. in the first example: 1×3 + 3×4 + 1×0 + 0x0 = 15, not 19). The versions used in the solution code are correct.

Fixed four errors. I didn’t do that very well, did I?

[...] Praxis – Solving Systems Of Linear Equations By Remco Niemeijer In yesterday’s Programming Praxis exercise our task is to implement some more matrix-related functions, [...]

My Haskell solution (see http://bonsaicode.wordpress.com/2010/07/21/programming-praxis-solving-systems-of-linear-equations/ for a version with comments):