mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
enhance the ability of add (#2178)
* 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 <cclauss@me.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
48df91d48b
commit
367f8ceddd
@ -5,16 +5,20 @@ Functions for 2D matrix operations
|
||||
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]:
|
||||
@ -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]:
|
||||
@ -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:
|
||||
@ -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]:
|
||||
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user