矩阵分析与应用——求矩阵的秩,并判断方程是否可解
{ 0.835 x + 0.667 y = 0.168 0.333 x + 0.266 y = 0.067 \begin{equation} % equation带*之后的意思不整体标号 \begin{cases} 0.835x +0.667y=0.168\\ 0.333x+0.266y=0.067 \end{cases} \end{equation} {
0.835x+0.667y=0.1680.333x+0.266y=0.067
记矩阵 A 和 (A |b) 为此线性方程组的系数矩阵和增广矩阵。分别使用 5 个 和 6 个有效数字计算矩阵 A 和 (A |b) 的 Rank,并判断方程组是否可解。
import numpy as np
def Rank_CoefficientMatrix(Mat, num):
if (Mat.shape[0] != Mat.shape[1]):
raise ValueError("size of matrix doesn't match!")
A = Mat.copy()
arg = '.' + str(num) + 'g'
l_ik = float(format(A[1, 0] / A[0, 0], arg))
# print(l_ik)
a1 = float(format(A[0, 0], arg))
b1 = float(format(A[0, 1], arg))
# print('b1:',b1)
a2 = float(format(A[1, 0], arg))
b2 = float(format(A[1, 1], arg))
b2 = float(format(b2 - float(format(l_ik * b1, arg)), arg))
if (b2 == 0):
return 1
else:
return 2
def Rank_AugmentedMatrix(Mat, Bais, num):
if (Mat.shape[0] != Mat.shape[1]) or (Mat.shape[0] != Bais.shape[0]):
raise ValueError("size of matrix doesn't match!")
A = Mat.copy()
B = Bais.copy()
arg = '.' + str(num) + 'g'
l_ik = float(format(A[1, 0] / A[0, 0], arg))
# print(l_ik)
a1 = float(format(A[0, 0], arg))
b1 = float(format(A[0, 1], arg))
a2 = float(format(A[1, 0], arg))
b2 = float(format(A[1, 1], arg))
c1 = float(format(B[0, 0], arg))
c2 = float(format(B[1, 0], arg))
b2 = float(format(b2 - float(format(l_ik * b1, arg)), arg))
c2 = float(format(c2 - float(format(l_ik * c1, arg)), arg))
if(b2==0 and c2==0):
return 1
else:
return 2
def test(x, y, num):
print('系数矩阵的秩:', Rank_CoefficientMatrix(x, num))
print('增广矩阵的秩:', Rank_AugmentedMatrix(x, y, num))
if (Rank_CoefficientMatrix(x, num) == Rank_AugmentedMatrix(x, y, num) and Rank_AugmentedMatrix(x, y, num) == x.shape[1]):
print('方程组有唯一解')
if (Rank_CoefficientMatrix(x, num) == Rank_AugmentedMatrix(x, y, num) and Rank_AugmentedMatrix(x, y, num) < x.shape[1]):
print('方程组有多个解')
if (Rank_CoefficientMatrix(x, num) < Rank_AugmentedMatrix(x, y, num)):
print('方程组无解')
if __name__ == '__main__':
x = np.mat([[0.835, 0.667],
[0.333, 0.266]])
y = np.mat([[0.168],
[0.067]])
print(test(x,y,5))
print('---------')
print(test(x,y,6))