3
3
"""
4
4
5
5
from __future__ import annotations
6
+ from typing import Optional
6
7
7
8
8
9
def add (* matrix_s : list [list ]) -> list [list ]:
@@ -18,6 +19,7 @@ def add(*matrix_s: list[list]) -> list[list]:
18
19
for i in matrix_s [1 :]:
19
20
_verify_matrix_sizes (matrix_s [0 ], i )
20
21
return [[sum (t ) for t in zip (* m )] for m in zip (* matrix_s )]
22
+ raise TypeError ("Expected a matrix, got int/list instead" )
21
23
22
24
23
25
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]:
33
35
and _verify_matrix_sizes (matrix_a , matrix_b )
34
36
):
35
37
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" )
36
39
37
40
38
- def scalar_multiply (matrix : list [list ], n : int ) -> list [list ]:
41
+ def scalar_multiply (matrix : list [list ], n : int | float ) -> list [list ]:
39
42
"""
40
43
>>> scalar_multiply([[1,2],[3,4]],5)
41
44
[[5, 10], [15, 20]]
@@ -79,7 +82,7 @@ def identity(n: int) -> list[list]:
79
82
return [[int (row == column ) for column in range (n )] for row in range (n )]
80
83
81
84
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 ] :
83
86
"""
84
87
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
85
88
<map object at ...
@@ -91,7 +94,7 @@ def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
91
94
return map (list , zip (* matrix ))
92
95
else :
93
96
return list (map (list , zip (* matrix )))
94
-
97
+ raise TypeError ( "Expected a matrix, got int/list instead" )
95
98
96
99
def minor (matrix : list [list ], row : int , column : int ) -> list [list ]:
97
100
"""
@@ -118,7 +121,7 @@ def determinant(matrix: list[list]) -> int:
118
121
)
119
122
120
123
121
- def inverse (matrix : list [list ]) -> list [list ]:
124
+ def inverse (matrix : list [list ]) -> Optional [ list [list ] ]:
122
125
"""
123
126
>>> inverse([[1, 2], [3, 4]])
124
127
[[-2.0, 1.0], [1.5, -0.5]]
@@ -138,21 +141,21 @@ def inverse(matrix: list[list]) -> list[list]:
138
141
[x * (- 1 ) ** (row + col ) for col , x in enumerate (matrix_minor [row ])]
139
142
for row in range (len (matrix ))
140
143
]
141
- adjugate = transpose (cofactors )
144
+ adjugate = list ( transpose (cofactors ) )
142
145
return scalar_multiply (adjugate , 1 / det )
143
146
144
147
145
148
def _check_not_integer (matrix : list [list ]) -> bool :
146
149
if not isinstance (matrix , int ) and not isinstance (matrix [0 ], int ):
147
150
return True
148
- raise TypeError ( "Expected a matrix, got int/list instead" )
151
+ return False
149
152
150
153
151
- def _shape (matrix : list [list ]) -> list :
154
+ def _shape (matrix : list [list ]) -> tuple [ int , int ] :
152
155
return len (matrix ), len (matrix [0 ])
153
156
154
157
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 ]:
156
159
shape = _shape (matrix_a ) + _shape (matrix_b )
157
160
if shape [0 ] != shape [3 ] or shape [1 ] != shape [2 ]:
158
161
raise ValueError (
0 commit comments