Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +159 to 160
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't lines 159 and 160 need to have identical indentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E125 continuation line with same indent as next logical line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 159

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