mfgnik

Untitled

Oct 12th, 2020
1,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import copy
  2. from sys import stdin
  3.  
  4.  
  5. class MatrixError(BaseException):
  6.     def __init__(self, matrix1, matrix2):
  7.         self.matrix1 = matrix1
  8.         self.matrix2 = matrix2
  9.  
  10.  
  11. class Matrix:
  12.     def __init__(self, matrix):
  13.         self.matrix = copy.deepcopy(matrix)
  14.  
  15.     def __str__(self):
  16.         result = ''
  17.         for i in range(len(self.matrix)):
  18.             if i < len(self.matrix) - 1:
  19.                 for j in range(len(self.matrix[i])):
  20.                     if j < len(self.matrix[i]) - 1:
  21.                         result += str(self.matrix[i][j]) + '\t'
  22.                     else:
  23.                         result += str(self.matrix[i][j]) + '\n'
  24.             else:
  25.                 for j in range(len(self.matrix[i])):
  26.                     if j < len(self.matrix[i]) - 1:
  27.                         result += str(self.matrix[i][j]) + '\t'
  28.                     else:
  29.                         result += str(self.matrix[i][j])
  30.         return result
  31.  
  32.     def size(self):
  33.         return len(self.matrix), len(self.matrix[1])
  34.  
  35.     def __add__(self, other):
  36.         arrows = len(self.matrix) == len(other.matrix)
  37.         columns = len(self.matrix[0]) == len(other.matrix[0])
  38.         if arrows and columns:
  39.             newMatrix = list()
  40.             for i in range(len(self.matrix)):
  41.                 line = []
  42.                 for j in range(len(self.matrix[i])):
  43.                     line.append(self.matrix[i][j] + other.matrix[i][j])
  44.                 newMatrix.append(line)
  45.                 line = []
  46.             return Matrix(newMatrix)
  47.         else:
  48.             raise MatrixError(self, other)
  49.  
  50.     def __mul__(self, other):
  51.         if isinstance(other, float) or isinstance(other, int):
  52.             newMatrix = list()
  53.             for i in range(len(self.matrix)):
  54.                 line = []
  55.                 for j in range(len(self.matrix[i])):
  56.                     line.append(self.matrix[i][j] * other)
  57.                 newMatrix.append(line)
  58.                 line = []
  59.         elif isinstance(other, Matrix):
  60.             arrows = len(other.matrix)
  61.             columns = len(self.matrix[0])
  62.             if arrows == columns:
  63.                 pass
  64.  
  65.         return Matrix(newMatrix)
  66.  
  67.     __rmul__ = __mul__
  68.  
  69.     def transposed(self):
  70.         newMatrix = []
  71.         for j in range(len(self.matrix[0])):
  72.             newMatrix.append([])
  73.             for i in range(len(self.matrix)):
  74.                 newMatrix[j].append(self.matrix[i][j])
  75.         return Matrix(newMatrix)
  76.  
  77.     def transpose(self):
  78.         newMatrix = []
  79.         for j in range(len(self.matrix[0])):
  80.             newMatrix.append([])
  81.             for i in range(len(self.matrix)):
  82.                 newMatrix[j].append(self.matrix[i][j])
  83.         self.matrix = newMatrix
  84.         return Matrix(newMatrix)
  85.  
  86.  
  87. exec(stdin.read())
Advertisement
Add Comment
Please, Sign In to add comment