Good day estimated, implement the gauss-jordan method in python in the following way and it is working:
#La idea de este metodo es que el usuario ingrese una matriz MxM y un vector de tamano M
import numpy
def gaussJordan(m):
#creamos una matriz, un vector lleno de ceros, y el vector solucion con la misma cantidad de zeros
matrix = numpy.zeros((m,m))
vector = numpy.zeros((m))
x = numpy.zeros((m))
#se llena la matriz y el vector
for r in range(0, m):
for c in range(0, m):
matrix[(r), (c)] =(input("Elemento a[" + str(r+1)+"," +str(c+1)+"] "))
vector[(r)]=(input('b[]'+str(r+1)+']: '))
#asi funciona el metodo
for k in range(0, m):
for r in range(k+1, m):
factor=(matrix[r,k]/matrix[k,k])
vector[r]=vector[r]-(factor*vector[k])
for c in range(0,m):
matrix[r,c]=matrix[r,c]-(factor*matrix[k,c])
x[m-1]=vector[m-1]/matrix[m-1, m-1]
for r in range(m-2, -1, -1):
suma = 0
for c in range(0,m):
suma=suma+matrix[r,c]*x[c]
x[r]=(vector[r]-suma)/matrix[r, r]
return x
m = int(input('Valor de m:'))
print(gaussJordan(m))
The problem is that a colleague asked me if it would be possible that instead of entering m in the method and then building the matrix, I could enter the matrix and the vector already made, and it seemed like a simple change but I came across a couple of errors that I do not understand the truth, first when entering the matrix [[2,6,1], [1,2, -1], [5,7, -4]] I returned an error in which apparently did not recognize it as a matrix so use numpy.matrix and the same with the vector just in case numpy.array. After this I had no problems in executing the method but for some reason I returned a different result than it should be ... when I execute it by filling the matrix within the method, if it returns the result it should be, it should return [10, - 3, 5] but it returns me [21.5, -8, 12] if someone knows how to tell me why I really appreciate it, I do not think it's an algorithm problem, because it would throw an incorrect result in which if it asks to fill the matrix internally, I think what is the problem of how the matrix is reading the program, THE DATA I USE FOR THE TEST ARE MATRIX = [[2,6,1], [1,2, -1], [5,7, -4]] VECTOR = [7, -1,9] here is the code:
#La idea de este metodo es que el usuario ingrese una matriz MxM y un vector de tamano M
import numpy
def gaussJordan(matriz, vector):
matrix = numpy.matrix(matriz)
vector = numpy.array(vector)
m = len(vector)
x = numpy.zeros((m))
#se llena la matriz y el vector
## for r in range(0, m):
## for c in range(0, m):
## matrix[(r), (c)] =(input("Elemento a[" + str(r+1)+"," +str(c+1)+"] "))
## vector[(r)]=(input('b[]'+str(r+1)+']: '))
#asi funciona el metodo
for k in range(0, m):
for r in range(k+1, m):
factor=(matrix[r,k]/matrix[k,k])
vector[r]=vector[r]-(factor*vector[k])
for c in range(0,m):
matrix[r,c]=matrix[r,c]-(factor*matrix[k,c])
x[m-1]=vector[m-1]/matrix[m-1, m-1]
#print (x[m-1])
for r in range(m-2, -1, -1):
suma = 0
for c in range(0,m):
suma=suma+matrix[r,c]*x[c]
x[r]=(vector[r]-suma)/matrix[r, r]
return x
print(gaussJordan([[2,6,1],[1,2,-1],[5,7,-4]],[7,-1,9]))