Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. class InputError():
  2.     def errorSizes(self, n, m):
  3.         n_num = 0
  4.         m_num = 0
  5.         for i in range(len(n)):
  6.             if (n[i] not in '0123456789'):
  7.                 print('Matrix sizes are incorrect!')
  8.                 exit()
  9.             n_num = n_num * 10 + int(n[i])
  10.  
  11.         for i in range(len(m)):
  12.             if (m[i] not in '0123456789'):
  13.                 print('Matrix sizes are incorrect!')
  14.                 exit()
  15.             m_num = m_num * 10 + int(m[i])
  16.  
  17.  
  18.         if (n_num != m_num):
  19.             print('Matrix sizes are not equal!')
  20.             exit()
  21.  
  22.         return (n_num, m_num)
  23.  
  24.     def errorMatrix(self, A, B, n):
  25.         ans_A = A
  26.         ans_B = B
  27.  
  28.         if (len(A) != n or len(B) != n):
  29.             print('Matrix is incorrect!')
  30.             exit()
  31.  
  32.         for i in range(n):
  33.             if (len(A[i]) != n):
  34.                 print('Matrix is incorrect!')
  35.                 exit()
  36.  
  37.             for j in range(n):
  38.                 current = A[i][j]
  39.                 ans = 0
  40.                 for k in range(len(current)):
  41.                     if (current[k] not in '0123456789'):
  42.                         if (k == 0 and current[k] == '-'):
  43.                             continue
  44.                         print('Matrix is incorrect!')
  45.                         exit()
  46.                     ans = ans * 10 + int(current[k])
  47.                 if (current[0] == '-'):
  48.                     ans *= -1
  49.                 ans_A[i][j] = ans
  50.         for i in range(n):
  51.             current = B[i]
  52.             ans = 0
  53.             for j in range(len(current)):
  54.                 if (current[j] not in '0123456789'):
  55.                     if (j == 0 and current[j] == '-'):
  56.                         continue
  57.                     print('Matrix is incorrect!')
  58.                     exit()
  59.                 ans = ans * 10 + int(current[j])
  60.             if (current[0] == '-'):
  61.                 ans *= -1
  62.             ans_B[i] = ans
  63.         return(ans_A, ans_B)
  64.  
  65. class SlaeError():
  66.     def checkDet(self, A):
  67.         for i in range(len(A)):
  68.             if (A[i][i] == 0):
  69.                 print('There is not only one solution!')
  70.                 exit()
  71.  
  72.  
  73.  
  74.  
  75. class Slae(InputError, SlaeError):
  76.     stringLength = 0
  77.     numStrings = 0
  78.     A = []
  79.     B = []
  80.  
  81.     def read(self):
  82.         self.stringLength = input()
  83.         self.numStrings = input()
  84.  
  85.         data = self.errorSizes(self.stringLength, self.numStrings)
  86.         self.stringLength = data[0]
  87.         self.numStrings = data[1]
  88.  
  89.         for i in range(self.numStrings):
  90.             self.A.append(list(input().split()))
  91.         self.B = list(input().split())
  92.  
  93.         data = self.errorMatrix(self.A, self.B, self.numStrings)
  94.         self.A = data[0]
  95.         self.B = data[1]
  96.  
  97.     def writeSolution(self):
  98.         for i in range(self.numStrings):
  99.             print('X[', i + 1, '] = ', float(self.B[i]) / float(self.A[i][i]))
  100.     def swapStr(self, firstIndex, secondIndex):
  101.         for i in range(self.stringLength):
  102.             self.A[firstIndex][i], self.A[secondIndex][i] = self.A[secondIndex][i], self.A[firstIndex][i]
  103.         self.B[firstIndex], self.B[secondIndex] = self.B[secondIndex], self.B[firstIndex]
  104.     def rationing(self, index):
  105.         value = self.A[index][index]
  106.         if (value == 0):
  107.             return
  108.         for i in range(index, self.stringLength):
  109.             self.A[index][i] /= value
  110.         self.B[index] /= value
  111.     def partialPivoting(self, index):
  112.         maxElem = self.A[index][index]
  113.         indexMaxElem = index
  114.         for i in range(index, self.numStrings):
  115.             if (abs(self.A[i][index]) > maxElem):
  116.                 maxElem = abs(self.A[i][index])
  117.                 indexMaxElem = i
  118.         self.swapStr(index, indexMaxElem)
  119.     def gauss(self):
  120.         for i in range(self.numStrings):
  121.             self.partialPivoting(i)
  122.             self.rationing(i)
  123.             for j in range(self.numStrings):
  124.                 if (i == j):
  125.                     continue
  126.                 value = self.A[j][i]
  127.                 for k in range(i, self.stringLength):
  128.                     self.A[j][k] -= value * self.A[i][k]
  129.                 self.B[j] -= value * self.B[i]
  130.         self.checkDet(self.A)
  131.  
  132. task = Slae()
  133. task.read()
  134. task.gauss()
  135. task.writeSolution()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement