Created
June 19, 2015 23:03
-
-
Save alexflint/0511771532ce45146dcb to your computer and use it in GitHub Desktop.
Compute a symbolic determinant in python
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is this?