Added determinate function (#1429) · devdave/Python_TheAlgorithms@0748313 · GitHub
Skip to content

Commit 0748313

Browse files
codecaleccclauss
authored andcommitted
Added determinate function (TheAlgorithms#1429)
* Added determinate function * Changed determinate function name * Changed instance of .det() to .determinate() * Added force_test() function * Update tests.py
1 parent 7b3d385 commit 0748313

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

linear_algebra/README.md

Lines changed: 2 additions & 1 deletion

linear_algebra/src/lib.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,33 @@ def height(self):
277277
"""
278278
return self.__height
279279

280+
def determinate(self) -> float:
281+
"""
282+
returns the determinate of an nxn matrix using Laplace expansion
283+
"""
284+
if self.__height == self.__width and self.__width >= 2:
285+
total = 0
286+
if self.__width > 2:
287+
for x in range(0, self.__width):
288+
for y in range(0, self.__height):
289+
total += (
290+
self.__matrix[x][y]
291+
* (-1) ** (x + y)
292+
* Matrix(
293+
self.__matrix[0:x] + self.__matrix[x + 1 :],
294+
self.__width - 1,
295+
self.__height - 1,
296+
).determinate()
297+
)
298+
else:
299+
return (
300+
self.__matrix[0][0] * self.__matrix[1][1]
301+
- self.__matrix[0][1] * self.__matrix[1][0]
302+
)
303+
return total
304+
else:
305+
raise Exception("matrix is not square")
306+
280307
def __mul__(self, other):
281308
"""
282309
implements the matrix-vector multiplication.

linear_algebra/src/tests.py

Lines changed: 15 additions & 1 deletion

0 commit comments

Comments
 (0)