矩陣係數積(Scalar Multiplication)
矩陣的每一個元素乘上一個固定的數值,乘完之後矩陣大小不變
aX_{m,n}=\begin{bmatrix} a \, x_{0,0} & a \, x_{0,1} & \cdots & a \, x_{1,n-1} \\ a \, x_{1,0} & a \, x_{1,1} & \cdots & a \, x_{2,n-1} \\ \vdots & \vdots & \ddots & \vdots \\ a \, x_{m-1,1} & a \, x_{m-1,2} & \cdots & a \, x_{m-1,n-1} \end{bmatrix}
範例:
\begin{aligned} X=\begin{bmatrix} 1&2&3 \\ 4&5&6 \end{bmatrix} \\ 2X=\begin{bmatrix} 2&4&6 \\ 8&10&12 \end{bmatrix} \end{aligned}
實驗程式碼
Python
import numpy as np
X=np.array([[1,2,3],[4,5,6]])
print(X)
X=2*X
print(X)
程式輸出:
[[1 2 3]
[4 5 6]]
[[ 2 4 6]
[ 8 10 12]]
矩陣乘積(Matrix Product)
矩陣乘積為一個矩陣的所有列(橫向)向量(Row Vector)逐一與另一個矩陣的所有行(直向)向量(column vector)作向量內積所形成的矩陣。
\begin{aligned} & Z=XY \\\\ &\begin{bmatrix} z_{00} & z_{01} & \cdots & z_{0\,p-1}\\ z_{10} & z_{11} & \cdots & z_{1\,p-1}\\ \vdots & \vdots & \ddots & \vdots\\ z_{m-1\,0} & z_{m-1\,1} & \cdots & z_{m-1\,p-1} \end{bmatrix} =\\\\ & \begin{bmatrix} x_{00} & x_{01} & \cdots & x_{0\,n-1}\\ x_{10} & x_{11} & \cdots & x_{1\,n-1}\\ \vdots & \vdots & \ddots & \vdots\\ x_{m-1\,0} & x_{m-1\,1} & \cdots & x_{m-1\,n-1} \end{bmatrix} \times \begin{bmatrix} y_{00} & y_{01} & \cdots & y_{0\,p-1}\\ y_{10} & y_{11} & \cdots & y_{1\,p-1}\\ \vdots & \vdots & \ddots & \vdots\\ y_{n-1\,0} & y_{n-1\,1} & \cdots & y_{n-1\,p-1} \end{bmatrix} \end{aligned}
矩陣Z的每個元素為:
z_{m p}= x_{m0}\times y_{0p} + x_{m1} \times y_{1p} +\cdots+ x_{m\,n-1}\times y_{n-1\,p} = \sum_{k=0}^{n-1} x_{mk} \times y_{kp}
計算範例:
\begin{aligned} X=\begin{bmatrix} 1&2&3 \\ 4&5&6 \end{bmatrix} \\ Y=\begin{bmatrix} 5&6&7 \\ 8&9&10 \\ 11&12&13 \\ \end{bmatrix} \\ \end{aligned}
則矩陣乘法的結果,其矩陣第一個位置為X的第一個列向量與Y的第一個行向量的內積,也就是:
\begin{aligned} Z_{00}=(1\; 2\; ,3 ) \cdot ( 5\;,8\; ,11 )=1\times 5+2\times 8+3\times 11=5+16+33=54 \end{aligned}
類似的有:
\begin{aligned} Z_{01}=(1\; 2\; ,3 ) \cdot ( 6\;,9\; ,12 )=1\times 6+2\times 9+3\times 12=6+18+36=60 \\ Z_{02}=(1\; 2\; ,3 ) \cdot ( 7\;,10\; ,13 )=1\times 7+2\times 10+3\times 13=7+20+39=66 \\ Z_{10}=(4\;, 5\;, 6) \cdot ( 5\;,8\; ,11 )=4\times 5+5\times 8+6\times 11=20+40+66=126 \\ \end{aligned}
實驗程式碼
Python
import numpy as np
X=np.array([[1,2,3],[4,5,6]])
Y=np.array([[5,6,7],[8,9,10],[11,12,13]])
print(X)
print(Y)
Z=X.dot(Y)
print(Z)
輸出
[[1 2 3]
[4 5 6]]
[[ 5 6 7]
[ 8 9 10]
[11 12 13]]
[[ 54 60 66]
[126 141 156]]
矩陣哈達瑪積(Matrix Hadamard Product),又稱逐元素積(Element-wise product or Point-wise product)
兩矩陣的對應元素位置相乘的結果,也會是一個矩陣。
本文允許重製、散布、傳輸以及修改,但不得為商業目的之使用
使用時必須註明出處自:楊明翰 , 台灣人工智慧與資料科學研究室 https://aistudio.tw