MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / __add__

Method __add__

linear_algebra/src/lib.py:287–301  ·  view source on GitHub ↗

implements matrix addition.

(self, other: Matrix)

Source from the content-addressed store, hash-verified

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 """

Callers

nothing calls this directly

Calls 5

widthMethod · 0.80
heightMethod · 0.80
MatrixClass · 0.70
componentMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected