Skip to content

Instantly share code, notes, and snippets.

@alexflint
Created June 19, 2015 23:03
Show Gist options
  • Save alexflint/0511771532ce45146dcb to your computer and use it in GitHub Desktop.
Save alexflint/0511771532ce45146dcb to your computer and use it in GitHub Desktop.
Compute a symbolic determinant in python
import functools
import operator
def permutations_impl(xs):
if len(xs) == 1:
yield xs, 1
else:
for i in range(len(xs)):
xs[0], xs[i] = xs[i], xs[0]
for subperm, subsign in permutations_impl(xs[1:]):
yield [xs[0]] + subperm, subsign * (1 if i == 0 else -1)
xs[0], xs[i] = xs[i], xs[0]
def permutations(xs):
xs_copy = list(xs)
return permutations_impl(xs_copy)
def symbolic_determinant(x):
v = 0.
for p, s in permutations(range(len(x))):
v += s * functools.reduce(operator.mul, [row[pi] for row, pi in zip(x, p)])
return v
@Ikhwansong
Copy link

what is this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment