fork download
  1. # Autor - https://pt.stackoverflow.com/users/132
  2. class Matriz:
  3. def __init__(self, largura, altura, valores):
  4. if len(valores) != altura:
  5. raise IndexError("O conjunto de valores não tem o tamanho correto.")
  6. for linha in valores:
  7. if len(linha) != largura:
  8. raise IndexError("O conjunto de valores não tem o tamanho correto.")
  9. self.__valores = valores
  10.  
  11. @property
  12. def largura(self):
  13. return len(self.__valores[0])
  14.  
  15. @property
  16. def altura(self):
  17. return len(self.__valores)
  18.  
  19. def valor(self, num_linha, num_coluna):
  20. if num_linha < 0 or num_linha >= self.altura or num_coluna < 0 or num_coluna >= self.largura:
  21. raise IndexError(f"Não há posição a_{num_linha}_{num_coluna}.")
  22. return self.__valores[num_linha][num_coluna]
  23.  
  24. def definir(self, num_linha, num_coluna, valor):
  25. if num_linha < 0 or num_linha >= self.altura or num_coluna < 0 or num_coluna >= self.largura:
  26. raise IndexError(f"Não há posição a_{num_linha}_{num_coluna}.")
  27. self.__valores[num_linha][num_coluna] = valor
  28.  
  29. def multiplicar_linha(self, num_linha, valor):
  30. if num_linha < 0 or num_linha >= self.altura:
  31. raise IndexError(f"Não há linha {num_linha}.")
  32. for coluna in range(0, self.largura):
  33. self.__valores[num_linha][coluna] *= valor
  34.  
  35. def somar_linha(self, num_linha, valores, multiplicador):
  36. if num_linha < 0 or num_linha >= self.altura:
  37. raise IndexError(f"Não há linha {num_linha}.")
  38. if len(valores) != self.largura:
  39. raise IndexError("O conjunto de valores não tem o tamanho correto.")
  40. for coluna in range(0, self.largura):
  41. self.__valores[num_linha][coluna] += valores[coluna] * multiplicador
  42.  
  43. def linha(self, num_linha):
  44. if num_linha < 0 or num_linha >= self.altura:
  45. raise IndexError(f"Não há linha {num_linha}.")
  46. return self.__valores[num_linha]
  47.  
  48. @staticmethod
  49. def triangular_inferior(lista_1, lista_2):
  50. t = len(lista_2)
  51. if t * (t + 1) / 2 != len(lista_1):
  52. raise IndexError("A segunda lista não tem a quantidade correta de termos.")
  53. matriz = []
  54. n = 0
  55. for i in range(0, t):
  56. linha = []
  57. for j in range(0, i + 1):
  58. linha.append(lista_1[n])
  59. n += 1
  60. for j in range(i + 1, t):
  61. linha.append(0)
  62. matriz.append(linha)
  63. linha.append(lista_2[i])
  64. return Matriz(t + 1, t, matriz)
  65.  
  66. def eliminacao_gauss(self):
  67. for i in range(0, self.altura):
  68. if self.__valores[i][i] == 0:
  69. raise IndexError("Zero na coluna principal.")
  70. for i in range(0, self.altura):
  71. self.multiplicar_linha(i, 1 / self.__valores[i][i])
  72. for j in range(i + 1, self.altura):
  73. self.somar_linha(j, self.__valores[i], -self.__valores[j][i])
  74. ret = []
  75. for i in range(0, self.altura):
  76. ret.append(self.__valores[i][-1])
  77. return ret
  78.  
  79. def __str__(self):
  80. t = ""
  81. for i in range(0, self.altura):
  82. for j in range(0, self.largura):
  83. v = self.__valores[i][j]
  84. if j == self.largura - 1:
  85. t += f" = {v: >6.2f}"
  86. else:
  87. if j != 0: t += " "
  88. if v == 0:
  89. t += " " * (12 + len(str(j + 1)))
  90. else:
  91. s = "+" if v > 0 else "-"
  92. t += f"{s} {abs(v): >6.2f} * x{j + 1}"
  93. t += "\n"
  94. return t
  95.  
  96. def mostra_vetor(vetor):
  97. aux = ""
  98. for i in range(0, len(vetor)):
  99. aux += f"{vetor[i]} * x{i + 1}\n"
  100. return aux
  101.  
  102. m = Matriz.triangular_inferior([-13, 13, 3, -5, -1, -1, 3, 0, 1, 1], [-13, 13, -7, 4])
  103. print(m)
  104. resultado = m.eliminacao_gauss()
  105. print(resultado)
  106. print(mostra_vetor(resultado))
Success #stdin #stdout 0.02s 9228KB
stdin
Standard input is empty
stdout
-  13.00 * x1                                           = -13.00
+  13.00 * x1 +   3.00 * x2                             =  13.00
-   5.00 * x1 -   1.00 * x2 -   1.00 * x3               =  -7.00
+   3.00 * x1               +   1.00 * x3 +   1.00 * x4 =   4.00

[1.0, 0.0, 2.0, -1.0]
1.0 * x1
0.0 * x2
2.0 * x3
-1.0 * x4