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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With