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
In [25]:
# array conaining 12 consecutive values in shape 3 by 4
a = np.arange(12).reshape([3,4])
print(a)
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)
In [27]:
# Scalar multiplication just multiplies each element
sca_mul = np.multiply(2,A)
print(sca_mul)
In [28]:
B = np.linspace(0.5,12,24).reshape(3,8)
print('B =')
print(B)
print('\nB^T =')
print( np.transpose(B))
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)
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)
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
$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)
In [33]:
A = np.arange(4).reshape(2,2)
print('A =')
print(A)
print('\nA^-1 =')
print(np.linalg.inv(A))
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)$
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)$
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}
This gives us:
Matrix of Cofactors $= \left(\begin{array}{cc} -9 & 6 & 6 \\7 & -5 & -4\\ -1 & 2 & 1 \end{array}\right)$
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$
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]])))
In [ ]: