I'm trying to write a code for the row Echelon form of \ matrix using Python without using any inbuilt functions. But I'm getting an error for last column.
A=[[1,2,3],[4,5,6],[7,8,9]]
j=0;k=0
print("Input matrix",A)
for i in range(3):
a=A[i][i]
while j < 3:
A[i][j]=A[i][j]/a
j=j+1
j=1
b=A[j][k]
while k < 3:
if k < 3 and j < 3:
A[j][k]=A[j-1][k]*b-A[j][k]
k=k+1
j=1
k=1
print("output matrix",A)
output:
Input matrix [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
output matrix [[1.0, 2.0, 3.0], [0.0, 1.0, 2.0], [7, 0.888888, 1.0]]
the last row first element and second element should be zero.
Work down your matrix increasing the pivot column and increasing the pivot row when you have a non-zero pivot element to do anything with.
For a general matrix you will have to do partial pivoting, or there is a danger that you will end up with 0 in the pivot position.
In the code below I have used numpy arrays. If you regard this as a "library" routine then you will have to switch back to using lists and use more explicit loops.
Complete set of operations:
Code:
Output: