Python C extension to compute the permanent.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

48 líneas
1.3KB

  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. def explain_ryser(a):
  18. ''' the permanent calculated using the ryser formula. much faster than the naive approach '''
  19. n,n2=a.shape
  20. z=np.arange(n)
  21. irange=xrange(2**n)
  22. get_index=lambda i: (i & (1 << z)) != 0
  23. for q in irange:
  24. print get_index(q)
  25. #get_term=lambda index: ((-1)**np.sum(index))*np.prod(np.sum(a[index,:], 0))
  26. #indeces=map(get_index, irange)
  27. #terms=map(get_term, indeces)
  28. #return np.sum(terms)*((-1)**n)
  29. dimension=5
  30. real=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
  31. imag=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
  32. submatrix=real+1j*imag
  33. t=time.clock()
  34. for i in range(1000):
  35. perm_ryser(submatrix)
  36. print time.clock()-t
  37. t=time.clock()
  38. for i in range(1000):
  39. lib.permanent(submatrix)
  40. print time.clock()-t