定义
矩阵的乘法必须要求左边矩阵的列和右边矩阵的行相等。Kronecker积则没有这个限制,简单地说Kronecker积是把左边矩阵的每一项都乘以右边的矩阵,最终结果是一个放大了的矩阵。Kronecker积的定义,其实更像群和群的乘法(不是群元素的成分),也有点像集合的笛卡尔积。定义如下:
假如A是
m
×
n
m\times n
m×n矩阵,B是
p
×
q
p\times q
p×q矩阵,那么Kronecker积定义为:
A
⊗
B
=
(
A
11
B
⋯
A
1
n
B
⋮
⋱
⋮
A
m
1
B
⋯
A
m
n
B
)
A\otimes B=\begin{pmatrix}A_{11}B & \cdots & A_{1n}B\\ \vdots & \ddots & \vdots \\ A_{m1}B & \cdots & A_{mn}B \end{pmatrix}
A⊗B=
A11B⋮Am1B⋯⋱⋯A1nB⋮AmnB
举例
A = ( 1 3 2 − 1 − 2 1 ) B = ( 4 − 3 6 − 2 − 4 5 ) A ⊗ B = ( 4 − 3 12 − 9 8 − 6 6 − 2 18 − 6 12 − 4 − 4 5 − 12 15 − 8 10 − 4 3 − 8 6 4 − 3 − 5 2 − 12 4 6 − 2 4 − 5 8 − 10 − 4 5 ) A=\begin{pmatrix} 1 & 3 & 2\\ -1 & -2 & 1\end{pmatrix}\\ B=\begin{pmatrix} 4 & -3\\ 6 & -2 \\ -4 & 5\end{pmatrix}\\ A\otimes B=\begin{pmatrix} 4 & -3 & 12 & -9 & 8 & -6 \\ 6 & -2 & 18 & -6 & 12 & -4\\ -4 & 5 & -12 & 15 & -8 & 10\\ -4 & 3 & -8 & 6 & 4 & -3\\ -5 & 2 & -12 & 4 & 6 & -2\\ 4 & -5 & 8 & -10 & -4 & 5\\ \end{pmatrix} A=(1−13−221)B= 46−4−3−25 A⊗B= 46−4−4−54−3−2532−51218−12−8−128−9−61564−10812−846−4−6−410−3−25
Python程序
我的python程序是反过来的,也就是内层数组是列向量,所以这个程序会有点难以理解:
def kronecker(self, other):
# 因为采用列向量的形式,所以第一个数组长度才是行数
# m代表行
m = len(self.__lines[0])
# n代表列
n = len(self.__lines)
p = len(other.__lines[0])
q = len(other.__lines)
result = [[0] * m * p for _ in range(0, n * q)]
# 总之对左边用除法,对右边用取余
# i 代表矩阵的列
for i in range(0, n * q):
# j 代表矩阵的行
for j in range(0, m * p):
x = self.__lines[i // q][j // p]
y = other.__lines[i % m][j % n]
result[i][j] = x * y
return Matrix(result)
测试代码:
# _*_ coding:utf-8 _*_
import unittest
from com.youngthing.mathalgorithm.matrix import Matrix
class MyTestCase(unittest.TestCase):
def test(self):
a = Matrix([[1, 3, 2], [-1, -2, 1]])
b = Matrix([[4, -3], [6, -2], [-4, 5]])
print("A=", a.to_latex(), "B= ", b.to_latex(), "A \\otimes B = ", a.kronecker(b).to_latex())
if __name__ == '__main__':
if __name__ == '__main__':
unittest.main()
测试结果:
A
=
(
1
−
1
3
−
2
2
1
)
B
=
(
4
6
−
4
−
3
−
2
5
)
A
⊗
B
=
(
4
6
−
4
−
4
−
6
4
−
3
−
2
5
3
2
−
5
12
18
−
12
−
8
−
12
8
−
9
−
6
15
6
4
−
10
8
12
−
8
4
6
−
4
−
6
−
4
10
−
3
−
2
5
)
A= \begin{pmatrix}1 & -1\\ 3 & -2\\ 2 & 1\\ \end{pmatrix}\\ B= \begin{pmatrix}4 & 6 & -4\\ -3 & -2 & 5\\ \end{pmatrix}\\ A \otimes B = \begin{pmatrix}4 & 6 & -4 & -4 & -6 & 4\\ -3 & -2 & 5 & 3 & 2 & -5\\ 12 & 18 & -12 & -8 & -12 & 8\\ -9 & -6 & 15 & 6 & 4 & -10\\ 8 & 12 & -8 & 4 & 6 & -4\\ -6 & -4 & 10 & -3 & -2 & 5\\ \end{pmatrix}\\
A=
132−1−21
B=(4−36−2−45)A⊗B=
4−312−98−66−218−612−4−45−1215−810−43−864−3−62−1246−24−58−10−45
完整代码在GIT里.