7.5. 矩阵运算

7.5.1. 乘法

逐元素积

对于维度相同的任意两个矩阵 \({\bm A} = (a_{ij})_{M\times N}\), \({\bm B} = (b_{ij})_{M\times N}\), 其逐元素积为

(7.6)\[{\bm A}\odot{\bm B} = {\bm C} = (c_{ij})_{M\times N} = (a_{ij}b_{ij})_{M\times N} \]

点积/内积

定义矩阵 \({\bm A} = (a_{ij})_{M\times N}\) 与矩阵 \({\bm B} = (b_{ij})_{N\times P}\) 的点积为

(7.5)\[{\bm A}{\bm B} = {\bm C} = (c_{ij})_{M\times P} = \left(\sum_{n=1}^N a_{in}b_{nj}\right)_{M\times P} \]

设有矩阵 \({\bm A} = (a_{ij})_{M\times N}\), \({\bm B} = (b_{ij})_{N\times P}\), \({\bm C} = (b_{ij})_{P\times Q}\), 则他们的点积为

(7.5)\[{\bm A}{\bm B}{\bm C}= {\bm D} = (d_{ij})_{M\times Q} = \left(\sum_{p=1}^P\left(\sum_{n=1}^N a_{in} b_{np}\right)c_{pj}\right)_{M\times Q} \]

Kronecker积

对于任意维度的两个矩阵 \({\bm A} = (a_{ij})_{M\times N}\), \({\bm B} = (b_{ij})_{P\times Q}\), 其Kronecker积为用右矩阵乘以左矩阵的结果.

(7.6)\[{\bm A}\times{\bm B} = {\bm C} = (c_{ij})_{MP\times NQ} = (a_{ij}{\bm B})_{MP\times NQ} \]

示例

代码 7.1 demo_MatrixOperation.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[10, 20, 30], [40, 50, 60]])

print("---element-wise: ")
c = a * b
print(c)

print("---matmul: ")
c = np.matmul(a, b.transpose())
print(c)

c = np.dot(a, b.transpose())

print("---dot: ")
print(c)

c = np.kron(a, b)

print("---kron: ")
print(c)

结果如下:

---element-wise:
[[ 10  40  90]
 [160 250 360]]
---matmul:
[[140 320]
 [320 770]]
---dot:
[[140 320]
 [320 770]]
---kron:
[[ 10  20  30  20  40  60  30  60  90]
 [ 40  50  60  80 100 120 120 150 180]
 [ 40  80 120  50 100 150  60 120 180]
 [160 200 240 200 250 300 240 300 360]]