<a href="https://colab.research.google.com/github/LuisIMT/ComputacionIII-2021-1/blob/master/LIMT_Comp_II_Sistemas_de_ecuaciones_lineales.ipynb" target="_parent"></a>

# Computación II - Resolución de sistemas de ecuaciones lineales
+ Autor: Luis Ignacio Murillo Torres
+ CoAutor: Ulises Olivares 
+ Fecha01: 17/02/2021
+ FechaMod: 22/02/2012



```
import numpy as np
import sympy 

class GaussJordan:
  #Conductor 
    def intercambiarFilas(self, fila1, fila2,M):
        for i in range(len(M[0])):
            tmp = M[fila2][i]
            M[fila2][i] = M[fila1][i]
            M[fila1][i] = tmp
        return M

    def multiplicarFila(self, k, fila, colInicial, M):
        for i in range (colInicial, len(M[0])):
            M[fila][i] = k * M[fila][i]
        return M

    def restarFila(self,fila1,fila2,colInicial,M):
        for i in np.arange(colInicial, len(M[0])):
        #for i in np.arange(0, len(M[0])):
            M[fila1][i] = M[fila2][i]-M[fila1][i]
        return M

    def buscarPivote(self, filas, col, M):
        indiceFila = -1
        maxNum = np.inf *-1
        for i in range(col+1, filas):
            if(M[i][col] > maxNum and M[i][col] != 0):
                indiceFila = i
                maxNum = M[i][col]
        return indiceFila    
    
    def eliminicacionGaussiana(self, f, c, M):
        indicePiv = -1

        for i in range(f):
            pivote = M[i][i]
            if pivote == 0:
                indicePiv = self.buscarPivote(f, i, M)
                if indicePiv == -1:
                  print("El sistema no tiene solución")
                  exit(0)
                else:
                  M = self.intercambiarFilas(indicePiv, i, M)
                  pivote = M[i][i]

            for j in np.arange(i+1, f):
                if M[j][i] !=0:
                    k = pivote / M[j][i]
                    M = self.multiplicarFila(k,j,i,M)
                    M = self.restarFila(j,i,i,M)
        print("Matriz resultante : \n", M)
        return M

    def eliminacionGaussJordan(self,filaa,columanaa,M):
      
        indicePiv = -1

        for i in range(filaa):
            pivote = M[i][i]
            if pivote == 0:
                indicePiv = self.buscarPivote(filaa, i, M)
                if indicePiv == -1:
                  print("El sistema no tiene solución")
                  exit(0)
                else:
                  M = self.intercambiarFilas(indicePiv, i, M)
                  pivote = M[i][i] 

            for j in range(filaa):
                for n in range(filaa):
                    if j != n and M[j][n] != 0:
                        k = pivote / M[j][n]
                        M = self.multiplicarFila(k,j,n,M)
                        M = self.restarFila(j,n,n,M)
        print("Matriz resultante:\n ",np.round(M, decimals= 1))

        M = self.NormalizationGJ(filaa,columanaa,M)
        return M


    def NormalizationGJ (self,fila,columna,M):
        for i in range(fila):
            n = 1/M[i][i]
            M = self.multiplicarFila(n,i,0,M)
        print("Matriz normatizada\n",np.round(M, decimals= 1))
        return M

        

    def calculoMatrixInversa(self,f,M):

      I = np.identity(f)

      print("Matriz indentidad\n", I)

      MAum = np.concatenate([M,I],axis = 1)
      print("Matriz aumentada\n", MAum)

      return MAum

    """def vectorSolution(self, f,c,M):
      r = np.zero(len(M[1]), dtype = float)
      for i range(f):
          n = 1/M[i][i]
          r[i] = self.multiplicarFila(n,i,0,M)
          print("x%d = %0.5f",(i,x[i]))"""

    def AnalisisDatos (self, f, c, M):
        M = self.calculoMatrixInversa(f,M)
        M = self.eliminacionGaussJordan(f,c,M)
        #M = self.NormalizationGJ(f,2*f,M)
        return M

def main():

    f3 = 3
    f4 = 4
    f5 = 5
    #c = f+1

    #M = np.random.randint(10, size = (f,f), dtype = np.int32)
    M = np.array([[3,2,1],[0,4,2],[0,0,3],])
    print("matrix :\n", M)

    N = np.array([[4,3,2,1],[0,6,4,2],[0,0,6,3],[0,0,0,4]])

    O =  np.array([[5,4,3,2,1],[0,8,6,4,2],[0,0,9,6,3],[0,0,0,8,4],[0,0,0,0,5]])
    objG = GaussJordan()
    #print("Realizando eliminación Gaussiana")
    #print("Realizando eliminación Gauss-Jordan\t\n")
    #objG.eliminacionGaussJordan(f,c,M)    
    #N=objG.calculoMatrixInversa(f,M)
    #objG.eliminicacionGaussiana(f,2*f,N)
    #print("Valores propios de la matrix\n")
    #objG.eliminicacionGaussiana(f,2*f,N)
    #objG.eliminacionGaussJordan(f,2*f,N)
    objG.AnalisisDatos(f3,2*f3,M)
    objG.AnalisisDatos(f4,2*f4,N)
    objG.AnalisisDatos(f5,2*f5,O)


if __name__ == "__main__":
    main()
```

    matrix :
     [[3 2 1]
     [0 4 2]
     [0 0 3]]
    Matriz indentidad
     [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]
    Matriz aumentada
     [[3. 2. 1. 1. 0. 0.]
     [0. 4. 2. 0. 1. 0.]
     [0. 0. 3. 0. 0. 1.]]
    Matriz resultante:
      [[  3.    0.    0.  -36.   22.5  -3. ]
     [  0.    4.    0.    0.   -1.5   1. ]
     [  0.    0.    3.    0.    0.    1. ]]
    Matriz normatizada
     [[  1.    0.    0.  -12.    7.5  -1. ]
     [  0.    1.    0.    0.   -0.4   0.2]
     [  0.    0.    1.    0.    0.    0.3]]
    Matriz indentidad
     [[1. 0. 0. 0.]
     [0. 1. 0. 0.]
     [0. 0. 1. 0.]
     [0. 0. 0. 1.]]
    Matriz aumentada
     [[4. 3. 2. 1. 1. 0. 0. 0.]
     [0. 6. 4. 2. 0. 1. 0. 0.]
     [0. 0. 6. 3. 0. 0. 1. 0.]
     [0. 0. 0. 4. 0. 0. 0. 1.]]
    Matriz resultante:
      [[  4.    0.    0.    0.   72.  -48.   10.7  -2. ]
     [  0.    6.    0.    0.    0.  -12.   10.7  -2. ]
     [  0.    0.    6.    0.    0.    0.   -1.3   1. ]
     [  0.    0.    0.    4.    0.    0.    0.    1. ]]
    Matriz normatizada
     [[  1.    0.    0.    0.   18.  -12.    2.7  -0.5]
     [  0.    1.    0.    0.    0.   -2.    1.8  -0.3]
     [  0.    0.    1.    0.    0.    0.   -0.2   0.2]
     [  0.    0.    0.    1.    0.    0.    0.    0.2]]
    Matriz indentidad
     [[1. 0. 0. 0. 0.]
     [0. 1. 0. 0. 0.]
     [0. 0. 1. 0. 0.]
     [0. 0. 0. 1. 0.]
     [0. 0. 0. 0. 1.]]
    Matriz aumentada
     [[5. 4. 3. 2. 1. 1. 0. 0. 0. 0.]
     [0. 8. 6. 4. 2. 0. 1. 0. 0. 0.]
     [0. 0. 9. 6. 3. 0. 0. 1. 0. 0.]
     [0. 0. 0. 8. 4. 0. 0. 0. 1. 0.]
     [0. 0. 0. 0. 5. 0. 0. 0. 0. 1.]]
    Matriz resultante:
      [[ 5.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00  1.3333e+03
      -9.1670e+02  2.2590e+02 -6.1100e+01  1.3300e+01]
     [ 0.0000e+00  8.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
      -2.5000e+02  2.2590e+02 -6.1100e+01  1.3300e+01]
     [ 0.0000e+00  0.0000e+00  9.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
       0.0000e+00 -7.4000e+00  7.6000e+00 -1.7000e+00]
     [ 0.0000e+00  0.0000e+00  0.0000e+00  8.0000e+00  0.0000e+00  0.0000e+00
       0.0000e+00  0.0000e+00 -1.2000e+00  1.0000e+00]
     [ 0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00  5.0000e+00  0.0000e+00
       0.0000e+00  0.0000e+00  0.0000e+00  1.0000e+00]]
    Matriz normatizada
     [[ 1.000e+00  0.000e+00  0.000e+00  0.000e+00  0.000e+00  2.667e+02
      -1.833e+02  4.520e+01 -1.220e+01  2.700e+00]
     [ 0.000e+00  1.000e+00  0.000e+00  0.000e+00  0.000e+00  0.000e+00
      -3.120e+01  2.820e+01 -7.600e+00  1.700e+00]
     [ 0.000e+00  0.000e+00  1.000e+00  0.000e+00  0.000e+00  0.000e+00
       0.000e+00 -8.000e-01  8.000e-01 -2.000e-01]
     [ 0.000e+00  0.000e+00  0.000e+00  1.000e+00  0.000e+00  0.000e+00
       0.000e+00  0.000e+00 -2.000e-01  1.000e-01]
     [ 0.000e+00  0.000e+00  0.000e+00  0.000e+00  1.000e+00  0.000e+00
       0.000e+00  0.000e+00  0.000e+00  2.000e-01]]


07f4a91b054f58863f121d9857a6a5f25c85b94c
ipynb
Jupyter Notebook
LIMT_Comp_II_Sistemas_de_ecuaciones_lineales.ipynb
LuisIMT/ComputacionIII-2021-1
5bbee3ddb3bb6c4d4554ed311da7dc359589e620
W3C
2020-09-23T21:24:29.000Z
2020-09-23T21:24:29.000Z
LIMT_Comp_II_Sistemas_de_ecuaciones_lineales.ipynb
LuisIMT/ComputacionIII-2021-1
5bbee3ddb3bb6c4d4554ed311da7dc359589e620
W3C
LIMT_Comp_II_Sistemas_de_ecuaciones_lineales.ipynb
LuisIMT/ComputacionIII-2021-1
5bbee3ddb3bb6c4d4554ed311da7dc359589e620
W3C
Qwen/Qwen-72B
1. YES
2. YES