python中包含矩阵运算的第三方库 | 您所在的位置:网站首页 › Python中矩阵运算的第三方库 › python中包含矩阵运算的第三方库 |
我需要使用python反转包含float的矩阵,但不使用第三方库(即numpy)。以这种方式求逆矩阵的计算量最小的方法是什么?在 我试图用余因子矩阵的转置乘以倒数行列式等于逆矩阵。不过,我确信递归方法花费的时间太长: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 如果格式有问题,请道歉。在 |
CopyRight 2018-2019 实验室设备网 版权所有 |