brospresident

Untitled

Oct 20th, 2021
663
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. rng = np.random.default_rng(0)  #generator
  3. #----------- Input ---------------------------------
  4. n = 6  # dimensiunea matricei
  5. A = rng.integers(1,11, size = (n,n))
  6. A = np.triu(A,-1)
  7. A[n-1,:] = rng.integers(1,11, size = (1,n))
  8. A =A.astype('float')
  9. b =  rng.integers(-10,11, size = (n,1)).astype('float')
  10. #-------------------------------------------------------------
  11. print('-----------A------------')
  12. print(A)
  13. print('-----------b------------')
  14. print(b)
  15.  
  16. def EGPP(A):
  17.     n = len(A)
  18.  
  19.     p = np.zeros(len(A), dtype = int)
  20.  
  21.     for k in range(n - 1):
  22.         if abs(A[k + 1][k]) > abs(A[k][k]):
  23.             ik = k + 1
  24.             p[k] = ik
  25.             for i in range(k, n):
  26.                 A[ik][i], A[k][i] = A[k][i], A[ik][i]
  27.        
  28.         A[k + 1][k] /= A[k][k]
  29.  
  30.         for j in range(k + 1, n):
  31.             A[k + 1][j] -= A[k + 1][j] * A[k][j]
  32.  
  33.     return A, p
  34.  
  35. def S_SUP_TR(U, b):
  36.     n = len(U)
  37.  
  38.     rezultat = np.zeros(n, dtype = float)
  39.  
  40.     for i in range(n - 1, -1, -1):
  41.         s = b[i]
  42.         for j in range(i + 1, n):
  43.             s -= U[i][j] * rezultat[j]
  44.         rezultat[i] = s / U[i][i]
  45.  
  46.     return rezultat
  47.  
  48. def SL_GPP(A, b):
  49.     U, p = EGPP(A)
  50.  
  51.     n = len(A)
  52.  
  53.     for k in range(n - 1):
  54.         b[k], b[p[k]] = b[p[k]], b[k]
  55.         for i in range(k + 1, n):
  56.             b[i] -= A[i][k] * b[k]
  57.     x = S_SUP_TR(U, b)
  58.  
  59.     return x
  60.  
  61. solutie = SL_GPP(A, b)
  62. print(solutie)
  63.  
  64. inv = np.linalg.inv(A)
  65.  
  66. if solutie is inv * b:
  67.     print("Solutia gasita e corecta")
  68. else:
  69.     print("Solutia gasita e gresita.........")
  70.  
RAW Paste Data