expn_asy.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Precompute the polynomials for the asymptotic expansion of the
  2. generalized exponential integral.
  3. Sources
  4. -------
  5. [1] NIST, Digital Library of Mathematical Functions,
  6. https://dlmf.nist.gov/8.20#ii
  7. """
  8. import os
  9. try:
  10. import sympy
  11. from sympy import Poly
  12. x = sympy.symbols('x')
  13. except ImportError:
  14. pass
  15. def generate_A(K):
  16. A = [Poly(1, x)]
  17. for k in range(K):
  18. A.append(Poly(1 - 2*k*x, x)*A[k] + Poly(x*(x + 1))*A[k].diff())
  19. return A
  20. WARNING = """\
  21. /* This file was automatically generated by _precompute/expn_asy.py.
  22. * Do not edit it manually!
  23. */
  24. """
  25. def main():
  26. print(__doc__)
  27. fn = os.path.join('..', 'cephes', 'expn.h')
  28. K = 12
  29. A = generate_A(K)
  30. with open(fn + '.new', 'w') as f:
  31. f.write(WARNING)
  32. f.write("#define nA {}\n".format(len(A)))
  33. for k, Ak in enumerate(A):
  34. tmp = ', '.join([str(x.evalf(18)) for x in Ak.coeffs()])
  35. f.write("static const double A{}[] = {{{}}};\n".format(k, tmp))
  36. tmp = ", ".join(["A{}".format(k) for k in range(K + 1)])
  37. f.write("static const double *A[] = {{{}}};\n".format(tmp))
  38. tmp = ", ".join([str(Ak.degree()) for Ak in A])
  39. f.write("static const int Adegs[] = {{{}}};\n".format(tmp))
  40. os.rename(fn + '.new', fn)
  41. if __name__ == "__main__":
  42. main()