将python中的矩阵倒置略微关闭



我正在尝试使用此http://www.irma-international.org/viewtitle/41011/algorithm将NXN矩阵倒置。

我在此矩阵上运行了函数

[[1.0, -0.5],
 [-0.4444444444444444, 1.0]]

并获得了输出

[[ 1.36734694,  0.64285714]
 [ 0.57142857,  1.28571429]]

正确的输出将为

[[ 1.28571429,  0.64285714]
 [ 0.57142857,  1.28571429]]

我的功能:

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

我在哪里犯错?

输出

倒数:[[DECIMAL('1.285714285714285693893862813'(,(, 十进制('0.6428571428571428469469314065'],],],],] [DECIMAL('0.5714285714285713877777256260'(,(, 小数('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)))

我的思考过程:

起初,我认为您可能会潜伏着浮点圆形错误。事实证明这不是真的。这就是小数爵士乐的目的。

您的错误在这里

mI = m # this just creates a pointer that points to the SAME list as m

和这里

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

为了遵守算法,每个迭代都会创建一个新的A'矩阵,它不会继续修改相同的旧a。我推断这是指在循环中不变,a变成a'。为您的测试案例工作,事实证明是正确的。

相关内容

  • 没有找到相关文章

最新更新