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

47 行
1.3KB

  1. import os, sys
  2. import time
  3. import multiprocessing as mp
  4. import numpy as np
  5. import time
  6. from matplotlib import pyplot as plt
  7. from permanent import permanent
  8. def perm_ryser(a):
  9. ''' the permanent calculated using the ryser formula. much faster than the naive approach '''
  10. n,n2=a.shape
  11. z=np.arange(n)
  12. irange=xrange(2**n)
  13. get_index=lambda i: (i & (1 << z)) != 0
  14. get_term=lambda index: ((-1)**np.sum(index))*np.prod(np.sum(a[index,:], 0))
  15. indeces=map(get_index, irange)
  16. terms=map(get_term, indeces)
  17. return np.sum(terms)*((-1)**n)
  18. maxtime=1
  19. dimensions=range(1,11)
  20. for (function, label) in zip((permanent, perm_ryser), ("C", "Python")):
  21. counts=[]
  22. for dimension in dimensions:
  23. print dimension
  24. real=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
  25. imag=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
  26. submatrix=real+1j*imag
  27. t=time.clock()
  28. n=0
  29. while time.clock()-t < maxtime:
  30. for i in range(5):
  31. function(submatrix)
  32. n+=5
  33. counts.append(n)
  34. plt.plot(dimensions, counts, '.-', label=label)
  35. plt.ylabel('Number of permanents per second')
  36. plt.xlabel('Dimension')
  37. plt.xlim(min(dimensions), max(dimensions))
  38. plt.legend()
  39. plt.semilogy()
  40. plt.savefig('out.pdf')