Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不使用第三方库的情况下反转矩阵 我需要使用python反转包含浮点的矩阵,但不使用第三方库(即numpy)。以这种方式求逆矩阵的计算量最小的方法是什么?_Python_Matrix_Linear Algebra - Fatal编程技术网

在不使用第三方库的情况下反转矩阵 我需要使用python反转包含浮点的矩阵,但不使用第三方库(即numpy)。以这种方式求逆矩阵的计算量最小的方法是什么?

在不使用第三方库的情况下反转矩阵 我需要使用python反转包含浮点的矩阵,但不使用第三方库(即numpy)。以这种方式求逆矩阵的计算量最小的方法是什么?,python,matrix,linear-algebra,Python,Matrix,Linear Algebra,我试图利用这样一个事实:辅因子矩阵乘以倒数行列式的转置等于逆矩阵。然而,我确信递归方法花费的时间太长: def getMatrixMinor(m,i,j): return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])] def getMatrixDeternminant(m): if len(m) == 2: return m[0][0]*m[1][1]-m[0][1]*m[1][0] determinant = 0

我试图利用这样一个事实:辅因子矩阵乘以倒数行列式的转置等于逆矩阵。然而,我确信递归方法花费的时间太长:

def getMatrixMinor(m,i,j):
    return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]

def getMatrixDeternminant(m):
if len(m) == 2:
    return m[0][0]*m[1][1]-m[0][1]*m[1][0]
    determinant = 0
    for c in range(len(m)):
        determinant += ((-1)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m,0,c))
return determinant

def getMatrixInverse(m):
determinant = getMatrixDeternminant(m)
if len(m) == 2:
    return [[m[1][1]/determinant, -1*m[0][1]/determinant],
            [-1*m[1][0]/determinant, m[0][0]/determinant]]
cofactors = []
for r in range(len(m)):
    cofactorRow = []
    for c in range(len(m)):
        minor = getMatrixMinor(m,r,c)
        cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor))
    cofactors.append(cofactorRow)
cofactors = transposeMatrix(cofactors)
for r in range(len(cofactors)):
    for c in range(len(cofactors)):
        cofactors[r][c] = cofactors[r][c]/determinant
return cofactors

如果格式很奇怪,我深表歉意。

用伴随矩阵求逆基本上只有理论值


计算效率高的方法从不涉及计算行列式等。看一看,这需要高中数学才能理解,或多或少是这种“努力”的标准起点。

展示一些研究成果。你的矩阵是什么样子的?它的尺寸是多少?到目前为止你试过什么?