Skip to content

Commit 92cc003

Browse files
committed
Fix mypy matrix/matrix_operation.py
1 parent cbe4d5f commit 92cc003

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

matrix/matrix_operation.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44

55
from __future__ import annotations
66

7+
from collections.abc import Iterable
8+
from typing import Optional
9+
710

811
def add(*matrix_s: list[list]) -> list[list]:
912
"""
13+
>>> add([])
14+
[[]]
15+
>>> add([[]])
16+
[[]]
17+
>>> add([[1, 2]])
18+
[[1, 2]]
1019
>>> add([[1,2],[3,4]],[[2,3],[4,5]])
1120
[[3, 5], [7, 9]]
1221
>>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]])
1322
[[3.2, 5.4], [7, 9]]
1423
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
1524
[[7, 14], [12, 16]]
1625
"""
17-
if all(_check_not_integer(m) for m in matrix_s):
26+
if matrix_s and matrix_s[0] and all(_check_not_integer(m) for m in matrix_s):
1827
for i in matrix_s[1:]:
1928
_verify_matrix_sizes(matrix_s[0], i)
2029
return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]
30+
else:
31+
return [[]]
2132

2233

2334
def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
@@ -33,9 +44,11 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
3344
and _verify_matrix_sizes(matrix_a, matrix_b)
3445
):
3546
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
47+
else:
48+
return [[]]
3649

3750

38-
def scalar_multiply(matrix: list[list], n: int) -> list[list]:
51+
def scalar_multiply(matrix: Iterable[list], n: float) -> list[list]:
3952
"""
4053
>>> scalar_multiply([[1,2],[3,4]],5)
4154
[[5, 10], [15, 20]]
@@ -79,7 +92,7 @@ def identity(n: int) -> list[list]:
7992
return [[int(row == column) for column in range(n)] for row in range(n)]
8093

8194

82-
def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
95+
def transpose(matrix: list[list], return_map: bool = True) -> Iterable[list]:
8396
"""
8497
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
8598
<map object at ...
@@ -91,6 +104,8 @@ def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
91104
return map(list, zip(*matrix))
92105
else:
93106
return list(map(list, zip(*matrix)))
107+
else:
108+
raise TypeError("_check_not_integer() failed")
94109

95110

96111
def minor(matrix: list[list], row: int, column: int) -> list[list]:
@@ -118,7 +133,7 @@ def determinant(matrix: list[list]) -> int:
118133
)
119134

120135

121-
def inverse(matrix: list[list]) -> list[list]:
136+
def inverse(matrix: list[list]) -> Optional[list[list]]:
122137
"""
123138
>>> inverse([[1, 2], [3, 4]])
124139
[[-2.0, 1.0], [1.5, -0.5]]
@@ -148,11 +163,13 @@ def _check_not_integer(matrix: list[list]) -> bool:
148163
raise TypeError("Expected a matrix, got int/list instead")
149164

150165

151-
def _shape(matrix: list[list]) -> list:
166+
def _shape(matrix: list[list]) -> tuple[int, int]:
152167
return len(matrix), len(matrix[0])
153168

154169

155-
def _verify_matrix_sizes(matrix_a: list[list], matrix_b: list[list]) -> tuple[list]:
170+
def _verify_matrix_sizes(
171+
matrix_a: list[list], matrix_b: list[list]
172+
) -> tuple[tuple[int, int], tuple[int, int]]:
156173
shape = _shape(matrix_a) + _shape(matrix_b)
157174
if shape[0] != shape[3] or shape[1] != shape[2]:
158175
raise ValueError(

mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
ignore_missing_imports = True
33

44
; FIXME: #4052 fix mypy errors in the exclude directories and remove them below
5-
exclude = (data_structures|dynamic_programming|graphs|maths|matrix|other|project_euler|searches|strings*)/$
5+
exclude = (data_structures|dynamic_programming|graphs|maths|other|project_euler|searches|strings*)/$

0 commit comments

Comments
 (0)