gammainc_data.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. from __future__ import division, print_function, absolute_import
  17. import os
  18. from time import time
  19. import numpy as np
  20. from numpy import pi
  21. from scipy.special._mptestutils import mpf2float
  22. try:
  23. import mpmath as mp
  24. except ImportError:
  25. pass
  26. def gammainc(a, x, dps=50, maxterms=10**8):
  27. """Compute gammainc exactly like mpmath does but allow for more
  28. summands in hypercomb. See
  29. mpmath/functions/expintegrals.py#L134
  30. in the mpmath github repository.
  31. """
  32. with mp.workdps(dps):
  33. z, a, b = mp.mpf(a), mp.mpf(x), mp.mpf(x)
  34. G = [z]
  35. negb = mp.fneg(b, exact=True)
  36. def h(z):
  37. T1 = [mp.exp(negb), b, z], [1, z, -1], [], G, [1], [1+z], b
  38. return (T1,)
  39. res = mp.hypercomb(h, [z], maxterms=maxterms)
  40. return mpf2float(res)
  41. def gammaincc(a, x, dps=50, maxterms=10**8):
  42. """Compute gammaincc exactly like mpmath does but allow for more
  43. terms in hypercomb. See
  44. mpmath/functions/expintegrals.py#L187
  45. in the mpmath github repository.
  46. """
  47. with mp.workdps(dps):
  48. z, a = a, x
  49. if mp.isint(z):
  50. try:
  51. # mpmath has a fast integer path
  52. return mpf2float(mp.gammainc(z, a=a, regularized=True))
  53. except mp.libmp.NoConvergence:
  54. pass
  55. nega = mp.fneg(a, exact=True)
  56. G = [z]
  57. # Use 2F0 series when possible; fall back to lower gamma representation
  58. try:
  59. def h(z):
  60. r = z-1
  61. return [([mp.exp(nega), a], [1, r], [], G, [1, -r], [], 1/nega)]
  62. return mpf2float(mp.hypercomb(h, [z], force_series=True))
  63. except mp.libmp.NoConvergence:
  64. def h(z):
  65. T1 = [], [1, z-1], [z], G, [], [], 0
  66. T2 = [-mp.exp(nega), a, z], [1, z, -1], [], G, [1], [1+z], a
  67. return T1, T2
  68. return mpf2float(mp.hypercomb(h, [z], maxterms=maxterms))
  69. def main():
  70. t0 = time()
  71. # It would be nice to have data for larger values, but either this
  72. # requires prohibitively large precision (dps > 800) or mpmath has
  73. # a bug. For example, gammainc(1e20, 1e20, dps=800) returns a
  74. # value around 0.03, while the true value should be close to 0.5
  75. # (DLMF 8.12.15).
  76. print(__doc__)
  77. pwd = os.path.dirname(__file__)
  78. r = np.logspace(4, 14, 30)
  79. ltheta = np.logspace(np.log10(pi/4), np.log10(np.arctan(0.6)), 30)
  80. utheta = np.logspace(np.log10(pi/4), np.log10(np.arctan(1.4)), 30)
  81. regimes = [(gammainc, ltheta), (gammaincc, utheta)]
  82. for func, theta in regimes:
  83. rg, thetag = np.meshgrid(r, theta)
  84. a, x = rg*np.cos(thetag), rg*np.sin(thetag)
  85. a, x = a.flatten(), x.flatten()
  86. dataset = []
  87. for i, (a0, x0) in enumerate(zip(a, x)):
  88. if func == gammaincc:
  89. # Exploit the fast integer path in gammaincc whenever
  90. # possible so that the computation doesn't take too
  91. # long
  92. a0, x0 = np.floor(a0), np.floor(x0)
  93. dataset.append((a0, x0, func(a0, x0)))
  94. dataset = np.array(dataset)
  95. filename = os.path.join(pwd, '..', 'tests', 'data', 'local',
  96. '{}.txt'.format(func.__name__))
  97. np.savetxt(filename, dataset)
  98. print("{} minutes elapsed".format((time() - t0)/60))
  99. if __name__ == "__main__":
  100. main()