Skip to content

Commit 709fda0

Browse files
cclaussstokhos
authored andcommitted
Update matrix_class.py (TheAlgorithms#1175)
1 parent 09687af commit 709fda0

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

matrix/matrix_class.py

+16-29
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ class Matrix:
102102

103103
def __init__(self, rows):
104104
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."
107107
)
108108
if len(rows) != 0:
109109
cols = len(rows[0])
110110
if cols == 0:
111111
raise error
112112
for row in rows:
113-
if not len(row) == cols:
113+
if len(row) != cols:
114114
raise error
115115
for value in row:
116116
if not isinstance(value, (int, float)):
@@ -137,9 +137,7 @@ def order(self):
137137

138138
@property
139139
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]
143141

144142
def identity(self):
145143
values = [
@@ -168,9 +166,7 @@ def determinant(self):
168166
)
169167

170168
def is_invertable(self):
171-
if self.determinant():
172-
return True
173-
return False
169+
return bool(self.determinant())
174170

175171
def get_minor(self, row, column):
176172
values = [
@@ -218,9 +214,8 @@ def adjugate(self):
218214
return Matrix(values)
219215

220216
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)
224219

225220
def __repr__(self):
226221
return str(self.rows)
@@ -283,14 +278,10 @@ def add_column(self, column, position=None):
283278
def __eq__(self, other):
284279
if not isinstance(other, Matrix):
285280
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
289282

290283
def __ne__(self, other):
291-
if self == other:
292-
return False
293-
return True
284+
return not self == other
294285

295286
def __neg__(self):
296287
return self * -1
@@ -316,23 +307,20 @@ def __sub__(self, other):
316307
)
317308

318309
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)):
324311
return Matrix([[element * other for element in row] for row in self.rows])
325-
if type(other) is Matrix:
312+
elif isinstance(other, Matrix):
326313
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")
330316
return Matrix(
331317
[
332318
[Matrix.dot_product(row, column) for column in other.columns()]
333319
for row in self.rows
334320
]
335321
)
322+
else:
323+
raise TypeError("A Matrix can only be multiplied by an int, float, or another matrix")
336324

337325
def __pow__(self, other):
338326
if not isinstance(other, int):
@@ -360,5 +348,4 @@ def dot_product(cls, row, column):
360348
if __name__ == "__main__":
361349
import doctest
362350

363-
test = doctest.testmod()
364-
print(test)
351+
doctest.testmod()

0 commit comments

Comments
 (0)