Advertisement
Guest User

matrix_bobsya

a guest
Nov 12th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. from sys import stdin
  2. from copy import deepcopy
  3.  
  4.  
  5. class Matrix:
  6.  
  7. def __init__(self, a):
  8. self.matrix = deepcopy(a)
  9.  
  10. def __str__(self):
  11. string = ''
  12. for i in self.matrix:
  13. string += '\t'.join(map(str, i)) + '\n'
  14. return string.strip()
  15.  
  16. def size(self):
  17. self.column = len(self.matrix[0])
  18. if self.column == 0:
  19. return 0, 0
  20.  
  21. self.row = len(self.matrix)
  22.  
  23. return self.row, self.column
  24.  
  25. def __add__(self, other):
  26. add = deepcopy(other)
  27. self.size()
  28. new = Matrix([])
  29.  
  30. for i in range(self.row):
  31. newRow = []
  32. for j in range(self.column):
  33. newRow.append(self.matrix[i][j] + add.matrix[i][j])
  34. new.matrix.append(newRow)
  35.  
  36. return new.matrix
  37.  
  38. def __mul__(self, other):
  39. add = deepcopy(other)
  40. self.size()
  41. new = Matrix([])
  42.  
  43. for i in range(self.row):
  44. newRow = []
  45. for j in range(self.column):
  46. newRow.append(self.matrix[i][j] * add)
  47. new.matrix.append(newRow)
  48.  
  49. return new.matrix
  50.  
  51. def __rmul__(self, other):
  52. return self * other
  53.  
  54. # exec(stdin.read())
  55.  
  56.  
  57.  
  58. m1 = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  59. m2 = Matrix([[0, 1, 0], [20, 0, -1], [-1, -2, 0]])
  60. # print(m1 + m2)
  61. print(m1)
  62.  
  63. m = Matrix([[1, 1, 0], [0, 2, 10], [10, 15, 30]])
  64. alpha = 15
  65. #print(m * alpha)
  66. #print(alpha * m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement