Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdin
- from copy import deepcopy
- class MatrixError(BaseException):
- def __init__(self, Matrix, other):
- self.matrix1 = Matrix
- self.matrix2 = other
- class Matrix:
- def __init__(self, listOfLists):
- self.matrix = deepcopy(listOfLists)
- def __str__(self):
- string = ''
- for row in self.matrix:
- string += '\t'.join(map(str, row))
- string += '\n'
- string = string[:-1]
- return string
- def size(self):
- return len(self.matrix), len(self.matrix[0])
- def __add__(self, other):
- result = Matrix(self.matrix)
- size = self.size()
- sizeOther = other.size()
- if size[0] == sizeOther[0] and size[1] == sizeOther[1]:
- for i in range(size[0]):
- for j in range(size[1]):
- result.matrix[i][j] += other.matrix[i][j]
- else:
- raise MatrixError(self, other)
- return result
- def __mul__(self, other):
- if isinstance(self, int) or isinstance(self, float):
- result = Matrix(other.matrix)
- size = other.size()
- for i in range(size[0]):
- for j in range(size[1]):
- result.matrix[i][j] = other.matrix[i][j] * self
- elif isinstance(other, int) or isinstance(other, float):
- result = Matrix(self.matrix)
- size = self.size()
- for i in range(size[0]):
- for j in range(size[1]):
- result.matrix[i][j] = self.matrix[i][j] * other
- elif isinstance(self, Matrix) or isinstance(other, Matrix):
- result = []
- resultRow = []
- elem = 0
- size1 = self.size()
- size2 = other.size()
- if size1[1] == size2[0]:
- for i in range(size1[0]):
- for j in range(size2[1]):
- for k in range(size1[1]):
- elem += self.matrix[i][k] * other.matrix[k][j]
- resultRow.append(elem)
- elem = 0
- result.append(resultRow)
- resultRow = []
- result = Matrix(result)
- else:
- raise MatrixError(self, other)
- return result
- __rmul__ = __mul__
- def transpose(self):
- result = []
- resultRow = []
- size = self.size()
- for i in range(size[1]):
- for j in range(size[0]):
- resultRow.append(self.matrix[j][i])
- result.append(resultRow)
- resultRow = []
- self.matrix = result
- result = Matrix(result)
- return result
- @staticmethod
- def transposed(self):
- result = []
- resultRow = []
- size = self.size()
- for i in range(size[1]):
- for j in range(size[0]):
- resultRow.append(self.matrix[j][i])
- result.append(resultRow)
- resultRow = []
- result = Matrix(result)
- return result
- def elem1(self, first, second, const):
- result = Matrix(self.matrix)
- size = self.size()
- for j in range(size[1]):
- result.matrix[first][j] += self.matrix[second][j] * const
- return result
- def elem2(self, first, second):
- result = Matrix(self.matrix)
- size = self.size()
- for j in range(size[1]):
- result.matrix[first][j] = self.matrix[second][j]
- return result
- def elem3(self, first, const):
- result = Matrix(self.matrix)
- size = self.size()
- for j in range(size[1]):
- result.matrix[first][j] *= const
- return result
- def wide(self, other):
- width = []
- widthRow = []
- size = self.size()
- for i in range(size[0]):
- for j in range(size[1]):
- widthRow.append(self.matrix[i][j])
- widthRow.append(other[i])
- width.append(widthRow)
- widthRow = []
- self.matrix = width
- width = Matrix(width)
- return width
- def solve(self, other):
- ans = []
- row = 0
- col = 0
- width = self.wide(other)
- size = width.size()
- main = []
- while row != size[0]:
- flag = False
- for j in range(col, size[1]):
- for i in range(row, size[0]):
- if width.matrix[j][i] != 0:
- row = i
- col = j
- main.append(j)
- flag = True
- break
- if flag:
- break
- else:
- raise MatrixError
- width = width.elem2(0, row)
- for j in range(1, size[0]):
- div = -1 * width.matrix[j][col] / width.matrix[0][col]
- width.matrix = width.elem1(j, 0, div)
- length = len(main)
- main = main[::-1]
- if size[0] == size[1] - 1:
- for i in range(size[0]):
- elem = width.matrix[i][main[i]]
- width.matrix = width.elem3(i, 1 / elem)
- for j in main:
- for i in range(length - 2, 0, -1):
- elem = width.matrix[length - 2][j]
- width.matrix = width.elem1(i, length - 1, -1 * elem)
- for i in range(size[0]):
- ans = ans.append(width.matrix[i][size[1] - 1])
- return ans
- else:
- raise MatrixError
- exec(stdin.read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement