returns the determinant of an nxn matrix using Laplace expansion
(self)
| 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 | |
| 427 | def square_zero_matrix(n: int) -> Matrix: |