Skip to content

Instantly share code, notes, and snippets.

@hoogenm
Created July 13, 2023 19:20
Show Gist options
  • Save hoogenm/ba8d8d5720262408734317ffe75042d5 to your computer and use it in GitHub Desktop.
Save hoogenm/ba8d8d5720262408734317ffe75042d5 to your computer and use it in GitHub Desktop.
Determinant using standard Python (Leibniz rule)
# 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