A-Level: Matrices


In [24]:
# we will be using numpy to create the arrays
# the code isn't so important in this notebook, just the arrays are
import numpy as np

Matrices are 2d arrays written rows x columns


In [25]:
# array conaining 12 consecutive values in shape 3 by 4
a = np.arange(12).reshape([3,4])
print(a)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Simple Matrix Operations

Addition/Subtraction

$(A+B)_{i,j}$

$(A-B)_{i,j}$


In [26]:
# They can be added if they are the same size/shape
# they are added by adding the equivilent value in the other array
A = np.arange(9).reshape([3,3])
B = np.eye(3,3)

add = np.add(A,B)
sub = np.subtract(A,B)
print('A + B =\n', add)
print('A - B =\n', sub)


A + B =
 [[ 1.  1.  2.]
 [ 3.  5.  5.]
 [ 6.  7.  9.]]
A - B =
 [[-1.  1.  2.]
 [ 3.  3.  5.]
 [ 6.  7.  7.]]

Scalar Multiplication

$2\cdot A_{i,j}$


In [27]:
# Scalar multiplication just multiplies each element
sca_mul = np.multiply(2,A)

print(sca_mul)


[[ 0  2  4]
 [ 6  8 10]
 [12 14 16]]

Transposition

This is where each row and column is swapped with its corresponding row or column.

$A^T$


In [28]:
B = np.linspace(0.5,12,24).reshape(3,8)
print('B =')
print(B)
print('\nB^T =')
print( np.transpose(B))


B =
[[  0.5   1.    1.5   2.    2.5   3.    3.5   4. ]
 [  4.5   5.    5.5   6.    6.5   7.    7.5   8. ]
 [  8.5   9.    9.5  10.   10.5  11.   11.5  12. ]]

B^T =
[[  0.5   4.5   8.5]
 [  1.    5.    9. ]
 [  1.5   5.5   9.5]
 [  2.    6.   10. ]
 [  2.5   6.5  10.5]
 [  3.    7.   11. ]
 [  3.5   7.5  11.5]
 [  4.    8.   12. ]]

Matrix Multiplication

only works if

columns in A = rows in B

$A_{m,n}\cdot B_{n,p} = C_{m,p}$

Remember that the order affects the answer


In [29]:
A = np.linspace(1,6,6).reshape(2,3)
B = np.linspace(1,9,9).reshape(3,3)

# use numpy's matrix multiplication matmul
C = np.matmul(A, B)

print('A =')
print(A)
print('B =')
print(B)
print('\nWith A x B = C \nC =')
print(C)


A =
[[ 1.  2.  3.]
 [ 4.  5.  6.]]
B =
[[ 1.  2.  3.]
 [ 4.  5.  6.]
 [ 7.  8.  9.]]

With A x B = C 
C =
[[ 30.  36.  42.]
 [ 66.  81.  96.]]

In this Example,

the element $C_{i,j}$ will be

$\sum A[i,:] \cdot B[:,j]$

Another Example


In [30]:
A = np.linspace(1,6,6).reshape(2,3)
B = np.linspace(2,12,6).reshape(3,2)
C = np.matmul(A, B)
print('A =')
print(A)
print('B =')
print(B)
print('\nWith A x B = C \nC =')
print(C)


A =
[[ 1.  2.  3.]
 [ 4.  5.  6.]]
B =
[[  2.   4.]
 [  6.   8.]
 [ 10.  12.]]

With A x B = C 
C =
[[  44.   56.]
 [  98.  128.]]

Identitiy Matrices

Identity Matrices are Matrices which if you multiply it by any matrix, it returns the same matrix you multiplied it by

They are equivalent to the number 1 but for matrices. They must always be a square and have 1's in a diagonal from top left to bottom right with the rest zeros

Noted as:

$I_s$

with s as a dimension


In [31]:
# Here, we are using the numpy.eye function
# This creates an identity matrix with the specified number size
I = np.eye(5)
print(I)


[[ 1.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.]
 [ 0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  1.]]

Zero Matrices

These are Matrices full of only 0's

Noted as: $O$

Inverse Matrices

If $A\cdot B = C$, then $A = \frac{C}{B}$

But we cant divide matrices so we use $A = B^{-1}C$

$B^{-1}$ is the inverse of B

Also $B\cdot B^{-1} = I$

with I as an identity matrix

For $2\times 2$ matrices

