loggamma.py 1.1 KB

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