Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_135/sol1.py)
* Problem 144
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_144/sol1.py)
* Problem 145
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_145/sol1.py)
* Problem 173
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py)
* Problem 174
Expand Down
33 changes: 24 additions & 9 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ def add(*matrix_s: list[list]) -> list[list]:
[[3.2, 5.4], [7, 9]]
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
[[7, 14], [12, 16]]
>>> add([3], [4, 5])
Traceback (most recent call last):
...
TypeError: Expected a matrix, got int/list instead
"""
if all(_check_not_integer(m) for m in matrix_s):
for i in matrix_s[1:]:
_verify_matrix_sizes(matrix_s[0], i)
return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]
raise TypeError("Expected a matrix, got int/list instead")
Comment thread
Rohanrbharadwaj marked this conversation as resolved.
Resolved


def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
Expand All @@ -26,16 +31,21 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
[[-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
"""
if (
_check_not_integer(matrix_a)
and _check_not_integer(matrix_b)
and _verify_matrix_sizes(matrix_a, matrix_b)
):
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
raise TypeError("Expected a matrix, got int/list instead")


def scalar_multiply(matrix: list[list], n: int) -> list[list]:
def scalar_multiply(matrix: list[list], n: int | float) -> list[list]:
"""
>>> scalar_multiply([[1,2],[3,4]],5)
[[5, 10], [15, 20]]
Expand Down Expand Up @@ -79,18 +89,23 @@ def identity(n: int) -> list[list]:
return [[int(row == column) for column in range(n)] for row in range(n)]


def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
def transpose(matrix: list[list], return_map: bool = True) -> list[list] | map[list]:
"""
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
<map object at ...
>>> transpose([[1,2],[3,4]], return_map=False)
[[1, 3], [2, 4]]
>>> transpose([1, [2, 3]])
Traceback (most recent call last):
...
TypeError: Expected a matrix, got int/list instead
"""
if _check_not_integer(matrix):
if return_map:
return map(list, zip(*matrix))
else:
return list(map(list, zip(*matrix)))
raise TypeError("Expected a matrix, got int/list instead")
Comment thread
Rohanrbharadwaj marked this conversation as resolved.
Resolved


def minor(matrix: list[list], row: int, column: int) -> list[list]:
Expand Down Expand Up @@ -118,7 +133,7 @@ def determinant(matrix: list[list]) -> int:
)


def inverse(matrix: list[list]) -> list[list]:
def inverse(matrix: list[list]) -> list[list] | None:
"""
>>> inverse([[1, 2], [3, 4]])
[[-2.0, 1.0], [1.5, -0.5]]
Expand All @@ -138,21 +153,21 @@ def inverse(matrix: list[list]) -> list[list]:
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
for row in range(len(matrix))
]
adjugate = transpose(cofactors)
adjugate = list(transpose(cofactors))
return scalar_multiply(adjugate, 1 / det)


def _check_not_integer(matrix: list[list]) -> bool:
if not isinstance(matrix, int) and not isinstance(matrix[0], int):
return True
raise TypeError("Expected a matrix, got int/list instead")
return not isinstance(matrix, int) and not isinstance(matrix[0], int)


def _shape(matrix: list[list]) -> list:
def _shape(matrix: list[list]) -> tuple[int, int]:
return 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[tuple, tuple]:
shape = _shape(matrix_a) + _shape(matrix_b)
if shape[0] != shape[3] or shape[1] != shape[2]:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ignore_missing_imports = True
install_types = True
non_interactive = True
exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)
exclude = (other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)