loggamma.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """Precompute series coefficients for log-Gamma."""
  2. from __future__ import division, print_function, absolute_import
  3. try:
  4. import mpmath
  5. except ImportError:
  6. pass
  7. def stirling_series(N):
  8. coeffs = []
  9. with mpmath.workdps(100):
  10. for n in range(1, N + 1):
  11. coeffs.append(mpmath.bernoulli(2*n)/(2*n*(2*n - 1)))
  12. return coeffs
  13. def taylor_series_at_1(N):
  14. coeffs = []
  15. with mpmath.workdps(100):
  16. coeffs.append(-mpmath.euler)
  17. for n in range(2, N + 1):
  18. coeffs.append((-1)**n*mpmath.zeta(n)/n)
  19. return coeffs
  20. def main():
  21. print(__doc__)
  22. print()
  23. stirling_coeffs = [mpmath.nstr(x, 20, min_fixed=0, max_fixed=0)
  24. for x in stirling_series(8)[::-1]]
  25. taylor_coeffs = [mpmath.nstr(x, 20, min_fixed=0, max_fixed=0)
  26. for x in taylor_series_at_1(23)[::-1]]
  27. print("Stirling series coefficients")
  28. print("----------------------------")
  29. print("\n".join(stirling_coeffs))
  30. print()
  31. print("Taylor series coefficients")
  32. print("--------------------------")
  33. print("\n".join(taylor_coeffs))
  34. print()
  35. if __name__ == '__main__':
  36. main()