4
4
5
5
from __future__ import annotations
6
6
7
+ from collections .abc import Iterable
8
+ from typing import Optional
9
+
7
10
8
11
def add (* matrix_s : list [list ]) -> list [list ]:
9
12
"""
13
+ >>> add([])
14
+ [[]]
15
+ >>> add([[]])
16
+ [[]]
17
+ >>> add([[1, 2]])
18
+ [[1, 2]]
10
19
>>> add([[1,2],[3,4]],[[2,3],[4,5]])
11
20
[[3, 5], [7, 9]]
12
21
>>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]])
13
22
[[3.2, 5.4], [7, 9]]
14
23
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
15
24
[[7, 14], [12, 16]]
16
25
"""
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 ):
18
27
for i in matrix_s [1 :]:
19
28
_verify_matrix_sizes (matrix_s [0 ], i )
20
29
return [[sum (t ) for t in zip (* m )] for m in zip (* matrix_s )]
30
+ else :
31
+ return [[]]
21
32
22
33
23
34
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]:
33
44
and _verify_matrix_sizes (matrix_a , matrix_b )
34
45
):
35
46
return [[i - j for i , j in zip (* m )] for m in zip (matrix_a , matrix_b )]
47
+ else :
48
+ return [[]]
36
49
37
50
38
- def scalar_multiply (matrix : list [list ], n : int ) -> list [list ]:
51
+ def scalar_multiply (matrix : Iterable [list ], n : float ) -> list [list ]:
39
52
"""
40
53
>>> scalar_multiply([[1,2],[3,4]],5)
41
54
[[5, 10], [15, 20]]
@@ -79,7 +92,7 @@ def identity(n: int) -> list[list]:
79
92
return [[int (row == column ) for column in range (n )] for row in range (n )]
80
93
81
94
82
- def transpose (matrix : list [list ], return_map : bool = True ) -> list [list ]:
95
+ def transpose (matrix : list [list ], return_map : bool = True ) -> Iterable [list ]:
83
96
"""
84
97
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
85
98
<map object at ...
@@ -91,6 +104,8 @@ def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
91
104
return map (list , zip (* matrix ))
92
105
else :
93
106
return list (map (list , zip (* matrix )))
107
+ else :
108
+ raise TypeError ("_check_not_integer() failed" )
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 ]) -> Optional [ list [list ] ]:
122
137
"""
123
138
>>> inverse([[1, 2], [3, 4]])
124
139
[[-2.0, 1.0], [1.5, -0.5]]
@@ -148,11 +163,13 @@ def _check_not_integer(matrix: list[list]) -> bool:
148
163
raise TypeError ("Expected a matrix, got int/list instead" )
149
164
150
165
151
- def _shape (matrix : list [list ]) -> list :
166
+ def _shape (matrix : list [list ]) -> tuple [ int , int ] :
152
167
return len (matrix ), len (matrix [0 ])
153
168
154
169
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 ]]:
156
173
shape = _shape (matrix_a ) + _shape (matrix_b )
157
174
if shape [0 ] != shape [3 ] or shape [1 ] != shape [2 ]:
158
175
raise ValueError (
0 commit comments