Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.71 KB | None | 0 0
  1. from sys import stdin
  2. from copy import deepcopy
  3.  
  4.  
  5. class MatrixError(BaseException):
  6.     def __init__(self, Matrix, other):
  7.         self.matrix1 = Matrix
  8.         self.matrix2 = other
  9.  
  10.  
  11. class Matrix:
  12.     def __init__(self, listOfLists):
  13.         self.matrix = deepcopy(listOfLists)
  14.  
  15.     def __str__(self):
  16.         string = ''
  17.         for row in self.matrix:
  18.             string += '\t'.join(map(str, row))
  19.             string += '\n'
  20.         string = string[:-1]
  21.         return string
  22.  
  23.     def size(self):
  24.         return len(self.matrix), len(self.matrix[0])
  25.  
  26.     def __add__(self, other):
  27.         result = Matrix(self.matrix)
  28.         size = self.size()
  29.         sizeOther = other.size()
  30.         if size[0] == sizeOther[0] and size[1] == sizeOther[1]:
  31.             for i in range(size[0]):
  32.                 for j in range(size[1]):
  33.                     result.matrix[i][j] += other.matrix[i][j]
  34.         else:
  35.             raise MatrixError(self, other)
  36.         return result
  37.  
  38.     def __mul__(self, other):
  39.         if isinstance(self, int) or isinstance(self, float):
  40.             result = Matrix(other.matrix)
  41.             size = other.size()
  42.             for i in range(size[0]):
  43.                 for j in range(size[1]):
  44.                     result.matrix[i][j] = other.matrix[i][j] * self
  45.         elif isinstance(other, int) or isinstance(other, float):
  46.             result = Matrix(self.matrix)
  47.             size = self.size()
  48.             for i in range(size[0]):
  49.                 for j in range(size[1]):
  50.                     result.matrix[i][j] = self.matrix[i][j] * other
  51.         elif isinstance(self, Matrix) or isinstance(other, Matrix):
  52.             result = []
  53.             resultRow = []
  54.             elem = 0
  55.             size1 = self.size()
  56.             size2 = other.size()
  57.             if size1[1] == size2[0]:
  58.                 for i in range(size1[0]):
  59.                     for j in range(size2[1]):
  60.                         for k in range(size1[1]):
  61.                             elem += self.matrix[i][k] * other.matrix[k][j]
  62.                         resultRow.append(elem)
  63.                         elem = 0
  64.                     result.append(resultRow)
  65.                     resultRow = []
  66.                 result = Matrix(result)
  67.             else:
  68.                 raise MatrixError(self, other)
  69.         return result
  70.     __rmul__ = __mul__
  71.  
  72.     def transpose(self):
  73.         result = []
  74.         resultRow = []
  75.         size = self.size()
  76.         for i in range(size[1]):
  77.             for j in range(size[0]):
  78.                 resultRow.append(self.matrix[j][i])
  79.             result.append(resultRow)
  80.             resultRow = []
  81.         self.matrix = result
  82.         result = Matrix(result)
  83.         return result
  84.  
  85.     @staticmethod
  86.     def transposed(self):
  87.         result = []
  88.         resultRow = []
  89.         size = self.size()
  90.         for i in range(size[1]):
  91.             for j in range(size[0]):
  92.                 resultRow.append(self.matrix[j][i])
  93.             result.append(resultRow)
  94.             resultRow = []
  95.         result = Matrix(result)
  96.         return result
  97.  
  98.     def elem1(self, first, second, const):
  99.         result = Matrix(self.matrix)
  100.         size = self.size()
  101.         for j in range(size[1]):
  102.             result.matrix[first][j] += self.matrix[second][j] * const
  103.         return result
  104.  
  105.     def elem2(self, first, second):
  106.         result = Matrix(self.matrix)
  107.         size = self.size()
  108.         for j in range(size[1]):
  109.             (result.matrix[first][j], self.matrix[second][j]) = \
  110.                 (self.matrix[second][j], result.matrix[first][j])
  111.         return result
  112.  
  113.     def elem3(self, first, const):
  114.         result = Matrix(self.matrix)
  115.         size = self.size()
  116.         for j in range(size[1]):
  117.             result.matrix[first][j] *= const
  118.         return result
  119.  
  120.     def wide(self, other):
  121.         width = []
  122.         widthRow = []
  123.         size = self.size()
  124.         for i in range(size[0]):
  125.             for j in range(size[1]):
  126.                 widthRow.append(self.matrix[i][j])
  127.             widthRow.append(other[i])
  128.             width.append(widthRow)
  129.             widthRow = []
  130.         self.matrix = width
  131.         width = Matrix(width)
  132.         return width
  133.  
  134.     def solve(self, other):
  135.         ans = []
  136.         row = 0
  137.         col = -1
  138.         width = self.wide(other)
  139.         size = width.size()
  140.         main = []
  141.         while row != size[0] - 1:
  142.             flag = False
  143.             for j in range(col + 1, size[1]):
  144.                 for i in range(row, size[0]):
  145.                     if width.matrix[i][j] != 0:
  146.                         row += 1
  147.                         col = j
  148.                         main.append(j)
  149.                         flag = True
  150.                         break
  151.                 if flag:
  152.                     break
  153.             else:
  154.                 raise MatrixError
  155.             width = width.elem2(0, i)
  156.             for j in range(1, size[0]):
  157.                 div = width.matrix[j][col] / width.matrix[0][col] * (-1)
  158.                 width = width.elem1(j, 0, div)
  159.         length = len(main)
  160.         main = main[::-1]
  161.         if size[0] == size[1] - 1:
  162.             for i in range(size[0]):
  163.                 elem = width.matrix[i][main[i]]
  164.                 width = width.elem3(i, 1 / elem)
  165.             for j in main:
  166.                 for i in range(length - 2, 0, -1):
  167.                     elem = width.matrix[length - 2][j]
  168.                     width = width.elem1(i, length - 1, elem * (-1))
  169.             for i in range(size[0]):
  170.                 ans = ans.append(width.matrix[i][size[1] - 1])
  171.             return ans
  172.         else:
  173.             raise MatrixError
  174.  
  175. # Task 5 check 1
  176. m = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  177. print(m.solve([1,1,1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement