Advertisement
Guest User

goginskylovi

a guest
Apr 7th, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import numpy
  2. import copy
  3.  
  4.  
  5. def gaussFunc(a):
  6.     eps = 1e-16
  7.  
  8.     c = numpy.array(a)
  9.     a = numpy.array(a)
  10.  
  11.     len1 = len(a[:, 0])
  12.     len2 = len(a[0, :])
  13.     vectB = copy.deepcopy(a[:, len1])
  14.  
  15.     for g in range(len1):
  16.  
  17.         max = abs(a[g][g])
  18.         my = g
  19.         t1 = g
  20.         while t1 < len1:
  21.             # for t1 in range(len(a[:,0])):
  22.             if abs(a[t1][g]) > max:
  23.                 max = abs(a[t1][g])
  24.                 my = t1
  25.             t1 += 1
  26.  
  27.         if abs(max) < eps:
  28.             raise DetermExeption("Check determinant")
  29.  
  30.         if my != g:
  31.             # a[g][:], a[my][:] = a[my][:], a[g][:]
  32.             # numpy.swapaxes(a, 1, 0)
  33.             b = copy.deepcopy(a[g])
  34.             a[g] = copy.deepcopy(a[my])
  35.             a[my] = copy.deepcopy(b)
  36.  
  37.         amain = float(a[g][g])
  38.  
  39.         z = g
  40.         while z < len2:
  41.             a[g][z] = a[g][z] / amain
  42.             z += 1
  43.  
  44.         j = g + 1
  45.  
  46.         while j < len1:
  47.             b = a[j][g]
  48.             z = g
  49.  
  50.             while z < len2:
  51.                 a[j][z] = a[j][z] - a[g][z] * b
  52.                 z += 1
  53.             j += 1
  54.  
  55.     a = backTrace(a, len1, len2)
  56.  
  57.  
  58.     print("Погрешность:")
  59.  
  60.     print(vectorN(c, a, len1, vectB))
  61.  
  62.     return a
  63.  
  64.  
  65. class DetermExeption(Exception):
  66.     def __init__(self, value):
  67.         self.value = value
  68.  
  69.     def __str__(self):
  70.         return repr(self.value)
  71.  
  72.  
  73. def backTrace(a, len1, len2):
  74.     a = numpy.array(a)
  75.     i = len1 - 1
  76.     while i > 0:
  77.         j = i - 1
  78.  
  79.         while j >= 0:
  80.             a[j][len1] = a[j][len1] - a[j][i] * a[i][len1]
  81.             j -= 1
  82.         i -= 1
  83.     return a[:, len2 - 1]
  84.  
  85.  
  86. def vectorN(c, a, len1, vectB):  # c-начальная матрица a-ответ len-ранг, vectB-вектор B
  87.     c = numpy.array(c)
  88.     a = numpy.array(a)
  89.     vectB = numpy.array(vectB)
  90.  
  91.     b = numpy.zeros((len1))
  92.  
  93.     i = 0
  94.  
  95.  
  96.     while i<len1:
  97.         j = 0
  98.         while j<len1:
  99.  
  100.             b[i]+=c[i][j]*a[j]
  101.  
  102.             j+=1
  103.  
  104.         i=i+1
  105.  
  106.  
  107.  
  108.     c = copy.deepcopy(b)
  109.     print("!")
  110.  
  111.     for i in range(len1):
  112.         c[i] = abs(c[i] - vectB[i])
  113.  
  114.     return c
  115.  
  116. a = numpy.array([[8.30, 3.02, 4.10, 1.90, -11.05],
  117.  
  118.                 [3.92, 8.45, 7.38, 2.46, 12.21],
  119.  
  120.                 [3.77, 7.61, 8.04, 2.28, 15.05],
  121.                
  122.                 [2.21, 3.25, 1.69, 6.99, -8.350]])
  123. print(a)
  124. b = gaussFunc(a)
  125. print(b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement