Matrix Multiplication
Matrix multiplication is:
To do Matrix multiplication in Python:
from numpy import array
basis = array(
[[1,2],
[3,4]]
)
v = array([[5,6], [7,8]])
n_vector = basis.dot(v)
print(n_vector)
I used the matrix from the example above and implemented it in Python.
There are times when you might multiply B * A instead of A * B. You can write out:
from numpy import array
basis = array(
[[3,4],
[1,2]]
)
v = array([[5,6], [7,8]])
n_vector = basis.dot(v)
print(n_vector)
and it’ll multiply B * A, but you can use transpose() to swap the columns and rows such as:
from numpy import array
first_matrix = array([1,2])
second_matrix = array([3,4])
basis = array([first_matrix, second_matrix]).transpose()
v = array([[5,6], [7,8]])
n_vector = basis.dot(v)
print(n_vector)
It’ll produce the same results as B * A.
Determinants
Sometimes when we do linear transformations, we sometimes expand or condense space. Determinants would tell us how the transformation changes the volume of a given region in the vector space.
For example, take this equation:
You would take the 1st row and multiply it with the matrix (it’s hard to explain it but look at where they got the matrix numbers). Then after you would have to cross multiply, so when they got 2[0-(-4)], it’s basically from the matrix 0 * 5 = 0 then 4 * -1 = -4. Same goes for 3[10-(-1)], it’s 2 * 5 = 10 and 1 * -1 = -1.
Determinant in Python:
from numpy.linalg import det
from numpy import array
first = array([2, -3, 1])
second = array([2, 0, -1])
third = array([1, 4, 5])
basis = array([first, second, third])
determinant = det(basis)
print(determinant)
Using the same example from above, but just implementing it into Python.
If you want another example:
This specific example determinant in Python:
from numpy.linalg import det
from numpy import array
first = array([1, -2, 3])
second = array([2, 0, 3])
third = array([1, 5, 4])
basis = array([first, second, third])
determinant = det(basis)
print(determinant)
Special Matrices
There are a few special matrices:
Square Matrix: equal numbers of rows and columns
Identity Matrix: diagonal of 1s while other values are 0
Inverse Matrix: a matrix that undoes the transformation of another matrix
Diagonal matrix: similar to the identity matrix, but it has a diagonal of nonzero values
Triangular Matrix: diagonal of nonzero values in front of a triangle of values while the rest of the values are zero
Sparse Matrix: mostly zeros, but may contain one nonzero value
Equations
Let’s say that you have:
You would write this out in a matrix:
The function is AX = B. We need to transform A with some other matrix X to give us B.
We do need to calculate the inverse of A, but let’s just use Python and have it do everything for us (you would rarely calculate by hand):
from numpy.linalg import inv
from numpy import array
A = array([
[8, 5, 3],
[7, 2, 4],
[6, 9, 8]
])
B = array([24, 46, 64])
x = inv(A).dot(B)
print(x)
So Python gave us [ 1.08982036 -3.85628743 11.52095808], which means x = 1.08982036, y = -3.85628743, and z = 11.52095808.
Now if we plot it into our equation it matches our results:
(it’s rounded by the way)
[End]