Python C extension to compute the permanent.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

43 行
1.2KB

  1. import os, sys
  2. import numpy as np
  3. import time
  4. from matplotlib import pyplot as plt
  5. from permanent import permanent
  6. import itertools as it
  7. def permanent(a):
  8. """ Slow way to compute the permanent """
  9. r = range(len(a))
  10. return sum([np.prod(a[r, p]) for p in it.permutations(r)])
  11. if __name__ == '__main__':
  12. maxtime=1
  13. dimensions=range(1,11)
  14. for (function, label) in zip((permanent, perm_ryser), ("C", "Python")):
  15. counts=[]
  16. for dimension in dimensions:
  17. print dimension
  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. t=time.clock()
  22. n=0
  23. while time.clock()-t < maxtime:
  24. for i in range(5):
  25. function(submatrix)
  26. n+=5
  27. counts.append(n)
  28. plt.plot(dimensions, counts, ".-", label=label)
  29. plt.ylabel("Number of permanents per second")
  30. plt.xlabel("Dimension")
  31. plt.xlim(min(dimensions), max(dimensions))
  32. plt.legend()
  33. plt.semilogy()
  34. plt.savefig("out.pdf")