mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
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:

committed by
Christian Clauss

parent
7b3d385ad6
commit
07483139d1
@ -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.
|
||||
|
Reference in New Issue
Block a user