Update matrix_class.py (#1175)

This commit is contained in:
Christian Clauss 2019-09-10 07:49:07 +02:00 committed by GitHub
parent 3c3f92db53
commit 030600f9b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -102,15 +102,15 @@ class Matrix:
def __init__(self, rows): def __init__(self, rows):
error = TypeError( error = TypeError(
"Matrices must be formed from a list of zero or more lists containing at least one and the same number of values, \ "Matrices must be formed from a list of zero or more lists containing at least "
each of which must be of type int or float" "one and the same number of values, each of which must be of type int or float."
) )
if len(rows) != 0: if len(rows) != 0:
cols = len(rows[0]) cols = len(rows[0])
if cols == 0: if cols == 0:
raise error raise error
for row in rows: for row in rows:
if not len(row) == cols: if len(row) != cols:
raise error raise error
for value in row: for value in row:
if not isinstance(value, (int, float)): if not isinstance(value, (int, float)):
@ -137,9 +137,7 @@ class Matrix:
@property @property
def is_square(self): def is_square(self):
if self.order[0] == self.order[1]: return self.order[0] == self.order[1]
return True
return False
def identity(self): def identity(self):
values = [ values = [
@ -168,9 +166,7 @@ class Matrix:
) )
def is_invertable(self): def is_invertable(self):
if self.determinant(): return bool(self.determinant())
return True
return False
def get_minor(self, row, column): def get_minor(self, row, column):
values = [ values = [
@ -218,9 +214,8 @@ class Matrix:
return Matrix(values) return Matrix(values)
def inverse(self): def inverse(self):
if not self.is_invertable(): determinant = self.determinant()
return None return None if not determinant else self.adjugate() * (1 / determinant)
return self.adjugate() * (1 / self.determinant())
def __repr__(self): def __repr__(self):
return str(self.rows) return str(self.rows)
@ -283,14 +278,10 @@ class Matrix:
def __eq__(self, other): def __eq__(self, other):
if not isinstance(other, Matrix): if not isinstance(other, Matrix):
raise TypeError("A Matrix can only be compared with another Matrix") raise TypeError("A Matrix can only be compared with another Matrix")
if self.rows == other.rows: return self.rows == other.rows
return True
return False
def __ne__(self, other): def __ne__(self, other):
if self == other: return not self == other
return False
return True
def __neg__(self): def __neg__(self):
return self * -1 return self * -1
@ -316,23 +307,20 @@ class Matrix:
) )
def __mul__(self, other): def __mul__(self, other):
if not isinstance(other, (int, float, Matrix)): if isinstance(other, (int, float)):
raise TypeError(
"A Matrix can only be multiplied by an int, float, or another matrix"
)
if type(other) in (int, float):
return Matrix([[element * other for element in row] for row in self.rows]) return Matrix([[element * other for element in row] for row in self.rows])
if type(other) is Matrix: elif isinstance(other, Matrix):
if self.num_columns != other.num_rows: if self.num_columns != other.num_rows:
raise ValueError( raise ValueError("The number of columns in the first matrix must "
"The number of columns in the first matrix must be equal to the number of rows in the second" "be equal to the number of rows in the second")
)
return Matrix( return Matrix(
[ [
[Matrix.dot_product(row, column) for column in other.columns()] [Matrix.dot_product(row, column) for column in other.columns()]
for row in self.rows for row in self.rows
] ]
) )
else:
raise TypeError("A Matrix can only be multiplied by an int, float, or another matrix")
def __pow__(self, other): def __pow__(self, other):
if not isinstance(other, int): if not isinstance(other, int):
@ -360,5 +348,4 @@ class Matrix:
if __name__ == "__main__": if __name__ == "__main__":
import doctest import doctest
test = doctest.testmod() doctest.testmod()
print(test)