Skip to content

Commit d84fbca

Browse files
Update matrix_operation.py
1 parent d6a1623 commit d84fbca

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

matrix/matrix_operation.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from __future__ import annotations
6+
from typing import Optional
67

78

89
def add(*matrix_s: list[list]) -> list[list]:
@@ -18,6 +19,7 @@ def add(*matrix_s: list[list]) -> list[list]:
1819
for i in matrix_s[1:]:
1920
_verify_matrix_sizes(matrix_s[0], i)
2021
return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]
22+
raise TypeError("Expected a matrix, got int/list instead")
2123

2224

2325
def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
@@ -33,9 +35,10 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
3335
and _verify_matrix_sizes(matrix_a, matrix_b)
3436
):
3537
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
38+
raise TypeError("Expected a matrix, got int/list instead")
3639

3740

38-
def scalar_multiply(matrix: list[list], n: int) -> list[list]:
41+
def scalar_multiply(matrix: list[list], n: int | float) -> list[list]:
3942
"""
4043
>>> scalar_multiply([[1,2],[3,4]],5)
4144
[[5, 10], [15, 20]]
@@ -79,7 +82,7 @@ def identity(n: int) -> list[list]:
7982
return [[int(row == column) for column in range(n)] for row in range(n)]
8083

8184

82-
def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
85+
def transpose(matrix: list[list], return_map: bool = True) -> list[list] | map[list]:
8386
"""
8487
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
8588
<map object at ...
@@ -91,7 +94,7 @@ def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
9194
return map(list, zip(*matrix))
9295
else:
9396
return list(map(list, zip(*matrix)))
94-
97+
raise TypeError("Expected a matrix, got int/list instead")
9598

9699
def minor(matrix: list[list], row: int, column: int) -> list[list]:
97100
"""
@@ -118,7 +121,7 @@ def determinant(matrix: list[list]) -> int:
118121
)
119122

120123

121-
def inverse(matrix: list[list]) -> list[list]:
124+
def inverse(matrix: list[list]) -> Optional[list[list]]:
122125
"""
123126
>>> inverse([[1, 2], [3, 4]])
124127
[[-2.0, 1.0], [1.5, -0.5]]
@@ -138,21 +141,21 @@ def inverse(matrix: list[list]) -> list[list]:
138141
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
139142
for row in range(len(matrix))
140143
]
141-
adjugate = transpose(cofactors)
144+
adjugate = list(transpose(cofactors))
142145
return scalar_multiply(adjugate, 1 / det)
143146

144147

145148
def _check_not_integer(matrix: list[list]) -> bool:
146149
if not isinstance(matrix, int) and not isinstance(matrix[0], int):
147150
return True
148-
raise TypeError("Expected a matrix, got int/list instead")
151+
return False
149152

150153

151-
def _shape(matrix: list[list]) -> list:
154+
def _shape(matrix: list[list]) -> tuple[int, int]:
152155
return len(matrix), len(matrix[0])
153156

154157

155-
def _verify_matrix_sizes(matrix_a: list[list], matrix_b: list[list]) -> tuple[list]:
158+
def _verify_matrix_sizes(matrix_a: list[list], matrix_b: list[list]) -> tuple[tuple, tuple]:
156159
shape = _shape(matrix_a) + _shape(matrix_b)
157160
if shape[0] != shape[3] or shape[1] != shape[2]:
158161
raise ValueError(

0 commit comments

Comments
 (0)