If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our **Contribution Guidelines**.

# Fast Algorithms for Solving a System of Linear Equations

Last modified: November 4, 2022

## 1. Introduction

Every conceivable physical system or scientific problem involves at its core a system of linear algebraic equations. For instance, fitting a curve to a set of datapoints, optimizing a cost function, analyzing electrical networks etc, requires solving linear equations.

Some of the fundamental tasks in computational linear algebra are:

- Solving a system of linear equations
- Finding the inverse of a square matrix
- Computing the determinant of a matrix

An applied introduction to the algorithms for performing such fundamental tasks follows.

In this tutorial, we’ll learn, how a matrix can be decomposed into simpler lego blocks. Operating on these smaller lego blocks is often easier, than working with the original matrix.

## 2. Gaussian Elimination With Partial Pivoting

Consider an elementary system of three linear equations :

in three unknowns . We can write this system in the matrix form as :

The high-school algorithm to solve a system of linear equations consists of performing elementary row operations on the equations. Since performing operations on the equations also affects their right-hand sides, keeping track of everything is most easily done using the augmented matrix .

We shall call the entry of the coefficient matrix , the first pivot, the entry as the second pivot, and so on. **In regular Gaussian Elimination, the basic idea is to make all entries in the column below the th pivot equal to .**

**A key requirement, therefore, is that, a pivot element must always be non-zero**. We sometimes need to interchange rows and , to ensure that the pivot is non-zero. The most common practice is to choose , such that is maximal. This is called partial pivoting.

Since we have performed elementary operations on both the left and right-hand sides of the system, the resulting system of equations , is equivalent to the original system . and have the same solution vector .

Note that, is an upper-triangular matrix with all non-zero elements – the pivots on the diagonal. **A square matrix is called non-singular if it can be reduced to an upper triangular form by elementary row operations. **

The Gaussian Elimination algorithm with partial pivoting in the form of psuedocode is given below:

The resulting system of equations is easy to solve by Back Substitution:

The solution vector of the currents in the electrical network example is .

## 3. LU Factorization

Suppose we’re able to write the matrix as the product of two matrices :

where is a lower triangular matrix, is an upper triangular matrix. We can use such a decomposition to solve the system of linear equations:

We first solve for the vector such that,

and then solving

Solving the lower-triangular system can be done by forward-substitution.

Solving the upper-triangular system can be done by backward-substitution. In this way, factorization can be used to find the solution of a system of equations.

What is the advantage of breaking one linear system of equations into two successive ones? **The advantage is that the solution of a triangular set of equations is easy to find. Once we know the -factorization of the coefficient matrix , it is relatively cheap to solve for a large number of right-hand side vectors .**

### 3.1. LU Decomposition Algorithm

Let be an non-singular square matrix. Let’s write the matrix factorization in the block form:

In the above block form of the matrix , the entry is a scalar, is a row vector, is an column vector, and is an matrix.

Comparing the left- and right-hand sides of the above block matrix equation, we see that:

Rearranging these equations to solve for the components of and matrices, we have:

We can quickly evaluate the first three equations to get the first row and column of the matrices , . We can then evaluate the right hand of the last equation, which gives the Schur’s complement of . We’re thus left with the equation , which is a smaller sub-problem of size that we can recursively solve.

We conceive a simple algorithm based on this intuitive idea:

## 4. QR Factorization

Like the -decomposition, there is another matrix factorization that is sometimes very useful, the so-called -decomposition.

### 4.1. The Gram-Schmidt Process

Suppose, we’re working in a basis for . We’re interested to build an orthonormal basis from . **A practical algorithm to construct an orthonormal basis is the Gram Schmidt process. The Gram Schmidt process is one of the premier algorithms of applied and computational linear algebra.**

We assume that, we already know some basis of an -dimensional space. Our goal is to use this information to construct an orthogonal basis .

We’ll construct the orthogonal basis elements one by one. Since initially, we’re not worrying about normality, there are no conditions on the first orthogonal basis element and so there is no harm in choosing

Note that, , since appears in the original basis. Starting with , the second basis vector must be orthogonal to the first : . Suppose that, it is possible to write as some suitable scalar multiple of and the orthogonal vector . Set:

where is a scalar to be determined:

The orthogonality condition yields:

Consequently:

and therefore:

Next, we construct:

by subtracting multiples of the first two orthogonal basis elements from . We want to be orthogonal to both and . This gives us two equations:

and hence

In this fashion, we can establish the general Gram-Schmidt formula

### 4.2. Modifications to the Gram-Schmidt Process

With the basic Gram-Schmidt algorithm in hand, let’s look at a reformulation that has both practical and theoretical advantages. This can be used to construct the orthonormal basis vectors directly from the basis .

We begin by replacing each orthogonal basis vector in the basic Gram-Schmidt formula by it’s normalized version .

Now, the original basis vectors can also be expressed in terms of the orthogonal basis vectors , via the lower-triangular system :

The coefficients can be directly computed from these formulas. Taking the dot product of the equation for with the orthonormal basis vector for , we get:

and hence:

On the other hand, taking the dot product of the th equation with itself, we have:

Thus, we can obtain by computing:

Once we have all these coefficients , the lower triangular system is easy to solve using forward substitution.

### 4.3. Orthogonal Matrices

**Matrices whose columns form an orthogonal basis of the -dimensional Euclidean space are called orthogonal matrices**. Orthogonal matrices have a plethora of applications ranging from geometry and physics to crystallography, quantum mechanics, and PDEs. Rotational motions of bodies in three-dimensional space are described by orthogonal matrices.

Mathematically, a square matrix is called orthogonal, if it satisfies :

Orthogonality of the matrix implies that we can easily invert the matrix:

### 4.4. The QR Factorization

Let be a basis of and let be the corresponding orthonormal basis that results from the Gram-Schmidt process. We assemble both sets of column vectors to form non-singular matrices:

Since the form an orthonormal basis for , is an orthogonal matrix. In view of the matrix multiplication formula, the Gram-Schmidt equations can be recast into the matrix form:

We can easily implement the modified Gram-Schmidt process.

### 4.5. Solving Linear Systems Using QR Factorization

Once the -decomposition of a matrix is known, it is fairly efficient to solve the linear system of equations . For we have:

The matrix is upper-triangular, so the system is very easy to solve using the back substitution algorithm.

## 5. Conclusion

In this article, we reviewed the high-school method of solving linear equations through the elimination of variables. We learned that matrix factorizations are building blocks of more complex algorithms.

Today, the implementations of , factorizations for solving systems involving square matrices of size , are commonplace in all commercial numerical software libraries such as MATLAB, Eigen, LAPACK, Linpack, NAG, and so on.