test_gammainc.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import assert_allclose
  4. import scipy.special as sc
  5. from scipy.special._testutils import FuncData
  6. def test_line():
  7. # Test on the line a = x where a simpler asymptotic expansion
  8. # (analog of DLMF 8.12.15) is available.
  9. def gammainc_line(x):
  10. c = np.array([-1/3, -1/540, 25/6048, 101/155520,
  11. -3184811/3695155200, -2745493/8151736420])
  12. res = 0
  13. xfac = 1
  14. for ck in c:
  15. res -= ck*xfac
  16. xfac /= x
  17. res /= np.sqrt(2*np.pi*x)
  18. res += 0.5
  19. return res
  20. x = np.logspace(np.log10(25), 300, 500)
  21. a = x.copy()
  22. dataset = np.vstack((a, x, gammainc_line(x))).T
  23. FuncData(sc.gammainc, dataset, (0, 1), 2, rtol=1e-11).check()
  24. def test_gammainc_roundtrip():
  25. a = np.logspace(-5, 10, 100)
  26. x = np.logspace(-5, 10, 100)
  27. y = sc.gammaincinv(a, sc.gammainc(a, x))
  28. assert_allclose(x, y, rtol=1e-10)
  29. def test_gammaincc_roundtrip():
  30. a = np.logspace(-5, 10, 100)
  31. x = np.logspace(-5, 10, 100)
  32. y = sc.gammainccinv(a, sc.gammaincc(a, x))
  33. assert_allclose(x, y, rtol=1e-14)