#13 · Linear Algebra · Hard
⊣ Solve on deep-ml.comCalculate the determinant of a 4x4 matrix using Laplace's expansion (cofactor expansion).
def determinant_4x4(matrix: list[list[int|float]]) -> float:
def det(m):
n = len(m)
if n == 1:
return m[0][0]
if n == 2:
return m[0][0] * m[1][1] - m[0][1] * m[1][0]
result = 0
for col in range(n):
# Build minor by removing row 0 and column col
minor = []
for row in range(1, n):
minor.append([m[row][c] for c in range(n) if c != col])
sign = (-1) ** col
result += sign * m[0][col] * det(minor)
return result
return float(det(matrix))