Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdin
- from copy import deepcopy
- class Matrix:
- def __init__(self, a):
- self.matrix = deepcopy(a)
- def __str__(self):
- string = ''
- for i in self.matrix:
- string += '\t'.join(map(str, i)) + '\n'
- return string.strip()
- def size(self):
- self.column = len(self.matrix[0])
- if self.column == 0:
- return 0, 0
- self.row = len(self.matrix)
- return self.row, self.column
- def __add__(self, other):
- add = deepcopy(other)
- self.size()
- new = Matrix([])
- for i in range(self.row):
- newRow = []
- for j in range(self.column):
- newRow.append(self.matrix[i][j] + add.matrix[i][j])
- new.matrix.append(newRow)
- return new.matrix
- def __mul__(self, other):
- add = deepcopy(other)
- self.size()
- new = Matrix([])
- for i in range(self.row):
- newRow = []
- for j in range(self.column):
- newRow.append(self.matrix[i][j] * add)
- new.matrix.append(newRow)
- return new.matrix
- def __rmul__(self, other):
- return self * other
- # exec(stdin.read())
- m1 = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
- m2 = Matrix([[0, 1, 0], [20, 0, -1], [-1, -2, 0]])
- # print(m1 + m2)
- print(m1)
- m = Matrix([[1, 1, 0], [0, 2, 10], [10, 15, 30]])
- alpha = 15
- #print(m * alpha)
- #print(alpha * m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement