MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / subtract

Function subtract

matrix/matrix_operation.py:30–47  ·  view source on GitHub ↗

>>> subtract([[1,2],[3,4]],[[2,3],[4,5]]) [[-1, -1], [-1, -1]] >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]]) [[-1, -0.5], [-1, -1.5]] >>> subtract([3], [4, 5]) Traceback (most recent call last): ... TypeError: Expected a matrix, got int/list instead

(matrix_a: list[list[int]], matrix_b: list[list[int]])

Source from the content-addressed store, hash-verified

28
29
30def subtract(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[int]]:
31 """
32 >>> subtract([[1,2],[3,4]],[[2,3],[4,5]])
33 [[-1, -1], [-1, -1]]
34 >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
35 [[-1, -0.5], [-1, -1.5]]
36 >>> subtract([3], [4, 5])
37 Traceback (most recent call last):
38 ...
39 TypeError: Expected a matrix, got int/list instead
40 """
41 if (
42 _check_not_integer(matrix_a)
43 and _check_not_integer(matrix_b)
44 and _verify_matrix_sizes(matrix_a, matrix_b)
45 ):
46 return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
47 raise TypeError("Expected a matrix, got int/list instead")
48
49
50def scalar_multiply(matrix: list[list[int]], n: float) -> list[list[float]]:

Callers

nothing calls this directly

Calls 2

_check_not_integerFunction · 0.85
_verify_matrix_sizesFunction · 0.85

Tested by

no test coverage detected