@@ -102,15 +102,15 @@ class Matrix:
102
102
103
103
def __init__ (self , rows ):
104
104
error = TypeError (
105
- "Matrices must be formed from a list of zero or more lists containing at least one and the same number of values, \
106
- each of which must be of type int or float"
105
+ "Matrices must be formed from a list of zero or more lists containing at least "
106
+ "one and the same number of values, each of which must be of type int or float. "
107
107
)
108
108
if len (rows ) != 0 :
109
109
cols = len (rows [0 ])
110
110
if cols == 0 :
111
111
raise error
112
112
for row in rows :
113
- if not len (row ) = = cols :
113
+ if len (row ) ! = cols :
114
114
raise error
115
115
for value in row :
116
116
if not isinstance (value , (int , float )):
@@ -137,9 +137,7 @@ def order(self):
137
137
138
138
@property
139
139
def is_square (self ):
140
- if self .order [0 ] == self .order [1 ]:
141
- return True
142
- return False
140
+ return self .order [0 ] == self .order [1 ]
143
141
144
142
def identity (self ):
145
143
values = [
@@ -168,9 +166,7 @@ def determinant(self):
168
166
)
169
167
170
168
def is_invertable (self ):
171
- if self .determinant ():
172
- return True
173
- return False
169
+ return bool (self .determinant ())
174
170
175
171
def get_minor (self , row , column ):
176
172
values = [
@@ -218,9 +214,8 @@ def adjugate(self):
218
214
return Matrix (values )
219
215
220
216
def inverse (self ):
221
- if not self .is_invertable ():
222
- return None
223
- return self .adjugate () * (1 / self .determinant ())
217
+ determinant = self .determinant ()
218
+ return None if not determinant else self .adjugate () * (1 / determinant )
224
219
225
220
def __repr__ (self ):
226
221
return str (self .rows )
@@ -283,14 +278,10 @@ def add_column(self, column, position=None):
283
278
def __eq__ (self , other ):
284
279
if not isinstance (other , Matrix ):
285
280
raise TypeError ("A Matrix can only be compared with another Matrix" )
286
- if self .rows == other .rows :
287
- return True
288
- return False
281
+ return self .rows == other .rows
289
282
290
283
def __ne__ (self , other ):
291
- if self == other :
292
- return False
293
- return True
284
+ return not self == other
294
285
295
286
def __neg__ (self ):
296
287
return self * - 1
@@ -316,23 +307,20 @@ def __sub__(self, other):
316
307
)
317
308
318
309
def __mul__ (self , other ):
319
- if not isinstance (other , (int , float , Matrix )):
320
- raise TypeError (
321
- "A Matrix can only be multiplied by an int, float, or another matrix"
322
- )
323
- if type (other ) in (int , float ):
310
+ if isinstance (other , (int , float )):
324
311
return Matrix ([[element * other for element in row ] for row in self .rows ])
325
- if type (other ) is Matrix :
312
+ elif isinstance (other , Matrix ) :
326
313
if self .num_columns != other .num_rows :
327
- raise ValueError (
328
- "The number of columns in the first matrix must be equal to the number of rows in the second"
329
- )
314
+ raise ValueError ("The number of columns in the first matrix must "
315
+ "be equal to the number of rows in the second" )
330
316
return Matrix (
331
317
[
332
318
[Matrix .dot_product (row , column ) for column in other .columns ()]
333
319
for row in self .rows
334
320
]
335
321
)
322
+ else :
323
+ raise TypeError ("A Matrix can only be multiplied by an int, float, or another matrix" )
336
324
337
325
def __pow__ (self , other ):
338
326
if not isinstance (other , int ):
@@ -360,5 +348,4 @@ def dot_product(cls, row, column):
360
348
if __name__ == "__main__" :
361
349
import doctest
362
350
363
- test = doctest .testmod ()
364
- print (test )
351
+ doctest .testmod ()
0 commit comments