之前的笔记中曾提及过矩阵的逆的概念,但是并没有具体说明矩阵的逆的求法。
求解矩阵的逆
矩阵的逆运算
矩阵中 A B = B A = I AB=BA=I AB=BA=I,则称B是A的逆矩阵,记为 B = A − 1 B=A^{-1} B=A−1。只有方阵才有逆矩阵。
求解逆矩阵以下面的例子作为引入,
因为Gauss-Jordan消元法是针对系数矩阵进行操作,而结果矩阵是随着系数矩阵的变化而变化的,所以可以将增广矩阵进一步变化,
(
1
2
∣
1
0
3
4
∣
0
1
)
⟹
(
1
0
∣
x
11
x
12
0
1
∣
x
21
x
22
)
\begin{pmatrix}1&2&|&1&0\\3&4&|&0&1\end{pmatrix}\Longrightarrow\begin{pmatrix}1&0&|&x_{11}&x_{12}\\0&1&|&x_{21}&x_{22}\end{pmatrix}
(1324∣∣1001)⟹(1001∣∣x11x21x12x22)
即左侧是系数矩阵,右侧是与之同形的单位矩阵,经过Gauss-Jordan消元法将左侧的系数矩阵化为行最简形式,如果化为行最简形式后左侧是一个单位矩阵,此时对应着唯一解的情况。上面的过程可以使用符号语言表示为,
(
A
∣
I
)
⟹
(
I
∣
A
−
1
)
\begin{pmatrix}&A&|&I&\end{pmatrix}\Longrightarrow\begin{pmatrix}&I&|&A^{-1}&\end{pmatrix}
(A∣I)⟹(I∣A−1)
矩阵的逆不可能存在无穷解,即一个方阵不可能存在无数个逆矩阵,只有存在一个逆矩阵或不存在逆矩阵这两种情况。
原因说明:
1.因为线性系统存在无穷解的情况可以看作是增广矩阵的最后一行为全0时对应的情况,但是求解逆矩阵时,增广矩阵右方是单位矩阵,不可能出现最下方的行为全0的情况。2.当系数矩阵化为行最简形式后存在0行时,此时不存在逆矩阵。
上面的求解矩阵的逆的方式仅仅求取的是矩阵的右逆,与最开始矩阵的逆的定义相比,这看似是不严谨的。但是,另一个性质说明这种求取方式得到的结果是正确的,“如果一个方阵A有右逆B,则B也是A的左逆,即B是A的逆”。(以后给出证明)
实现矩阵的逆
求解矩阵的逆的过程本质上也是求解一个线性系统,只不过在一般的线性系统中,增广矩阵的右侧是一个列向量,而求逆时,增广矩阵的右侧是一个矩阵。所以依旧是在之前笔记的 LinearSystem类的基础上进行改造,
# -*- coding: utf-8 -*-
from .matrix import Matrix
from .vector import Vector
from ._global import EPSILON
class LinearSystem:
def __init__(self, A, b):
self._m = A.row_num()
self._n = A.col_num()
# 对构造方法进行如下修改
if isinstance(b, Vector):
self.Ab = [Vector(A.row_vector(i).underlying_list() + [b[i]]) for i in range(self._m)]
if isinstance(b, Matrix):
self.Ab = [Vector(A.row_vector(i).underlying_list() + b.row_vector(i).underlying_list() for i in range(self._m)]
self.pivots = []
def _forward(self):
......
def _backward(self):
......
# 对Gauss-Jordan消元法的返回值进行修改
def gauss_jordan_elimination(self):
self._forward()
self._backward()
# 检查系数矩阵为0时,结果矩阵不为0。此时是无解的情况
for i in range(len(self.pivots, self._m):
if not abs(self.Ab[i][-1]) < EPSILON:
return False
return True
def inv(A):
"""求解矩阵的逆"""
if A.row_num() != A.col_num():
return None
n = A.row_num()
ls = LinearSystem(A, Matrix.identity(n))
if not ls.gauss_jordan_elimination():
return None
invA = [[row[i] for i in range(n, 2*n)] for row in ls.Ab]
return Matrix(invA)
初等矩阵
矩阵的初等变换及初等矩阵
以该矩阵乘法进行说明,
(
1
0
0
0
1
0
0
0
1
)
⋅
(
a
b
c
d
e
f
g
h
i
)
=
(
a
b
c
d
e
f
g
h
i
)
\begin{pmatrix}1&0&0\\0&1&0\\0&0&1\end{pmatrix}\cdot \begin{pmatrix}a&b&c\\d&e&f\\g&h&i\end{pmatrix}= \begin{pmatrix}a&b&c\\d&e&f\\g&h&i\end{pmatrix}\\
⎝⎛100010001⎠⎞⋅⎝⎛adgbehcfi⎠⎞=⎝⎛adgbehcfi⎠⎞
- 矩阵的某一行乘以一个常数
( k 0 0 0 1 0 0 0 1 ) ⋅ ( a b c d e f g h i ) = ( k ⋅ a k ⋅ b k ⋅ c d e f g h i ) \begin{pmatrix}k&0&0\\0&1&0\\0&0&1\end{pmatrix}\cdot \begin{pmatrix}a&b&c\\d&e&f\\g&h&i\end{pmatrix}= \begin{pmatrix}k\cdot a&k\cdot b&k\cdot c\\d&e&f\\g&h&i\end{pmatrix} ⎝⎛k00010001⎠⎞⋅⎝⎛adgbehcfi⎠⎞=⎝⎛k⋅adgk⋅behk⋅cfi⎠⎞
可总结为,
( k 0 0 0 1 0 0 0 1 ) \begin{pmatrix}k&0&0\\0&1&0\\0&0&1\end{pmatrix} ⎝⎛k00010001⎠⎞单位矩阵的某一行乘以一个常数。 - 矩阵的一行加(减)另一行
( 1 0 0 0 1 0 1 0 1 ) ⋅ ( a b c d e f g h i ) = ( a b c d e f a + g b + h c + i ) \begin{pmatrix}1&0&0\\0&1&0\\1&0&1\end{pmatrix}\cdot \begin{pmatrix}a&b&c\\d&e&f\\g&h&i\end{pmatrix}= \begin{pmatrix}a&b&c\\d&e&f\\a+g&b+h&c+i\end{pmatrix}\\ ⎝⎛101010001⎠⎞⋅⎝⎛adgbehcfi⎠⎞=⎝⎛ada+gbeb+hcfc+i⎠⎞
可总结为,
( 1 0 0 0 1 − p 0 0 1 ) \begin{pmatrix}1&0&0\\0&1&-p\\0&0&1\end{pmatrix} ⎝⎛1000100−p1⎠⎞单位矩阵的一行加(减)另一行的若干倍。 - 交换矩阵的两行
( 1 0 0 0 0 1 0 1 0 ) ⋅ ( a b c d e f g h i ) = ( a b c g h i d e f ) \begin{pmatrix}1&0&0\\0&0&1\\0&1&0\end{pmatrix}\cdot \begin{pmatrix}a&b&c\\d&e&f\\g&h&i\end{pmatrix}= \begin{pmatrix}a&b&c\\g&h&i\\d&e&f\end{pmatrix}\\ ⎝⎛100001010⎠⎞⋅⎝⎛adgbehcfi⎠⎞=⎝⎛agdbhecif⎠⎞
可总结为,
( 1 0 0 0 0 1 0 1 0 ) \begin{pmatrix}1&0&0\\0&0&1\\0&1&0\end{pmatrix} ⎝⎛100001010⎠⎞交换单位矩阵的两行。
如上这3种操作统称为“矩阵的初等变换”,任何形式的初等变换都可以用相应形式的矩阵进行表示,而这些用于表示初等变换的矩阵称为“初等矩阵”,通常记为 E Ε E。
回顾之前的Gauss-Jordan消元法,把原矩阵化为行最简形式的过程实际上可以看作是寻找到一系列初等矩阵
E
Ε
E,使得
E
p
⋅
.
.
.
⋅
E
3
⋅
E
2
⋅
E
1
⋅
A
=
r
r
e
f
(
A
)
E_p\cdot ...\cdot E_3\cdot E_2\cdot E_1\cdot A=rref(A)
Ep⋅...⋅E3⋅E2⋅E1⋅A=rref(A)
初等矩阵和可逆性的关系
初等矩阵一定是可逆的。
因为初等变换是可逆的,所以初等矩阵也是可逆的。对于前一小节的初等矩阵,其对应的逆矩阵形式如下,
(
k
0
0
0
1
0
0
0
1
)
逆矩阵
→
(
1
k
0
0
0
1
0
0
0
1
)
\begin{pmatrix}k&0&0\\0&1&0\\0&0&1\end{pmatrix}\underrightarrow{\text{逆矩阵}}\begin{pmatrix}\frac{1}{k}&0&0\\0&1&0\\0&0&1\end{pmatrix}
⎝⎛k00010001⎠⎞逆矩阵⎝⎛k100010001⎠⎞
(
1
0
0
0
1
−
p
0
0
1
)
逆矩阵
→
(
1
0
0
0
1
p
0
0
1
)
\begin{pmatrix}1&0&0\\0&1&-p\\0&0&1\end{pmatrix}\underrightarrow{\text{逆矩阵}}\begin{pmatrix}1&0&0\\0&1&p\\0&0&1\end{pmatrix}
⎝⎛1000100−p1⎠⎞逆矩阵⎝⎛1000100p1⎠⎞
(
1
0
0
0
0
1
0
1
0
)
逆矩阵
→
(
1
0
0
0
0
1
0
1
0
)
\begin{pmatrix}1&0&0\\0&0&1\\0&1&0\end{pmatrix}\underrightarrow{\text{逆矩阵}}\begin{pmatrix}1&0&0\\0&0&1\\0&1&0\end{pmatrix}
⎝⎛100001010⎠⎞逆矩阵⎝⎛100001010⎠⎞
回顾Gauss-Jordan消元过程求取矩阵的逆,
E
p
⋅
.
.
.
⋅
E
3
⋅
E
2
⋅
E
1
⋅
A
=
r
r
e
f
(
A
)
=
I
E_p\cdot ...\cdot E_3\cdot E_2\cdot E_1\cdot A=rref(A)=I
Ep⋅...⋅E3⋅E2⋅E1⋅A=rref(A)=I
如果矩阵
A
A
A可逆,则存在一系列初等矩阵
E
E
E,使得
E
p
⋅
.
.
.
⋅
E
3
⋅
E
2
⋅
E
1
⋅
A
⋅
A
−
1
=
I
⋅
A
−
1
E_p\cdot ...\cdot E_3\cdot E_2\cdot E_1\cdot A\cdot A^{-1}=I\cdot A^{-1}
Ep⋅...⋅E3⋅E2⋅E1⋅A⋅A−1=I⋅A−1
E
p
⋅
.
.
.
⋅
E
3
⋅
E
2
⋅
E
1
⋅
I
=
A
−
1
E_p\cdot ...\cdot E_3\cdot E_2\cdot E_1\cdot I=A^{-1}
Ep⋅...⋅E3⋅E2⋅E1⋅I=A−1
即,
(
A
∣
I
)
⟹
(
I
∣
A
−
1
)
\begin{pmatrix}&A&|&I&\end{pmatrix}\Longrightarrow\begin{pmatrix}&I&|&A^{-1}&\end{pmatrix}
(A∣I)⟹(I∣A−1)
矩阵可逆的重要性
加速线性系统计算
对于线性系统
A
x
=
b
Ax=b
Ax=b,有如下推导,
A
−
1
⋅
A
⋅
x
=
A
−
1
⋅
b
x
=
A
−
1
⋅
b
A^{-1}\cdot A\cdot x=A^{-1}\cdot b\\x=A^{-1}\cdot b
A−1⋅A⋅x=A−1⋅bx=A−1⋅b
当线性系统的系数矩阵A不变,b会变化的情况下,使用矩阵的逆进行计算会大大加快计算速度。
矩阵的逆相关重要命题
矩阵的逆与很多重要的命题相关,这一系列的命题都是等价的。
对于上面诸多问题的证明,逐一证明时间太长,最好的方式是将四个命题串成串来证明,如
顺时针方向证明各个命题,
- 证明:对于方阵
A
A
A,若
A
A
A可逆(是非奇异矩阵),则当线性系统
A
x
=
0
Ax=0
Ax=0时,仅有唯一解
x
=
0
x=0
x=0
A ⋅ x = 0 A\cdot x=0 A⋅x=0
两端同乘 A − 1 A^{-1} A−1,得到
A − 1 ⋅ A ⋅ x = A − 1 ⋅ 0 I ⋅ x = 0 x = 0 A^{-1}\cdot A\cdot x=A^{-1}\cdot0\\I\cdot x=0\\x=0 A−1⋅A⋅x=A−1⋅0I⋅x=0x=0 - 证明:若线性系统
A
x
=
0
Ax=0
Ax=0只有唯一的0解,则
A
A
A的行最简形式
r
r
e
f
(
A
)
=
I
rref(A)=I
rref(A)=I
因为 A A A是n*n的矩阵,且 A x = 0 Ax=0 Ax=0有唯一解,则有 n n n个未知数,且 r e f f ( A ) reff(A) reff(A)存在 n n n个非0行
根据行最简形式的定义: r r e f ( A ) = I rref(A)=I rref(A)=I - 证明:若矩阵
A
A
A的行最简形式
r
r
e
f
(
A
)
=
I
rref(A)=I
rref(A)=I,则
A
A
A可以表示成一系列初等矩阵的乘积
依据上面的表述可写出,
E p ⋅ . . . ⋅ E 3 ⋅ E 2 ⋅ E 1 ⋅ A = r r e f ( A ) = I E_p\cdot ...\cdot E_3\cdot E_2\cdot E_1\cdot A=rref(A)=I Ep⋅...⋅E3⋅E2⋅E1⋅A=rref(A)=I
等式两端同乘一连串逆矩阵,
E 1 − 1 ⋅ E 2 − 1 ⋅ E 3 − 1 ⋅ . . . ⋅ E p − 1 ⋅ E p ⋅ . . . ⋅ E 3 ⋅ E 2 ⋅ E 1 ⋅ A = E 1 − 1 ⋅ E 2 − 1 ⋅ E 3 − 1 ⋅ . . . ⋅ E p − 1 ⋅ I E_1^{-1}\cdot E_2^{-1}\cdot E_3^{-1}\cdot ...\cdot E_p^{-1}\cdot E_p\cdot ...\cdot E_3\cdot E_2\cdot E_1\cdot A=E_1^{-1}\cdot E_2^{-1}\cdot E_3^{-1}\cdot ...\cdot E_p^{-1}\cdot I E1−1⋅E2−1⋅E3−1⋅...⋅Ep−1⋅Ep⋅...⋅E3⋅E2⋅E1⋅A=E1−1⋅E2−1⋅E3−1⋅...⋅Ep−1⋅I
可得,
A = E 1 − 1 ⋅ E 2 − 1 ⋅ E 3 − 1 ⋅ . . . ⋅ E p − 1 A=E_1^{-1}\cdot E_2^{-1}\cdot E_3^{-1}\cdot ...\cdot E_p^{-1} A=E1−1⋅E2−1⋅E3−1⋅...⋅Ep−1 - 证明:若矩阵
A
A
A可以表示成一系列初等矩阵的乘积,则该矩阵可逆(是非奇异矩阵)
A = E 1 ⋅ E 2 ⋅ E 3 ⋅ . . . ⋅ E p A=E_1\cdot E_2\cdot E_3\cdot ...\cdot E_p A=E1⋅E2⋅E3⋅...⋅Ep
假设存在一个矩阵 B B B,满足
B = E p − 1 ⋅ . . . ⋅ E 3 − 1 ⋅ E 2 − 1 ⋅ E 1 − 1 B=E_p^{-1}\cdot ...\cdot E_3^{-1}\cdot E_2^{-1}\cdot E_1^{-1} B=Ep−1⋅...⋅E3−1⋅E2−1⋅E1−1
有矩阵的逆的运算性质, B B B可以表示为,
B = ( E 1 ⋅ E 2 ⋅ E 3 ⋅ . . . ⋅ E p ) − 1 B=(E_1\cdot E_2\cdot E_3\cdot ...\cdot E_p)^{-1} B=(E1⋅E2⋅E3⋅...⋅Ep)−1
计算可得,
A ⋅ B = I B ⋅ A = I A\cdot B=I\\B\cdot A=I A⋅B=IB⋅A=I
故, A A A是可逆矩阵,其逆矩阵为 B B B。
由此整个环证明完成,随便抽取出两个命题,都可以间接地通过环得出这两个命题是等价的。同时这四个命题的否命题也是等价的。
所谓两个事物“等价”可以很简单的理解为,说其中一件就等同于说另一件。在上面的四个命题中,提及矩阵可逆,则等同于又说了其他三个命题。