在 python 处将矩阵转换为阶梯形式

Converting matrix to echelon form at python

from sympy import *

matrix = []
print("Enter the entries of the 3x3 matrix:")

for i in range(0,3):
    a =[]
    for j in range(0,3):
         a.append(int(input()))
    matrix.append(a)

for i in range(0,3):
    for j in range(0,3):
        print(matrix[i][j], end = " ")
    print()
    
for i in range(0,3):
    for j in range(0,3):
        M[i][j]=list(matrix[i][j])

M_rref = M.rref()
print("The Row echelon form of matrix M and the pivot columns : {}".format(M_rref))

我在互相传递数组时出错。我只想将 3x3 矩阵转换为梯形。 TypeError: 'int' object is not iterable 有时 AttributeError: 'list' object has no attribute 'rref'

只需将您的 matrix 转换为 SymPy Matrix,如下所示:

from sympy import *

matrix = []
print("Enter the entries of the 3x3 matrix:")

for i in range(0,3):
    a =[]
    for j in range(0,3):
         a.append(int(input()))
    matrix.append(a)

for i in range(0,3):
    print(matrix[i])

M = Matrix(matrix)
M_rref = M.rref()
print("The Row echelon form of matrix M and the pivot columns : {}".format(M_rref))