Python C extension to compute the permanent.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run-tests.py 826B

9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import time
  3. import multiprocessing as mp
  4. import numpy as np
  5. import lib
  6. import time
  7. def perm_ryser(a):
  8. ''' the permanent calculated using the ryser formula. much faster than the naive approach '''
  9. n,n2=a.shape
  10. z=np.arange(n)
  11. irange=xrange(2**n)
  12. get_index=lambda i: (i & (1 << z)) != 0
  13. get_term=lambda index: ((-1)**np.sum(index))*np.prod(np.sum(a[index,:], 0))
  14. indeces=map(get_index, irange)
  15. terms=map(get_term, indeces)
  16. return np.sum(terms)*((-1)**n)
  17. dimension=8
  18. real=np.ones((dimension, dimension))
  19. imag=np.ones((dimension, dimension))*0
  20. submatrix=real+1j*imag
  21. submatrix[0,0]*=2
  22. print submatrix
  23. t=time.clock()
  24. for i in range(10000):
  25. lib.permanent(submatrix)
  26. print time.clock()-t
  27. #t=time.clock()
  28. #for i in range(4000):
  29. #perm_ryser(submatrix)
  30. #print time.clock()-t