MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / determinant

Method determinant

linear_algebra/src/lib.py:405–424  ·  view source on GitHub ↗

returns the determinant of an nxn matrix using Laplace expansion

(self)

Source from the content-addressed store, hash-verified

403 raise Exception("Indices out of bounds")
404
405 def determinant(self) -> float:
406 """
407 returns the determinant of an nxn matrix using Laplace expansion
408 """
409 if self.__height != self.__width:
410 raise Exception("Matrix is not square")
411 if self.__height < 1:
412 raise Exception("Matrix has no element")
413 elif self.__height == 1:
414 return self.__matrix[0][0]
415 elif self.__height == 2:
416 return (
417 self.__matrix[0][0] * self.__matrix[1][1]
418 - self.__matrix[0][1] * self.__matrix[1][0]
419 )
420 else:
421 cofactor_prods = [
422 self.__matrix[0][y] * self.cofactor(0, y) for y in range(self.__width)
423 ]
424 return sum(cofactor_prods)
425
426
427def square_zero_matrix(n: int) -> Matrix:

Callers 2

test_determinantMethod · 0.95
minorMethod · 0.45

Calls 1

cofactorMethod · 0.95

Tested by 1

test_determinantMethod · 0.76