fork download
  1. from sympy import *
  2. import numpy as np
  3. import string
  4.  
  5.  
  6. # this code implement Gauss_elimination to solve
  7. # set of equation
  8.  
  9. # edit in line 119
  10.  
  11. def isZero(_vect):
  12. n = _vect.shape[0]
  13.  
  14. result = True
  15. for i in range(n):
  16. if _vect[i] != 0:
  17. result = False
  18. break
  19.  
  20. return result
  21.  
  22.  
  23. def swap(a, b):
  24. temp = a[0:].copy()
  25. a[0:] = b[0:]
  26. b[0:] = temp
  27.  
  28.  
  29. def rank(A):
  30. rows = A.shape[0]
  31.  
  32. result = 0
  33. for i in range(rows):
  34. if (isZero(A[i]) == False):
  35. result += 1
  36. return result
  37.  
  38.  
  39. def getMatrix(A):
  40. return (A.T)[0:-1].copy().T
  41.  
  42.  
  43. def isAllBelowZero(A, index):
  44. rows = A.shape[0]
  45.  
  46. result = True
  47. for i in range(index + 1, rows):
  48. if A[i][index] != 0:
  49. result = False
  50. break
  51.  
  52. return result
  53.  
  54.  
  55. def Gauss_elimination(A):
  56. rows, cols = A.shape
  57. loop_epoch = cols if cols < rows else rows
  58. i = 0
  59.  
  60. while i < loop_epoch:
  61. if A[i][i] != 0:
  62. for j in range(i + 1, rows):
  63. scaling_value = A[j][i] / A[i][i]
  64. A[j] = A[j] - scaling_value * A[i]
  65. else:
  66. for index in range(i + 1, rows):
  67. if A[index][i] != 0:
  68. swap(A[i], A[index])
  69. i -= 1
  70. break
  71. i += 1
  72.  
  73. for i in range(rows - 1):
  74. if (isZero(A[i])):
  75. for j in range(i + 1, rows):
  76. if (isZero(A[j]) == False):
  77. swap(A[i], A[j])
  78. break
  79.  
  80.  
  81. def back_substitution(A):
  82. Gauss_elimination(A)
  83. rows, cols = A.shape
  84. cols -= 1
  85. rA = rank(getMatrix(A))
  86. rAB = rank(A)
  87.  
  88. x = np.zeros(cols)
  89.  
  90. if (rA + 1 == rAB):
  91. print(f"Setequation have no solution")
  92. elif (rA == rAB and rA == cols):
  93. print(f"Setequation have one solution: ")
  94. # back substitution
  95. i = cols - 1
  96. while i >= 0:
  97. x[i] = (A[i][cols] - x.dot(getMatrix(A)[i])) / A[i][i]
  98. i -= 1
  99. else:
  100. alphabet = symbols(list(string.ascii_lowercase))
  101. print(f"Setequation have infinity solution with {cols - rA} ")
  102. n_iter = 0
  103. pivot_row = 0
  104. y = []
  105. for i in range(cols):
  106. if A[i + pivot_row][i] == 0:
  107. pivot_row -= 1
  108. y.append(alphabet[n_iter])
  109. n_iter += 1
  110. else:
  111. zero = symbols('0')
  112. y.append(zero)
  113.  
  114. i = cols - 1
  115. while i >= 0:
  116. s = (getMatrix(A)[i]).dot(y)
  117. if A[i][i] != 0:
  118. # simplify right this expression
  119. y[i] = (((A[i][cols] - s) / A[i][i]))
  120.  
  121. i -= 1
  122. x = y
  123.  
  124. return x
  125.  
  126.  
  127. def main():
  128. # matrix = np.array([[1, 2, 2, 9], [3, 4, 4, 1], [5, 6, 7, 0]])
  129. matrix = np.array([[1, 2, 3, 0],
  130. [3, 2, 3, 2],
  131. [2, 2, 3, 1]])
  132. print(matrix)
  133. Gauss_elimination(matrix)
  134. print(matrix)
  135. print((back_substitution(matrix)))
  136.  
  137.  
  138. main()
  139.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/py_compile.py", line 117, in compile
    raise py_exc
py_compile.PyCompileError:   File "prog.py", line 91
    print(f"Setequation have no solution")
                                        ^
SyntaxError: invalid syntax

stdout
Standard output is empty