點乘
基本概念
- 簡而言之就是矩陣各對應元素相乘。
- 需滿足乘數矩陣和被乘數矩陣的行向量或列向量相等,或兩者同時相等。
數學公式
S1 矩陣尺寸不完全相同
S2 矩陣尺寸完全相同
Python測試代碼
numpy
庫中可使用運算符 *
或 multiply
函數計算。
A = np.array([[1],[2]])
B = np.array([[1,2,4],[1,4,5]])
C = np.array([[1,2,3],[4,5,6]])
X = A*B
array([[ 1, 2, 4],
[ 2, 8, 10]])
X == np.multiply(A,B)
array([[ True, True, True],
[ True, True, True]])
Y = B*C
array([[ 1, 4, 12],
[ 4, 20, 30]])
Y == np.multiply(B,C)
array([[ True, True, True],
[ True, True, True]])
需要點出的是:
當矩陣A和矩陣B的維度相同時,矩陣點乘即爲哈達瑪積(Hadamard Product),如下圖所示:
叉乘
基本概念
- 就是我們熟知的矩陣乘法。
- 中間相同留兩邊。
S1 示例
Python測試代碼
numpy
庫中可使用運算符 @
或 dot
函數計算。
A = np.array([[1,2],[3,4],[1,5]])
B = np.array([[1,2],[2,1]])
A@B
array([[ 5, 4],
[11, 10],
[11, 7]])
A@B == np.dot(A,B)
array([[ True, True],
[ True, True],
[ True, True]])