Skip to content

Commit

Permalink
enhance the ability of add (TheAlgorithms#2178)
Browse files Browse the repository at this point in the history
* enhance the ability of add

* Update matrix_operation.py

* Update matrix_operation.py

* Update matrix_operation.py

* Update matrix/matrix_operation.py

Co-authored-by: Christian Clauss <[email protected]>

Co-authored-by: Christian Clauss <[email protected]>
  • Loading branch information
wuyudi and cclauss authored Jul 6, 2020
1 parent 48df91d commit 367f8ce
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
from typing import List, Tuple


def add(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
def add(*matrix_s: List[list]) -> List[list]:
"""
>>> add([[1,2],[3,4]],[[2,3],[4,5]])
[[3, 5], [7, 9]]
>>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]])
[[3.2, 5.4], [7, 9]]
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
[[7, 14], [12, 16]]
"""
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
_verify_matrix_sizes(matrix_a, matrix_b)
return [[i + j for i, j in zip(m, n)] for m, n in zip(matrix_a, matrix_b)]
if all(_check_not_integer(m) for m in matrix_s):
a, *b = matrix_s
for matrix in b:
_verify_matrix_sizes(a, matrix)
return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]


def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
Expand All @@ -26,7 +30,7 @@ def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
"""
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
_verify_matrix_sizes(matrix_a, matrix_b)
return [[i - j for i, j in zip(m, n)] for m, n in zip(matrix_a, matrix_b)]
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]


def scalar_multiply(matrix: List[list], n: int) -> List[list]:
Expand Down Expand Up @@ -97,8 +101,8 @@ def minor(matrix: List[list], row: int, column: int) -> List[list]:
>>> minor([[1, 2], [3, 4]], 1, 1)
[[1]]
"""
minor = matrix[:row] + matrix[row + 1 :]
return [row[:column] + row[column + 1 :] for row in minor]
minor = matrix[:row] + matrix[row + 1:]
return [row[:column] + row[column + 1:] for row in minor]


def determinant(matrix: List[list]) -> int:
Expand Down Expand Up @@ -151,7 +155,8 @@ def _shape(matrix: List[list]) -> list:
return list((len(matrix), len(matrix[0])))


def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[list]:
def _verify_matrix_sizes(
matrix_a: List[list], matrix_b: List[list]) -> Tuple[list]:
shape = _shape(matrix_a)
shape += _shape(matrix_b)
if shape[0] != shape[2] or shape[1] != shape[3]:
Expand All @@ -165,9 +170,12 @@ def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[li
def main():
matrix_a = [[12, 10], [3, 9]]
matrix_b = [[3, 4], [7, 4]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24],
[31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(f"Add Operation, {matrix_a} + {matrix_b} = {add(matrix_a, matrix_b)} \n")
print(
f"Add Operation, {matrix_a} + {matrix_b} ="
f"{add(matrix_a, matrix_b)} \n")
print(
f"Multiply Operation, {matrix_a} * {matrix_b}",
f"= {multiply(matrix_a, matrix_b)} \n",
Expand Down

0 comments on commit 367f8ce

Please sign in to comment.