File size: 3,815 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import pytest
import numpy as np
from numpy.testing import assert_allclose, assert_array_equal
import scipy.special as sc
from scipy.special._testutils import FuncData
INVALID_POINTS = [
(1, -1),
(0, 0),
(-1, 1),
(np.nan, 1),
(1, np.nan)
]
class TestGammainc:
@pytest.mark.parametrize('a, x', INVALID_POINTS)
def test_domain(self, a, x):
assert np.isnan(sc.gammainc(a, x))
def test_a_eq_0_x_gt_0(self):
assert sc.gammainc(0, 1) == 1
@pytest.mark.parametrize('a, x, desired', [
(np.inf, 1, 0),
(np.inf, 0, 0),
(np.inf, np.inf, np.nan),
(1, np.inf, 1)
])
def test_infinite_arguments(self, a, x, desired):
result = sc.gammainc(a, x)
if np.isnan(desired):
assert np.isnan(result)
else:
assert result == desired
def test_infinite_limits(self):
# Test that large arguments converge to the hard-coded limits
# at infinity.
assert_allclose(
sc.gammainc(1000, 100),
sc.gammainc(np.inf, 100),
atol=1e-200, # Use `atol` since the function converges to 0.
rtol=0
)
assert sc.gammainc(100, 1000) == sc.gammainc(100, np.inf)
def test_x_zero(self):
a = np.arange(1, 10)
assert_array_equal(sc.gammainc(a, 0), 0)
def test_limit_check(self):
result = sc.gammainc(1e-10, 1)
limit = sc.gammainc(0, 1)
assert np.isclose(result, limit)
def gammainc_line(self, x):
# The line a = x where a simpler asymptotic expansion (analog
# of DLMF 8.12.15) is available.
c = np.array([-1/3, -1/540, 25/6048, 101/155520,
-3184811/3695155200, -2745493/8151736420])
res = 0
xfac = 1
for ck in c:
res -= ck*xfac
xfac /= x
res /= np.sqrt(2*np.pi*x)
res += 0.5
return res
def test_line(self):
x = np.logspace(np.log10(25), 300, 500)
a = x
dataset = np.vstack((a, x, self.gammainc_line(x))).T
FuncData(sc.gammainc, dataset, (0, 1), 2, rtol=1e-11).check()
def test_roundtrip(self):
a = np.logspace(-5, 10, 100)
x = np.logspace(-5, 10, 100)
y = sc.gammaincinv(a, sc.gammainc(a, x))
assert_allclose(x, y, rtol=1e-10)
class TestGammaincc:
@pytest.mark.parametrize('a, x', INVALID_POINTS)
def test_domain(self, a, x):
assert np.isnan(sc.gammaincc(a, x))
def test_a_eq_0_x_gt_0(self):
assert sc.gammaincc(0, 1) == 0
@pytest.mark.parametrize('a, x, desired', [
(np.inf, 1, 1),
(np.inf, 0, 1),
(np.inf, np.inf, np.nan),
(1, np.inf, 0)
])
def test_infinite_arguments(self, a, x, desired):
result = sc.gammaincc(a, x)
if np.isnan(desired):
assert np.isnan(result)
else:
assert result == desired
def test_infinite_limits(self):
# Test that large arguments converge to the hard-coded limits
# at infinity.
assert sc.gammaincc(1000, 100) == sc.gammaincc(np.inf, 100)
assert_allclose(
sc.gammaincc(100, 1000),
sc.gammaincc(100, np.inf),
atol=1e-200, # Use `atol` since the function converges to 0.
rtol=0
)
def test_limit_check(self):
result = sc.gammaincc(1e-10,1)
limit = sc.gammaincc(0,1)
assert np.isclose(result, limit)
def test_x_zero(self):
a = np.arange(1, 10)
assert_array_equal(sc.gammaincc(a, 0), 1)
def test_roundtrip(self):
a = np.logspace(-5, 10, 100)
x = np.logspace(-5, 10, 100)
y = sc.gammainccinv(a, sc.gammaincc(a, x))
assert_allclose(x, y, rtol=1e-14)
|