Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

row echelon in python

this code is for 3*3 matrix. i need it to be applicable on ant m*n matrix.

import numpy as np
b=np.arange(1,10).reshape(3,3)

this checks the shape of matrix but it only applicable for 3*3 matrix

if b.shape[0]==b.shape[1]:
    for i in range(b.shape[0]): 
        for j in range(b.shape[1]):
            if i==1:
                b[i]=b[i-i][j]*b[i]-[b[i][j]*b[i-i]]
                i=i+1
                b[i]=b[i-i][j]*b[i]-[b[i][j]*b[i-i]]
                j=j+1
                b[i]=b[i]-[b[i-1]*(b[i][j]/b[i-1][j])]
print(b)
like image 520
abhishek gujjar Avatar asked Sep 03 '25 02:09

abhishek gujjar


1 Answers

There's actually a built-in library in python called sympy. The function Matrix().rref() can be used to obtain the reduced row echelon form of a matrix. The return value of this function includes two things: 1) the reduced row echelon form of the given matrix and 2) the indices of the columns in the matrix which contain the pivots (note that columns are 0-indexed).

Here's an example of how to use this function:

import sympy
sympy.Matrix([[1,2,3],[2,3,4]]).rref()

(Matrix([
[1, 0, -1],
[0, 1,  2]]), (0, 1))

You can find the implementation of Matrix().rref() here.

like image 80
anusha Avatar answered Sep 05 '25 12:09

anusha