Skip to content

Commit

Permalink
Update matrix_class.py (TheAlgorithms#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss authored Sep 10, 2019
1 parent 3c3f92d commit 030600f
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ class Matrix:

def __init__(self, rows):
error = TypeError(
"Matrices must be formed from a list of zero or more lists containing at least one and the same number of values, \
each of which must be of type int or float"
"Matrices must be formed from a list of zero or more lists containing at least "
"one and the same number of values, each of which must be of type int or float."
)
if len(rows) != 0:
cols = len(rows[0])
if cols == 0:
raise error
for row in rows:
if not len(row) == cols:
if len(row) != cols:
raise error
for value in row:
if not isinstance(value, (int, float)):
Expand All @@ -137,9 +137,7 @@ def order(self):

@property
def is_square(self):
if self.order[0] == self.order[1]:
return True
return False
return self.order[0] == self.order[1]

def identity(self):
values = [
Expand Down Expand Up @@ -168,9 +166,7 @@ def determinant(self):
)

def is_invertable(self):
if self.determinant():
return True
return False
return bool(self.determinant())

def get_minor(self, row, column):
values = [
Expand Down Expand Up @@ -218,9 +214,8 @@ def adjugate(self):
return Matrix(values)

def inverse(self):
if not self.is_invertable():
return None
return self.adjugate() * (1 / self.determinant())
determinant = self.determinant()
return None if not determinant else self.adjugate() * (1 / determinant)

def __repr__(self):
return str(self.rows)
Expand Down Expand Up @@ -283,14 +278,10 @@ def add_column(self, column, position=None):
def __eq__(self, other):
if not isinstance(other, Matrix):
raise TypeError("A Matrix can only be compared with another Matrix")
if self.rows == other.rows:
return True
return False
return self.rows == other.rows

def __ne__(self, other):
if self == other:
return False
return True
return not self == other

def __neg__(self):
return self * -1
Expand All @@ -316,23 +307,20 @@ def __sub__(self, other):
)

def __mul__(self, other):
if not isinstance(other, (int, float, Matrix)):
raise TypeError(
"A Matrix can only be multiplied by an int, float, or another matrix"
)
if type(other) in (int, float):
if isinstance(other, (int, float)):
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:
raise ValueError(
"The number of columns in the first matrix must be equal to the number of rows in the second"
)
raise ValueError("The number of columns in the first matrix must "
"be equal to the number of rows in the second")
return Matrix(
[
[Matrix.dot_product(row, column) for column in other.columns()]
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):
if not isinstance(other, int):
Expand Down Expand Up @@ -360,5 +348,4 @@ def dot_product(cls, row, column):
if __name__ == "__main__":
import doctest

test = doctest.testmod()
print(test)
doctest.testmod()

0 comments on commit 030600f

Please sign in to comment.