我在python中有一个要转换的矩阵。但是,当将反向矩阵乘以原始矩阵的结果没有时,将生成单位矩阵。
M = np.matrix(cv)
invM = np.linalg.inv(M)
M @ invM
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9NSkd6NC5wbmcifQ==” alt =“显示矩阵乘法结果的图像”>
我不确定这可能是什么问题,因为这是一个相当简单的操作。有没有其他人有这个问题?还是有人知道如何解决这个问题?谢谢!
m = np.matrix([[2,3],[4,5]])
n = m.I
i = m@n
print(i)
out:
[[1. 0.]
[0. 1.]]
尝试这种方式。
类似,您的矩阵为ill-conditioned,这意味着该矩阵接近不可逆。这是一些具有不同条件编号和其逆质量的矩阵:
import numpy as np
np.random.seed(1)
n = 100
def test_inv(a):
print(f'Condition number: {np.linalg.cond(a):.3g}')
max_err = np.abs(a @ np.linalg.inv(a) - np.eye(n)).max()
print(f'a @ a_inv - eye: maximum error = {max_err:.3g}')
# identity matrix
test_inv(np.eye(n))
# random numbers
randmat = np.random.uniform(-1, 1, size=(n, n))
test_inv(randmat)
# random numbers, but one row is almost a linear combination of
# two other rows.
badmat = randmat.copy()
badmat[1, :] = badmat[0, :] + badmat[2, :] - 1e-9
test_inv(badmat)
输出:
Condition number: 1
a @ a_inv - eye: maximum error = 0
Condition number: 626
a @ a_inv - eye: maximum error = 2.84e-14
Condition number: 1.64e+10
a @ a_inv - eye: maximum error = 1.53e-06
双精度浮点的相对精度约为1e-16。对于条件数K,您的精度损失大约为系数K。