$\left(\begin{array}{cc} a & b \\c & d \end{array}\right)^{-1} = \frac{1}{ad-bc}\left(\begin{array}{cc} d & -b \\-c & a \end{array}\right)$

Matrices with inverses are called nonsingulars or invertables

A Matrix with no inverse, where $ad-bc=0$, is called a singular

The Determinant

$ad-bc$ is the determinant from above.

Noted as: $det(M)$ or $|M|$

with M as the Matrix


In [33]:
A = np.arange(4).reshape(2,2)
print('A =')
print(A)
print('\nA^-1 =')
print(np.linalg.inv(A))


A =
[[0 1]
 [2 3]]

A^-1 =
[[-1.5  0.5]
 [ 1.   0. ]]

For $3\times 3$ or more matrices

$M = \left(\begin{array}{cc} 1 & -1 & 3 \\2 & -1 & 4\\ 2 & 2 & 1 \end{array}\right)$

To find $M^{-1}$:

First, find the Determinant:

For each element in the matrix, remove the row and column its in so you're left with a 2x2 matrix

if $M^{0,0}$ is choosen $\left(\begin{array}{cc} T & - & - \\- & -1 & 4\\ - & 2 & 1 \end{array}\right)$

with T as $M^{0,0}$

Returns $\left(\begin{array}{cc} -1 & 4\\2 & 1 \end{array}\right)$

or if $M^{1,2}$ is choosen $\left(\begin{array}{cc} 1 & -1 & - \\- & - & T\\ 2 & 2 & - \end{array}\right)$

with T as $M^{0,0}$

Returns $\left(\begin{array}{cc} 1 & -1\\2 & 2 \end{array}\right)$

Now find the determinant of this submatrix using $ad-bc$

det$\left(\begin{array}{cc} -1 & 4\\2 & 1 \end{array}\right)$ = $(-1\times 1) - (4\times 2$) = $-9$

Since this submatrix was choosen from point $M^{0,0}$, in our matrix of minors, $M^{0,0} = -9$

if we do this for every point:

Matrix of Minors $= \left(\begin{array}{cc} -9 & -6 & 6 \\-7 & -5 & 4\\ -1 & -2 & 1 \end{array}\right)$

Now we need to find the cofactors of the original matrix

We do this by multiplying each element by $-1^{i+j}$

for $M^{0,0}$, i and j = 0 so:

$-9\times -1^{0+0} = -9\times 1 = -9$

or for $M^{2,1}$, i = 2 and j = 1 so:

$-2\times -1^{2+1} = -2\times -1 = 2$

it can also be visualised as an array with alternating positive and negatives while always starting with a positive like:

$\left(\begin{array}{cc}

  • & - & + \- & + & -\ + & - & + \end{array}\right)$

This gives us:

Matrix of Cofactors $= \left(\begin{array}{cc} -9 & 6 & 6 \\7 & -5 & -4\\ -1 & 2 & 1 \end{array}\right)$

Now find the determinant of the original matrix

Do this by multiplying each individual determinant on one row from our matrix of cofactors by the corresponding original value. It doesnt matter what row you choose, it will always be the same as the calculation endsup using all the elements in the original matrix

$|M| = (-9\times 1) + (6\times -1) + (6\times 3) = 3$

Now find the adjugate (or known as adjoint)

All we do is transpose the matrix of cofactors

This will allow us to multiply it by the original matrix and return an identity matrix

adjugate = $= \left(\begin{array}{cc} -9 & 7 & -1 \\6 & -5 & 2\\ 6 & -4 & 1 \end{array}\right)$

Finally, multiply it by $|M|^{-1}$

$M^{-1} = \frac{1}{3} \left(\begin{array}{cc} -9 & 7 & -1 \\6 & -5 & 2\\ 6 & -4 & 1\end{array}\right)$

We must multiply is by $|M|^{-1}$ because if we multiply any row by corresponding column in the original matrix, we will recieve the determinant because that is how we calculated the determinant in the first place

If the matrix is any bigger, do the same but calculate more submatrices until left with 2x2 matrices like in this example.


In [39]:
# The same example but in 1 line of python

print(np.linalg.inv(np.array([[1,-1,3],[2,-1,4],[2,2,1]])))


[[-3.          2.33333333 -0.33333333]
 [ 2.         -1.66666667  0.66666667]
 [ 2.         -1.33333333  0.33333333]]

In [ ]: