19 Matrices
Matrices¶
update: 2023
An [latex]m \times n[/latex] dimensional matrix is a collection of
- n m-dimensional column vectors OR
- m n-dimensional row vectors
That’s not confusing at all!
A [latex]3 \times 2[/latex] matrix:
$$A = \begin{pmatrix}7 & 2\\8 & 5\\ 1&4\end{pmatrix}$$
As opposed to a 2 by 3 mattrix:
$$B = \begin{pmatrix}1 & 2&3\\4 & 5&6\end{pmatrix}$$
A square matrix has the same number of rows and columns:
$$C = \begin{pmatrix} 5&4\\7& 2\end{pmatrix}$$
A vector is just a matrix with one column.
## let's just put this in Python, shall we?
import numpy as np
A =np.array([(7,2),(8,5), (1,4)])
B = np.array([(1,2,3),(4,5,6)])
C = np.array([(5,4),(7,2)])
print("A = ",A)
print("B = ", B)
print("and C = ", C)
A = [[7 2] [8 5] [1 4]] B = [[1 2 3] [4 5 6]] and C = [[5 4] [7 2]]
Adding Matrices¶
To add matrices, they need to be the sane shape. Then we just piecewise add them – kind of like we did with adding vectors:
$$\begin{pmatrix} a&b\\c& d\end{pmatrix}+\begin{pmatrix} e&f\\g& h\end{pmatrix}=\begin{pmatrix} a+e&b+f\\c+g& d+h\end{pmatrix}$$
Or with numbers:
$$\begin{pmatrix} 5&4\\7& 2\end{pmatrix}+\begin{pmatrix} 1&2\\3& 4\end{pmatrix}+\begin{pmatrix} 5+1&4+2\\7+3& 2+4\end{pmatrix} = \begin{pmatrix} 6&6\\10& 6\end{pmatrix}$$
## or in Python, we can just add:
C = np.array([(5,4),(7,2)])
D = np.array([(1,2),(3,4)])
print("Adding C and D equals", C+ D)
Adding C and D equals [[ 6 6] [10 6]]
Multiplying is harder¶
We can define a kind of multiplication on Matrices, using the dot product as our guide. For this to work, we need the columns of the first matric to be equal to the rows of the second. Pretend you have row vectors in the first, and column vectors in the second, then do a whole lot of dot products. Here’s an example:
[latex]\begin{align*} \begin{pmatrix}1&0\\3&2\\5&1\end{pmatrix} \cdot \begin{pmatrix}5&1&2&-1\\6&0&1&5\end{pmatrix}\\ = \begin{pmatrix}(1, 0)\\(3,2)\\ (5,1)\end{pmatrix} \cdot \begin{pmatrix} \begin{pmatrix} 5\\6 \end{pmatrix}&\begin{pmatrix} 1\\0 \end{pmatrix}&\begin{pmatrix} 2\\1 \end{pmatrix}&\begin{pmatrix} -1\\5 \end{pmatrix} \end{pmatrix}\\ =\begin{pmatrix} \begin{pmatrix} 1\\0\end{pmatrix}\cdot\begin{pmatrix} 5\\6\end{pmatrix} &\begin{pmatrix} 1\\0\end{pmatrix}\cdot\begin{pmatrix} 1\\0\end{pmatrix} &\begin{pmatrix} 1\\0\end{pmatrix}\cdot\begin{pmatrix} 2\\1\end{pmatrix} &\begin{pmatrix} 1\\0\end{pmatrix}\cdot\begin{pmatrix} -1\\5\end{pmatrix}\\ \begin{pmatrix} 3\\2\end{pmatrix}\cdot\begin{pmatrix} 5\\6\end{pmatrix} &\begin{pmatrix} 3\\2\end{pmatrix}\cdot\begin{pmatrix} 1\\0\end{pmatrix} &\begin{pmatrix} 3\\2\end{pmatrix}\cdot\begin{pmatrix} 2\\1\end{pmatrix} &\begin{pmatrix} 3\\2\end{pmatrix}\cdot\begin{pmatrix} -1\\5\end{pmatrix}\\ \begin{pmatrix} 5\\1\end{pmatrix}\cdot\begin{pmatrix} 5\\6\end{pmatrix} &\begin{pmatrix} 5\\1\end{pmatrix}\cdot\begin{pmatrix} 1\\0\end{pmatrix} &\begin{pmatrix} 5\\1\end{pmatrix}\cdot\begin{pmatrix} 2\\1\end{pmatrix} &\begin{pmatrix} 5\\1\end{pmatrix}\cdot\begin{pmatrix} -1\\5\end{pmatrix}\\ \end{pmatrix}\\ =\begin{pmatrix} 5 &1&2&-1\\ 27&3 & 8&7\\ 31&5&11&0\end{pmatrix} \end{align*}[/latex]
This will make a 3 by 4 matrix,
## this works WAY better with a computer than by hand:
A = np.array([(1,0),(3,2), (5,1)])
B = np.array([(5,1,2,-1),(6,0,1,5)])
np.matmul(A,B)
array([[ 5, 1, 2, -1], [27, 3, 8, 7], [31, 5, 11, 0]])
Matrix Algebra is non-commutative. That meas that [latex]AB \neq BA[/latex]. See?
A =np.array([(7,2),(8,5), (1,4)])
B = np.array([(1,2,3),(4,5,6)])
print("A 3 by 2 matrix: A = ")
print(A)
print("A 2 by 3 matrix:B = ")
print(B)
print("A times B is a 3 by 3 matrix:")
print(np.matmul(A,B))
print("But B times A is a 2 by 2 matrix:")
print(np.matmul(B,A))
A 3 by 2 matrix: A = [[7 2] [8 5] [1 4]] A 2 by 3 matrix:B = [[1 2 3] [4 5 6]] A times B is a 3 by 3 matrix: [[15 24 33] [28 41 54] [17 22 27]] But B times A is a 2 by 2 matrix: [[26 24] [74 57]]