[英]Inverting matrix in python slightly off
I'm trying to use this http://www.irma-international.org/viewtitle/41011/ algorithm to invert a nxn matrix.我正在尝试使用这个http://www.irma-international.org/viewtitle/41011/算法来反转 nxn 矩阵。
I ran the function on this matrix我在这个矩阵上运行了这个函数
[[1.0, -0.5],
[-0.4444444444444444, 1.0]]
and got the output并得到输出
[[ 1.36734694, 0.64285714]
[ 0.57142857, 1.28571429]]
the correct output is meant to be正确的输出应该是
[[ 1.28571429, 0.64285714]
[ 0.57142857, 1.28571429]]
My function:我的功能:
def inverse(m):
n = len(m)
P = -1
D = 1
mI = m
while True:
P += 1
if m[P][P] == 0:
raise Exception("Not Invertible")
else:
D = D * m[P][P]
for j in range(n):
if j != P:
mI[P][j] = m[P][j] / m[P][P]
for i in range(n):
if i != P:
mI[i][P] = -m[i][P] / m[P][P]
for i in range(n):
for j in range(n):
if i != P and j != P:
mI[i][j] = m[i][j] + (m[P][j] * mI[i][P])
mI[P][P] = 1 / m[P][P]
if P == n - 1: # All elements have been looped through
break
return mI
Where am I making my mistake?我在哪里犯了错误?
https://repl.it/repls/PowerfulOriginalOpensoundsystem https://repl.it/repls/PowerfulOriginalOpensoundsystem
Output输出
inverse: [[Decimal('1.285714285714285693893862813'), Decimal('0.6428571428571428469469314065')], [Decimal('0.5714285714285713877877256260'), Decimal('1.285714285714285693893862813')]] numpy: [[ 1.28571429 0.64285714] [ 0.57142857 1.28571429]]逆:[[十进制( '1.285714285714285693893862813'),十进制( '0.6428571428571428469469314065')],[十进制( '0.5714285714285713877877256260'),十进制( '1.285714285714285693893862813')]] numpy的:[[1.28571429 0.64285714] [0.57142857 1.28571429]]
from decimal import Decimal
import numpy as np
def inverse(m):
m = [[Decimal(n) for n in a] for a in m]
n = len(m)
P = -1
D = Decimal(1)
mI = [[Decimal(0) for n in a] for a in m]
while True:
P += 1
if m[P][P] == 0:
raise Exception("Not Invertible")
else:
D = D * m[P][P]
for j in range(n):
if j != P:
mI[P][j] = m[P][j] / m[P][P]
for i in range(n):
if i != P:
mI[i][P] = -m[i][P] / m[P][P]
for i in range(n):
for j in range(n):
if i != P and j != P:
mI[i][j] = m[i][j] + (m[P][j] * mI[i][P])
mI[P][P] = 1 / m[P][P]
m = [[Decimal(n) for n in a] for a in mI]
mI = [[Decimal(0) for n in a] for a in m]
if P == n - 1: # All elements have been looped through
break
return m
m = [[1.0, -0.5],
[-0.4444444444444444, 1.0]]
print(inverse(m))
print(np.linalg.inv(np.array(m)))
My thought process:我的思考过程:
At first, I thought you might have lurking floating point roundoff errors.起初,我认为您可能存在潜在的浮点舍入错误。 This turned out to not be true.事实证明这不是真的。 That's what the Decimal jazz is for.这就是十进制爵士乐的用途。
Your bug is here你的错误在这里
mI = m # this just creates a pointer that points to the SAME list as m
and here和这里
for i in range(n):
for j in range(n):
if i != P and j != P:
mI[i][j] = m[i][j] + (m[P][j] * mI[i][P])
mI[P][P] = 1 / m[P][P]
# you are not copying mI to m for the next iteration
# you are also not zeroing mI
if P == n - 1: # All elements have been looped through
break
return mI
In adherence to the algorithm, every iteration creates a NEW a' matrix, it does not continue to modify the same old a.根据算法,每次迭代都会创建一个新的 a' 矩阵,它不会继续修改相同的旧 a。 I inferred this to mean that in the loop invariant, a becomes a'.我推断这意味着在循环不变式中,a 变为 a'。 Works for your test case, turns out to be true.适用于您的测试用例,结果证明是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.