Skip to content

Instantly share code, notes, and snippets.

@stevenRush
Created May 25, 2014 08:33
Show Gist options
  • Save stevenRush/420e5c8095ec59533d39 to your computer and use it in GitHub Desktop.
Save stevenRush/420e5c8095ec59533d39 to your computer and use it in GitHub Desktop.
Permanent calcucator
from operator import mul
from math import fsum
from functools import reduce
from itertools import permutations, combinations
def prod(lst):
return reduce(mul, lst, 1)
def perm_sq(a):
n = len(a)
r = range(n)
s = permutations(r)
return fsum(prod(a[i][sigma[i]] for i in r) for sigma in s)
def perm_rect(a):
n = len(a[0])
m = len(a)
res = 0
for c in combinations(range(n), m):
temp_matrix = []
for i in range(m):
temp_matrix.append([])
for j in range(m):
temp_matrix[-1].append(a[i][c[j]])
res += perm_sq(temp_matrix)
return res
if __name__ == '__main__':
a = [[1, 0, 0, 1, 1, 0, 1, 0, 1],
[1, 1, 0, 0, 1, 1, 0, 0, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 0]]
print(perm_rect(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment