Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 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. return result
  111.  
  112. def elem3(self, first, const):
  113. result = Matrix(self.matrix)
  114. size = self.size()
  115. for j in range(size[1]):
  116. result.matrix[first][j] *= const
  117. return result
  118.  
  119. def wide(self, other):
  120. width = []
  121. widthRow = []
  122. size = self.size()
  123. for i in range(size[0]):
  124. for j in range(size[1]):
  125. widthRow.append(self.matrix[i][j])
  126. widthRow.append(other[i])
  127. width.append(widthRow)
  128. widthRow = []
  129. self.matrix = width
  130. width = Matrix(width)
  131. return width
  132.  
  133. def solve(self, other):
  134. ans = []
  135. row = 0
  136. col = 0
  137. width = self.wide(other)
  138. size = width.size()
  139. main = []
  140. while row != size[0]:
  141. flag = False
  142. for j in range(col, size[1]):
  143. for i in range(row, size[0]):
  144. if width.matrix[j][i] != 0:
  145. row = i
  146. col = j
  147. main.append(j)
  148. flag = True
  149. break
  150. if flag:
  151. break
  152. else:
  153. raise MatrixError
  154. width = width.elem2(0, row)
  155. for j in range(1, size[0]):
  156. div = -1 * width.matrix[j][col] / width.matrix[0][col]
  157. width.matrix = width.elem1(j, 0, div)
  158. length = len(main)
  159. main = main[::-1]
  160. if size[0] == size[1] - 1:
  161. for i in range(size[0]):
  162. elem = width.matrix[i][main[i]]
  163. width.matrix = width.elem3(i, 1 / elem)
  164. for j in main:
  165. for i in range(length - 2, 0, -1):
  166. elem = width.matrix[length - 2][j]
  167. width.matrix = width.elem1(i, length - 1, -1 * elem)
  168. for i in range(size[0]):
  169. ans = ans.append(width.matrix[i][size[1] - 1])
  170. return ans
  171. else:
  172. raise MatrixError
  173.  
  174. exec(stdin.read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement