Skip to main content

Scientific Computing

Linear Algebra Sol...

Gaussian Elim... []

This material has been adapted from material by Martin Robinson from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

This material has been adapted from material by Martin Robinson from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

Creative Commons License
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

Creative Commons License

Matrix form of equations

Systems of linear equations

Linear algebra is largely concerned with representing, manipulating and solving large systems of linear equations. Consider the following 2 linear equations:

a1x+b1y=c1,(1)a2x+b2y=c2,(2)\begin{aligned} a_1x+b_1y &= c_1, \quad (1)\\ a_2x+b_2y &= c_2, \quad (2) \end{aligned}

where the values   x  \;x\; and   y  \;y\; are to be found, and   a1,  b1,  a2,  b2,  c1  \;a_1, \;b_1, \;a_2, \;b_2, \;c_1\; and   c2  \;c_2\; are given constants. We know that we can use linear combinations of these two equations to solve this sytem for   x  \;x\; and   y  \;y\;, like so:

(1)×b2:               b2a1x+b2b1y=b2c1,(3)(2)×b1:               b1a2x+b1b2y=b1c2,(4)(3)(4):               b2a1xb1a2x=b2c1b1c2.\begin{aligned} (1) \times b_2:~~~~~~~~~~~~~~~ b_2a_1x+b_2b_1y &=& b_2c_1, \quad (3)\\ (2) \times b_1:~~~~~~~~~~~~~~~ b_1a_2x+b_1b_2y &=& b_1c_2, \quad (4)\\ (3) - (4):~~~~~~~~~~~~~~~ b_2a_1x-b_1a_2x &=& b_2c_1-b_1c_2. \end{aligned}

The Matrix

We can also write these linear equations using a matrix. Matrices are structures that allow us to more easily manipulate linear systems. While not particularly useful for just 2 equations, using a matrix representation allows us to generalise to, say NN equations and MM unknowns, or to solve large systems of equations on a computer.

Consider the original system:

a1x+b1y=c1,a2x+b2y=c2.\begin{aligned} a_1x+b_1y &= c_1, \\ a_2x+b_2y &= c_2. \end{aligned}

We rewrite this, in the form of a matrix as:

(a1b1a2b2)(xy)=(c1c2).\left(\begin{matrix}a_1&b_1\\ a_2&b_2\end{matrix}\right) \left(\begin{matrix}x\\y\end{matrix}\right) =\left(\begin{matrix}c_1\\ c_2 \end{matrix}\right).

Think about how this form relates to the original linear system.

Geometry of linear equations

Consider the following system of equations

x+2y=1,x+3y=3.\begin{aligned} x + -2y &= -1, \\ -x + 3y &= 3. \end{aligned}

That is,

A=(1213)A = \left(\begin{matrix} 1 & -2 \\ -1 & 3\end{matrix}\right)

Plotting these two linear equations on a graph shows graphically the solution to this equation given by

(xy)=A1(13)=(32)\left(\begin{matrix} x \\ y \end{matrix}\right) = A^{-1} \left(\begin{matrix} -1 \\ 3 \end{matrix}\right) = \left(\begin{matrix} 3 \\ 2 \end{matrix}\right)

single solution

Now lets consider two different system of equations represented by the matrix:

A=(1212)A = \left(\begin{matrix} 1 & -2 \\ -1 & 2\end{matrix}\right)
x+2y=1,x+2y=3.\begin{aligned} x + -2y &= -1, \\ -x + 2y &= 3. \end{aligned}

and

x+2y=1,x+2y=1.\begin{aligned} x + -2y &= -1, \\ -x + 2y &= 1. \end{aligned}

(left) infinite solutions, (right) no solutions

The first gives the plot on the left, while the second, which has a different vector of constants on the RHS, gives the plot on the right. You can see that depending on the constants, the system of equations can have an infinite number of solutions, or no solutions at all.

The matrix AA in this case is singular, and therefore does not have an inverse. Looking at the equations again, you can see that the two rows of the matrix AA are multiples of the other, and thus there is only one independent row. That is, the rank of the matrix is one.

Singular matrices

The rank of an   n×n  \;n\,\times\,n\; matrix   A  \;A\; is the number of linearly independent rows in   A  \;A\; (rows not combinations of other rows).

When   rank(A)<n  \;\text{rank}(A) < n\; then

  • The matrix is said to be 'rank deficient'

  • The system   Ax=b  \;A\textbf{x} = \textbf{b}\; has fewer equations than unknowns

  • The matrix is said to be singular

  • The matrix is said to be underdetermined

  • A  A\; has no inverse

  • The determinant of   A  \;A\; is 0

  • The equation   Au=0  \;A\textbf{u} = \textbf{0}\; has non-trivial solutions (u0\textbf{u} \neq \textbf{0})

The determinant

One way of solving a system of equations represented by Ax=bA x = b is to calculate the inverse of A, giving the solution as x=A1bx = A^{-1} b. This can be done by calculating what is known as the determinant of AA.

If

A=(pqrs)A = \left(\begin{matrix} p & q \\ r & s\end{matrix}\right)

then the determinant of A is:

A=psqr|A| = ps-qr

The inverse of AA can be found using the determinant:

A1=1psqr(sqrp)A^{-1} = \frac{1}{ps-qr} \left(\begin{matrix} s & -q \\ -r & p\end{matrix}\right)

Calculating the inverse of a matrix using its determinant can be very costly for larger matrices, therefore other algorithms are used (e.g. Gaussian Elimination, which is introduced in the next section)

If A=0|A| = 0, A is said to be singular (have no inverse). Graphically, this is represented by the parallel or non-intersecting lines in the figure above.

Using Python to calculate the inverse

To find A1A^{-1} for

A=(302303011)A = \left(\begin{matrix}3&0&2\\ 3&0&-3\\ 0&1&-1\end{matrix}\right)

you can using numpy like so:

import numpy as np A = np.array([[3, 0, 2], [3, 0, -3], [0, 1, 1]]) np.linalg.inv(A)

Output:

array([[ 0.2 , 0.13333333, 0. ], [-0.2 , 0.2 , 1. ], [ 0.2 , -0.2 , -0. ]])

It doesn't always work. Consider the following system

A=(1112427107)A = \left(\begin{matrix}1&1&1\\ 2&4&2\\ 7&10&7\end{matrix}\right)
A = np.array([[1, 1, 1], [2, 5, 2], [7, 10, 7]]) np.linalg.inv(A)

Output:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<__array_function__ internals>", line 6, in inv File "/home/mrobins/git/scientific-computing/env/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 546, in inv ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj) File "/home/mrobins/git/scientific-computing/env/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 88, in _raise_linalgerror_singular raise LinAlgError("Singular matrix") numpy.linalg.LinAlgError: Singular matrix

Other Reading

  • Linear algebra by Ward Cheney

  • Linear algebra and its applications by David C. Lay.

  • Strang, G. (2016). Introduction to linear algebra (Fifth ed.). Wellesley.

  • Linear algebra and its applications by Gilbert Strang

  • LA from an ODE perspective: Kapitula, T. (2015). Ordinary Differential Equations and Linear Algebra. Society for Industrial and Applied Mathematics.

Problems

Intersection of planes

  1. Describe the intersection of the three planes u+v+w+z=6u+v+w+z = 6, u+w+z=4u+w+z = 4 and u+w=2u+w = 2 (all in four-dimensional space). Is it a line or a point or a fourth equation that leaves us with no solution. an empty set? What is the intersection if the fourth plane u=1u = −1 is included? Find a fourth equation that leaves us with no solution.

Python: Intersection of planes

Sketch or plot in Python these three lines and decide if the equations are solvable: 3 by 2 system x+2y=2x + 2y = 2, xy=2x − y = 2, and y=1y = 1. What happens if all right-hand sides are zero? Is there any nonzero choice of right- hand sides that allows the three lines to intersect at the same point?

Upper triangular matrix

Write a Python function that takes in a 3×33 \times 3 upper triangular matrix AA represented as an ndarray, and a rhs vector bb, and solves the equation Ax=bA x = b. i.e. the function will solve the following triangular system for x=(x1,x2,x3)x = (x_1, x_2, x_3):

A11x1+A12x2+A13x3=b1,A22x2+A23x3=b2,A33x3=b3\begin{aligned} A_{11} x_1 + A_{12} x_2 + A_{13} x_3 &= b_1, \\ A_{22} x_2 + A_{23} x_3 &= b_2, \\ A_{33} x_3 &= b_3 \end{aligned}

Generalise this function to a n×nn \times n triangular matrix input.