Added determinate function (#1429)

* Added determinate function

* Changed determinate function name

* Changed instance of .det() to .determinate()

* Added force_test() function

* Update tests.py
This commit is contained in:
Alex Veltman
2019-10-24 11:08:45 +02:00
committed by Christian Clauss
parent 7b3d385ad6
commit 07483139d1
3 changed files with 44 additions and 2 deletions

View File

@ -277,6 +277,33 @@ class Matrix(object):
"""
return self.__height
def determinate(self) -> float:
"""
returns the determinate of an nxn matrix using Laplace expansion
"""
if self.__height == self.__width and self.__width >= 2:
total = 0
if self.__width > 2:
for x in range(0, self.__width):
for y in range(0, self.__height):
total += (
self.__matrix[x][y]
* (-1) ** (x + y)
* Matrix(
self.__matrix[0:x] + self.__matrix[x + 1 :],
self.__width - 1,
self.__height - 1,
).determinate()
)
else:
return (
self.__matrix[0][0] * self.__matrix[1][1]
- self.__matrix[0][1] * self.__matrix[1][0]
)
return total
else:
raise Exception("matrix is not square")
def __mul__(self, other):
"""
implements the matrix-vector multiplication.