Created
July 13, 2023 19:20
-
-
Save hoogenm/ba8d8d5720262408734317ffe75042d5 to your computer and use it in GitHub Desktop.
Determinant using standard Python (Leibniz rule)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not for efficiency | |
def permutations(data, sign=0): | |
return ([(1, data)] | |
if len(data) == 1 else | |
[(sign * -1 if idx % 2 == 1 else sign, [value] + nested_permutation) | |
for idx, value in enumerate(data) | |
for sign, nested_permutation in permutations(data[:idx] + data[idx+1:])] | |
) | |
def determinant(m): | |
indices = range(len(m)) | |
return sum(sign * np.prod([m[i][permutation[i]] for i in indices]) | |
for sign, permutation | |
in permutations(indices)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment