implements matrix addition.
(self, other: Matrix)
| 285 | return ans |
| 286 | |
| 287 | def __add__(self, other: Matrix) -> Matrix: |
| 288 | """ |
| 289 | implements matrix addition. |
| 290 | """ |
| 291 | if self.__width == other.width() and self.__height == other.height(): |
| 292 | matrix = [] |
| 293 | for i in range(self.__height): |
| 294 | row = [ |
| 295 | self.__matrix[i][j] + other.component(i, j) |
| 296 | for j in range(self.__width) |
| 297 | ] |
| 298 | matrix.append(row) |
| 299 | return Matrix(matrix, self.__width, self.__height) |
| 300 | else: |
| 301 | raise Exception("matrix must have the same dimension!") |
| 302 | |
| 303 | def __sub__(self, other: Matrix) -> Matrix: |
| 304 | """ |