This material has been adapted from material by Fergus Cooper and others from the "Essential Mathematics" module at the Doctoral Training Centre, University of Oxford.
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1
Linear algebra 1
Learning outcomes
Be able to express a set of simultaneous equations as a matrix equation
Understand the size of a matrix and the notation for its elements
Be able to add and subtract matrices, and multiply a matrix by a scalar
Be able to multiply matrices, and know when a product is defined
Know that matrix multiplication is associative and distributive, but not commutative
Be able to find the determinant and the inverse of a 2x2 matrix, and know what it means for a matrix to be singular
Be able to find the transpose of a matrix
Be able to solve a matrix equation $A\mathbf{x}=\mathbf{b}$, by hand for a 2x2 matrix and with Python for larger ones
YouTube lecture recording from October 2020
A recording of this lecture from October 2020; the material has changed a little since.
Why matrices?
Many problems give us several equations that must all hold at once.
For example, the steady state of a model with several interacting chemicals or species is where all of their rates of change are zero at the same time, and the currents in an electrical circuit must balance at every junction at once.
When the equations are linear, matrices let us write any number of them as a single equation, Ax=b, and solve them all in the same way: by hand for small systems, and with Python for large ones.
In this lecture we learn how to add, multiply and invert matrices, and use them to solve simultaneous equations.
Simultaneous equations
Consider 2 simultaneous equations:
a1x+b1ya2x+b2y=c1,=c2,(1)(2)
where the values x and y are to be found, and a1, b1, a2, b2, c1 and c2 are given constants.
While the algebraic manipulation is straightforward when solving two equations, it can get really messy when solving large systems.
What we want is a way to be able to easily manipulate linear systems, regardless of how big they are.
Two equations, two lines
Each of the two equations is a straight line in the (x,y) plane, and the solution is the point where the two lines cross.
For example, take
x+5yax+5y=11,=9,
for which a1b2−a2b1=5−5a.
With a=−1 the lines cross at x=1, y=2 (we solve this system with matrices later in the lecture):
As a approaches 1, the second line turns until it is parallel to the first, and the point where they cross moves off to infinity.
At a=1, a1b2−a2b1=0: the lines are parallel and never cross, so there is no solution.
If the two lines had been the same line (e.g. if the second equation were x+5y=11 too), every point on it would be a solution, so there would be infinitely many.
Either way, there is no single solution when a1b2−a2b1=0.
The matrix
Matrices are a structure that allow us to more easily manipulate linear systems.
Consider the original system
a1x+b1ya2x+b2y=c1,=c2.
We collect the coefficients into a matrix, and the unknowns and the right-hand sides into columns, and rewrite the system in the form of a matrix as:
(a1a2b1b2)(xy)=(c1c2).
We will see why this is the same system once we know how to multiply matrices.
What is a matrix?
A matrix is a rectangular array of numbers, such as
A=(142536).
A matrix with m rows and n columns has sizem×n (rows first), so A is a 2×3 matrix.
The element in row i and column j of a matrix A is written aij, using the lower-case letter.
Here, for example, a11=1, a23=6 and a21=4.
A matrix with as many rows as columns is square, and an n×n matrix has order (dimension) n.
A matrix with a single column is a column vector, and we write vectors in bold.
For example,
x=(xy)
is a 2×1 matrix.
Similarly, a matrix with a single row is a row vector.
Addition, subtraction, and scalar multiplication
We can add or subtract two matrices as long as they have the same size, by adding or subtracting the elements in the same positions:
(231−4)+(61−5−7)=(84−4−11)
We can multiply a matrix by a scalar, by multiplying every element:
5×(231−4)=(10155−20)
Matrix multiplication
To multiply two matrices, we multiply each row in the first matrix by each column in the second one, and put the results into a new matrix.
A row and column are multiplied by summing up each element in the row, multiplied by the corresponding element in the column.
For example, the element in row 2 and column 1 of the product is row 2 of the first matrix, (3,4), multiplied by column 1 of the second, (5,7): 3×5+4×7=43.
If the number of columns in the first matrix doesn't match the number of rows in the second, they cannot be multiplied.
(223−113)(1−10−4)=???
Here each row of the first matrix has three elements, but each column of the second has only two, so there is nothing to pair the third element with.
In general, an m×n matrix times an n×p matrix gives an m×p matrix: the inner sizes must match, and the outer sizes give the size of the product.
Back to the matrix form
Now we can multiply out the matrix form of our two simultaneous equations:
(a1a2b1b2)(xy)=(a1x+b1ya2x+b2y).
Each row gives back the left-hand side of one of the equations, so Ax=c is exactly the original system, with
A=(a1a2b1b2),x=(xy),c=(c1c2).
The same works for any number of equations and unknowns: A has one row per equation and one column per unknown.
Matrix multiplication is not commutative
In general AB is not the same as BA: the order matters.
This can be easily seen from the fact that multiplying different sized matrices doesn't always work:
Multiplying by this matrix on the right swaps the columns, while multiplying by it on the left swaps the rows.
Laws of matrix multiplication
Matrix multiplication is not commutative, but it does obey two laws we know from numbers (whenever the products are defined):
it is associative: (AB)C=A(BC), so we can write ABC without brackets;
it is distributive: A(B+C)=AB+AC and (A+B)C=AC+BC.
We need both forms of the distributive law, because A(B+C) and (B+C)A are not the same.
For the same reason, when we multiply both sides of a matrix equation by a matrix, we must multiply both sides on the same side (both on the left, or both on the right).
The identity matrix
I is the identity matrix, which has the property that:
AI=IA=A
for a square matrix A of the same size.
It is the matrix equivalent of multiplying by 1.
It has 1s on the leading diagonal (from top left to bottom right) and 0s everywhere else.
The 2x2 identity matrix is:
I2=(1001).
The 3x3 identity matrix is:
I3=100010001.
and so on for higher dimensions.
Inverting matrices
To solve a matrix equation, we need the matrix equivalent of dividing: the inverse.
The inverse of a matrix
For a number a=0, its inverse a−1=1/a satisfies aa−1=a−1a=1.
There is no matrix division, but the inverse of a matrix plays the same role.
If A and B are square matrices with
AB=BA=I,
then B is the inverse of A, written B=A−1, and A is the inverse of B, A=B−1.
For a 2x2 matrix there is a formula for the inverse: if
A=(prqs)
then:
A−1=ps−qr1(s−r−qp).
That is, swap the two elements on the leading diagonal, change the sign of the other two, and divide by ps−qr.
We can check that this works by multiplying out:
The formula for A−1 only works if ps−qr=0.
This number is the determinant of A, written ∣A∣ or detA:
∣A∣=ps−qr
That is, (top left × bottom right) - (top right × bottom left).
If ∣A∣=0, A is said to be singular, and it has no inverse.
For our two simultaneous equations, the determinant of the matrix of coefficients is
a1a2b1b2=a1b2−a2b1,
which is the number we had to divide by when we solved them at the start of the lecture.
When it is zero, the two lines are parallel, and there is no single solution.
Larger square matrices have determinants too, and the same holds: a square matrix has an inverse exactly when its determinant is not zero.
We won't need to calculate them by hand in this course.
Note
The determinant as an area
Multiplying by a 2x2 matrix A moves every point of the plane: the point (x,y) goes to A(xy).
The corners (1,0) and (0,1) of the unit square go to the two columns of A, so the unit square goes to the parallelogram with those columns as its sides.
Its area is ∣detA∣, and if detA<0 the square is also flipped over.
For example, with
A=(2112),
the arrows below show the columns (2,1) (orange) and (1,2) (blue), and the parallelogram has area detA=4−1=3:
If detA=0, the square is squashed flat onto a line (or a point), so different points end up in the same place.
Then there is no way to undo the transformation, which is why a singular matrix has no inverse.
Example of inverting a 2x2 matrix
Let us take a matrix
A=(2−2−34).
First, calculate its determinant:
∣A∣=(2×4)−(−3×−2)=8−6=2.
This is not zero, so A has an inverse, and from the formula:
AT, the transpose of A, swaps the rows and columns: row i of A becomes column i of AT, so that (AT)ij=aji.
For a square matrix, this reflects the elements across the leading diagonal:
AAT=21114−1262=2121461−12
Solving a linear system using matrices
To solve the matrix equation Ax=b for the unknown vector x, we multiply both sides on the left by A−1:
A−1AxIxx=A−1b=A−1b=A−1b
We must multiply on the left on both sides, because the order of multiplication matters: bA−1 is not even defined when b is a column vector.
To find x:
for a 2x2 matrix, use the formula for A−1 and calculate x=A−1b;
for larger matrices by hand, use Gaussian elimination, which is the elimination we used at the start of the lecture, done systematically one unknown at a time (not in this course);
in practice, use a linear algebra package such as NumPy or SymPy, which does this for us.
Example of solving a 2x2 linear system
Let's take an example:
x+5y−x+5y=11,=9
In matrix form, this gives:
(1−155)(xy)=(119)
The determinant is ∣A∣=(1×5)−(5×−1)=10, so from the formula:
A−1=101(51−51)
Thus:
(xy)=101(51−51)(119)=101(1020)=(12)
And x=1,y=2: the point where the two lines cross in the figure above.
This process seems like more effort than it's worth for small systems.
But it allows for a much more systematic approach when dealing with large systems.
As the size of the matrix grows, this process can be easily performed with Python (or other tools).
Example: solving a 4x4 system in Python
x+5y+3z−wx−2y+z+4w−3x+y−z+2wx+y+z=5,=2,=−5,=0.
In matrix form, this gives:
11−315−21131−11−1420xyzw=52−50.
Numerically, using NumPy
In Python, we store matrices and vectors as NumPy arrays, and np.linalg.solve solves Ax=b.
We could instead calculate the inverse of A with np.linalg.inv and multiply, but solving directly is faster and more accurate.
The @ operator multiplies matrices, so we can check the answer by calculating Ax:
import numpy as np
A = np.array([[1,5,3,-1],[1,-2,1,4],[-3,1,-1,2],[1,1,1,0]])b = np.array([5,2,-5,0])# Solve A x = b directly: faster and more accurate than finding A^-1x = np.linalg.solve(A, b)print(x)# Check: A x should give back bprint(A @ x)
The last entry of Ax is −8.9×10−16, not 0.
Computers store numbers to about 16 significant figures, so tiny rounding errors like this appear in almost every numerical calculation.
Symbolically, using SymPy
SymPy works with exact fractions instead, so there are no rounding errors:
import sympy as sp
A = sp.Matrix([[1,5,3,-1],[1,-2,1,4],[-3,1,-1,2],[1,1,1,0]])b = sp.Matrix([5,2,-5,0])A.solve(b)
−18107−94618199−310
Introductory problems
Tip
For more practice, an AI tool can write new problems like the ones below.
Ask it for problems of the same type and difficulty, or for problems set in your own field.
For example, you could ask it to "write five engineering problems on matrices and simultaneous equations, at the level of these ones".
Work them on paper first, then ask the tool to check your answers and to explain any step you are unsure of.
Introductory problems 1
Write the following system of equations:
x+y+z2x−y+zx−2y+2z=7=2=5
in the form
Ax=b
yB=c
Where A and B are 3×3 matrices, and x, y, b and c are vectors whose size and shape you should carefully indicate.
Check that your answers make sense by expanding your expressions to ensure you get back to the original equations.
The coefficients of each equation form a row of A, and the unknowns form a column vector:
1211−1−2112xyz=725
A is 3×3, and x and b are 3×1 column vectors.
Multiplying out, the first row of A times x gives x+y+z, which must equal the first entry of b, 7, and so on.
Now the unknowns form a row vector on the left, so the coefficients of each equation form a column of B.
This means that B=AT:
(xyz)1112−111−22=(725)
B is 3×3, and y and c are 1×3 row vectors.
Multiplying out, y times the first column of B gives x+y+z, which must equal the first entry of c, 7, and so on.
But this does not prove that matrix multiplication is associative.
It is only one example, and a proof has to work for all matrices.
(Matrix multiplication is associative, but showing it needs a general argument.)
One example can disprove a rule, though, as part 4. does for commuting.
Again, this is one example, so it does not prove the distributive property (which does hold for all matrices).
Main problems
Main problems 1
If
A=21(1111),
find A2 and A3 and comment on your results.
A2=41(1111)(1111)=41(2222)=21(1111)=A
So, A2=A, and then A3=A2A=AA=A.
In fact An=A for every positive whole number n.
A matrix with A2=A is called idempotent.
Multiplying a vector by this A replaces both of its entries by their average, and doing that a second time changes nothing.
AB=BA, so A and B do not commute.
But (AB)T=BTAT and (BA)T=ATBT.
In other words, the transpose of a product is the product of the transposes in the reverse order (this is true for all matrices).
Main problems 3
Find the determinant, ∣A∣, of the following matrices:
A=(1126)
A=(12−1−4)
For a 2×2 matrix, ∣A∣=a11a22−a12a21.
∣A∣=1×6−2×1=4
∣A∣=1×(−4)−(−1)×2=−4+2=−2
Main problems 4
Find the inverse, A−1, of the following matrices:
A=(2−154)
A=(−3−127)
For a 2×2 matrix, swap the diagonal entries, change the sign of the other two, and divide by the determinant:
(acbd)−1=ad−bc1(d−c−ba)
∣A∣=2×4−5×(−1)=13, so
A−1=131(41−52)
∣A∣=(−3)×7−2×(−1)=−19, so
A−1=−191(71−2−3)=191(−7−123)
You can check each answer by multiplying it by A, which should give the identity matrix.
Main problems 5
Use Python's numpy.linalg.solve to solve the following systems of equations:
x+2y−3z2x−y+z4x−y+z=9,=0,=4.
x+5y+3z5x+y−2zx+2y+z=17,=4,=7.
2y+zx−2y−3z−x+y+2z=−8,=0,=3.
# hintimport numpy as np
A = np.array([[1,2,-3],[2,-1,1],[4,-1,1]])b = np.array([9,0,4])x = np.linalg.solve(A, b)print(x)
Write each system as Ax=b, with the coefficients of x, y and z in the rows of A (using a 0 for a missing term):
import numpy as np
A = np.array([[1,2,-3],[2,-1,1],[4,-1,1]])b = np.array([9,0,4])print(np.linalg.solve(A, b))A = np.array([[1,5,3],[5,1,-2],[1,2,1]])b = np.array([17,4,7])print(np.linalg.solve(A, b))A = np.array([[0,2,1],[1,-2,-3],[-1,1,2]])b = np.array([-8,0,3])print(np.linalg.solve(A, b))
So, (XY)−1=Y−1X−1 and (YX)−1=X−1Y−1.
Like the transpose, the inverse of a product is the product of the inverses in the reverse order.
This makes sense: to undo "multiply by Y, then by X", we first undo X, then undo Y.
Note also that ∣XY∣=∣X∣∣Y∣=4.
Main problems 7
Write each of the following systems in the form Ax=b, and solve it by hand by finding A−1 and calculating x=A−1b:
3x−2yx+y=4,=3.
2x+3y4x−y=1,=9.
Check your answers using Python's numpy.linalg.solve.
In matrix form:
(31−21)(xy)=(43)
∣A∣=3×1−(−2)×1=5, so
A−1=51(1−123)
and
(xy)=51(1−123)(43)=51(105)=(21)
So, x=2 and y=1.
In matrix form:
(243−1)(xy)=(19)
∣A∣=2×(−1)−3×4=−14, so
A−1=−141(−1−4−32)
and
(xy)=−141(−1−4−32)(19)=−141(−2814)=(2−1)
So, x=2 and y=−1.
In Python:
import numpy as np
A = np.array([[3,-2],[1,1]])b = np.array([4,3])print(np.linalg.solve(A, b))A = np.array([[2,3],[4,-1]])b = np.array([1,9])print(np.linalg.solve(A, b))
So, λ1=4 and λ2=2.
Multiplying each of these vectors by the matrix just stretches it, without changing its direction.
Vectors like this are called eigenvectors, and λ1 and λ2 are their eigenvalues (more on this in the next lecture).