@@ -13,11 +13,16 @@ def add(*matrix_s: list[list]) -> list[list]:
1313    [[3.2, 5.4], [7, 9]] 
1414    >>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]]) 
1515    [[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 
1620    """ 
1721    if  all (_check_not_integer (m ) for  m  in  matrix_s ):
1822        for  i  in  matrix_s [1 :]:
1923            _verify_matrix_sizes (matrix_s [0 ], i )
2024        return  [[sum (t ) for  t  in  zip (* m )] for  m  in  zip (* matrix_s )]
25+     raise  TypeError ("Expected a matrix, got int/list instead" )
2126
2227
2328def  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]:
2631    [[-1, -1], [-1, -1]] 
2732    >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]]) 
2833    [[-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 
2938    """ 
3039    if  (
3140        _check_not_integer (matrix_a )
3241        and  _check_not_integer (matrix_b )
3342        and  _verify_matrix_sizes (matrix_a , matrix_b )
3443    ):
3544        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" )
3646
3747
38- def  scalar_multiply (matrix : list [list ], n : int ) ->  list [list ]:
48+ def  scalar_multiply (matrix : list [list ], n : int   |   float ) ->  list [list ]:
3949    """ 
4050    >>> scalar_multiply([[1,2],[3,4]],5) 
4151    [[5, 10], [15, 20]] 
@@ -79,18 +89,23 @@ def identity(n: int) -> list[list]:
7989    return  [[int (row  ==  column ) for  column  in  range (n )] for  row  in  range (n )]
8090
8191
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 ] :
8393    """ 
8494    >>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS 
8595    <map object at ... 
8696    >>> transpose([[1,2],[3,4]], return_map=False) 
8797    [[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 
88102    """ 
89103    if  _check_not_integer (matrix ):
90104        if  return_map :
91105            return  map (list , zip (* matrix ))
92106        else :
93107            return  list (map (list , zip (* matrix )))
108+     raise  TypeError ("Expected a matrix, got int/list instead" )
94109
95110
96111def  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 ]) ->  list [list ]  |   None :
122137    """ 
123138    >>> inverse([[1, 2], [3, 4]]) 
124139    [[-2.0, 1.0], [1.5, -0.5]] 
@@ -138,21 +153,21 @@ def inverse(matrix: list[list]) -> list[list]:
138153        [x  *  (- 1 ) **  (row  +  col ) for  col , x  in  enumerate (matrix_minor [row ])]
139154        for  row  in  range (len (matrix ))
140155    ]
141-     adjugate  =  transpose (cofactors )
156+     adjugate  =  list ( transpose (cofactors ) )
142157    return  scalar_multiply (adjugate , 1  /  det )
143158
144159
145160def  _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 )
149162
150163
151- def  _shape (matrix : list [list ]) ->  list :
164+ def  _shape (matrix : list [list ]) ->  tuple [ int ,  int ] :
152165    return  len (matrix ), len (matrix [0 ])
153166
154167
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 ]:
156171    shape  =  _shape (matrix_a ) +  _shape (matrix_b )
157172    if  shape [0 ] !=  shape [3 ] or  shape [1 ] !=  shape [2 ]:
158173        raise  ValueError (
0 commit comments