初めに
einsumを使うため、行列の演算を整理しておく。
基本操作
matrix generation
import numpy as np
mat = np.array([[1,2],
[3,4],
[5,6]])
array([[1, 2],
[3, 4],
[5, 6]])
inverse matrix
mat = np.array([[1,2],
[3,4],
])
mat_inv = np.linalg.inv(mat)
array([[-2. , 1. ],
[ 1.5, -0.5]])
pseudo inverser matrix
mat = np.array([[1,2],
[3,4],
])
mat_inv2 = np.linalg.pinv(mat)
array([[-2. , 1. ],
[ 1.5, -0.5]])
transpose
mat = np.array([[1,2],
[3,4],
])
mat_t = mat.T
array([[1, 3],
[2, 4]])
積 Product
element-wise multiplication
matA = np.array([[1, 2], [3, 4]])
matB = np.array([[5, 6], [7, 8]])
print(matA)
print(matB)
[[1 2]
[3 4]]
[[5 6]
[7 8]]
matAB = np.multiply(matA, matB)
[[ 5 12]
[21 32]]
inner product (dot product)
matA_dot_matB = np.dot(matA,matB)
[[19 22]
[43 50]]
matmul()
matA_matmul_matB = np.matmul(matA, matB)
[[19, 22],
[43, 50]]
outer product (cross product)
np.cross(matA, matB)
[-4, -4]