gammainc_data.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """Compute gammainc and gammaincc for large arguments and parameters
  2. and save the values to data files for use in tests. We can't just
  3. compare to mpmath's gammainc in test_mpmath.TestSystematic because it
  4. would take too long.
  5. Note that mpmath's gammainc is computed using hypercomb, but since it
  6. doesn't allow the user to increase the maximum number of terms used in
  7. the series it doesn't converge for many arguments. To get around this
  8. we copy the mpmath implementation but use more terms.
  9. This takes about 17 minutes to run on a 2.3 GHz Macbook Pro with 4GB
  10. ram.
  11. Sources:
  12. [1] Fredrik Johansson and others. mpmath: a Python library for
  13. arbitrary-precision floating-point arithmetic (version 0.19),
  14. December 2013. http://mpmath.org/.
  15. """
  16. import os
  17. from time import time
  18. import numpy as np
  19. from numpy import pi
  20. from scipy.special._mptestutils import mpf2float
  21. try:
  22. import mpmath as mp
  23. except ImportError:
  24. pass
  25. def gammainc(a, x, dps=50, maxterms=10**8):
  26. """Compute gammainc exactly like mpmath does but allow for more
  27. summands in hypercomb. See
  28. mpmath/functions/expintegrals.py#L134
  29. in the mpmath github repository.
  30. """
  31. with mp.workdps(dps):
  32. z, a, b = mp.mpf(a), mp.mpf(x), mp.mpf(x)
  33. G = [z]
  34. negb = mp.fneg(b, exact=True)
  35. def h(z):
  36. T1 = [mp.exp(negb), b, z], [1, z, -1], [], G, [1], [1+z], b
  37. return (T1,)
  38. res = mp.hypercomb(h, [z], maxterms=maxterms)
  39. return mpf2float(res)
  40. def gammaincc(a, x, dps=50, maxterms=10**8):
  41. """Compute gammaincc exactly like mpmath does but allow for more
  42. terms in hypercomb. See
  43. mpmath/functions/expintegrals.py#L187
  44. in the mpmath github repository.
  45. """
  46. with mp.workdps(dps):
  47. z, a = a, x
  48. if mp.isint(z):
  49. try:
  50. # mpmath has a fast integer path
  51. return mpf2float(mp.gammainc(z, a=a, regularized=True))
  52. except mp.libmp.NoConvergence:
  53. pass
  54. nega = mp.fneg(a, exact=True)
  55. G = [z]
  56. # Use 2F0 series when possible; fall back to lower gamma representation
  57. try:
  58. def h(z):
  59. r = z-1
  60. return [([mp.exp(nega), a], [1, r], [], G, [1, -r], [], 1/nega)]
  61. return mpf2float(mp.hypercomb(h, [z], force_series=True))
  62. except mp.libmp.NoConvergence:
  63. def h(z):
  64. T1 = [], [1, z-1], [z], G, [], [], 0
  65. T2 = [-mp.exp(nega), a, z], [1, z, -1], [], G, [1], [1+z], a
  66. return T1, T2
  67. return mpf2float(mp.hypercomb(h, [z], maxterms=maxterms))
  68. def main():
  69. t0 = time()
  70. # It would be nice to have data for larger values, but either this
  71. # requires prohibitively large precision (dps > 800) or mpmath has
  72. # a bug. For example, gammainc(1e20, 1e20, dps=800) returns a
  73. # value around 0.03, while the true value should be close to 0.5
  74. # (DLMF 8.12.15).
  75. print(__doc__)
  76. pwd = os.path.dirname(__file__)
  77. r = np.logspace(4, 14, 30)
  78. ltheta = np.logspace(np.log10(pi/4), np.log10(np.arctan(0.6)), 30)
  79. utheta = np.logspace(np.log10(pi/4), np.log10(np.arctan(1.4)), 30)
  80. regimes = [(gammainc, ltheta), (gammaincc, utheta)]
  81. for func, theta in regimes:
  82. rg, thetag = np.meshgrid(r, theta)
  83. a, x = rg*np.cos(thetag), rg*np.sin(thetag)
  84. a, x = a.flatten(), x.flatten()
  85. dataset = []
  86. for i, (a0, x0) in enumerate(zip(a, x)):
  87. if func == gammaincc:
  88. # Exploit the fast integer path in gammaincc whenever
  89. # possible so that the computation doesn't take too
  90. # long
  91. a0, x0 = np.floor(a0), np.floor(x0)
  92. dataset.append((a0, x0, func(a0, x0)))
  93. dataset = np.array(dataset)
  94. filename = os.path.join(pwd, '..', 'tests', 'data', 'local',
  95. '{}.txt'.format(func.__name__))
  96. np.savetxt(filename, dataset)
  97. print("{} minutes elapsed".format((time() - t0)/60))
  98. if __name__ == "__main__":
  99. main()