Python C extension to compute the permanent.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

43 lines
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")