高等数学问题的数值解(5)--线性代数问题

矩阵的运算

例:已知$A = \begin{bmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \end{bmatrix}, B = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}$
(1)求$\mid A \mid ,R(A),A^{T},(A+10E)^{-1},A^{2},AB$
(2)构造分块矩阵$\begin{bmatrix} A & B \end{bmatrix}, \begin{bmatrix} A \\ B \end{bmatrix}$
(3)构造矩阵A1(提出A的左上角子块得到的矩阵)
(4)构造矩阵A2(删除A的第四行得到的矩阵)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np
import numpy.linalg as LA

A = np.arange(1,17).reshape(4,4)
B = np.eye(4)

print('A的行列式为', LA.det(A))
print('A的秩为', LA.matrix_rank(A))
print('A的转置矩阵为', A.T)
print('所求的逆矩阵为', LA.inv(A+10*B))
print('A的平方为', A.dot(A))
print('A和B的乘积为', A.dot(B))

print('横连矩阵为:', np.c_[A,B])
print('纵连矩阵为:', np.r_[A,B])

print('A1为:', A[0:2,0:2])

A2 = A.copy()
A2 = np.delete(A2,3,axis=0)
print('A2为:', A2)

齐次线性方程组的数值解

例:求下列齐次线性方程组的基础解系
$
\begin{cases}
x_{1}-5x_{2}+2x_{3}-3x_{4} = 0\\
5x_{1}+3x_{2}+6x_{3}-x_{4} = 0\\
2x_{1}+4x_{2}+2x_{3}+x_{4} = 0
\end{cases}
$

1
2
3
4
5
6
import numpy as np
from scipy.linalg import null_space

A = np.array([[1,-5,2,-3],[5,3,6,-1],[2,4,2,1]])

print('A的零空间(基础解系)为', null_space(A))

非齐次线性方程组的数值解

例:求下列非齐次线性方程组的数值解
$
\begin{cases}
2x_{1}+x_{2}-5x_{3}+x_{4} = 8\\
x_{1}-3x_{2}-6x_{4} = 6\\
2x_{2}-x_{3}+2x_{4} = -2\\
x_{1}+4x_{2}-7x_{3}+6x_{4} = 2
\end{cases}
$

1
2
3
4
5
6
7
8
9
10
11
import numpy as np
import numpy.linalg as LA

A = np.array([[2,1,-5,1],[1,-3,0,-6],[0,2,-1,2],[1,4,-7,6]])
b = np.array([8,6,-2,2])
b = b.reshape(4,1)

print('系数矩阵A的秩为', LA.matrix_rank(A))
print('线性方程组的唯一解为', LA.inv(A).dot(b)) # 使用逆矩阵
print('线性方程组的唯一解为', LA.pinv(A).dot(b)) # 使用伪造
print('线性方程组的唯一解为', LA.solve(A,b)) # 使用solve求解

例:下列非齐次线性方程组有无穷多解,求它的最小范数解
$
\begin{cases}
x_{1}+x_{2}-3x_{3}-x_{4} = 1\\
3x_{1}-x_{2}-3x_{3}+4x_{4} = 4\\
x_{1}+5x_{2}-9x_{3}-8x_{4} = 0
\end{cases}
$

1
2
3
4
5
6
7
8
9
10
import numpy as np
from numpy.linalg import pinv

A = np.array([[1,1,-3,-1],[3,-1,-3,4],[1,5,-9,-8]])
b = np.array([1,4,0])
b = b.reshape(3,1)

x = pinv(A).dot(b)

print('最小范数解为', x)

例:求下列非齐次矛盾线性方程组的最小二乘解
$
\begin{cases}
x_{1}+x_{2} = 1\\
2x_{1}+2x_{2} = 3\\
x_{1}+2x_{2} = 2
\end{cases}
$

1
2
3
4
5
6
7
8
9
10
import numpy as np
from numpy.linalg import pinv

A = np.array([[1,1],[2,2],[1,2]])
b = np.array([1,3,2])
b = b.reshape(3,1)

x = pinv(A).dot(b)

print('最小二乘解为', x)

特征值和特征向量

例:求下列矩阵的特征值和特征向量
$A = \begin{bmatrix} 0 & -2 & 2 \\ -2 & -3 & 4 \\ 2 & 4 & -3 \end{bmatrix}$

1
2
3
4
5
6
7
8
import numpy as np
from numpy.linalg import eig

A = np.array([[0,-2,2],[-2,-3,4],[2,4,-3]])
values, vectors = eig(A)

print('A的特征值为:', values)
print('A的特征向量为:', vectors)

求得的特征值为$\lambda_{1} = 1,\lambda_{2} = -8,\lambda_{3} = 1$
对应的特征向量分别为
$\xi_{1} = [0.94280904 -0.33333333 -0.2981424 ]^{T}$
$\xi_{2} = [-0.23570226 -0.66666667 0.74535599]^{T}$
$\xi_{3} = [0.23570226 0.66666667 0.59628479]^{T}$

例:对于矩阵
$A = \begin{bmatrix} 0 & -2 & 2 \\ -2 & -3 & 4 \\ 2 & 4 & -3 \end{bmatrix} $,$ P = \begin{bmatrix} 0.9428 & -0.3333 & -0.4254 \\ -0.2357 & -0.6667 & 0.7454 \\ 0.2357 & 0.6667 & 0.5247 \end{bmatrix} $,$\Lambda = \begin{bmatrix} 1 & 0 & 0 \\ 0 & -8 & 0 \\ 0 & 0 & 1 \end{bmatrix}$
验证$P^{-1}AP = \Lambda$

1
2
3
4
5
6
7
8
9
import numpy as np
from numpy.linalg import eig,inv

A = np.array([[0,-2,2],[-2,-3,4],[2,4,-3]])
values, vectors = eig(A)

check = np.dot(inv(vectors),A).dot(vectors)

print('check=\n', check)
0%