import numpy as np
A = np.array([
[1, 2, 3],
[4, 5, 6]])
B = np.array([
[2, 2, 2],
[2, 2, 2]])
# matrix addition
C = A + B
print(A," +\n")
print(B," =\n")
print(C)
[[1 2 3] [4 5 6]] + [[2 2 2] [2 2 2]] = [[3 4 5] [6 7 8]]
#matrix substraction
C = A - B
print(A," -\n")
print(B," =\n")
print(C)
[[1 2 3] [4 5 6]] - [[2 2 2] [2 2 2]] = [[-1 0 1] [ 2 3 4]]
#matrix Hadamard product
C = A * B
print(A," *\n")
print(B," =\n")
print(C)
[[1 2 3] [4 5 6]] * [[2 2 2] [2 2 2]] = [[ 2 4 6] [ 8 10 12]]
#matrix division
C = A / B
print(A," /\n")
print(B," =\n")
print(C)
[[1 2 3] [4 5 6]] / [[2 2 2] [2 2 2]] = [[0.5 1. 1.5] [2. 2.5 3. ]]
#matrix multiplication (dot product) (rows form 1st matrix x cols from 2nd matrix)
#rule: The number of columns (n) in the first matrix (A) must equal the number of rows (m) in
#the second matrix (B)
#C = A dot B
#C(m,k)=A(m,n) dot B(n,k)
$ A = \begin{pmatrix} a_{1,1} &a_{1,2} \\ a_{2,1} &a_{2,2} \\ a_{3,1} &a_{3,2} \end{pmatrix} \quad B = \begin{pmatrix} b_{1,1} &b_{1,2} \\ b_{2,1} &b_{2,2} \\ \end{pmatrix} \\ C = A \cdot B \\ $
$ C = \begin{pmatrix} a_{1,1} \times b_{1,1} + a_{1,2} \times b_{2,1} &a_{1,1} \times b_{1,2} + a_{1,2} \times b_{2,2} \\ a_{2,1} \times b_{1,1} + a_{2,2} \times b_{2,1} &a_{2,1} \times b_{1,2} + a_{2,2} \times b_{2,2} \\ a_{3,1} \times b_{1,1} + a_{3,2} \times b_{2,1} &a_{3,1} \times b_{1,2} + a_{3,2} \times b_{2,2} \\ \end{pmatrix} $
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8],[9,10],[11,12]])
print(A," x\n")
print(B," =\n")
print(A.dot(B))
[[1 2 3] [4 5 6]] x [[ 7 8] [ 9 10] [11 12]] = [[ 58 64] [139 154]]
# multiply matrices with @ operator
D = A @ B
print(D)
[[ 58 64] [139 154]]