New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypy] Fix matrix_operation.py #5808

Merged
merged 7 commits into from Nov 10, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -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")
This conversation was marked as resolved by Rohanrbharadwaj

This comment has been minimized.

@cclauss

cclauss Nov 10, 2021
Member

Could you please add a doctest that proves this TypeError will be raised? It should look something like this:

>>> add(<something_problematic_here>)
Traceback (most recent call last):
  ...
TypeError: Expected a matrix, got int/list instead

This comment has been minimized.

@Rohanrbharadwaj

Rohanrbharadwaj Nov 10, 2021
Author Contributor

Alright, on it



def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
@@ -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]]
@@ -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")
This conversation was marked as resolved by Rohanrbharadwaj

This comment has been minimized.

@cclauss

cclauss Nov 10, 2021
Member

One more doctest for this exception, please.



def minor(matrix: list[list], row: int, column: int) -> list[list]:
@@ -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]]
@@ -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(
@@ -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)
[mypy] Fix `matrix_operation.py` by Rohanrbharadwaj · Pull Request #5808 · TheAlgorithms/Python · GitHub
24 201