@@ -13,11 +13,16 @@ def add(*matrix_s: list[list]) -> list[list]:
13
13
[[3.2, 5.4], [7, 9]]
14
14
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
15
15
[[7, 14], [12, 16]]
16
+ >>> add([3], [4, 5])
17
+ Traceback (most recent call last):
18
+ ...
19
+ TypeError: Expected a matrix, got int/list instead
16
20
"""
17
21
if all (_check_not_integer (m ) for m in matrix_s ):
18
22
for i in matrix_s [1 :]:
19
23
_verify_matrix_sizes (matrix_s [0 ], i )
20
24
return [[sum (t ) for t in zip (* m )] for m in zip (* matrix_s )]
25
+ raise TypeError ("Expected a matrix, got int/list instead" )
21
26
22
27
23
28
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]:
26
31
[[-1, -1], [-1, -1]]
27
32
>>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
28
33
[[-1, -0.5], [-1, -1.5]]
34
+ >>> subtract([3], [4, 5])
35
+ Traceback (most recent call last):
36
+ ...
37
+ TypeError: Expected a matrix, got int/list instead
29
38
"""
30
39
if (
31
40
_check_not_integer (matrix_a )
32
41
and _check_not_integer (matrix_b )
33
42
and _verify_matrix_sizes (matrix_a , matrix_b )
34
43
):
35
44
return [[i - j for i , j in zip (* m )] for m in zip (matrix_a , matrix_b )]
45
+ raise TypeError ("Expected a matrix, got int/list instead" )
36
46
37
47
38
- def scalar_multiply (matrix : list [list ], n : int ) -> list [list ]:
48
+ def scalar_multiply (matrix : list [list ], n : int | float ) -> list [list ]:
39
49
"""
40
50
>>> scalar_multiply([[1,2],[3,4]],5)
41
51
[[5, 10], [15, 20]]
@@ -79,18 +89,23 @@ def identity(n: int) -> list[list]:
79
89
return [[int (row == column ) for column in range (n )] for row in range (n )]
80
90
81
91
82
- def transpose (matrix : list [list ], return_map : bool = True ) -> list [list ]:
92
+ def transpose (matrix : list [list ], return_map : bool = True ) -> list [list ] | map [ list ] :
83
93
"""
84
94
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
85
95
<map object at ...
86
96
>>> transpose([[1,2],[3,4]], return_map=False)
87
97
[[1, 3], [2, 4]]
98
+ >>> transpose([1, [2, 3]])
99
+ Traceback (most recent call last):
100
+ ...
101
+ TypeError: Expected a matrix, got int/list instead
88
102
"""
89
103
if _check_not_integer (matrix ):
90
104
if return_map :
91
105
return map (list , zip (* matrix ))
92
106
else :
93
107
return list (map (list , zip (* matrix )))
108
+ raise TypeError ("Expected a matrix, got int/list instead" )
94
109
95
110
96
111
def minor (matrix : list [list ], row : int , column : int ) -> list [list ]:
@@ -118,7 +133,7 @@ def determinant(matrix: list[list]) -> int:
118
133
)
119
134
120
135
121
- def inverse (matrix : list [list ]) -> list [list ]:
136
+ def inverse (matrix : list [list ]) -> list [list ] | None :
122
137
"""
123
138
>>> inverse([[1, 2], [3, 4]])
124
139
[[-2.0, 1.0], [1.5, -0.5]]
@@ -138,21 +153,21 @@ def inverse(matrix: list[list]) -> list[list]:
138
153
[x * (- 1 ) ** (row + col ) for col , x in enumerate (matrix_minor [row ])]
139
154
for row in range (len (matrix ))
140
155
]
141
- adjugate = transpose (cofactors )
156
+ adjugate = list ( transpose (cofactors ) )
142
157
return scalar_multiply (adjugate , 1 / det )
143
158
144
159
145
160
def _check_not_integer (matrix : list [list ]) -> bool :
146
- if not isinstance (matrix , int ) and not isinstance (matrix [0 ], int ):
147
- return True
148
- raise TypeError ("Expected a matrix, got int/list instead" )
161
+ return not isinstance (matrix , int ) and not isinstance (matrix [0 ], int )
149
162
150
163
151
- def _shape (matrix : list [list ]) -> list :
164
+ def _shape (matrix : list [list ]) -> tuple [ int , int ] :
152
165
return len (matrix ), len (matrix [0 ])
153
166
154
167
155
- def _verify_matrix_sizes (matrix_a : list [list ], matrix_b : list [list ]) -> tuple [list ]:
168
+ def _verify_matrix_sizes (
169
+ matrix_a : list [list ], matrix_b : list [list ]
170
+ ) -> tuple [tuple , tuple ]:
156
171
shape = _shape (matrix_a ) + _shape (matrix_b )
157
172
if shape [0 ] != shape [3 ] or shape [1 ] != shape [2 ]:
158
173
raise ValueError (
0 commit comments