Python C extension to compute the permanent.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

36 行
947B

  1. import os, sys
  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=5
  18. real=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
  19. imag=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
  20. submatrix=real+1j*imag
  21. print lib.permanent(submatrix), perm_ryser(submatrix)
  22. sys.exit(0)
  23. t=time.clock()
  24. for i in range(1000):
  25. perm_ryser(submatrix)
  26. print time.clock()-t
  27. t=time.clock()
  28. for i in range(1000):
  29. lib.permanent(submatrix)
  30. print time.clock()-t