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

Function multiply

matrix/matrix_operation.py:60–80  ·  view source on GitHub ↗

>>> multiply([[1,2],[3,4]],[[5,5],[7,5]]) [[19, 15], [43, 35]] >>> multiply([[1,2.5],[3,4.5]],[[5,5],[7,5]]) [[22.5, 17.5], [46.5, 37.5]] >>> multiply([[1, 2, 3]], [[2], [3], [4]]) [[20]]

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

Source from the content-addressed store, hash-verified

58
59
60def multiply(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[int]]:
61 """
62 >>> multiply([[1,2],[3,4]],[[5,5],[7,5]])
63 [[19, 15], [43, 35]]
64 >>> multiply([[1,2.5],[3,4.5]],[[5,5],[7,5]])
65 [[22.5, 17.5], [46.5, 37.5]]
66 >>> multiply([[1, 2, 3]], [[2], [3], [4]])
67 [[20]]
68 """
69 if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
70 rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
71
72 if cols[0] != rows[1]:
73 msg = (
74 "Cannot multiply matrix of dimensions "
75 f"({rows[0]},{cols[0]}) and ({rows[1]},{cols[1]})"
76 )
77 raise ValueError(msg)
78 return [
79 [sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] for i in matrix_a
80 ]
81
82
83def identity(n: int) -> list[list[int]]:

Callers 2

mainFunction · 0.70
median_filterFunction · 0.50

Calls 2

_check_not_integerFunction · 0.85
_verify_matrix_sizesFunction · 0.85

Tested by

no test coverage detected