Row Echelon form of matrix using python coding without using any inbuilt functions

791 Views Asked by At

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.

1

There are 1 best solutions below

0
lastchance On

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:

Start:
1    2     3
4    5     6
7    8     9

COLUMN = 0
PIVOT ROW = 0
Swap rows:                   Normalise pivot row:          Elimination operations:
7    8     9                 1    8/7   9/7                1    8/7   9/7
4    5     6                 4    5     6                  0    3/7   6/7
1    2     3                 1    2     3                  0    6/7   12/7

PIVOT ROW -> 1


COLUMN = 1
Swap rows:                   Normalise pivot row:          Elimination operations:
1    8/7   9/7               1    8/7   9/7                1    8/7   9/7
0    6/7   12/7              0    1     2                  0    1     2
0    3/7   6/7               0    3/7   6/7                0    0     0

PIVOT ROW -> 2


COLUMN = 2
No operations possible


Final matrix in row-echelon form
1    8/7   9/7
0    1     2
0    0     0

Code:

import numpy as np
NEARZERO = 1.0e-10

A = np.array( [ [1,2,3], [4,5,6], [7,8,9] ], dtype="float" )

print( "Input matrix:\n", A )

N = len( A )
det = 1.0

pivotRow = 0
for column in range( N ):

    # Partial pivot: swap rows if necessary to put the largest absolute element in this column into the pivot row
    bestRow = pivotRow
    for row in range( pivotRow + 1, N ):                                       # find largest in this column
        if ( abs( A[row,column] ) > abs( A[bestRow,column] ) ): bestRow = row
    if bestRow != pivotRow: 
        A[ [ pivotRow, bestRow ], column: ] = A[ [ bestRow, pivotRow ], column: ]                                              # swap rows
        det = -det

    # Now subtract the appropriate multiples of the pivot row from lower rows (like Gaussian elimination)
    if abs( A[pivotRow,column] ) > NEARZERO:                                   # provided we have a non-zero pivot
        det *= A[pivotRow,column]
        A[pivotRow,column:] = A[pivotRow,column:] / A[pivotRow,column]         # make the pivot entry 1
        for row in range( pivotRow + 1, N ):
            A[row,column:] -= A[row,column] * A[pivotRow,column:]
            A[row,column] = 0.0                                                # avoid round-off errors
        pivotRow += 1
    else:
        A[pivotRow,column] = 0.0                                               # avoid round-off errors
        det = 0.0

print( "\nOutput matrix:\n", A )
print( "\nRank = ", pivotRow )
print( "Determinant = ", det )
if pivotRow < N: print( "Matrix is singular" )

Output:

Input matrix:
 [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]

Output matrix:
 [[1.         1.14285714 1.28571429]
 [0.         1.         2.        ]
 [0.         0.         0.        ]]

Rank =  2
Determinant =  0.0
Matrix is singular