MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / determinant

Function determinant

matrix/matrix_operation.py:125–138  ·  view source on GitHub ↗

>>> determinant([[1, 2], [3, 4]]) -2 >>> determinant([[1.5, 2.5], [3, 4]]) -1.5

(matrix: list[list[int]])

Source from the content-addressed store, hash-verified

123
124
125def determinant(matrix: list[list[int]]) -> Any:
126 """
127 >>> determinant([[1, 2], [3, 4]])
128 -2
129 >>> determinant([[1.5, 2.5], [3, 4]])
130 -1.5
131 """
132 if len(matrix) == 1:
133 return matrix[0][0]
134
135 return sum(
136 x * determinant(minor(matrix, 0, i)) * (-1) ** i
137 for i, x in enumerate(matrix[0])
138 )
139
140
141def inverse(matrix: list[list[int]]) -> list[list[float]] | None:

Callers 2

inverseFunction · 0.85
mainFunction · 0.85

Calls 1

minorFunction · 0.85

Tested by

no test coverage detected