Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/_logsumexp.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/_mptestutils.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/_orthogonal.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/_spfun_stats.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/_support_alternative_backends.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/orthogonal.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/__pycache__/spfun_stats.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__init__.py +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_basic.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_bdtr.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_boxcox.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_cosine_distr.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_data.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_ellip_harm.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_hypergeometric.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_kolmogorov.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_loggamma.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_orthogonal_eval.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_pcf.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_pdtr.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_round.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_specfun.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_spence.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/data/__init__.py +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/data/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_basic.py +0 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_bdtr.py +112 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_boxcox.py +106 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_cdflib.py +527 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_cdft_asymptotic.py +49 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_cosine_distr.py +83 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_cython_special.py +363 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_data.py +725 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_dd.py +46 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_digamma.py +45 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_ellip_harm.py +278 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_erfinv.py +89 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_exponential_integrals.py +118 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_faddeeva.py +85 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_gamma.py +12 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_gammainc.py +136 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_hyp2f1.py +2180 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_hypergeometric.py +140 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_kolmogorov.py +495 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_lambertw.py +109 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_log_softmax.py +109 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_loggamma.py +70 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_logit.py +145 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_logsumexp.py +207 -0
- venv/lib/python3.10/site-packages/scipy/special/tests/test_mpmath.py +2272 -0
venv/lib/python3.10/site-packages/scipy/special/__pycache__/_logsumexp.cpython-310.pyc
ADDED
Binary file (8.62 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/__pycache__/_mptestutils.cpython-310.pyc
ADDED
Binary file (13.1 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/__pycache__/_orthogonal.cpython-310.pyc
ADDED
Binary file (74.2 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/__pycache__/_spfun_stats.cpython-310.pyc
ADDED
Binary file (2.6 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/__pycache__/_support_alternative_backends.cpython-310.pyc
ADDED
Binary file (2.25 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/__pycache__/orthogonal.cpython-310.pyc
ADDED
Binary file (1.44 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/__pycache__/spfun_stats.cpython-310.pyc
ADDED
Binary file (629 Bytes). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_basic.cpython-310.pyc
ADDED
Binary file (155 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_bdtr.cpython-310.pyc
ADDED
Binary file (4.55 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_boxcox.cpython-310.pyc
ADDED
Binary file (2.75 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_cosine_distr.cpython-310.pyc
ADDED
Binary file (1.97 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_data.cpython-310.pyc
ADDED
Binary file (20.8 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_ellip_harm.cpython-310.pyc
ADDED
Binary file (12.9 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_hypergeometric.cpython-310.pyc
ADDED
Binary file (4.8 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_kolmogorov.cpython-310.pyc
ADDED
Binary file (16 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_loggamma.cpython-310.pyc
ADDED
Binary file (2.94 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_orthogonal_eval.cpython-310.pyc
ADDED
Binary file (10.5 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_pcf.cpython-310.pyc
ADDED
Binary file (833 Bytes). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_pdtr.cpython-310.pyc
ADDED
Binary file (2.29 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_round.cpython-310.pyc
ADDED
Binary file (687 Bytes). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_specfun.cpython-310.pyc
ADDED
Binary file (1.51 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_spence.cpython-310.pyc
ADDED
Binary file (1.23 kB). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/data/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/scipy/special/tests/data/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (192 Bytes). View file
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_basic.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_bdtr.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import scipy.special as sc
|
3 |
+
import pytest
|
4 |
+
from numpy.testing import assert_allclose, assert_array_equal, suppress_warnings
|
5 |
+
|
6 |
+
|
7 |
+
class TestBdtr:
|
8 |
+
def test(self):
|
9 |
+
val = sc.bdtr(0, 1, 0.5)
|
10 |
+
assert_allclose(val, 0.5)
|
11 |
+
|
12 |
+
def test_sum_is_one(self):
|
13 |
+
val = sc.bdtr([0, 1, 2], 2, 0.5)
|
14 |
+
assert_array_equal(val, [0.25, 0.75, 1.0])
|
15 |
+
|
16 |
+
def test_rounding(self):
|
17 |
+
double_val = sc.bdtr([0.1, 1.1, 2.1], 2, 0.5)
|
18 |
+
int_val = sc.bdtr([0, 1, 2], 2, 0.5)
|
19 |
+
assert_array_equal(double_val, int_val)
|
20 |
+
|
21 |
+
@pytest.mark.parametrize('k, n, p', [
|
22 |
+
(np.inf, 2, 0.5),
|
23 |
+
(1.0, np.inf, 0.5),
|
24 |
+
(1.0, 2, np.inf)
|
25 |
+
])
|
26 |
+
def test_inf(self, k, n, p):
|
27 |
+
with suppress_warnings() as sup:
|
28 |
+
sup.filter(DeprecationWarning)
|
29 |
+
val = sc.bdtr(k, n, p)
|
30 |
+
assert np.isnan(val)
|
31 |
+
|
32 |
+
def test_domain(self):
|
33 |
+
val = sc.bdtr(-1.1, 1, 0.5)
|
34 |
+
assert np.isnan(val)
|
35 |
+
|
36 |
+
|
37 |
+
class TestBdtrc:
|
38 |
+
def test_value(self):
|
39 |
+
val = sc.bdtrc(0, 1, 0.5)
|
40 |
+
assert_allclose(val, 0.5)
|
41 |
+
|
42 |
+
def test_sum_is_one(self):
|
43 |
+
val = sc.bdtrc([0, 1, 2], 2, 0.5)
|
44 |
+
assert_array_equal(val, [0.75, 0.25, 0.0])
|
45 |
+
|
46 |
+
def test_rounding(self):
|
47 |
+
double_val = sc.bdtrc([0.1, 1.1, 2.1], 2, 0.5)
|
48 |
+
int_val = sc.bdtrc([0, 1, 2], 2, 0.5)
|
49 |
+
assert_array_equal(double_val, int_val)
|
50 |
+
|
51 |
+
@pytest.mark.parametrize('k, n, p', [
|
52 |
+
(np.inf, 2, 0.5),
|
53 |
+
(1.0, np.inf, 0.5),
|
54 |
+
(1.0, 2, np.inf)
|
55 |
+
])
|
56 |
+
def test_inf(self, k, n, p):
|
57 |
+
with suppress_warnings() as sup:
|
58 |
+
sup.filter(DeprecationWarning)
|
59 |
+
val = sc.bdtrc(k, n, p)
|
60 |
+
assert np.isnan(val)
|
61 |
+
|
62 |
+
def test_domain(self):
|
63 |
+
val = sc.bdtrc(-1.1, 1, 0.5)
|
64 |
+
val2 = sc.bdtrc(2.1, 1, 0.5)
|
65 |
+
assert np.isnan(val2)
|
66 |
+
assert_allclose(val, 1.0)
|
67 |
+
|
68 |
+
def test_bdtr_bdtrc_sum_to_one(self):
|
69 |
+
bdtr_vals = sc.bdtr([0, 1, 2], 2, 0.5)
|
70 |
+
bdtrc_vals = sc.bdtrc([0, 1, 2], 2, 0.5)
|
71 |
+
vals = bdtr_vals + bdtrc_vals
|
72 |
+
assert_allclose(vals, [1.0, 1.0, 1.0])
|
73 |
+
|
74 |
+
|
75 |
+
class TestBdtri:
|
76 |
+
def test_value(self):
|
77 |
+
val = sc.bdtri(0, 1, 0.5)
|
78 |
+
assert_allclose(val, 0.5)
|
79 |
+
|
80 |
+
def test_sum_is_one(self):
|
81 |
+
val = sc.bdtri([0, 1], 2, 0.5)
|
82 |
+
actual = np.asarray([1 - 1/np.sqrt(2), 1/np.sqrt(2)])
|
83 |
+
assert_allclose(val, actual)
|
84 |
+
|
85 |
+
def test_rounding(self):
|
86 |
+
double_val = sc.bdtri([0.1, 1.1], 2, 0.5)
|
87 |
+
int_val = sc.bdtri([0, 1], 2, 0.5)
|
88 |
+
assert_allclose(double_val, int_val)
|
89 |
+
|
90 |
+
@pytest.mark.parametrize('k, n, p', [
|
91 |
+
(np.inf, 2, 0.5),
|
92 |
+
(1.0, np.inf, 0.5),
|
93 |
+
(1.0, 2, np.inf)
|
94 |
+
])
|
95 |
+
def test_inf(self, k, n, p):
|
96 |
+
with suppress_warnings() as sup:
|
97 |
+
sup.filter(DeprecationWarning)
|
98 |
+
val = sc.bdtri(k, n, p)
|
99 |
+
assert np.isnan(val)
|
100 |
+
|
101 |
+
@pytest.mark.parametrize('k, n, p', [
|
102 |
+
(-1.1, 1, 0.5),
|
103 |
+
(2.1, 1, 0.5)
|
104 |
+
])
|
105 |
+
def test_domain(self, k, n, p):
|
106 |
+
val = sc.bdtri(k, n, p)
|
107 |
+
assert np.isnan(val)
|
108 |
+
|
109 |
+
def test_bdtr_bdtri_roundtrip(self):
|
110 |
+
bdtr_vals = sc.bdtr([0, 1, 2], 2, 0.5)
|
111 |
+
roundtrip_vals = sc.bdtri([0, 1, 2], 2, bdtr_vals)
|
112 |
+
assert_allclose(roundtrip_vals, [0.5, 0.5, np.nan])
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_boxcox.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import assert_equal, assert_almost_equal, assert_allclose
|
3 |
+
from scipy.special import boxcox, boxcox1p, inv_boxcox, inv_boxcox1p
|
4 |
+
|
5 |
+
|
6 |
+
# There are more tests of boxcox and boxcox1p in test_mpmath.py.
|
7 |
+
|
8 |
+
def test_boxcox_basic():
|
9 |
+
x = np.array([0.5, 1, 2, 4])
|
10 |
+
|
11 |
+
# lambda = 0 => y = log(x)
|
12 |
+
y = boxcox(x, 0)
|
13 |
+
assert_almost_equal(y, np.log(x))
|
14 |
+
|
15 |
+
# lambda = 1 => y = x - 1
|
16 |
+
y = boxcox(x, 1)
|
17 |
+
assert_almost_equal(y, x - 1)
|
18 |
+
|
19 |
+
# lambda = 2 => y = 0.5*(x**2 - 1)
|
20 |
+
y = boxcox(x, 2)
|
21 |
+
assert_almost_equal(y, 0.5*(x**2 - 1))
|
22 |
+
|
23 |
+
# x = 0 and lambda > 0 => y = -1 / lambda
|
24 |
+
lam = np.array([0.5, 1, 2])
|
25 |
+
y = boxcox(0, lam)
|
26 |
+
assert_almost_equal(y, -1.0 / lam)
|
27 |
+
|
28 |
+
def test_boxcox_underflow():
|
29 |
+
x = 1 + 1e-15
|
30 |
+
lmbda = 1e-306
|
31 |
+
y = boxcox(x, lmbda)
|
32 |
+
assert_allclose(y, np.log(x), rtol=1e-14)
|
33 |
+
|
34 |
+
|
35 |
+
def test_boxcox_nonfinite():
|
36 |
+
# x < 0 => y = nan
|
37 |
+
x = np.array([-1, -1, -0.5])
|
38 |
+
y = boxcox(x, [0.5, 2.0, -1.5])
|
39 |
+
assert_equal(y, np.array([np.nan, np.nan, np.nan]))
|
40 |
+
|
41 |
+
# x = 0 and lambda <= 0 => y = -inf
|
42 |
+
x = 0
|
43 |
+
y = boxcox(x, [-2.5, 0])
|
44 |
+
assert_equal(y, np.array([-np.inf, -np.inf]))
|
45 |
+
|
46 |
+
|
47 |
+
def test_boxcox1p_basic():
|
48 |
+
x = np.array([-0.25, -1e-20, 0, 1e-20, 0.25, 1, 3])
|
49 |
+
|
50 |
+
# lambda = 0 => y = log(1+x)
|
51 |
+
y = boxcox1p(x, 0)
|
52 |
+
assert_almost_equal(y, np.log1p(x))
|
53 |
+
|
54 |
+
# lambda = 1 => y = x
|
55 |
+
y = boxcox1p(x, 1)
|
56 |
+
assert_almost_equal(y, x)
|
57 |
+
|
58 |
+
# lambda = 2 => y = 0.5*((1+x)**2 - 1) = 0.5*x*(2 + x)
|
59 |
+
y = boxcox1p(x, 2)
|
60 |
+
assert_almost_equal(y, 0.5*x*(2 + x))
|
61 |
+
|
62 |
+
# x = -1 and lambda > 0 => y = -1 / lambda
|
63 |
+
lam = np.array([0.5, 1, 2])
|
64 |
+
y = boxcox1p(-1, lam)
|
65 |
+
assert_almost_equal(y, -1.0 / lam)
|
66 |
+
|
67 |
+
|
68 |
+
def test_boxcox1p_underflow():
|
69 |
+
x = np.array([1e-15, 1e-306])
|
70 |
+
lmbda = np.array([1e-306, 1e-18])
|
71 |
+
y = boxcox1p(x, lmbda)
|
72 |
+
assert_allclose(y, np.log1p(x), rtol=1e-14)
|
73 |
+
|
74 |
+
|
75 |
+
def test_boxcox1p_nonfinite():
|
76 |
+
# x < -1 => y = nan
|
77 |
+
x = np.array([-2, -2, -1.5])
|
78 |
+
y = boxcox1p(x, [0.5, 2.0, -1.5])
|
79 |
+
assert_equal(y, np.array([np.nan, np.nan, np.nan]))
|
80 |
+
|
81 |
+
# x = -1 and lambda <= 0 => y = -inf
|
82 |
+
x = -1
|
83 |
+
y = boxcox1p(x, [-2.5, 0])
|
84 |
+
assert_equal(y, np.array([-np.inf, -np.inf]))
|
85 |
+
|
86 |
+
|
87 |
+
def test_inv_boxcox():
|
88 |
+
x = np.array([0., 1., 2.])
|
89 |
+
lam = np.array([0., 1., 2.])
|
90 |
+
y = boxcox(x, lam)
|
91 |
+
x2 = inv_boxcox(y, lam)
|
92 |
+
assert_almost_equal(x, x2)
|
93 |
+
|
94 |
+
x = np.array([0., 1., 2.])
|
95 |
+
lam = np.array([0., 1., 2.])
|
96 |
+
y = boxcox1p(x, lam)
|
97 |
+
x2 = inv_boxcox1p(y, lam)
|
98 |
+
assert_almost_equal(x, x2)
|
99 |
+
|
100 |
+
|
101 |
+
def test_inv_boxcox1p_underflow():
|
102 |
+
x = 1e-15
|
103 |
+
lam = 1e-306
|
104 |
+
y = inv_boxcox1p(x, lam)
|
105 |
+
assert_allclose(y, x, rtol=1e-14)
|
106 |
+
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_cdflib.py
ADDED
@@ -0,0 +1,527 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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bdtrin
|
13 |
+
- pdtrik
|
14 |
+
- nctdtr
|
15 |
+
- nctdtrit
|
16 |
+
- nctdtridf
|
17 |
+
- nctdtrinc
|
18 |
+
|
19 |
+
"""
|
20 |
+
import itertools
|
21 |
+
|
22 |
+
import numpy as np
|
23 |
+
from numpy.testing import assert_equal, assert_allclose
|
24 |
+
import pytest
|
25 |
+
|
26 |
+
import scipy.special as sp
|
27 |
+
from scipy.special._testutils import (
|
28 |
+
MissingModule, check_version, FuncData)
|
29 |
+
from scipy.special._mptestutils import (
|
30 |
+
Arg, IntArg, get_args, mpf2float, assert_mpmath_equal)
|
31 |
+
|
32 |
+
try:
|
33 |
+
import mpmath
|
34 |
+
except ImportError:
|
35 |
+
mpmath = MissingModule('mpmath')
|
36 |
+
|
37 |
+
|
38 |
+
class ProbArg:
|
39 |
+
"""Generate a set of probabilities on [0, 1]."""
|
40 |
+
|
41 |
+
def __init__(self):
|
42 |
+
# Include the endpoints for compatibility with Arg et. al.
|
43 |
+
self.a = 0
|
44 |
+
self.b = 1
|
45 |
+
|
46 |
+
def values(self, n):
|
47 |
+
"""Return an array containing approximately n numbers."""
|
48 |
+
m = max(1, n//3)
|
49 |
+
v1 = np.logspace(-30, np.log10(0.3), m)
|
50 |
+
v2 = np.linspace(0.3, 0.7, m + 1, endpoint=False)[1:]
|
51 |
+
v3 = 1 - np.logspace(np.log10(0.3), -15, m)
|
52 |
+
v = np.r_[v1, v2, v3]
|
53 |
+
return np.unique(v)
|
54 |
+
|
55 |
+
|
56 |
+
class EndpointFilter:
|
57 |
+
def __init__(self, a, b, rtol, atol):
|
58 |
+
self.a = a
|
59 |
+
self.b = b
|
60 |
+
self.rtol = rtol
|
61 |
+
self.atol = atol
|
62 |
+
|
63 |
+
def __call__(self, x):
|
64 |
+
mask1 = np.abs(x - self.a) < self.rtol*np.abs(self.a) + self.atol
|
65 |
+
mask2 = np.abs(x - self.b) < self.rtol*np.abs(self.b) + self.atol
|
66 |
+
return np.where(mask1 | mask2, False, True)
|
67 |
+
|
68 |
+
|
69 |
+
class _CDFData:
|
70 |
+
def __init__(self, spfunc, mpfunc, index, argspec, spfunc_first=True,
|
71 |
+
dps=20, n=5000, rtol=None, atol=None,
|
72 |
+
endpt_rtol=None, endpt_atol=None):
|
73 |
+
self.spfunc = spfunc
|
74 |
+
self.mpfunc = mpfunc
|
75 |
+
self.index = index
|
76 |
+
self.argspec = argspec
|
77 |
+
self.spfunc_first = spfunc_first
|
78 |
+
self.dps = dps
|
79 |
+
self.n = n
|
80 |
+
self.rtol = rtol
|
81 |
+
self.atol = atol
|
82 |
+
|
83 |
+
if not isinstance(argspec, list):
|
84 |
+
self.endpt_rtol = None
|
85 |
+
self.endpt_atol = None
|
86 |
+
elif endpt_rtol is not None or endpt_atol is not None:
|
87 |
+
if isinstance(endpt_rtol, list):
|
88 |
+
self.endpt_rtol = endpt_rtol
|
89 |
+
else:
|
90 |
+
self.endpt_rtol = [endpt_rtol]*len(self.argspec)
|
91 |
+
if isinstance(endpt_atol, list):
|
92 |
+
self.endpt_atol = endpt_atol
|
93 |
+
else:
|
94 |
+
self.endpt_atol = [endpt_atol]*len(self.argspec)
|
95 |
+
else:
|
96 |
+
self.endpt_rtol = None
|
97 |
+
self.endpt_atol = None
|
98 |
+
|
99 |
+
def idmap(self, *args):
|
100 |
+
if self.spfunc_first:
|
101 |
+
res = self.spfunc(*args)
|
102 |
+
if np.isnan(res):
|
103 |
+
return np.nan
|
104 |
+
args = list(args)
|
105 |
+
args[self.index] = res
|
106 |
+
with mpmath.workdps(self.dps):
|
107 |
+
res = self.mpfunc(*tuple(args))
|
108 |
+
# Imaginary parts are spurious
|
109 |
+
res = mpf2float(res.real)
|
110 |
+
else:
|
111 |
+
with mpmath.workdps(self.dps):
|
112 |
+
res = self.mpfunc(*args)
|
113 |
+
res = mpf2float(res.real)
|
114 |
+
args = list(args)
|
115 |
+
args[self.index] = res
|
116 |
+
res = self.spfunc(*tuple(args))
|
117 |
+
return res
|
118 |
+
|
119 |
+
def get_param_filter(self):
|
120 |
+
if self.endpt_rtol is None and self.endpt_atol is None:
|
121 |
+
return None
|
122 |
+
|
123 |
+
filters = []
|
124 |
+
for rtol, atol, spec in zip(self.endpt_rtol, self.endpt_atol, self.argspec):
|
125 |
+
if rtol is None and atol is None:
|
126 |
+
filters.append(None)
|
127 |
+
continue
|
128 |
+
elif rtol is None:
|
129 |
+
rtol = 0.0
|
130 |
+
elif atol is None:
|
131 |
+
atol = 0.0
|
132 |
+
|
133 |
+
filters.append(EndpointFilter(spec.a, spec.b, rtol, atol))
|
134 |
+
return filters
|
135 |
+
|
136 |
+
def check(self):
|
137 |
+
# Generate values for the arguments
|
138 |
+
args = get_args(self.argspec, self.n)
|
139 |
+
param_filter = self.get_param_filter()
|
140 |
+
param_columns = tuple(range(args.shape[1]))
|
141 |
+
result_columns = args.shape[1]
|
142 |
+
args = np.hstack((args, args[:, self.index].reshape(args.shape[0], 1)))
|
143 |
+
FuncData(self.idmap, args,
|
144 |
+
param_columns=param_columns, result_columns=result_columns,
|
145 |
+
rtol=self.rtol, atol=self.atol, vectorized=False,
|
146 |
+
param_filter=param_filter).check()
|
147 |
+
|
148 |
+
|
149 |
+
def _assert_inverts(*a, **kw):
|
150 |
+
d = _CDFData(*a, **kw)
|
151 |
+
d.check()
|
152 |
+
|
153 |
+
|
154 |
+
def _binomial_cdf(k, n, p):
|
155 |
+
k, n, p = mpmath.mpf(k), mpmath.mpf(n), mpmath.mpf(p)
|
156 |
+
if k <= 0:
|
157 |
+
return mpmath.mpf(0)
|
158 |
+
elif k >= n:
|
159 |
+
return mpmath.mpf(1)
|
160 |
+
|
161 |
+
onemp = mpmath.fsub(1, p, exact=True)
|
162 |
+
return mpmath.betainc(n - k, k + 1, x2=onemp, regularized=True)
|
163 |
+
|
164 |
+
|
165 |
+
def _f_cdf(dfn, dfd, x):
|
166 |
+
if x < 0:
|
167 |
+
return mpmath.mpf(0)
|
168 |
+
dfn, dfd, x = mpmath.mpf(dfn), mpmath.mpf(dfd), mpmath.mpf(x)
|
169 |
+
ub = dfn*x/(dfn*x + dfd)
|
170 |
+
res = mpmath.betainc(dfn/2, dfd/2, x2=ub, regularized=True)
|
171 |
+
return res
|
172 |
+
|
173 |
+
|
174 |
+
def _student_t_cdf(df, t, dps=None):
|
175 |
+
if dps is None:
|
176 |
+
dps = mpmath.mp.dps
|
177 |
+
with mpmath.workdps(dps):
|
178 |
+
df, t = mpmath.mpf(df), mpmath.mpf(t)
|
179 |
+
fac = mpmath.hyp2f1(0.5, 0.5*(df + 1), 1.5, -t**2/df)
|
180 |
+
fac *= t*mpmath.gamma(0.5*(df + 1))
|
181 |
+
fac /= mpmath.sqrt(mpmath.pi*df)*mpmath.gamma(0.5*df)
|
182 |
+
return 0.5 + fac
|
183 |
+
|
184 |
+
|
185 |
+
def _noncentral_chi_pdf(t, df, nc):
|
186 |
+
res = mpmath.besseli(df/2 - 1, mpmath.sqrt(nc*t))
|
187 |
+
res *= mpmath.exp(-(t + nc)/2)*(t/nc)**(df/4 - 1/2)/2
|
188 |
+
return res
|
189 |
+
|
190 |
+
|
191 |
+
def _noncentral_chi_cdf(x, df, nc, dps=None):
|
192 |
+
if dps is None:
|
193 |
+
dps = mpmath.mp.dps
|
194 |
+
x, df, nc = mpmath.mpf(x), mpmath.mpf(df), mpmath.mpf(nc)
|
195 |
+
with mpmath.workdps(dps):
|
196 |
+
res = mpmath.quad(lambda t: _noncentral_chi_pdf(t, df, nc), [0, x])
|
197 |
+
return res
|
198 |
+
|
199 |
+
|
200 |
+
def _tukey_lmbda_quantile(p, lmbda):
|
201 |
+
# For lmbda != 0
|
202 |
+
return (p**lmbda - (1 - p)**lmbda)/lmbda
|
203 |
+
|
204 |
+
|
205 |
+
@pytest.mark.slow
|
206 |
+
@check_version(mpmath, '0.19')
|
207 |
+
class TestCDFlib:
|
208 |
+
|
209 |
+
@pytest.mark.xfail(run=False)
|
210 |
+
def test_bdtrik(self):
|
211 |
+
_assert_inverts(
|
212 |
+
sp.bdtrik,
|
213 |
+
_binomial_cdf,
|
214 |
+
0, [ProbArg(), IntArg(1, 1000), ProbArg()],
|
215 |
+
rtol=1e-4)
|
216 |
+
|
217 |
+
def test_bdtrin(self):
|
218 |
+
_assert_inverts(
|
219 |
+
sp.bdtrin,
|
220 |
+
_binomial_cdf,
|
221 |
+
1, [IntArg(1, 1000), ProbArg(), ProbArg()],
|
222 |
+
rtol=1e-4, endpt_atol=[None, None, 1e-6])
|
223 |
+
|
224 |
+
def test_btdtria(self):
|
225 |
+
_assert_inverts(
|
226 |
+
sp.btdtria,
|
227 |
+
lambda a, b, x: mpmath.betainc(a, b, x2=x, regularized=True),
|
228 |
+
0, [ProbArg(), Arg(0, 1e2, inclusive_a=False),
|
229 |
+
Arg(0, 1, inclusive_a=False, inclusive_b=False)],
|
230 |
+
rtol=1e-6)
|
231 |
+
|
232 |
+
def test_btdtrib(self):
|
233 |
+
# Use small values of a or mpmath doesn't converge
|
234 |
+
_assert_inverts(
|
235 |
+
sp.btdtrib,
|
236 |
+
lambda a, b, x: mpmath.betainc(a, b, x2=x, regularized=True),
|
237 |
+
1,
|
238 |
+
[Arg(0, 1e2, inclusive_a=False), ProbArg(),
|
239 |
+
Arg(0, 1, inclusive_a=False, inclusive_b=False)],
|
240 |
+
rtol=1e-7,
|
241 |
+
endpt_atol=[None, 1e-18, 1e-15])
|
242 |
+
|
243 |
+
@pytest.mark.xfail(run=False)
|
244 |
+
def test_fdtridfd(self):
|
245 |
+
_assert_inverts(
|
246 |
+
sp.fdtridfd,
|
247 |
+
_f_cdf,
|
248 |
+
1,
|
249 |
+
[IntArg(1, 100), ProbArg(), Arg(0, 100, inclusive_a=False)],
|
250 |
+
rtol=1e-7)
|
251 |
+
|
252 |
+
def test_gdtria(self):
|
253 |
+
_assert_inverts(
|
254 |
+
sp.gdtria,
|
255 |
+
lambda a, b, x: mpmath.gammainc(b, b=a*x, regularized=True),
|
256 |
+
0,
|
257 |
+
[ProbArg(), Arg(0, 1e3, inclusive_a=False),
|
258 |
+
Arg(0, 1e4, inclusive_a=False)],
|
259 |
+
rtol=1e-7,
|
260 |
+
endpt_atol=[None, 1e-7, 1e-10])
|
261 |
+
|
262 |
+
def test_gdtrib(self):
|
263 |
+
# Use small values of a and x or mpmath doesn't converge
|
264 |
+
_assert_inverts(
|
265 |
+
sp.gdtrib,
|
266 |
+
lambda a, b, x: mpmath.gammainc(b, b=a*x, regularized=True),
|
267 |
+
1,
|
268 |
+
[Arg(0, 1e2, inclusive_a=False), ProbArg(),
|
269 |
+
Arg(0, 1e3, inclusive_a=False)],
|
270 |
+
rtol=1e-5)
|
271 |
+
|
272 |
+
def test_gdtrix(self):
|
273 |
+
_assert_inverts(
|
274 |
+
sp.gdtrix,
|
275 |
+
lambda a, b, x: mpmath.gammainc(b, b=a*x, regularized=True),
|
276 |
+
2,
|
277 |
+
[Arg(0, 1e3, inclusive_a=False), Arg(0, 1e3, inclusive_a=False),
|
278 |
+
ProbArg()],
|
279 |
+
rtol=1e-7,
|
280 |
+
endpt_atol=[None, 1e-7, 1e-10])
|
281 |
+
|
282 |
+
# Overall nrdtrimn and nrdtrisd are not performing well with infeasible/edge
|
283 |
+
# combinations of sigma and x, hence restricted the domains to still use the
|
284 |
+
# testing machinery, also see gh-20069
|
285 |
+
|
286 |
+
# nrdtrimn signature: p, sd, x
|
287 |
+
# nrdtrisd signature: mn, p, x
|
288 |
+
def test_nrdtrimn(self):
|
289 |
+
_assert_inverts(
|
290 |
+
sp.nrdtrimn,
|
291 |
+
lambda x, y, z: mpmath.ncdf(z, x, y),
|
292 |
+
0,
|
293 |
+
[ProbArg(), # CDF value p
|
294 |
+
Arg(0.1, np.inf, inclusive_a=False, inclusive_b=False), # sigma
|
295 |
+
Arg(-1e10, 1e10)], # x
|
296 |
+
rtol=1e-5)
|
297 |
+
|
298 |
+
def test_nrdtrisd(self):
|
299 |
+
_assert_inverts(
|
300 |
+
sp.nrdtrisd,
|
301 |
+
lambda x, y, z: mpmath.ncdf(z, x, y),
|
302 |
+
1,
|
303 |
+
[Arg(-np.inf, 10, inclusive_a=False, inclusive_b=False), # mn
|
304 |
+
ProbArg(), # CDF value p
|
305 |
+
Arg(10, 1e100)], # x
|
306 |
+
rtol=1e-5)
|
307 |
+
|
308 |
+
def test_stdtr(self):
|
309 |
+
# Ideally the left endpoint for Arg() should be 0.
|
310 |
+
assert_mpmath_equal(
|
311 |
+
sp.stdtr,
|
312 |
+
_student_t_cdf,
|
313 |
+
[IntArg(1, 100), Arg(1e-10, np.inf)], rtol=1e-7)
|
314 |
+
|
315 |
+
@pytest.mark.xfail(run=False)
|
316 |
+
def test_stdtridf(self):
|
317 |
+
_assert_inverts(
|
318 |
+
sp.stdtridf,
|
319 |
+
_student_t_cdf,
|
320 |
+
0, [ProbArg(), Arg()], rtol=1e-7)
|
321 |
+
|
322 |
+
def test_stdtrit(self):
|
323 |
+
_assert_inverts(
|
324 |
+
sp.stdtrit,
|
325 |
+
_student_t_cdf,
|
326 |
+
1, [IntArg(1, 100), ProbArg()], rtol=1e-7,
|
327 |
+
endpt_atol=[None, 1e-10])
|
328 |
+
|
329 |
+
def test_chdtriv(self):
|
330 |
+
_assert_inverts(
|
331 |
+
sp.chdtriv,
|
332 |
+
lambda v, x: mpmath.gammainc(v/2, b=x/2, regularized=True),
|
333 |
+
0, [ProbArg(), IntArg(1, 100)], rtol=1e-4)
|
334 |
+
|
335 |
+
@pytest.mark.xfail(run=False)
|
336 |
+
def test_chndtridf(self):
|
337 |
+
# Use a larger atol since mpmath is doing numerical integration
|
338 |
+
_assert_inverts(
|
339 |
+
sp.chndtridf,
|
340 |
+
_noncentral_chi_cdf,
|
341 |
+
1, [Arg(0, 100, inclusive_a=False), ProbArg(),
|
342 |
+
Arg(0, 100, inclusive_a=False)],
|
343 |
+
n=1000, rtol=1e-4, atol=1e-15)
|
344 |
+
|
345 |
+
@pytest.mark.xfail(run=False)
|
346 |
+
def test_chndtrinc(self):
|
347 |
+
# Use a larger atol since mpmath is doing numerical integration
|
348 |
+
_assert_inverts(
|
349 |
+
sp.chndtrinc,
|
350 |
+
_noncentral_chi_cdf,
|
351 |
+
2, [Arg(0, 100, inclusive_a=False), IntArg(1, 100), ProbArg()],
|
352 |
+
n=1000, rtol=1e-4, atol=1e-15)
|
353 |
+
|
354 |
+
def test_chndtrix(self):
|
355 |
+
# Use a larger atol since mpmath is doing numerical integration
|
356 |
+
_assert_inverts(
|
357 |
+
sp.chndtrix,
|
358 |
+
_noncentral_chi_cdf,
|
359 |
+
0, [ProbArg(), IntArg(1, 100), Arg(0, 100, inclusive_a=False)],
|
360 |
+
n=1000, rtol=1e-4, atol=1e-15,
|
361 |
+
endpt_atol=[1e-6, None, None])
|
362 |
+
|
363 |
+
def test_tklmbda_zero_shape(self):
|
364 |
+
# When lmbda = 0 the CDF has a simple closed form
|
365 |
+
one = mpmath.mpf(1)
|
366 |
+
assert_mpmath_equal(
|
367 |
+
lambda x: sp.tklmbda(x, 0),
|
368 |
+
lambda x: one/(mpmath.exp(-x) + one),
|
369 |
+
[Arg()], rtol=1e-7)
|
370 |
+
|
371 |
+
def test_tklmbda_neg_shape(self):
|
372 |
+
_assert_inverts(
|
373 |
+
sp.tklmbda,
|
374 |
+
_tukey_lmbda_quantile,
|
375 |
+
0, [ProbArg(), Arg(-25, 0, inclusive_b=False)],
|
376 |
+
spfunc_first=False, rtol=1e-5,
|
377 |
+
endpt_atol=[1e-9, 1e-5])
|
378 |
+
|
379 |
+
@pytest.mark.xfail(run=False)
|
380 |
+
def test_tklmbda_pos_shape(self):
|
381 |
+
_assert_inverts(
|
382 |
+
sp.tklmbda,
|
383 |
+
_tukey_lmbda_quantile,
|
384 |
+
0, [ProbArg(), Arg(0, 100, inclusive_a=False)],
|
385 |
+
spfunc_first=False, rtol=1e-5)
|
386 |
+
|
387 |
+
# The values of lmdba are chosen so that 1/lmbda is exact.
|
388 |
+
@pytest.mark.parametrize('lmbda', [0.5, 1.0, 8.0])
|
389 |
+
def test_tklmbda_lmbda1(self, lmbda):
|
390 |
+
bound = 1/lmbda
|
391 |
+
assert_equal(sp.tklmbda([-bound, bound], lmbda), [0.0, 1.0])
|
392 |
+
|
393 |
+
|
394 |
+
funcs = [
|
395 |
+
("btdtria", 3),
|
396 |
+
("btdtrib", 3),
|
397 |
+
("bdtrik", 3),
|
398 |
+
("bdtrin", 3),
|
399 |
+
("chdtriv", 2),
|
400 |
+
("chndtr", 3),
|
401 |
+
("chndtrix", 3),
|
402 |
+
("chndtridf", 3),
|
403 |
+
("chndtrinc", 3),
|
404 |
+
("fdtridfd", 3),
|
405 |
+
("ncfdtr", 4),
|
406 |
+
("ncfdtri", 4),
|
407 |
+
("ncfdtridfn", 4),
|
408 |
+
("ncfdtridfd", 4),
|
409 |
+
("ncfdtrinc", 4),
|
410 |
+
("gdtrix", 3),
|
411 |
+
("gdtrib", 3),
|
412 |
+
("gdtria", 3),
|
413 |
+
("nbdtrik", 3),
|
414 |
+
("nbdtrin", 3),
|
415 |
+
("nrdtrimn", 3),
|
416 |
+
("nrdtrisd", 3),
|
417 |
+
("pdtrik", 2),
|
418 |
+
("stdtr", 2),
|
419 |
+
("stdtrit", 2),
|
420 |
+
("stdtridf", 2),
|
421 |
+
("nctdtr", 3),
|
422 |
+
("nctdtrit", 3),
|
423 |
+
("nctdtridf", 3),
|
424 |
+
("nctdtrinc", 3),
|
425 |
+
("tklmbda", 2),
|
426 |
+
]
|
427 |
+
|
428 |
+
|
429 |
+
@pytest.mark.parametrize('func,numargs', funcs, ids=[x[0] for x in funcs])
|
430 |
+
def test_nonfinite(func, numargs):
|
431 |
+
|
432 |
+
rng = np.random.default_rng(1701299355559735)
|
433 |
+
func = getattr(sp, func)
|
434 |
+
args_choices = [(float(x), np.nan, np.inf, -np.inf) for x in rng.random(numargs)]
|
435 |
+
|
436 |
+
for args in itertools.product(*args_choices):
|
437 |
+
res = func(*args)
|
438 |
+
|
439 |
+
if any(np.isnan(x) for x in args):
|
440 |
+
# Nan inputs should result to nan output
|
441 |
+
assert_equal(res, np.nan)
|
442 |
+
else:
|
443 |
+
# All other inputs should return something (but not
|
444 |
+
# raise exceptions or cause hangs)
|
445 |
+
pass
|
446 |
+
|
447 |
+
|
448 |
+
def test_chndtrix_gh2158():
|
449 |
+
# test that gh-2158 is resolved; previously this blew up
|
450 |
+
res = sp.chndtrix(0.999999, 2, np.arange(20.)+1e-6)
|
451 |
+
|
452 |
+
# Generated in R
|
453 |
+
# options(digits=16)
|
454 |
+
# ncp <- seq(0, 19) + 1e-6
|
455 |
+
# print(qchisq(0.999999, df = 2, ncp = ncp))
|
456 |
+
res_exp = [27.63103493142305, 35.25728589950540, 39.97396073236288,
|
457 |
+
43.88033702110538, 47.35206403482798, 50.54112500166103,
|
458 |
+
53.52720257322766, 56.35830042867810, 59.06600769498512,
|
459 |
+
61.67243118946381, 64.19376191277179, 66.64228141346548,
|
460 |
+
69.02756927200180, 71.35726934749408, 73.63759723904816,
|
461 |
+
75.87368842650227, 78.06984431185720, 80.22971052389806,
|
462 |
+
82.35640899964173, 84.45263768373256]
|
463 |
+
assert_allclose(res, res_exp)
|
464 |
+
|
465 |
+
@pytest.mark.xfail_on_32bit("32bit fails due to algorithm threshold")
|
466 |
+
def test_nctdtr_gh19896():
|
467 |
+
# test that gh-19896 is resolved.
|
468 |
+
# Compared to SciPy 1.11 results from Fortran code.
|
469 |
+
dfarr = [0.98, 9.8, 98, 980]
|
470 |
+
pnoncarr = [-3.8, 0.38, 3.8, 38]
|
471 |
+
tarr = [0.0015, 0.15, 1.5, 15]
|
472 |
+
resarr = [0.9999276519560749, 0.9999276519560749, 0.9999908831755221,
|
473 |
+
0.9999990265452424, 0.3524153312279712, 0.39749697267251416,
|
474 |
+
0.7168629634895805, 0.9656246449259646, 7.234804392512006e-05,
|
475 |
+
7.234804392512006e-05, 0.03538804607509127, 0.795482701508521,
|
476 |
+
0.0, 0.0, 0.0,
|
477 |
+
0.011927908523093889, 0.9999276519560749, 0.9999276519560749,
|
478 |
+
0.9999997441133123, 1.0, 0.3525155979118013,
|
479 |
+
0.4076312014048369, 0.8476794017035086, 0.9999999297116268,
|
480 |
+
7.234804392512006e-05, 7.234804392512006e-05, 0.013477443099785824,
|
481 |
+
0.9998501512331494, 0.0, 0.0,
|
482 |
+
0.0, 6.561112613212572e-07, 0.9999276519560749,
|
483 |
+
0.9999276519560749, 0.9999999313496014, 1.0,
|
484 |
+
0.3525281784865706, 0.40890253001898014, 0.8664672830017024,
|
485 |
+
1.0, 7.234804392512006e-05, 7.234804392512006e-05,
|
486 |
+
0.010990889489704836, 1.0, 0.0,
|
487 |
+
0.0, 0.0, 0.0,
|
488 |
+
0.9999276519560749, 0.9999276519560749, 0.9999999418789304,
|
489 |
+
1.0, 0.35252945487817355, 0.40903153246690993,
|
490 |
+
0.8684247068528264, 1.0, 7.234804392512006e-05,
|
491 |
+
7.234804392512006e-05, 0.01075068918582911, 1.0,
|
492 |
+
0.0, 0.0, 0.0, 0.0]
|
493 |
+
actarr = []
|
494 |
+
for df, p, t in itertools.product(dfarr, pnoncarr, tarr):
|
495 |
+
actarr += [sp.nctdtr(df, p, t)]
|
496 |
+
# The rtol is kept high on purpose to make it pass on 32bit systems
|
497 |
+
assert_allclose(actarr, resarr, rtol=1e-6, atol=0.0)
|
498 |
+
|
499 |
+
|
500 |
+
def test_nctdtrinc_gh19896():
|
501 |
+
# test that gh-19896 is resolved.
|
502 |
+
# Compared to SciPy 1.11 results from Fortran code.
|
503 |
+
dfarr = [0.001, 0.98, 9.8, 98, 980, 10000, 98, 9.8, 0.98, 0.001]
|
504 |
+
parr = [0.001, 0.1, 0.3, 0.8, 0.999, 0.001, 0.1, 0.3, 0.8, 0.999]
|
505 |
+
tarr = [0.0015, 0.15, 1.5, 15, 300, 0.0015, 0.15, 1.5, 15, 300]
|
506 |
+
desired = [3.090232306168629, 1.406141304556198, 2.014225177124157,
|
507 |
+
13.727067118283456, 278.9765683871208, 3.090232306168629,
|
508 |
+
1.4312427877936222, 2.014225177124157, 3.712743137978295,
|
509 |
+
-3.086951096691082]
|
510 |
+
actual = sp.nctdtrinc(dfarr, parr, tarr)
|
511 |
+
assert_allclose(actual, desired, rtol=5e-12, atol=0.0)
|
512 |
+
|
513 |
+
|
514 |
+
def test_stdtr_stdtrit_neg_inf():
|
515 |
+
# -inf was treated as +inf and values from the normal were returned
|
516 |
+
assert np.all(np.isnan(sp.stdtr(-np.inf, [-np.inf, -1.0, 0.0, 1.0, np.inf])))
|
517 |
+
assert np.all(np.isnan(sp.stdtrit(-np.inf, [0.0, 0.25, 0.5, 0.75, 1.0])))
|
518 |
+
|
519 |
+
|
520 |
+
def test_bdtrik_nbdtrik_inf():
|
521 |
+
y = np.array(
|
522 |
+
[np.nan,-np.inf,-10.0, -1.0, 0.0, .00001, .5, 0.9999, 1.0, 10.0, np.inf])
|
523 |
+
y = y[:,None]
|
524 |
+
p = np.atleast_2d(
|
525 |
+
[np.nan, -np.inf, -10.0, -1.0, 0.0, .00001, .5, 1.0, np.inf])
|
526 |
+
assert np.all(np.isnan(sp.bdtrik(y, np.inf, p)))
|
527 |
+
assert np.all(np.isnan(sp.nbdtrik(y, np.inf, p)))
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_cdft_asymptotic.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# gh-14777 regression tests
|
2 |
+
# Test stdtr and stdtrit with infinite df and large values of df
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
from numpy.testing import assert_allclose, assert_equal
|
6 |
+
from scipy.special import stdtr, stdtrit, ndtr, ndtri
|
7 |
+
|
8 |
+
|
9 |
+
def test_stdtr_vs_R_large_df():
|
10 |
+
df = [1e10, 1e12, 1e120, np.inf]
|
11 |
+
t = 1.
|
12 |
+
res = stdtr(df, t)
|
13 |
+
# R Code:
|
14 |
+
# options(digits=20)
|
15 |
+
# pt(1., c(1e10, 1e12, 1e120, Inf))
|
16 |
+
res_R = [0.84134474605644460343,
|
17 |
+
0.84134474606842180044,
|
18 |
+
0.84134474606854281475,
|
19 |
+
0.84134474606854292578]
|
20 |
+
assert_allclose(res, res_R, rtol=2e-15)
|
21 |
+
# last value should also agree with ndtr
|
22 |
+
assert_equal(res[3], ndtr(1.))
|
23 |
+
|
24 |
+
|
25 |
+
def test_stdtrit_vs_R_large_df():
|
26 |
+
df = [1e10, 1e12, 1e120, np.inf]
|
27 |
+
p = 0.1
|
28 |
+
res = stdtrit(df, p)
|
29 |
+
# R Code:
|
30 |
+
# options(digits=20)
|
31 |
+
# qt(0.1, c(1e10, 1e12, 1e120, Inf))
|
32 |
+
res_R = [-1.2815515656292593150,
|
33 |
+
-1.2815515655454472466,
|
34 |
+
-1.2815515655446008125,
|
35 |
+
-1.2815515655446008125]
|
36 |
+
assert_allclose(res, res_R, rtol=1e-14, atol=1e-15)
|
37 |
+
# last value should also agree with ndtri
|
38 |
+
assert_equal(res[3], ndtri(0.1))
|
39 |
+
|
40 |
+
|
41 |
+
def test_stdtr_stdtri_invalid():
|
42 |
+
# a mix of large and inf df with t/p equal to nan
|
43 |
+
df = [1e10, 1e12, 1e120, np.inf]
|
44 |
+
x = np.nan
|
45 |
+
res1 = stdtr(df, x)
|
46 |
+
res2 = stdtrit(df, x)
|
47 |
+
res_ex = 4*[np.nan]
|
48 |
+
assert_equal(res1, res_ex)
|
49 |
+
assert_equal(res2, res_ex)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_cosine_distr.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import assert_allclose
|
3 |
+
import pytest
|
4 |
+
from scipy.special._ufuncs import _cosine_cdf, _cosine_invcdf
|
5 |
+
|
6 |
+
|
7 |
+
# These values are (x, p) where p is the expected exact value of
|
8 |
+
# _cosine_cdf(x). These values will be tested for exact agreement.
|
9 |
+
_coscdf_exact = [
|
10 |
+
(-4.0, 0.0),
|
11 |
+
(0, 0.5),
|
12 |
+
(np.pi, 1.0),
|
13 |
+
(4.0, 1.0),
|
14 |
+
]
|
15 |
+
|
16 |
+
@pytest.mark.parametrize("x, expected", _coscdf_exact)
|
17 |
+
def test_cosine_cdf_exact(x, expected):
|
18 |
+
assert _cosine_cdf(x) == expected
|
19 |
+
|
20 |
+
|
21 |
+
# These values are (x, p), where p is the expected value of
|
22 |
+
# _cosine_cdf(x). The expected values were computed with mpmath using
|
23 |
+
# 50 digits of precision. These values will be tested for agreement
|
24 |
+
# with the computed values using a very small relative tolerance.
|
25 |
+
# The value at -np.pi is not 0, because -np.pi does not equal -π.
|
26 |
+
_coscdf_close = [
|
27 |
+
(3.1409, 0.999999999991185),
|
28 |
+
(2.25, 0.9819328173287907),
|
29 |
+
# -1.6 is the threshold below which the Pade approximant is used.
|
30 |
+
(-1.599, 0.08641959838382553),
|
31 |
+
(-1.601, 0.086110582992713),
|
32 |
+
(-2.0, 0.0369709335961611),
|
33 |
+
(-3.0, 7.522387241801384e-05),
|
34 |
+
(-3.1415, 2.109869685443648e-14),
|
35 |
+
(-3.14159, 4.956444476505336e-19),
|
36 |
+
(-np.pi, 4.871934450264861e-50),
|
37 |
+
]
|
38 |
+
|
39 |
+
@pytest.mark.parametrize("x, expected", _coscdf_close)
|
40 |
+
def test_cosine_cdf(x, expected):
|
41 |
+
assert_allclose(_cosine_cdf(x), expected, rtol=5e-15)
|
42 |
+
|
43 |
+
|
44 |
+
# These values are (p, x) where x is the expected exact value of
|
45 |
+
# _cosine_invcdf(p). These values will be tested for exact agreement.
|
46 |
+
_cosinvcdf_exact = [
|
47 |
+
(0.0, -np.pi),
|
48 |
+
(0.5, 0.0),
|
49 |
+
(1.0, np.pi),
|
50 |
+
]
|
51 |
+
|
52 |
+
@pytest.mark.parametrize("p, expected", _cosinvcdf_exact)
|
53 |
+
def test_cosine_invcdf_exact(p, expected):
|
54 |
+
assert _cosine_invcdf(p) == expected
|
55 |
+
|
56 |
+
|
57 |
+
def test_cosine_invcdf_invalid_p():
|
58 |
+
# Check that p values outside of [0, 1] return nan.
|
59 |
+
assert np.isnan(_cosine_invcdf([-0.1, 1.1])).all()
|
60 |
+
|
61 |
+
|
62 |
+
# These values are (p, x), where x is the expected value of _cosine_invcdf(p).
|
63 |
+
# The expected values were computed with mpmath using 50 digits of precision.
|
64 |
+
_cosinvcdf_close = [
|
65 |
+
(1e-50, -np.pi),
|
66 |
+
(1e-14, -3.1415204137058454),
|
67 |
+
(1e-08, -3.1343686589124524),
|
68 |
+
(0.0018001, -2.732563923138336),
|
69 |
+
(0.010, -2.41276589008678),
|
70 |
+
(0.060, -1.7881244975330157),
|
71 |
+
(0.125, -1.3752523669869274),
|
72 |
+
(0.250, -0.831711193579736),
|
73 |
+
(0.400, -0.3167954512395289),
|
74 |
+
(0.419, -0.25586025626919906),
|
75 |
+
(0.421, -0.24947570750445663),
|
76 |
+
(0.750, 0.831711193579736),
|
77 |
+
(0.940, 1.7881244975330153),
|
78 |
+
(0.9999999996, 3.1391220839917167),
|
79 |
+
]
|
80 |
+
|
81 |
+
@pytest.mark.parametrize("p, expected", _cosinvcd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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from typing import Callable
|
3 |
+
|
4 |
+
import pytest
|
5 |
+
from itertools import product
|
6 |
+
from numpy.testing import assert_allclose, suppress_warnings
|
7 |
+
from scipy import special
|
8 |
+
from scipy.special import cython_special
|
9 |
+
|
10 |
+
|
11 |
+
bint_points = [True, False]
|
12 |
+
int_points = [-10, -1, 1, 10]
|
13 |
+
real_points = [-10.0, -1.0, 1.0, 10.0]
|
14 |
+
complex_points = [complex(*tup) for tup in product(real_points, repeat=2)]
|
15 |
+
|
16 |
+
|
17 |
+
CYTHON_SIGNATURE_MAP = {
|
18 |
+
'b': 'bint',
|
19 |
+
'f': 'float',
|
20 |
+
'd': 'double',
|
21 |
+
'g': 'long double',
|
22 |
+
'F': 'float complex',
|
23 |
+
'D': 'double complex',
|
24 |
+
'G': 'long double complex',
|
25 |
+
'i': 'int',
|
26 |
+
'l': 'long'
|
27 |
+
}
|
28 |
+
|
29 |
+
|
30 |
+
TEST_POINTS = {
|
31 |
+
'b': bint_points,
|
32 |
+
'f': real_points,
|
33 |
+
'd': real_points,
|
34 |
+
'g': real_points,
|
35 |
+
'F': complex_points,
|
36 |
+
'D': complex_points,
|
37 |
+
'G': complex_points,
|
38 |
+
'i': int_points,
|
39 |
+
'l': int_points,
|
40 |
+
}
|
41 |
+
|
42 |
+
|
43 |
+
PARAMS: list[tuple[Callable, Callable, tuple[str, ...], str | None]] = [
|
44 |
+
(special.agm, cython_special.agm, ('dd',), None),
|
45 |
+
(special.airy, cython_special._airy_pywrap, ('d', 'D'), None),
|
46 |
+
(special.airye, cython_special._airye_pywrap, ('d', 'D'), None),
|
47 |
+
(special.bdtr, cython_special.bdtr, ('dld', 'ddd'), None),
|
48 |
+
(special.bdtrc, cython_special.bdtrc, ('dld', 'ddd'), None),
|
49 |
+
(special.bdtri, cython_special.bdtri, ('dld', 'ddd'), None),
|
50 |
+
(special.bdtrik, cython_special.bdtrik, ('ddd',), None),
|
51 |
+
(special.bdtrin, cython_special.bdtrin, ('ddd',), None),
|
52 |
+
(special.bei, cython_special.bei, ('d',), None),
|
53 |
+
(special.beip, cython_special.beip, ('d',), None),
|
54 |
+
(special.ber, cython_special.ber, ('d',), None),
|
55 |
+
(special.berp, cython_special.berp, ('d',), None),
|
56 |
+
(special.besselpoly, cython_special.besselpoly, ('ddd',), None),
|
57 |
+
(special.beta, cython_special.beta, ('dd',), None),
|
58 |
+
(special.betainc, cython_special.betainc, ('ddd',), None),
|
59 |
+
(special.betaincc, cython_special.betaincc, ('ddd',), None),
|
60 |
+
(special.betaincinv, cython_special.betaincinv, ('ddd',), None),
|
61 |
+
(special.betainccinv, cython_special.betainccinv, ('ddd',), None),
|
62 |
+
(special.betaln, cython_special.betaln, ('dd',), None),
|
63 |
+
(special.binom, cython_special.binom, ('dd',), None),
|
64 |
+
(special.boxcox, cython_special.boxcox, ('dd',), None),
|
65 |
+
(special.boxcox1p, cython_special.boxcox1p, ('dd',), None),
|
66 |
+
(special.btdtr, cython_special.btdtr, ('ddd',), None),
|
67 |
+
(special.btdtri, cython_special.btdtri, ('ddd',), None),
|
68 |
+
(special.btdtria, cython_special.btdtria, ('ddd',), None),
|
69 |
+
(special.btdtrib, cython_special.btdtrib, ('ddd',), None),
|
70 |
+
(special.cbrt, cython_special.cbrt, ('d',), None),
|
71 |
+
(special.chdtr, cython_special.chdtr, ('dd',), None),
|
72 |
+
(special.chdtrc, cython_special.chdtrc, ('dd',), None),
|
73 |
+
(special.chdtri, cython_special.chdtri, ('dd',), None),
|
74 |
+
(special.chdtriv, cython_special.chdtriv, ('dd',), None),
|
75 |
+
(special.chndtr, cython_special.chndtr, ('ddd',), None),
|
76 |
+
(special.chndtridf, cython_special.chndtridf, ('ddd',), None),
|
77 |
+
(special.chndtrinc, cython_special.chndtrinc, ('ddd',), None),
|
78 |
+
(special.chndtrix, cython_special.chndtrix, ('ddd',), None),
|
79 |
+
(special.cosdg, cython_special.cosdg, ('d',), None),
|
80 |
+
(special.cosm1, cython_special.cosm1, ('d',), None),
|
81 |
+
(special.cotdg, cython_special.cotdg, ('d',), None),
|
82 |
+
(special.dawsn, cython_special.dawsn, ('d', 'D'), None),
|
83 |
+
(special.ellipe, cython_special.ellipe, ('d',), None),
|
84 |
+
(special.ellipeinc, cython_special.ellipeinc, ('dd',), None),
|
85 |
+
(special.ellipj, cython_special._ellipj_pywrap, ('dd',), None),
|
86 |
+
(special.ellipkinc, cython_special.ellipkinc, ('dd',), None),
|
87 |
+
(special.ellipkm1, cython_special.ellipkm1, ('d',), None),
|
88 |
+
(special.ellipk, cython_special.ellipk, ('d',), None),
|
89 |
+
(special.elliprc, cython_special.elliprc, ('dd', 'DD'), None),
|
90 |
+
(special.elliprd, cython_special.elliprd, ('ddd', 'DDD'), None),
|
91 |
+
(special.elliprf, cython_special.elliprf, ('ddd', 'DDD'), None),
|
92 |
+
(special.elliprg, cython_special.elliprg, ('ddd', 'DDD'), None),
|
93 |
+
(special.elliprj, cython_special.elliprj, ('dddd', 'DDDD'), None),
|
94 |
+
(special.entr, cython_special.entr, ('d',), None),
|
95 |
+
(special.erf, cython_special.erf, ('d', 'D'), None),
|
96 |
+
(special.erfc, cython_special.erfc, ('d', 'D'), None),
|
97 |
+
(special.erfcx, cython_special.erfcx, ('d', 'D'), None),
|
98 |
+
(special.erfi, cython_special.erfi, ('d', 'D'), None),
|
99 |
+
(special.erfinv, cython_special.erfinv, ('d',), None),
|
100 |
+
(special.erfcinv, cython_special.erfcinv, ('d',), None),
|
101 |
+
(special.eval_chebyc, cython_special.eval_chebyc, ('dd', 'dD', 'ld'), None),
|
102 |
+
(special.eval_chebys, cython_special.eval_chebys, ('dd', 'dD', 'ld'),
|
103 |
+
'd and l differ for negative int'),
|
104 |
+
(special.eval_chebyt, cython_special.eval_chebyt, ('dd', 'dD', 'ld'),
|
105 |
+
'd and l differ for negative int'),
|
106 |
+
(special.eval_chebyu, cython_special.eval_chebyu, ('dd', 'dD', 'ld'),
|
107 |
+
'd and l differ for negative int'),
|
108 |
+
(special.eval_gegenbauer, cython_special.eval_gegenbauer, ('ddd', 'ddD', 'ldd'),
|
109 |
+
'd and l differ for negative int'),
|
110 |
+
(special.eval_genlaguerre, cython_special.eval_genlaguerre, ('ddd', 'ddD', 'ldd'),
|
111 |
+
'd and l differ for negative int'),
|
112 |
+
(special.eval_hermite, cython_special.eval_hermite, ('ld',), None),
|
113 |
+
(special.eval_hermitenorm, cython_special.eval_hermitenorm, ('ld',), None),
|
114 |
+
(special.eval_jacobi, cython_special.eval_jacobi, ('dddd', 'dddD', 'lddd'),
|
115 |
+
'd and l differ for negative int'),
|
116 |
+
(special.eval_laguerre, cython_special.eval_laguerre, ('dd', 'dD', 'ld'),
|
117 |
+
'd and l differ for negative int'),
|
118 |
+
(special.eval_legendre, cython_special.eval_legendre, ('dd', 'dD', 'ld'), None),
|
119 |
+
(special.eval_sh_chebyt, cython_special.eval_sh_chebyt, ('dd', 'dD', 'ld'), None),
|
120 |
+
(special.eval_sh_chebyu, cython_special.eval_sh_chebyu, ('dd', 'dD', 'ld'),
|
121 |
+
'd and l differ for negative int'),
|
122 |
+
(special.eval_sh_jacobi, cython_special.eval_sh_jacobi, ('dddd', 'dddD', 'lddd'),
|
123 |
+
'd and l differ for negative int'),
|
124 |
+
(special.eval_sh_legendre, cython_special.eval_sh_legendre, ('dd', 'dD', 'ld'),
|
125 |
+
None),
|
126 |
+
(special.exp1, cython_special.exp1, ('d', 'D'), None),
|
127 |
+
(special.exp10, cython_special.exp10, ('d',), None),
|
128 |
+
(special.exp2, cython_special.exp2, ('d',), None),
|
129 |
+
(special.expi, cython_special.expi, ('d', 'D'), None),
|
130 |
+
(special.expit, cython_special.expit, ('f', 'd', 'g'), None),
|
131 |
+
(special.expm1, cython_special.expm1, ('d', 'D'), None),
|
132 |
+
(special.expn, cython_special.expn, ('ld', 'dd'), None),
|
133 |
+
(special.exprel, cython_special.exprel, ('d',), None),
|
134 |
+
(special.fdtr, cython_special.fdtr, ('ddd',), None),
|
135 |
+
(special.fdtrc, cython_special.fdtrc, ('ddd',), None),
|
136 |
+
(special.fdtri, cython_special.fdtri, ('ddd',), None),
|
137 |
+
(special.fdtridfd, cython_special.fdtridfd, ('ddd',), None),
|
138 |
+
(special.fresnel, cython_special._fresnel_pywrap, ('d', 'D'), None),
|
139 |
+
(special.gamma, cython_special.gamma, ('d', 'D'), None),
|
140 |
+
(special.gammainc, cython_special.gammainc, ('dd',), None),
|
141 |
+
(special.gammaincc, cython_special.gammaincc, ('dd',), None),
|
142 |
+
(special.gammainccinv, cython_special.gammainccinv, ('dd',), None),
|
143 |
+
(special.gammaincinv, cython_special.gammaincinv, ('dd',), None),
|
144 |
+
(special.gammaln, cython_special.gammaln, ('d',), None),
|
145 |
+
(special.gammasgn, cython_special.gammasgn, ('d',), None),
|
146 |
+
(special.gdtr, cython_special.gdtr, ('ddd',), None),
|
147 |
+
(special.gdtrc, cython_special.gdtrc, ('ddd',), None),
|
148 |
+
(special.gdtria, cython_special.gdtria, ('ddd',), None),
|
149 |
+
(special.gdtrib, cython_special.gdtrib, ('ddd',), None),
|
150 |
+
(special.gdtrix, cython_special.gdtrix, ('ddd',), None),
|
151 |
+
(special.hankel1, cython_special.hankel1, ('dD',), None),
|
152 |
+
(special.hankel1e, cython_special.hankel1e, ('dD',), None),
|
153 |
+
(special.hankel2, cython_special.hankel2, ('dD',), None),
|
154 |
+
(special.hankel2e, cython_special.hankel2e, ('dD',), None),
|
155 |
+
(special.huber, cython_special.huber, ('dd',), None),
|
156 |
+
(special.hyp0f1, cython_special.hyp0f1, ('dd', 'dD'), None),
|
157 |
+
(special.hyp1f1, cython_special.hyp1f1, ('ddd', 'ddD'), None),
|
158 |
+
(special.hyp2f1, cython_special.hyp2f1, ('dddd', 'dddD'), None),
|
159 |
+
(special.hyperu, cython_special.hyperu, ('ddd',), None),
|
160 |
+
(special.i0, cython_special.i0, ('d',), None),
|
161 |
+
(special.i0e, cython_special.i0e, ('d',), None),
|
162 |
+
(special.i1, cython_special.i1, ('d',), None),
|
163 |
+
(special.i1e, cython_special.i1e, ('d',), None),
|
164 |
+
(special.inv_boxcox, cython_special.inv_boxcox, ('dd',), None),
|
165 |
+
(special.inv_boxcox1p, cython_special.inv_boxcox1p, ('dd',), None),
|
166 |
+
(special.it2i0k0, cython_special._it2i0k0_pywrap, ('d',), None),
|
167 |
+
(special.it2j0y0, cython_special._it2j0y0_pywrap, ('d',), None),
|
168 |
+
(special.it2struve0, cython_special.it2struve0, ('d',), None),
|
169 |
+
(special.itairy, cython_special._itairy_pywrap, ('d',), None),
|
170 |
+
(special.iti0k0, cython_special._iti0k0_pywrap, ('d',), None),
|
171 |
+
(special.itj0y0, cython_special._itj0y0_pywrap, ('d',), None),
|
172 |
+
(special.itmodstruve0, cython_special.itmodstruve0, ('d',), None),
|
173 |
+
(special.itstruve0, cython_special.itstruve0, ('d',), None),
|
174 |
+
(special.iv, cython_special.iv, ('dd', 'dD'), None),
|
175 |
+
(special.ive, cython_special.ive, ('dd', 'dD'), None),
|
176 |
+
(special.j0, cython_special.j0, ('d',), None),
|
177 |
+
(special.j1, cython_special.j1, ('d',), None),
|
178 |
+
(special.jv, cython_special.jv, ('dd', 'dD'), None),
|
179 |
+
(special.jve, cython_special.jve, ('dd', 'dD'), None),
|
180 |
+
(special.k0, cython_special.k0, ('d',), None),
|
181 |
+
(special.k0e, cython_special.k0e, ('d',), None),
|
182 |
+
(special.k1, cython_special.k1, ('d',), None),
|
183 |
+
(special.k1e, cython_special.k1e, ('d',), None),
|
184 |
+
(special.kei, cython_special.kei, ('d',), None),
|
185 |
+
(special.keip, cython_special.keip, ('d',), None),
|
186 |
+
(special.kelvin, cython_special._kelvin_pywrap, ('d',), None),
|
187 |
+
(special.ker, cython_special.ker, ('d',), None),
|
188 |
+
(special.kerp, cython_special.kerp, ('d',), None),
|
189 |
+
(special.kl_div, cython_special.kl_div, ('dd',), None),
|
190 |
+
(special.kn, cython_special.kn, ('ld', 'dd'), None),
|
191 |
+
(special.kolmogi, cython_special.kolmogi, ('d',), None),
|
192 |
+
(special.kolmogorov, cython_special.kolmogorov, ('d',), None),
|
193 |
+
(special.kv, cython_special.kv, ('dd', 'dD'), None),
|
194 |
+
(special.kve, cython_special.kve, ('dd', 'dD'), None),
|
195 |
+
(special.log1p, cython_special.log1p, ('d', 'D'), None),
|
196 |
+
(special.log_expit, cython_special.log_expit, ('f', 'd', 'g'), None),
|
197 |
+
(special.log_ndtr, cython_special.log_ndtr, ('d', 'D'), None),
|
198 |
+
(special.ndtri_exp, cython_special.ndtri_exp, ('d',), None),
|
199 |
+
(special.loggamma, cython_special.loggamma, ('D',), None),
|
200 |
+
(special.logit, cython_special.logit, ('f', 'd', 'g'), None),
|
201 |
+
(special.lpmv, cython_special.lpmv, ('ddd',), None),
|
202 |
+
(special.mathieu_a, cython_special.mathieu_a, ('dd',), None),
|
203 |
+
(special.mathieu_b, cython_special.mathieu_b, ('dd',), None),
|
204 |
+
(special.mathieu_cem, cython_special._mathieu_cem_pywrap, ('ddd',), None),
|
205 |
+
(special.mathieu_modcem1, cython_special._mathieu_modcem1_pywrap, ('ddd',), None),
|
206 |
+
(special.mathieu_modcem2, cython_special._mathieu_modcem2_pywrap, ('ddd',), None),
|
207 |
+
(special.mathieu_modsem1, cython_special._mathieu_modsem1_pywrap, ('ddd',), None),
|
208 |
+
(special.mathieu_modsem2, cython_special._mathieu_modsem2_pywrap, ('ddd',), None),
|
209 |
+
(special.mathieu_sem, cython_special._mathieu_sem_pywrap, ('ddd',), None),
|
210 |
+
(special.modfresnelm, cython_special._modfresnelm_pywrap, ('d',), None),
|
211 |
+
(special.modfresnelp, cython_special._modfresnelp_pywrap, ('d',), None),
|
212 |
+
(special.modstruve, cython_special.modstruve, ('dd',), None),
|
213 |
+
(special.nbdtr, cython_special.nbdtr, ('lld', 'ddd'), None),
|
214 |
+
(special.nbdtrc, cython_special.nbdtrc, ('lld', 'ddd'), None),
|
215 |
+
(special.nbdtri, cython_special.nbdtri, ('lld', 'ddd'), None),
|
216 |
+
(special.nbdtrik, cython_special.nbdtrik, ('ddd',), None),
|
217 |
+
(special.nbdtrin, cython_special.nbdtrin, ('ddd',), None),
|
218 |
+
(special.ncfdtr, cython_special.ncfdtr, ('dddd',), None),
|
219 |
+
(special.ncfdtri, cython_special.ncfdtri, ('dddd',), None),
|
220 |
+
(special.ncfdtridfd, cython_special.ncfdtridfd, ('dddd',), None),
|
221 |
+
(special.ncfdtridfn, cython_special.ncfdtridfn, ('dddd',), None),
|
222 |
+
(special.ncfdtrinc, cython_special.ncfdtrinc, ('dddd',), None),
|
223 |
+
(special.nctdtr, cython_special.nctdtr, ('ddd',), None),
|
224 |
+
(special.nctdtridf, cython_special.nctdtridf, ('ddd',), None),
|
225 |
+
(special.nctdtrinc, cython_special.nctdtrinc, ('ddd',), None),
|
226 |
+
(special.nctdtrit, cython_special.nctdtrit, ('ddd',), None),
|
227 |
+
(special.ndtr, cython_special.ndtr, ('d', 'D'), None),
|
228 |
+
(special.ndtri, cython_special.ndtri, ('d',), None),
|
229 |
+
(special.nrdtrimn, cython_special.nrdtrimn, ('ddd',), None),
|
230 |
+
(special.nrdtrisd, cython_special.nrdtrisd, ('ddd',), None),
|
231 |
+
(special.obl_ang1, cython_special._obl_ang1_pywrap, ('dddd',), None),
|
232 |
+
(special.obl_ang1_cv, cython_special._obl_ang1_cv_pywrap, ('ddddd',), None),
|
233 |
+
(special.obl_cv, cython_special.obl_cv, ('ddd',), None),
|
234 |
+
(special.obl_rad1, cython_special._obl_rad1_pywrap, ('dddd',), "see gh-6211"),
|
235 |
+
(special.obl_rad1_cv, cython_special._obl_rad1_cv_pywrap, ('ddddd',),
|
236 |
+
"see gh-6211"),
|
237 |
+
(special.obl_rad2, cython_special._obl_rad2_pywrap, ('dddd',), "see gh-6211"),
|
238 |
+
(special.obl_rad2_cv, cython_special._obl_rad2_cv_pywrap, ('ddddd',),
|
239 |
+
"see gh-6211"),
|
240 |
+
(special.pbdv, cython_special._pbdv_pywrap, ('dd',), None),
|
241 |
+
(special.pbvv, cython_special._pbvv_pywrap, ('dd',), None),
|
242 |
+
(special.pbwa, cython_special._pbwa_pywrap, ('dd',), None),
|
243 |
+
(special.pdtr, cython_special.pdtr, ('dd', 'dd'), None),
|
244 |
+
(special.pdtrc, cython_special.pdtrc, ('dd', 'dd'), None),
|
245 |
+
(special.pdtri, cython_special.pdtri, ('ld', 'dd'), None),
|
246 |
+
(special.pdtrik, cython_special.pdtrik, ('dd',), None),
|
247 |
+
(special.poch, cython_special.poch, ('dd',), None),
|
248 |
+
(special.powm1, cython_special.powm1, ('dd',), None),
|
249 |
+
(special.pro_ang1, cython_special._pro_ang1_pywrap, ('dddd',), None),
|
250 |
+
(special.pro_ang1_cv, cython_special._pro_ang1_cv_pywrap, ('ddddd',), None),
|
251 |
+
(special.pro_cv, cython_special.pro_cv, ('ddd',), None),
|
252 |
+
(special.pro_rad1, cython_special._pro_rad1_pywrap, ('dddd',), "see gh-6211"),
|
253 |
+
(special.pro_rad1_cv, cython_special._pro_rad1_cv_pywrap, ('ddddd',),
|
254 |
+
"see gh-6211"),
|
255 |
+
(special.pro_rad2, cython_special._pro_rad2_pywrap, ('dddd',), "see gh-6211"),
|
256 |
+
(special.pro_rad2_cv, cython_special._pro_rad2_cv_pywrap, ('ddddd',),
|
257 |
+
"see gh-6211"),
|
258 |
+
(special.pseudo_huber, cython_special.pseudo_huber, ('dd',), None),
|
259 |
+
(special.psi, cython_special.psi, ('d', 'D'), None),
|
260 |
+
(special.radian, cython_special.radian, ('ddd',), None),
|
261 |
+
(special.rel_entr, cython_special.rel_entr, ('dd',), None),
|
262 |
+
(special.rgamma, cython_special.rgamma, ('d', 'D'), None),
|
263 |
+
(special.round, cython_special.round, ('d',), None),
|
264 |
+
(special.spherical_jn, cython_special.spherical_jn, ('ld', 'ldb', 'lD', 'lDb'),
|
265 |
+
None),
|
266 |
+
(special.spherical_yn, cython_special.spherical_yn, ('ld', 'ldb', 'lD', 'lDb'),
|
267 |
+
None),
|
268 |
+
(special.spherical_in, cython_special.spherical_in, ('ld', 'ldb', 'lD', 'lDb'),
|
269 |
+
None),
|
270 |
+
(special.spherical_kn, cython_special.spherical_kn, ('ld', 'ldb', 'lD', 'lDb'),
|
271 |
+
None),
|
272 |
+
(special.shichi, cython_special._shichi_pywrap, ('d', 'D'), None),
|
273 |
+
(special.sici, cython_special._sici_pywrap, ('d', 'D'), None),
|
274 |
+
(special.sindg, cython_special.sindg, ('d',), None),
|
275 |
+
(special.smirnov, cython_special.smirnov, ('ld', 'dd'), None),
|
276 |
+
(special.smirnovi, cython_special.smirnovi, ('ld', 'dd'), None),
|
277 |
+
(special.spence, cython_special.spence, ('d', 'D'), None),
|
278 |
+
(special.sph_harm, cython_special.sph_harm, ('lldd', 'dddd'), None),
|
279 |
+
(special.stdtr, cython_special.stdtr, ('dd',), None),
|
280 |
+
(special.stdtridf, cython_special.stdtridf, ('dd',), None),
|
281 |
+
(special.stdtrit, cython_special.stdtrit, ('dd',), None),
|
282 |
+
(special.struve, cython_special.struve, ('dd',), None),
|
283 |
+
(special.tandg, cython_special.tandg, ('d',), None),
|
284 |
+
(special.tklmbda, cython_special.tklmbda, ('dd',), None),
|
285 |
+
(special.voigt_profile, cython_special.voigt_profile, ('ddd',), None),
|
286 |
+
(special.wofz, cython_special.wofz, ('D',), None),
|
287 |
+
(special.wright_bessel, cython_special.wright_bessel, ('ddd',), None),
|
288 |
+
(special.wrightomega, cython_special.wrightomega, ('D',), None),
|
289 |
+
(special.xlog1py, cython_special.xlog1py, ('dd', 'DD'), None),
|
290 |
+
(special.xlogy, cython_special.xlogy, ('dd', 'DD'), None),
|
291 |
+
(special.y0, cython_special.y0, ('d',), None),
|
292 |
+
(special.y1, cython_special.y1, ('d',), None),
|
293 |
+
(special.yn, cython_special.yn, ('ld', 'dd'), None),
|
294 |
+
(special.yv, cython_special.yv, ('dd', 'dD'), None),
|
295 |
+
(special.yve, cython_special.yve, ('dd', 'dD'), None),
|
296 |
+
(special.zetac, cython_special.zetac, ('d',), None),
|
297 |
+
(special.owens_t, cython_special.owens_t, ('dd',), None)
|
298 |
+
]
|
299 |
+
|
300 |
+
|
301 |
+
IDS = [x[0].__name__ for x in PARAMS]
|
302 |
+
|
303 |
+
|
304 |
+
def _generate_test_points(typecodes):
|
305 |
+
axes = tuple(TEST_POINTS[x] for x in typecodes)
|
306 |
+
pts = list(product(*axes))
|
307 |
+
return pts
|
308 |
+
|
309 |
+
|
310 |
+
def test_cython_api_completeness():
|
311 |
+
# Check that everything is tested
|
312 |
+
for name in dir(cython_special):
|
313 |
+
func = getattr(cython_special, name)
|
314 |
+
if callable(func) and not name.startswith('_'):
|
315 |
+
for _, cyfun, _, _ in PARAMS:
|
316 |
+
if cyfun is func:
|
317 |
+
break
|
318 |
+
else:
|
319 |
+
raise RuntimeError(f"{name} missing from tests!")
|
320 |
+
|
321 |
+
|
322 |
+
@pytest.mark.parametrize("param", PARAMS, ids=IDS)
|
323 |
+
def test_cython_api(param):
|
324 |
+
pyfunc, cyfunc, specializations, knownfailure = param
|
325 |
+
if knownfailure:
|
326 |
+
pytest.xfail(reason=knownfailure)
|
327 |
+
|
328 |
+
# Check which parameters are expected to be fused types
|
329 |
+
max_params = max(len(spec) for spec in specializations)
|
330 |
+
values = [set() for _ in range(max_params)]
|
331 |
+
for typecodes in specializations:
|
332 |
+
for j, v in enumerate(typecodes):
|
333 |
+
values[j].add(v)
|
334 |
+
seen = set()
|
335 |
+
is_fused_code = [False] * len(values)
|
336 |
+
for j, v in enumerate(values):
|
337 |
+
vv = tuple(sorted(v))
|
338 |
+
if vv in seen:
|
339 |
+
continue
|
340 |
+
is_fused_code[j] = (len(v) > 1)
|
341 |
+
seen.add(vv)
|
342 |
+
|
343 |
+
# Check results
|
344 |
+
for typecodes in specializations:
|
345 |
+
# Pick the correct specialized function
|
346 |
+
signature = [CYTHON_SIGNATURE_MAP[code]
|
347 |
+
for j, code in enumerate(typecodes)
|
348 |
+
if is_fused_code[j]]
|
349 |
+
|
350 |
+
if signature:
|
351 |
+
cy_spec_func = cyfunc[tuple(signature)]
|
352 |
+
else:
|
353 |
+
signature = None
|
354 |
+
cy_spec_func = cyfunc
|
355 |
+
|
356 |
+
# Test it
|
357 |
+
pts = _generate_test_points(typecodes)
|
358 |
+
for pt in pts:
|
359 |
+
with suppress_warnings() as sup:
|
360 |
+
sup.filter(DeprecationWarning)
|
361 |
+
pyval = pyfunc(*pt)
|
362 |
+
cyval = cy_spec_func(*pt)
|
363 |
+
assert_allclose(cyval, pyval, err_msg=f"{pt} {typecodes} {signature}")
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_data.py
ADDED
@@ -0,0 +1,725 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import importlib.resources
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from numpy.testing import suppress_warnings
|
5 |
+
import pytest
|
6 |
+
|
7 |
+
from scipy.special import (
|
8 |
+
lpn, lpmn, lpmv, lqn, lqmn, sph_harm, eval_legendre, eval_hermite,
|
9 |
+
eval_laguerre, eval_genlaguerre, binom, cbrt, expm1, log1p, zeta,
|
10 |
+
jn, jv, jvp, yn, yv, yvp, iv, ivp, kn, kv, kvp,
|
11 |
+
gamma, gammaln, gammainc, gammaincc, gammaincinv, gammainccinv, digamma,
|
12 |
+
beta, betainc, betaincinv, poch,
|
13 |
+
ellipe, ellipeinc, ellipk, ellipkm1, ellipkinc,
|
14 |
+
elliprc, elliprd, elliprf, elliprg, elliprj,
|
15 |
+
erf, erfc, erfinv, erfcinv, exp1, expi, expn,
|
16 |
+
bdtrik, btdtr, btdtri, btdtria, btdtrib, chndtr, gdtr, gdtrc, gdtrix, gdtrib,
|
17 |
+
nbdtrik, pdtrik, owens_t,
|
18 |
+
mathieu_a, mathieu_b, mathieu_cem, mathieu_sem, mathieu_modcem1,
|
19 |
+
mathieu_modsem1, mathieu_modcem2, mathieu_modsem2,
|
20 |
+
ellip_harm, ellip_harm_2, spherical_jn, spherical_yn, wright_bessel
|
21 |
+
)
|
22 |
+
from scipy.integrate import IntegrationWarning
|
23 |
+
|
24 |
+
from scipy.special._testutils import FuncData
|
25 |
+
|
26 |
+
|
27 |
+
# The npz files are generated, and hence may live in the build dir. We can only
|
28 |
+
# access them through `importlib.resources`, not an explicit path from `__file__`
|
29 |
+
_datadir = importlib.resources.files('scipy.special.tests.data')
|
30 |
+
|
31 |
+
_boost_npz = _datadir.joinpath('boost.npz')
|
32 |
+
with importlib.resources.as_file(_boost_npz) as f:
|
33 |
+
DATASETS_BOOST = np.load(f)
|
34 |
+
|
35 |
+
_gsl_npz = _datadir.joinpath('gsl.npz')
|
36 |
+
with importlib.resources.as_file(_gsl_npz) as f:
|
37 |
+
DATASETS_GSL = np.load(f)
|
38 |
+
|
39 |
+
_local_npz = _datadir.joinpath('local.npz')
|
40 |
+
with importlib.resources.as_file(_local_npz) as f:
|
41 |
+
DATASETS_LOCAL = np.load(f)
|
42 |
+
|
43 |
+
|
44 |
+
def data(func, dataname, *a, **kw):
|
45 |
+
kw.setdefault('dataname', dataname)
|
46 |
+
return FuncData(func, DATASETS_BOOST[dataname], *a, **kw)
|
47 |
+
|
48 |
+
|
49 |
+
def data_gsl(func, dataname, *a, **kw):
|
50 |
+
kw.setdefault('dataname', dataname)
|
51 |
+
return FuncData(func, DATASETS_GSL[dataname], *a, **kw)
|
52 |
+
|
53 |
+
|
54 |
+
def data_local(func, dataname, *a, **kw):
|
55 |
+
kw.setdefault('dataname', dataname)
|
56 |
+
return FuncData(func, DATASETS_LOCAL[dataname], *a, **kw)
|
57 |
+
|
58 |
+
|
59 |
+
def ellipk_(k):
|
60 |
+
return ellipk(k*k)
|
61 |
+
|
62 |
+
|
63 |
+
def ellipkinc_(f, k):
|
64 |
+
return ellipkinc(f, k*k)
|
65 |
+
|
66 |
+
|
67 |
+
def ellipe_(k):
|
68 |
+
return ellipe(k*k)
|
69 |
+
|
70 |
+
|
71 |
+
def ellipeinc_(f, k):
|
72 |
+
return ellipeinc(f, k*k)
|
73 |
+
|
74 |
+
|
75 |
+
def zeta_(x):
|
76 |
+
return zeta(x, 1.)
|
77 |
+
|
78 |
+
|
79 |
+
def assoc_legendre_p_boost_(nu, mu, x):
|
80 |
+
# the boost test data is for integer orders only
|
81 |
+
return lpmv(mu, nu.astype(int), x)
|
82 |
+
|
83 |
+
def legendre_p_via_assoc_(nu, x):
|
84 |
+
return lpmv(0, nu, x)
|
85 |
+
|
86 |
+
def lpn_(n, x):
|
87 |
+
return lpn(n.astype('l'), x)[0][-1]
|
88 |
+
|
89 |
+
def lqn_(n, x):
|
90 |
+
return lqn(n.astype('l'), x)[0][-1]
|
91 |
+
|
92 |
+
def legendre_p_via_lpmn(n, x):
|
93 |
+
return lpmn(0, n, x)[0][0,-1]
|
94 |
+
|
95 |
+
def legendre_q_via_lqmn(n, x):
|
96 |
+
return lqmn(0, n, x)[0][0,-1]
|
97 |
+
|
98 |
+
def mathieu_ce_rad(m, q, x):
|
99 |
+
return mathieu_cem(m, q, x*180/np.pi)[0]
|
100 |
+
|
101 |
+
|
102 |
+
def mathieu_se_rad(m, q, x):
|
103 |
+
return mathieu_sem(m, q, x*180/np.pi)[0]
|
104 |
+
|
105 |
+
|
106 |
+
def mathieu_mc1_scaled(m, q, x):
|
107 |
+
# GSL follows a different normalization.
|
108 |
+
# We follow Abramowitz & Stegun, they apparently something else.
|
109 |
+
return mathieu_modcem1(m, q, x)[0] * np.sqrt(np.pi/2)
|
110 |
+
|
111 |
+
|
112 |
+
def mathieu_ms1_scaled(m, q, x):
|
113 |
+
return mathieu_modsem1(m, q, x)[0] * np.sqrt(np.pi/2)
|
114 |
+
|
115 |
+
|
116 |
+
def mathieu_mc2_scaled(m, q, x):
|
117 |
+
return mathieu_modcem2(m, q, x)[0] * np.sqrt(np.pi/2)
|
118 |
+
|
119 |
+
|
120 |
+
def mathieu_ms2_scaled(m, q, x):
|
121 |
+
return mathieu_modsem2(m, q, x)[0] * np.sqrt(np.pi/2)
|
122 |
+
|
123 |
+
def eval_legendre_ld(n, x):
|
124 |
+
return eval_legendre(n.astype('l'), x)
|
125 |
+
|
126 |
+
def eval_legendre_dd(n, x):
|
127 |
+
return eval_legendre(n.astype('d'), x)
|
128 |
+
|
129 |
+
def eval_hermite_ld(n, x):
|
130 |
+
return eval_hermite(n.astype('l'), x)
|
131 |
+
|
132 |
+
def eval_laguerre_ld(n, x):
|
133 |
+
return eval_laguerre(n.astype('l'), x)
|
134 |
+
|
135 |
+
def eval_laguerre_dd(n, x):
|
136 |
+
return eval_laguerre(n.astype('d'), x)
|
137 |
+
|
138 |
+
def eval_genlaguerre_ldd(n, a, x):
|
139 |
+
return eval_genlaguerre(n.astype('l'), a, x)
|
140 |
+
|
141 |
+
def eval_genlaguerre_ddd(n, a, x):
|
142 |
+
return eval_genlaguerre(n.astype('d'), a, x)
|
143 |
+
|
144 |
+
def bdtrik_comp(y, n, p):
|
145 |
+
return bdtrik(1-y, n, p)
|
146 |
+
|
147 |
+
def btdtri_comp(a, b, p):
|
148 |
+
return btdtri(a, b, 1-p)
|
149 |
+
|
150 |
+
def btdtria_comp(p, b, x):
|
151 |
+
return btdtria(1-p, b, x)
|
152 |
+
|
153 |
+
def btdtrib_comp(a, p, x):
|
154 |
+
return btdtrib(a, 1-p, x)
|
155 |
+
|
156 |
+
def gdtr_(p, x):
|
157 |
+
return gdtr(1.0, p, x)
|
158 |
+
|
159 |
+
def gdtrc_(p, x):
|
160 |
+
return gdtrc(1.0, p, x)
|
161 |
+
|
162 |
+
def gdtrix_(b, p):
|
163 |
+
return gdtrix(1.0, b, p)
|
164 |
+
|
165 |
+
def gdtrix_comp(b, p):
|
166 |
+
return gdtrix(1.0, b, 1-p)
|
167 |
+
|
168 |
+
def gdtrib_(p, x):
|
169 |
+
return gdtrib(1.0, p, x)
|
170 |
+
|
171 |
+
def gdtrib_comp(p, x):
|
172 |
+
return gdtrib(1.0, 1-p, x)
|
173 |
+
|
174 |
+
def nbdtrik_comp(y, n, p):
|
175 |
+
return nbdtrik(1-y, n, p)
|
176 |
+
|
177 |
+
def pdtrik_comp(p, m):
|
178 |
+
return pdtrik(1-p, m)
|
179 |
+
|
180 |
+
def poch_(z, m):
|
181 |
+
return 1.0 / poch(z, m)
|
182 |
+
|
183 |
+
def poch_minus(z, m):
|
184 |
+
return 1.0 / poch(z, -m)
|
185 |
+
|
186 |
+
def spherical_jn_(n, x):
|
187 |
+
return spherical_jn(n.astype('l'), x)
|
188 |
+
|
189 |
+
def spherical_yn_(n, x):
|
190 |
+
return spherical_yn(n.astype('l'), x)
|
191 |
+
|
192 |
+
def sph_harm_(m, n, theta, phi):
|
193 |
+
y = sph_harm(m, n, theta, phi)
|
194 |
+
return (y.real, y.imag)
|
195 |
+
|
196 |
+
def cexpm1(x, y):
|
197 |
+
z = expm1(x + 1j*y)
|
198 |
+
return z.real, z.imag
|
199 |
+
|
200 |
+
def clog1p(x, y):
|
201 |
+
z = log1p(x + 1j*y)
|
202 |
+
return z.real, z.imag
|
203 |
+
|
204 |
+
|
205 |
+
BOOST_TESTS = [
|
206 |
+
data(assoc_legendre_p_boost_, 'assoc_legendre_p_ipp-assoc_legendre_p',
|
207 |
+
(0,1,2), 3, rtol=1e-11),
|
208 |
+
|
209 |
+
data(legendre_p_via_assoc_, 'legendre_p_ipp-legendre_p',
|
210 |
+
(0,1), 2, rtol=1e-11),
|
211 |
+
data(legendre_p_via_assoc_, 'legendre_p_large_ipp-legendre_p_large',
|
212 |
+
(0,1), 2, rtol=9.6e-14),
|
213 |
+
data(legendre_p_via_lpmn, 'legendre_p_ipp-legendre_p',
|
214 |
+
(0,1), 2, rtol=5e-14, vectorized=False),
|
215 |
+
data(legendre_p_via_lpmn, 'legendre_p_large_ipp-legendre_p_large',
|
216 |
+
(0,1), 2, rtol=9.6e-14, vectorized=False),
|
217 |
+
data(lpn_, 'legendre_p_ipp-legendre_p',
|
218 |
+
(0,1), 2, rtol=5e-14, vectorized=False),
|
219 |
+
data(lpn_, 'legendre_p_large_ipp-legendre_p_large',
|
220 |
+
(0,1), 2, rtol=3e-13, vectorized=False),
|
221 |
+
data(eval_legendre_ld, 'legendre_p_ipp-legendre_p',
|
222 |
+
(0,1), 2, rtol=6e-14),
|
223 |
+
data(eval_legendre_ld, 'legendre_p_large_ipp-legendre_p_large',
|
224 |
+
(0,1), 2, rtol=2e-13),
|
225 |
+
data(eval_legendre_dd, 'legendre_p_ipp-legendre_p',
|
226 |
+
(0,1), 2, rtol=2e-14),
|
227 |
+
data(eval_legendre_dd, 'legendre_p_large_ipp-legendre_p_large',
|
228 |
+
(0,1), 2, rtol=2e-13),
|
229 |
+
|
230 |
+
data(lqn_, 'legendre_p_ipp-legendre_p',
|
231 |
+
(0,1), 3, rtol=2e-14, vectorized=False),
|
232 |
+
data(lqn_, 'legendre_p_large_ipp-legendre_p_large',
|
233 |
+
(0,1), 3, rtol=2e-12, vectorized=False),
|
234 |
+
data(legendre_q_via_lqmn, 'legendre_p_ipp-legendre_p',
|
235 |
+
(0,1), 3, rtol=2e-14, vectorized=False),
|
236 |
+
data(legendre_q_via_lqmn, 'legendre_p_large_ipp-legendre_p_large',
|
237 |
+
(0,1), 3, rtol=2e-12, vectorized=False),
|
238 |
+
|
239 |
+
data(beta, 'beta_exp_data_ipp-beta_exp_data',
|
240 |
+
(0,1), 2, rtol=1e-13),
|
241 |
+
data(beta, 'beta_exp_data_ipp-beta_exp_data',
|
242 |
+
(0,1), 2, rtol=1e-13),
|
243 |
+
data(beta, 'beta_med_data_ipp-beta_med_data',
|
244 |
+
(0,1), 2, rtol=5e-13),
|
245 |
+
|
246 |
+
data(betainc, 'ibeta_small_data_ipp-ibeta_small_data',
|
247 |
+
(0,1,2), 5, rtol=6e-15),
|
248 |
+
data(betainc, 'ibeta_data_ipp-ibeta_data',
|
249 |
+
(0,1,2), 5, rtol=5e-13),
|
250 |
+
data(betainc, 'ibeta_int_data_ipp-ibeta_int_data',
|
251 |
+
(0,1,2), 5, rtol=2e-14),
|
252 |
+
data(betainc, 'ibeta_large_data_ipp-ibeta_large_data',
|
253 |
+
(0,1,2), 5, rtol=4e-10),
|
254 |
+
|
255 |
+
data(betaincinv, 'ibeta_inv_data_ipp-ibeta_inv_data',
|
256 |
+
(0,1,2), 3, rtol=1e-5),
|
257 |
+
|
258 |
+
data(btdtr, 'ibeta_small_data_ipp-ibeta_small_data',
|
259 |
+
(0,1,2), 5, rtol=6e-15),
|
260 |
+
data(btdtr, 'ibeta_data_ipp-ibeta_data',
|
261 |
+
(0,1,2), 5, rtol=4e-13),
|
262 |
+
data(btdtr, 'ibeta_int_data_ipp-ibeta_int_data',
|
263 |
+
(0,1,2), 5, rtol=2e-14),
|
264 |
+
data(btdtr, 'ibeta_large_data_ipp-ibeta_large_data',
|
265 |
+
(0,1,2), 5, rtol=4e-10),
|
266 |
+
|
267 |
+
data(btdtri, 'ibeta_inv_data_ipp-ibeta_inv_data',
|
268 |
+
(0,1,2), 3, rtol=1e-5),
|
269 |
+
data(btdtri_comp, 'ibeta_inv_data_ipp-ibeta_inv_data',
|
270 |
+
(0,1,2), 4, rtol=8e-7),
|
271 |
+
|
272 |
+
data(btdtria, 'ibeta_inva_data_ipp-ibeta_inva_data',
|
273 |
+
(2,0,1), 3, rtol=5e-9),
|
274 |
+
data(btdtria_comp, 'ibeta_inva_data_ipp-ibeta_inva_data',
|
275 |
+
(2,0,1), 4, rtol=5e-9),
|
276 |
+
|
277 |
+
data(btdtrib, 'ibeta_inva_data_ipp-ibeta_inva_data',
|
278 |
+
(0,2,1), 5, rtol=5e-9),
|
279 |
+
data(btdtrib_comp, 'ibeta_inva_data_ipp-ibeta_inva_data',
|
280 |
+
(0,2,1), 6, rtol=5e-9),
|
281 |
+
|
282 |
+
data(binom, 'binomial_data_ipp-binomial_data',
|
283 |
+
(0,1), 2, rtol=1e-13),
|
284 |
+
data(binom, 'binomial_large_data_ipp-binomial_large_data',
|
285 |
+
(0,1), 2, rtol=5e-13),
|
286 |
+
|
287 |
+
data(bdtrik, 'binomial_quantile_ipp-binomial_quantile_data',
|
288 |
+
(2,0,1), 3, rtol=5e-9),
|
289 |
+
data(bdtrik_comp, 'binomial_quantile_ipp-binomial_quantile_data',
|
290 |
+
(2,0,1), 4, rtol=5e-9),
|
291 |
+
|
292 |
+
data(nbdtrik, 'negative_binomial_quantile_ipp-negative_binomial_quantile_data',
|
293 |
+
(2,0,1), 3, rtol=4e-9),
|
294 |
+
data(nbdtrik_comp,
|
295 |
+
'negative_binomial_quantile_ipp-negative_binomial_quantile_data',
|
296 |
+
(2,0,1), 4, rtol=4e-9),
|
297 |
+
|
298 |
+
data(pdtrik, 'poisson_quantile_ipp-poisson_quantile_data',
|
299 |
+
(1,0), 2, rtol=3e-9),
|
300 |
+
data(pdtrik_comp, 'poisson_quantile_ipp-poisson_quantile_data',
|
301 |
+
(1,0), 3, rtol=4e-9),
|
302 |
+
|
303 |
+
data(cbrt, 'cbrt_data_ipp-cbrt_data', 1, 0),
|
304 |
+
|
305 |
+
data(digamma, 'digamma_data_ipp-digamma_data', 0, 1),
|
306 |
+
data(digamma, 'digamma_data_ipp-digamma_data', 0j, 1),
|
307 |
+
data(digamma, 'digamma_neg_data_ipp-digamma_neg_data', 0, 1, rtol=2e-13),
|
308 |
+
data(digamma, 'digamma_neg_data_ipp-digamma_neg_data', 0j, 1, rtol=1e-13),
|
309 |
+
data(digamma, 'digamma_root_data_ipp-digamma_root_data', 0, 1, rtol=1e-15),
|
310 |
+
data(digamma, 'digamma_root_data_ipp-digamma_root_data', 0j, 1, rtol=1e-15),
|
311 |
+
data(digamma, 'digamma_small_data_ipp-digamma_small_data', 0, 1, rtol=1e-15),
|
312 |
+
data(digamma, 'digamma_small_data_ipp-digamma_small_data', 0j, 1, rtol=1e-14),
|
313 |
+
|
314 |
+
data(ellipk_, 'ellint_k_data_ipp-ellint_k_data', 0, 1),
|
315 |
+
data(ellipkinc_, 'ellint_f_data_ipp-ellint_f_data', (0,1), 2, rtol=1e-14),
|
316 |
+
data(ellipe_, 'ellint_e_data_ipp-ellint_e_data', 0, 1),
|
317 |
+
data(ellipeinc_, 'ellint_e2_data_ipp-ellint_e2_data', (0,1), 2, rtol=1e-14),
|
318 |
+
|
319 |
+
data(erf, 'erf_data_ipp-erf_data', 0, 1),
|
320 |
+
data(erf, 'erf_data_ipp-erf_data', 0j, 1, rtol=1e-13),
|
321 |
+
data(erfc, 'erf_data_ipp-erf_data', 0, 2, rtol=6e-15),
|
322 |
+
data(erf, 'erf_large_data_ipp-erf_large_data', 0, 1),
|
323 |
+
data(erf, 'erf_large_data_ipp-erf_large_data', 0j, 1),
|
324 |
+
data(erfc, 'erf_large_data_ipp-erf_large_data', 0, 2, rtol=4e-14),
|
325 |
+
data(erf, 'erf_small_data_ipp-erf_small_data', 0, 1),
|
326 |
+
data(erf, 'erf_small_data_ipp-erf_small_data', 0j, 1, rtol=1e-13),
|
327 |
+
data(erfc, 'erf_small_data_ipp-erf_small_data', 0, 2),
|
328 |
+
|
329 |
+
data(erfinv, 'erf_inv_data_ipp-erf_inv_data', 0, 1),
|
330 |
+
data(erfcinv, 'erfc_inv_data_ipp-erfc_inv_data', 0, 1),
|
331 |
+
data(erfcinv, 'erfc_inv_big_data_ipp-erfc_inv_big_data', 0, 1,
|
332 |
+
param_filter=(lambda s: s > 0)),
|
333 |
+
|
334 |
+
data(exp1, 'expint_1_data_ipp-expint_1_data', 1, 2, rtol=1e-13),
|
335 |
+
data(exp1, 'expint_1_data_ipp-expint_1_data', 1j, 2, rtol=5e-9),
|
336 |
+
data(expi, 'expinti_data_ipp-expinti_data', 0, 1, rtol=1e-13),
|
337 |
+
data(expi, 'expinti_data_double_ipp-expinti_data_double', 0, 1, rtol=1e-13),
|
338 |
+
data(expi, 'expinti_data_long_ipp-expinti_data_long', 0, 1),
|
339 |
+
|
340 |
+
data(expn, 'expint_small_data_ipp-expint_small_data', (0,1), 2),
|
341 |
+
data(expn, 'expint_data_ipp-expint_data', (0,1), 2, rtol=1e-14),
|
342 |
+
|
343 |
+
data(gamma, 'test_gamma_data_ipp-near_0', 0, 1),
|
344 |
+
data(gamma, 'test_gamma_data_ipp-near_1', 0, 1),
|
345 |
+
data(gamma, 'test_gamma_data_ipp-near_2', 0, 1),
|
346 |
+
data(gamma, 'test_gamma_data_ipp-near_m10', 0, 1),
|
347 |
+
data(gamma, 'test_gamma_data_ipp-near_m55', 0, 1, rtol=7e-12),
|
348 |
+
data(gamma, 'test_gamma_data_ipp-factorials', 0, 1, rtol=4e-14),
|
349 |
+
data(gamma, 'test_gamma_data_ipp-near_0', 0j, 1, rtol=2e-9),
|
350 |
+
data(gamma, 'test_gamma_data_ipp-near_1', 0j, 1, rtol=2e-9),
|
351 |
+
data(gamma, 'test_gamma_data_ipp-near_2', 0j, 1, rtol=2e-9),
|
352 |
+
data(gamma, 'test_gamma_data_ipp-near_m10', 0j, 1, rtol=2e-9),
|
353 |
+
data(gamma, 'test_gamma_data_ipp-near_m55', 0j, 1, rtol=2e-9),
|
354 |
+
data(gamma, 'test_gamma_data_ipp-factorials', 0j, 1, rtol=2e-13),
|
355 |
+
data(gammaln, 'test_gamma_data_ipp-near_0', 0, 2, rtol=5e-11),
|
356 |
+
data(gammaln, 'test_gamma_data_ipp-near_1', 0, 2, rtol=5e-11),
|
357 |
+
data(gammaln, 'test_gamma_data_ipp-near_2', 0, 2, rtol=2e-10),
|
358 |
+
data(gammaln, 'test_gamma_data_ipp-near_m10', 0, 2, rtol=5e-11),
|
359 |
+
data(gammaln, 'test_gamma_data_ipp-near_m55', 0, 2, rtol=5e-11),
|
360 |
+
data(gammaln, 'test_gamma_data_ipp-factorials', 0, 2),
|
361 |
+
|
362 |
+
data(gammainc, 'igamma_small_data_ipp-igamma_small_data', (0,1), 5, rtol=5e-15),
|
363 |
+
data(gammainc, 'igamma_med_data_ipp-igamma_med_data', (0,1), 5, rtol=2e-13),
|
364 |
+
data(gammainc, 'igamma_int_data_ipp-igamma_int_data', (0,1), 5, rtol=2e-13),
|
365 |
+
data(gammainc, 'igamma_big_data_ipp-igamma_big_data', (0,1), 5, rtol=1e-12),
|
366 |
+
|
367 |
+
data(gdtr_, 'igamma_small_data_ipp-igamma_small_data', (0,1), 5, rtol=1e-13),
|
368 |
+
data(gdtr_, 'igamma_med_data_ipp-igamma_med_data', (0,1), 5, rtol=2e-13),
|
369 |
+
data(gdtr_, 'igamma_int_data_ipp-igamma_int_data', (0,1), 5, rtol=2e-13),
|
370 |
+
data(gdtr_, 'igamma_big_data_ipp-igamma_big_data', (0,1), 5, rtol=2e-9),
|
371 |
+
|
372 |
+
data(gammaincc, 'igamma_small_data_ipp-igamma_small_data',
|
373 |
+
(0,1), 3, rtol=1e-13),
|
374 |
+
data(gammaincc, 'igamma_med_data_ipp-igamma_med_data',
|
375 |
+
(0,1), 3, rtol=2e-13),
|
376 |
+
data(gammaincc, 'igamma_int_data_ipp-igamma_int_data',
|
377 |
+
(0,1), 3, rtol=4e-14),
|
378 |
+
data(gammaincc, 'igamma_big_data_ipp-igamma_big_data',
|
379 |
+
(0,1), 3, rtol=1e-11),
|
380 |
+
|
381 |
+
data(gdtrc_, 'igamma_small_data_ipp-igamma_small_data', (0,1), 3, rtol=1e-13),
|
382 |
+
data(gdtrc_, 'igamma_med_data_ipp-igamma_med_data', (0,1), 3, rtol=2e-13),
|
383 |
+
data(gdtrc_, 'igamma_int_data_ipp-igamma_int_data', (0,1), 3, rtol=4e-14),
|
384 |
+
data(gdtrc_, 'igamma_big_data_ipp-igamma_big_data', (0,1), 3, rtol=1e-11),
|
385 |
+
|
386 |
+
data(gdtrib_, 'igamma_inva_data_ipp-igamma_inva_data', (1,0), 2, rtol=5e-9),
|
387 |
+
data(gdtrib_comp, 'igamma_inva_data_ipp-igamma_inva_data', (1,0), 3, rtol=5e-9),
|
388 |
+
|
389 |
+
data(poch_, 'tgamma_delta_ratio_data_ipp-tgamma_delta_ratio_data',
|
390 |
+
(0,1), 2, rtol=2e-13),
|
391 |
+
data(poch_, 'tgamma_delta_ratio_int_ipp-tgamma_delta_ratio_int',
|
392 |
+
(0,1), 2,),
|
393 |
+
data(poch_, 'tgamma_delta_ratio_int2_ipp-tgamma_delta_ratio_int2',
|
394 |
+
(0,1), 2,),
|
395 |
+
data(poch_minus, 'tgamma_delta_ratio_data_ipp-tgamma_delta_ratio_data',
|
396 |
+
(0,1), 3, rtol=2e-13),
|
397 |
+
data(poch_minus, 'tgamma_delta_ratio_int_ipp-tgamma_delta_ratio_int',
|
398 |
+
(0,1), 3),
|
399 |
+
data(poch_minus, 'tgamma_delta_ratio_int2_ipp-tgamma_delta_ratio_int2',
|
400 |
+
(0,1), 3),
|
401 |
+
|
402 |
+
data(eval_hermite_ld, 'hermite_ipp-hermite',
|
403 |
+
(0,1), 2, rtol=2e-14),
|
404 |
+
|
405 |
+
data(eval_laguerre_ld, 'laguerre2_ipp-laguerre2',
|
406 |
+
(0,1), 2, rtol=7e-12),
|
407 |
+
data(eval_laguerre_dd, 'laguerre2_ipp-laguerre2',
|
408 |
+
(0,1), 2, knownfailure='hyp2f1 insufficiently accurate.'),
|
409 |
+
data(eval_genlaguerre_ldd, 'laguerre3_ipp-laguerre3',
|
410 |
+
(0,1,2), 3, rtol=2e-13),
|
411 |
+
data(eval_genlaguerre_ddd, 'laguerre3_ipp-laguerre3',
|
412 |
+
(0,1,2), 3, knownfailure='hyp2f1 insufficiently accurate.'),
|
413 |
+
|
414 |
+
data(log1p, 'log1p_expm1_data_ipp-log1p_expm1_data', 0, 1),
|
415 |
+
data(expm1, 'log1p_expm1_data_ipp-log1p_expm1_data', 0, 2),
|
416 |
+
|
417 |
+
data(iv, 'bessel_i_data_ipp-bessel_i_data',
|
418 |
+
(0,1), 2, rtol=1e-12),
|
419 |
+
data(iv, 'bessel_i_data_ipp-bessel_i_data',
|
420 |
+
(0,1j), 2, rtol=2e-10, atol=1e-306),
|
421 |
+
data(iv, 'bessel_i_int_data_ipp-bessel_i_int_data',
|
422 |
+
(0,1), 2, rtol=1e-9),
|
423 |
+
data(iv, 'bessel_i_int_data_ipp-bessel_i_int_data',
|
424 |
+
(0,1j), 2, rtol=2e-10),
|
425 |
+
|
426 |
+
data(ivp, 'bessel_i_prime_int_data_ipp-bessel_i_prime_int_data',
|
427 |
+
(0,1), 2, rtol=1.2e-13),
|
428 |
+
data(ivp, 'bessel_i_prime_int_data_ipp-bessel_i_prime_int_data',
|
429 |
+
(0,1j), 2, rtol=1.2e-13, atol=1e-300),
|
430 |
+
|
431 |
+
data(jn, 'bessel_j_int_data_ipp-bessel_j_int_data', (0,1), 2, rtol=1e-12),
|
432 |
+
data(jn, 'bessel_j_int_data_ipp-bessel_j_int_data', (0,1j), 2, rtol=1e-12),
|
433 |
+
data(jn, 'bessel_j_large_data_ipp-bessel_j_large_data', (0,1), 2, rtol=6e-11),
|
434 |
+
data(jn, 'bessel_j_large_data_ipp-bessel_j_large_data', (0,1j), 2, rtol=6e-11),
|
435 |
+
|
436 |
+
data(jv, 'bessel_j_int_data_ipp-bessel_j_int_data', (0,1), 2, rtol=1e-12),
|
437 |
+
data(jv, 'bessel_j_int_data_ipp-bessel_j_int_data', (0,1j), 2, rtol=1e-12),
|
438 |
+
data(jv, 'bessel_j_data_ipp-bessel_j_data', (0,1), 2, rtol=1e-12),
|
439 |
+
data(jv, 'bessel_j_data_ipp-bessel_j_data', (0,1j), 2, rtol=1e-12),
|
440 |
+
|
441 |
+
data(jvp, 'bessel_j_prime_int_data_ipp-bessel_j_prime_int_data',
|
442 |
+
(0,1), 2, rtol=1e-13),
|
443 |
+
data(jvp, 'bessel_j_prime_int_data_ipp-bessel_j_prime_int_data',
|
444 |
+
(0,1j), 2, rtol=1e-13),
|
445 |
+
data(jvp, 'bessel_j_prime_large_data_ipp-bessel_j_prime_large_data',
|
446 |
+
(0,1), 2, rtol=1e-11),
|
447 |
+
data(jvp, 'bessel_j_prime_large_data_ipp-bessel_j_prime_large_data',
|
448 |
+
(0,1j), 2, rtol=2e-11),
|
449 |
+
|
450 |
+
data(kn, 'bessel_k_int_data_ipp-bessel_k_int_data', (0,1), 2, rtol=1e-12),
|
451 |
+
|
452 |
+
data(kv, 'bessel_k_int_data_ipp-bessel_k_int_data', (0,1), 2, rtol=1e-12),
|
453 |
+
data(kv, 'bessel_k_int_data_ipp-bessel_k_int_data', (0,1j), 2, rtol=1e-12),
|
454 |
+
data(kv, 'bessel_k_data_ipp-bessel_k_data', (0,1), 2, rtol=1e-12),
|
455 |
+
data(kv, 'bessel_k_data_ipp-bessel_k_data', (0,1j), 2, rtol=1e-12),
|
456 |
+
|
457 |
+
data(kvp, 'bessel_k_prime_int_data_ipp-bessel_k_prime_int_data',
|
458 |
+
(0,1), 2, rtol=3e-14),
|
459 |
+
data(kvp, 'bessel_k_prime_int_data_ipp-bessel_k_prime_int_data',
|
460 |
+
(0,1j), 2, rtol=3e-14),
|
461 |
+
data(kvp, 'bessel_k_prime_data_ipp-bessel_k_prime_data', (0,1), 2, rtol=7e-14),
|
462 |
+
data(kvp, 'bessel_k_prime_data_ipp-bessel_k_prime_data', (0,1j), 2, rtol=7e-14),
|
463 |
+
|
464 |
+
data(yn, 'bessel_y01_data_ipp-bessel_y01_data', (0,1), 2, rtol=1e-12),
|
465 |
+
data(yn, 'bessel_yn_data_ipp-bessel_yn_data', (0,1), 2, rtol=1e-12),
|
466 |
+
|
467 |
+
data(yv, 'bessel_yn_data_ipp-bessel_yn_data', (0,1), 2, rtol=1e-12),
|
468 |
+
data(yv, 'bessel_yn_data_ipp-bessel_yn_data', (0,1j), 2, rtol=1e-12),
|
469 |
+
data(yv, 'bessel_yv_data_ipp-bessel_yv_data', (0,1), 2, rtol=1e-10),
|
470 |
+
data(yv, 'bessel_yv_data_ipp-bessel_yv_data', (0,1j), 2, rtol=1e-10),
|
471 |
+
|
472 |
+
data(yvp, 'bessel_yv_prime_data_ipp-bessel_yv_prime_data',
|
473 |
+
(0, 1), 2, rtol=4e-9),
|
474 |
+
data(yvp, 'bessel_yv_prime_data_ipp-bessel_yv_prime_data',
|
475 |
+
(0, 1j), 2, rtol=4e-9),
|
476 |
+
|
477 |
+
data(zeta_, 'zeta_data_ipp-zeta_data', 0, 1,
|
478 |
+
param_filter=(lambda s: s > 1)),
|
479 |
+
data(zeta_, 'zeta_neg_data_ipp-zeta_neg_data', 0, 1,
|
480 |
+
param_filter=(lambda s: s > 1)),
|
481 |
+
data(zeta_, 'zeta_1_up_data_ipp-zeta_1_up_data', 0, 1,
|
482 |
+
param_filter=(lambda s: s > 1)),
|
483 |
+
data(zeta_, 'zeta_1_below_data_ipp-zeta_1_below_data', 0, 1,
|
484 |
+
param_filter=(lambda s: s > 1)),
|
485 |
+
|
486 |
+
data(gammaincinv, 'gamma_inv_small_data_ipp-gamma_inv_small_data',
|
487 |
+
(0,1), 2, rtol=1e-11),
|
488 |
+
data(gammaincinv, 'gamma_inv_data_ipp-gamma_inv_data',
|
489 |
+
(0,1), 2, rtol=1e-14),
|
490 |
+
data(gammaincinv, 'gamma_inv_big_data_ipp-gamma_inv_big_data',
|
491 |
+
(0,1), 2, rtol=1e-11),
|
492 |
+
|
493 |
+
data(gammainccinv, 'gamma_inv_small_data_ipp-gamma_inv_small_data',
|
494 |
+
(0,1), 3, rtol=1e-12),
|
495 |
+
data(gammainccinv, 'gamma_inv_data_ipp-gamma_inv_data',
|
496 |
+
(0,1), 3, rtol=1e-14),
|
497 |
+
data(gammainccinv, 'gamma_inv_big_data_ipp-gamma_inv_big_data',
|
498 |
+
(0,1), 3, rtol=1e-14),
|
499 |
+
|
500 |
+
data(gdtrix_, 'gamma_inv_small_data_ipp-gamma_inv_small_data',
|
501 |
+
(0,1), 2, rtol=3e-13, knownfailure='gdtrix unflow some points'),
|
502 |
+
data(gdtrix_, 'gamma_inv_data_ipp-gamma_inv_data',
|
503 |
+
(0,1), 2, rtol=3e-15),
|
504 |
+
data(gdtrix_, 'gamma_inv_big_data_ipp-gamma_inv_big_data',
|
505 |
+
(0,1), 2),
|
506 |
+
data(gdtrix_comp, 'gamma_inv_small_data_ipp-gamma_inv_small_data',
|
507 |
+
(0,1), 2, knownfailure='gdtrix bad some points'),
|
508 |
+
data(gdtrix_comp, 'gamma_inv_data_ipp-gamma_inv_data',
|
509 |
+
(0,1), 3, rtol=6e-15),
|
510 |
+
data(gdtrix_comp, 'gamma_inv_big_data_ipp-gamma_inv_big_data',
|
511 |
+
(0,1), 3),
|
512 |
+
|
513 |
+
data(chndtr, 'nccs_ipp-nccs',
|
514 |
+
(2,0,1), 3, rtol=3e-5),
|
515 |
+
data(chndtr, 'nccs_big_ipp-nccs_big',
|
516 |
+
(2,0,1), 3, rtol=5e-4, knownfailure='chndtr inaccurate some points'),
|
517 |
+
|
518 |
+
data(sph_harm_, 'spherical_harmonic_ipp-spherical_harmonic',
|
519 |
+
(1,0,3,2), (4,5), rtol=5e-11,
|
520 |
+
param_filter=(lambda p: np.ones(p.shape, '?'),
|
521 |
+
lambda p: np.ones(p.shape, '?'),
|
522 |
+
lambda p: np.logical_and(p < 2*np.pi, p >= 0),
|
523 |
+
lambda p: np.logical_and(p < np.pi, p >= 0))),
|
524 |
+
|
525 |
+
data(spherical_jn_, 'sph_bessel_data_ipp-sph_bessel_data',
|
526 |
+
(0,1), 2, rtol=1e-13),
|
527 |
+
data(spherical_yn_, 'sph_neumann_data_ipp-sph_neumann_data',
|
528 |
+
(0,1), 2, rtol=8e-15),
|
529 |
+
|
530 |
+
data(owens_t, 'owens_t_ipp-owens_t',
|
531 |
+
(0, 1), 2, rtol=5e-14),
|
532 |
+
data(owens_t, 'owens_t_large_data_ipp-owens_t_large_data',
|
533 |
+
(0, 1), 2, rtol=8e-12),
|
534 |
+
|
535 |
+
# -- test data exists in boost but is not used in scipy --
|
536 |
+
|
537 |
+
# ibeta_derivative_data_ipp/ibeta_derivative_data.txt
|
538 |
+
# ibeta_derivative_int_data_ipp/ibeta_derivative_int_data.txt
|
539 |
+
# ibeta_derivative_large_data_ipp/ibeta_derivative_large_data.txt
|
540 |
+
# ibeta_derivative_small_data_ipp/ibeta_derivative_small_data.txt
|
541 |
+
|
542 |
+
# bessel_y01_prime_data_ipp/bessel_y01_prime_data.txt
|
543 |
+
# bessel_yn_prime_data_ipp/bessel_yn_prime_data.txt
|
544 |
+
# sph_bessel_prime_data_ipp/sph_bessel_prime_data.txt
|
545 |
+
# sph_neumann_prime_data_ipp/sph_neumann_prime_data.txt
|
546 |
+
|
547 |
+
# ellint_d2_data_ipp/ellint_d2_data.txt
|
548 |
+
# ellint_d_data_ipp/ellint_d_data.txt
|
549 |
+
# ellint_pi2_data_ipp/ellint_pi2_data.txt
|
550 |
+
# ellint_pi3_data_ipp/ellint_pi3_data.txt
|
551 |
+
# ellint_pi3_large_data_ipp/ellint_pi3_large_data.txt
|
552 |
+
data(elliprc, 'ellint_rc_data_ipp-ellint_rc_data', (0, 1), 2,
|
553 |
+
rtol=5e-16),
|
554 |
+
data(elliprd, 'ellint_rd_data_ipp-ellint_rd_data', (0, 1, 2), 3,
|
555 |
+
rtol=5e-16),
|
556 |
+
data(elliprd, 'ellint_rd_0xy_ipp-ellint_rd_0xy', (0, 1, 2), 3,
|
557 |
+
rtol=5e-16),
|
558 |
+
data(elliprd, 'ellint_rd_0yy_ipp-ellint_rd_0yy', (0, 1, 2), 3,
|
559 |
+
rtol=5e-16),
|
560 |
+
data(elliprd, 'ellint_rd_xxx_ipp-ellint_rd_xxx', (0, 1, 2), 3,
|
561 |
+
rtol=5e-16),
|
562 |
+
# Some of the following rtol for elliprd may be larger than 5e-16 to
|
563 |
+
# work around some hard cases in the Boost test where we get slightly
|
564 |
+
# larger error than the ideal bound when the x (==y) input is close to
|
565 |
+
# zero.
|
566 |
+
# Also the accuracy on 32-bit builds with g++ may suffer from excess
|
567 |
+
# loss of precision; see GCC bugzilla 323
|
568 |
+
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
|
569 |
+
data(elliprd, 'ellint_rd_xxz_ipp-ellint_rd_xxz', (0, 1, 2), 3,
|
570 |
+
rtol=6.5e-16),
|
571 |
+
data(elliprd, 'ellint_rd_xyy_ipp-ellint_rd_xyy', (0, 1, 2), 3,
|
572 |
+
rtol=6e-16),
|
573 |
+
data(elliprf, 'ellint_rf_data_ipp-ellint_rf_data', (0, 1, 2), 3,
|
574 |
+
rtol=5e-16),
|
575 |
+
data(elliprf, 'ellint_rf_xxx_ipp-ellint_rf_xxx', (0, 1, 2), 3,
|
576 |
+
rtol=5e-16),
|
577 |
+
data(elliprf, 'ellint_rf_xyy_ipp-ellint_rf_xyy', (0, 1, 2), 3,
|
578 |
+
rtol=5e-16),
|
579 |
+
data(elliprf, 'ellint_rf_xy0_ipp-ellint_rf_xy0', (0, 1, 2), 3,
|
580 |
+
rtol=5e-16),
|
581 |
+
data(elliprf, 'ellint_rf_0yy_ipp-ellint_rf_0yy', (0, 1, 2), 3,
|
582 |
+
rtol=5e-16),
|
583 |
+
# The accuracy of R_G is primarily limited by R_D that is used
|
584 |
+
# internally. It is generally worse than R_D. Notice that we increased
|
585 |
+
# the rtol for R_G here. The cases with duplicate arguments are
|
586 |
+
# slightly less likely to be unbalanced (at least two arguments are
|
587 |
+
# already balanced) so the error bound is slightly better. Again,
|
588 |
+
# precision with g++ 32-bit is even worse.
|
589 |
+
data(elliprg, 'ellint_rg_ipp-ellint_rg', (0, 1, 2), 3,
|
590 |
+
rtol=8.0e-16),
|
591 |
+
data(elliprg, 'ellint_rg_xxx_ipp-ellint_rg_xxx', (0, 1, 2), 3,
|
592 |
+
rtol=6e-16),
|
593 |
+
data(elliprg, 'ellint_rg_xyy_ipp-ellint_rg_xyy', (0, 1, 2), 3,
|
594 |
+
rtol=7.5e-16),
|
595 |
+
data(elliprg, 'ellint_rg_xy0_ipp-ellint_rg_xy0', (0, 1, 2), 3,
|
596 |
+
rtol=5e-16),
|
597 |
+
data(elliprg, 'ellint_rg_00x_ipp-ellint_rg_00x', (0, 1, 2), 3,
|
598 |
+
rtol=5e-16),
|
599 |
+
data(elliprj, 'ellint_rj_data_ipp-ellint_rj_data', (0, 1, 2, 3), 4,
|
600 |
+
rtol=5e-16, atol=1e-25,
|
601 |
+
param_filter=(lambda s: s <= 5e-26,)),
|
602 |
+
# ellint_rc_data_ipp/ellint_rc_data.txt
|
603 |
+
# ellint_rd_0xy_ipp/ellint_rd_0xy.txt
|
604 |
+
# ellint_rd_0yy_ipp/ellint_rd_0yy.txt
|
605 |
+
# ellint_rd_data_ipp/ellint_rd_data.txt
|
606 |
+
# ellint_rd_xxx_ipp/ellint_rd_xxx.txt
|
607 |
+
# ellint_rd_xxz_ipp/ellint_rd_xxz.txt
|
608 |
+
# ellint_rd_xyy_ipp/ellint_rd_xyy.txt
|
609 |
+
# ellint_rf_0yy_ipp/ellint_rf_0yy.txt
|
610 |
+
# ellint_rf_data_ipp/ellint_rf_data.txt
|
611 |
+
# ellint_rf_xxx_ipp/ellint_rf_xxx.txt
|
612 |
+
# ellint_rf_xy0_ipp/ellint_rf_xy0.txt
|
613 |
+
# ellint_rf_xyy_ipp/ellint_rf_xyy.txt
|
614 |
+
# ellint_rg_00x_ipp/ellint_rg_00x.txt
|
615 |
+
# ellint_rg_ipp/ellint_rg.txt
|
616 |
+
# ellint_rg_xxx_ipp/ellint_rg_xxx.txt
|
617 |
+
# ellint_rg_xy0_ipp/ellint_rg_xy0.txt
|
618 |
+
# ellint_rg_xyy_ipp/ellint_rg_xyy.txt
|
619 |
+
# ellint_rj_data_ipp/ellint_rj_data.txt
|
620 |
+
# ellint_rj_e2_ipp/ellint_rj_e2.txt
|
621 |
+
# ellint_rj_e3_ipp/ellint_rj_e3.txt
|
622 |
+
# ellint_rj_e4_ipp/ellint_rj_e4.txt
|
623 |
+
# ellint_rj_zp_ipp/ellint_rj_zp.txt
|
624 |
+
|
625 |
+
# jacobi_elliptic_ipp/jacobi_elliptic.txt
|
626 |
+
# jacobi_elliptic_small_ipp/jacobi_elliptic_small.txt
|
627 |
+
# jacobi_large_phi_ipp/jacobi_large_phi.txt
|
628 |
+
# jacobi_near_1_ipp/jacobi_near_1.txt
|
629 |
+
# jacobi_zeta_big_phi_ipp/jacobi_zeta_big_phi.txt
|
630 |
+
# jacobi_zeta_data_ipp/jacobi_zeta_data.txt
|
631 |
+
|
632 |
+
# heuman_lambda_data_ipp/heuman_lambda_data.txt
|
633 |
+
|
634 |
+
# hypergeometric_0F2_ipp/hypergeometric_0F2.txt
|
635 |
+
# hypergeometric_1F1_big_ipp/hypergeometric_1F1_big.txt
|
636 |
+
# hypergeometric_1F1_ipp/hypergeometric_1F1.txt
|
637 |
+
# hypergeometric_1F1_small_random_ipp/hypergeometric_1F1_small_random.txt
|
638 |
+
# hypergeometric_1F2_ipp/hypergeometric_1F2.txt
|
639 |
+
# hypergeometric_1f1_large_regularized_ipp/hypergeometric_1f1_large_regularized.txt # noqa: E501
|
640 |
+
# hypergeometric_1f1_log_large_unsolved_ipp/hypergeometric_1f1_log_large_unsolved.txt # noqa: E501
|
641 |
+
# hypergeometric_2F0_half_ipp/hypergeometric_2F0_half.txt
|
642 |
+
# hypergeometric_2F0_integer_a2_ipp/hypergeometric_2F0_integer_a2.txt
|
643 |
+
# hypergeometric_2F0_ipp/hypergeometric_2F0.txt
|
644 |
+
# hypergeometric_2F0_large_z_ipp/hypergeometric_2F0_large_z.txt
|
645 |
+
# hypergeometric_2F1_ipp/hypergeometric_2F1.txt
|
646 |
+
# hypergeometric_2F2_ipp/hypergeometric_2F2.txt
|
647 |
+
|
648 |
+
# ncbeta_big_ipp/ncbeta_big.txt
|
649 |
+
# nct_small_delta_ipp/nct_small_delta.txt
|
650 |
+
# nct_asym_ipp/nct_asym.txt
|
651 |
+
# ncbeta_ipp/ncbeta.txt
|
652 |
+
|
653 |
+
# powm1_data_ipp/powm1_big_data.txt
|
654 |
+
# powm1_sqrtp1m1_test_hpp/sqrtp1m1_data.txt
|
655 |
+
|
656 |
+
# sinc_data_ipp/sinc_data.txt
|
657 |
+
|
658 |
+
# test_gamma_data_ipp/gammap1m1_data.txt
|
659 |
+
# tgamma_ratio_data_ipp/tgamma_ratio_data.txt
|
660 |
+
|
661 |
+
# trig_data_ipp/trig_data.txt
|
662 |
+
# trig_data2_ipp/trig_data2.txt
|
663 |
+
]
|
664 |
+
|
665 |
+
|
666 |
+
@pytest.mark.parametrize('test', BOOST_TESTS, ids=repr)
|
667 |
+
def test_boost(test):
|
668 |
+
# Filter deprecation warnings of any deprecated functions.
|
669 |
+
if test.func in [btdtr, btdtri, btdtri_comp]:
|
670 |
+
with pytest.deprecated_call():
|
671 |
+
_test_factory(test)
|
672 |
+
else:
|
673 |
+
_test_factory(test)
|
674 |
+
|
675 |
+
|
676 |
+
GSL_TESTS = [
|
677 |
+
data_gsl(mathieu_a, 'mathieu_ab', (0, 1), 2, rtol=1e-13, atol=1e-13),
|
678 |
+
data_gsl(mathieu_b, 'mathieu_ab', (0, 1), 3, rtol=1e-13, atol=1e-13),
|
679 |
+
|
680 |
+
# Also the GSL output has limited accuracy...
|
681 |
+
data_gsl(mathieu_ce_rad, 'mathieu_ce_se', (0, 1, 2), 3, rtol=1e-7, atol=1e-13),
|
682 |
+
data_gsl(mathieu_se_rad, 'mathieu_ce_se', (0, 1, 2), 4, rtol=1e-7, atol=1e-13),
|
683 |
+
|
684 |
+
data_gsl(mathieu_mc1_scaled, 'mathieu_mc_ms',
|
685 |
+
(0, 1, 2), 3, rtol=1e-7, atol=1e-13),
|
686 |
+
data_gsl(mathieu_ms1_scaled, 'mathieu_mc_ms',
|
687 |
+
(0, 1, 2), 4, rtol=1e-7, atol=1e-13),
|
688 |
+
|
689 |
+
data_gsl(mathieu_mc2_scaled, 'mathieu_mc_ms',
|
690 |
+
(0, 1, 2), 5, rtol=1e-7, atol=1e-13),
|
691 |
+
data_gsl(mathieu_ms2_scaled, 'mathieu_mc_ms',
|
692 |
+
(0, 1, 2), 6, rtol=1e-7, atol=1e-13),
|
693 |
+
]
|
694 |
+
|
695 |
+
|
696 |
+
@pytest.mark.parametrize('test', GSL_TESTS, ids=repr)
|
697 |
+
def test_gsl(test):
|
698 |
+
_test_factory(test)
|
699 |
+
|
700 |
+
|
701 |
+
LOCAL_TESTS = [
|
702 |
+
data_local(ellipkinc, 'ellipkinc_neg_m', (0, 1), 2),
|
703 |
+
data_local(ellipkm1, 'ellipkm1', 0, 1),
|
704 |
+
data_local(ellipeinc, 'ellipeinc_neg_m', (0, 1), 2),
|
705 |
+
data_local(clog1p, 'log1p_expm1_complex', (0,1), (2,3), rtol=1e-14),
|
706 |
+
data_local(cexpm1, 'log1p_expm1_complex', (0,1), (4,5), rtol=1e-14),
|
707 |
+
data_local(gammainc, 'gammainc', (0, 1), 2, rtol=1e-12),
|
708 |
+
data_local(gammaincc, 'gammaincc', (0, 1), 2, rtol=1e-11),
|
709 |
+
data_local(ellip_harm_2, 'ellip',(0, 1, 2, 3, 4), 6, rtol=1e-10, atol=1e-13),
|
710 |
+
data_local(ellip_harm, 'ellip',(0, 1, 2, 3, 4), 5, rtol=1e-10, atol=1e-13),
|
711 |
+
data_local(wright_bessel, 'wright_bessel', (0, 1, 2), 3, rtol=1e-11),
|
712 |
+
]
|
713 |
+
|
714 |
+
|
715 |
+
@pytest.mark.parametrize('test', LOCAL_TESTS, ids=repr)
|
716 |
+
def test_local(test):
|
717 |
+
_test_factory(test)
|
718 |
+
|
719 |
+
|
720 |
+
def _test_factory(test, dtype=np.float64):
|
721 |
+
"""Boost test"""
|
722 |
+
with suppress_warnings() as sup:
|
723 |
+
sup.filter(IntegrationWarning, "The occurrence of roundoff error is detected")
|
724 |
+
with np.errstate(all='ignore'):
|
725 |
+
test.check(dtype=dtype)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_dd.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Tests for a few of the "double-double" C functions defined in cephes/dd_*.
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
from numpy.testing import assert_allclose
|
5 |
+
from scipy.special._test_internal import _dd_exp, _dd_log, _dd_expm1
|
6 |
+
|
7 |
+
|
8 |
+
# Each tuple in test_data contains:
|
9 |
+
# (dd_func, xhi, xlo, expected_yhi, expected_ylo)
|
10 |
+
# The expected values were computed with mpmath, e.g.
|
11 |
+
#
|
12 |
+
# import mpmath
|
13 |
+
# mpmath.mp.dps = 100
|
14 |
+
# xhi = 10.0
|
15 |
+
# xlo = 0.0
|
16 |
+
# x = mpmath.mpf(xhi) + mpmath.mpf(xlo)
|
17 |
+
# y = mpmath.log(x)
|
18 |
+
# expected_yhi = float(y)
|
19 |
+
# expected_ylo = float(y - expected_yhi)
|
20 |
+
#
|
21 |
+
test_data = [
|
22 |
+
(_dd_exp, -0.3333333333333333, -1.850371707708594e-17,
|
23 |
+
0.7165313105737893, -2.0286948382455594e-17),
|
24 |
+
(_dd_exp, 0.0, 0.0, 1.0, 0.0),
|
25 |
+
(_dd_exp, 10.0, 0.0, 22026.465794806718, -1.3780134700517372e-12),
|
26 |
+
(_dd_log, 0.03125, 0.0, -3.4657359027997265, -4.930038229799327e-18),
|
27 |
+
(_dd_log, 10.0, 0.0, 2.302585092994046, -2.1707562233822494e-16),
|
28 |
+
(_dd_expm1, -1.25, 0.0, -0.7134952031398099, -4.7031321153650186e-17),
|
29 |
+
(_dd_expm1, -0.484375, 0.0, -0.3839178722093218, 7.609376052156984e-18),
|
30 |
+
(_dd_expm1, -0.25, 0.0, -0.22119921692859512, -1.0231869534531498e-17),
|
31 |
+
(_dd_expm1, -0.0625, 0.0, -0.06058693718652421, -7.077887227488846e-19),
|
32 |
+
(_dd_expm1, 0.0, 0.0, 0.0, 0.0),
|
33 |
+
(_dd_expm1, 0.0625, 3.5e-18, 0.06449445891785943, 1.4323095758164254e-18),
|
34 |
+
(_dd_expm1, 0.25, 0.0, 0.2840254166877415, -2.133257464457841e-17),
|
35 |
+
(_dd_expm1, 0.498046875, 0.0, 0.645504254608231, -9.198435524984236e-18),
|
36 |
+
(_dd_expm1, 1.25, 0.0, 2.4903429574618414, -4.604261945372796e-17)
|
37 |
+
]
|
38 |
+
|
39 |
+
|
40 |
+
@pytest.mark.parametrize('dd_func, xhi, xlo, expected_yhi, expected_ylo',
|
41 |
+
test_data)
|
42 |
+
def test_dd(dd_func, xhi, xlo, expected_yhi, expected_ylo):
|
43 |
+
yhi, ylo = dd_func(xhi, xlo)
|
44 |
+
assert yhi == expected_yhi, (f"high double ({yhi}) does not equal the "
|
45 |
+
f"expected value {expected_yhi}")
|
46 |
+
assert_allclose(ylo, expected_ylo, rtol=5e-15)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_digamma.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy import pi, log, sqrt
|
3 |
+
from numpy.testing import assert_, assert_equal
|
4 |
+
|
5 |
+
from scipy.special._testutils import FuncData
|
6 |
+
import scipy.special as sc
|
7 |
+
|
8 |
+
# Euler-Mascheroni constant
|
9 |
+
euler = 0.57721566490153286
|
10 |
+
|
11 |
+
|
12 |
+
def test_consistency():
|
13 |
+
# Make sure the implementation of digamma for real arguments
|
14 |
+
# agrees with the implementation of digamma for complex arguments.
|
15 |
+
|
16 |
+
# It's all poles after -1e16
|
17 |
+
x = np.r_[-np.logspace(15, -30, 200), np.logspace(-30, 300, 200)]
|
18 |
+
dataset = np.vstack((x + 0j, sc.digamma(x))).T
|
19 |
+
FuncData(sc.digamma, dataset, 0, 1, rtol=5e-14, nan_ok=True).check()
|
20 |
+
|
21 |
+
|
22 |
+
def test_special_values():
|
23 |
+
# Test special values from Gauss's digamma theorem. See
|
24 |
+
#
|
25 |
+
# https://en.wikipedia.org/wiki/Digamma_function
|
26 |
+
|
27 |
+
dataset = [
|
28 |
+
(1, -euler),
|
29 |
+
(0.5, -2*log(2) - euler),
|
30 |
+
(1/3, -pi/(2*sqrt(3)) - 3*log(3)/2 - euler),
|
31 |
+
(1/4, -pi/2 - 3*log(2) - euler),
|
32 |
+
(1/6, -pi*sqrt(3)/2 - 2*log(2) - 3*log(3)/2 - euler),
|
33 |
+
(1/8,
|
34 |
+
-pi/2 - 4*log(2) - (pi + log(2 + sqrt(2)) - log(2 - sqrt(2)))/sqrt(2) - euler)
|
35 |
+
]
|
36 |
+
|
37 |
+
dataset = np.asarray(dataset)
|
38 |
+
FuncData(sc.digamma, dataset, 0, 1, rtol=1e-14).check()
|
39 |
+
|
40 |
+
|
41 |
+
def test_nonfinite():
|
42 |
+
pts = [0.0, -0.0, np.inf]
|
43 |
+
std = [-np.inf, np.inf, np.inf]
|
44 |
+
assert_equal(sc.digamma(pts), std)
|
45 |
+
assert_(all(np.isnan(sc.digamma([-np.inf, -1]))))
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_ellip_harm.py
ADDED
@@ -0,0 +1,278 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
# Tests for the Ellipsoidal Harmonic Function,
|
3 |
+
# Distributed under the same license as SciPy itself.
|
4 |
+
#
|
5 |
+
|
6 |
+
import numpy as np
|
7 |
+
from numpy.testing import (assert_equal, assert_almost_equal, assert_allclose,
|
8 |
+
assert_, suppress_warnings)
|
9 |
+
from scipy.special._testutils import assert_func_equal
|
10 |
+
from scipy.special import ellip_harm, ellip_harm_2, ellip_normal
|
11 |
+
from scipy.integrate import IntegrationWarning
|
12 |
+
from numpy import sqrt, pi
|
13 |
+
|
14 |
+
|
15 |
+
def test_ellip_potential():
|
16 |
+
def change_coefficient(lambda1, mu, nu, h2, k2):
|
17 |
+
x = sqrt(lambda1**2*mu**2*nu**2/(h2*k2))
|
18 |
+
y = sqrt((lambda1**2 - h2)*(mu**2 - h2)*(h2 - nu**2)/(h2*(k2 - h2)))
|
19 |
+
z = sqrt((lambda1**2 - k2)*(k2 - mu**2)*(k2 - nu**2)/(k2*(k2 - h2)))
|
20 |
+
return x, y, z
|
21 |
+
|
22 |
+
def solid_int_ellip(lambda1, mu, nu, n, p, h2, k2):
|
23 |
+
return (ellip_harm(h2, k2, n, p, lambda1)*ellip_harm(h2, k2, n, p, mu)
|
24 |
+
* ellip_harm(h2, k2, n, p, nu))
|
25 |
+
|
26 |
+
def solid_int_ellip2(lambda1, mu, nu, n, p, h2, k2):
|
27 |
+
return (ellip_harm_2(h2, k2, n, p, lambda1)
|
28 |
+
* ellip_harm(h2, k2, n, p, mu)*ellip_harm(h2, k2, n, p, nu))
|
29 |
+
|
30 |
+
def summation(lambda1, mu1, nu1, lambda2, mu2, nu2, h2, k2):
|
31 |
+
tol = 1e-8
|
32 |
+
sum1 = 0
|
33 |
+
for n in range(20):
|
34 |
+
xsum = 0
|
35 |
+
for p in range(1, 2*n+2):
|
36 |
+
xsum += (4*pi*(solid_int_ellip(lambda2, mu2, nu2, n, p, h2, k2)
|
37 |
+
* solid_int_ellip2(lambda1, mu1, nu1, n, p, h2, k2)) /
|
38 |
+
(ellip_normal(h2, k2, n, p)*(2*n + 1)))
|
39 |
+
if abs(xsum) < 0.1*tol*abs(sum1):
|
40 |
+
break
|
41 |
+
sum1 += xsum
|
42 |
+
return sum1, xsum
|
43 |
+
|
44 |
+
def potential(lambda1, mu1, nu1, lambda2, mu2, nu2, h2, k2):
|
45 |
+
x1, y1, z1 = change_coefficient(lambda1, mu1, nu1, h2, k2)
|
46 |
+
x2, y2, z2 = change_coefficient(lambda2, mu2, nu2, h2, k2)
|
47 |
+
res = sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)
|
48 |
+
return 1/res
|
49 |
+
|
50 |
+
pts = [
|
51 |
+
(120, sqrt(19), 2, 41, sqrt(17), 2, 15, 25),
|
52 |
+
(120, sqrt(16), 3.2, 21, sqrt(11), 2.9, 11, 20),
|
53 |
+
]
|
54 |
+
|
55 |
+
with suppress_warnings() as sup:
|
56 |
+
sup.filter(IntegrationWarning, "The occurrence of roundoff error")
|
57 |
+
sup.filter(IntegrationWarning, "The maximum number of subdivisions")
|
58 |
+
|
59 |
+
for p in pts:
|
60 |
+
err_msg = repr(p)
|
61 |
+
exact = potential(*p)
|
62 |
+
result, last_term = summation(*p)
|
63 |
+
assert_allclose(exact, result, atol=0, rtol=1e-8, err_msg=err_msg)
|
64 |
+
assert_(abs(result - exact) < 10*abs(last_term), err_msg)
|
65 |
+
|
66 |
+
|
67 |
+
def test_ellip_norm():
|
68 |
+
|
69 |
+
def G01(h2, k2):
|
70 |
+
return 4*pi
|
71 |
+
|
72 |
+
def G11(h2, k2):
|
73 |
+
return 4*pi*h2*k2/3
|
74 |
+
|
75 |
+
def G12(h2, k2):
|
76 |
+
return 4*pi*h2*(k2 - h2)/3
|
77 |
+
|
78 |
+
def G13(h2, k2):
|
79 |
+
return 4*pi*k2*(k2 - h2)/3
|
80 |
+
|
81 |
+
def G22(h2, k2):
|
82 |
+
res = (2*(h2**4 + k2**4) - 4*h2*k2*(h2**2 + k2**2) + 6*h2**2*k2**2 +
|
83 |
+
sqrt(h2**2 + k2**2 - h2*k2)*(-2*(h2**3 + k2**3) + 3*h2*k2*(h2 + k2)))
|
84 |
+
return 16*pi/405*res
|
85 |
+
|
86 |
+
def G21(h2, k2):
|
87 |
+
res = (2*(h2**4 + k2**4) - 4*h2*k2*(h2**2 + k2**2) + 6*h2**2*k2**2
|
88 |
+
+ sqrt(h2**2 + k2**2 - h2*k2)*(2*(h2**3 + k2**3) - 3*h2*k2*(h2 + k2)))
|
89 |
+
return 16*pi/405*res
|
90 |
+
|
91 |
+
def G23(h2, k2):
|
92 |
+
return 4*pi*h2**2*k2*(k2 - h2)/15
|
93 |
+
|
94 |
+
def G24(h2, k2):
|
95 |
+
return 4*pi*h2*k2**2*(k2 - h2)/15
|
96 |
+
|
97 |
+
def G25(h2, k2):
|
98 |
+
return 4*pi*h2*k2*(k2 - h2)**2/15
|
99 |
+
|
100 |
+
def G32(h2, k2):
|
101 |
+
res = (16*(h2**4 + k2**4) - 36*h2*k2*(h2**2 + k2**2) + 46*h2**2*k2**2
|
102 |
+
+ sqrt(4*(h2**2 + k2**2) - 7*h2*k2)*(-8*(h2**3 + k2**3) +
|
103 |
+
11*h2*k2*(h2 + k2)))
|
104 |
+
return 16*pi/13125*k2*h2*res
|
105 |
+
|
106 |
+
def G31(h2, k2):
|
107 |
+
res = (16*(h2**4 + k2**4) - 36*h2*k2*(h2**2 + k2**2) + 46*h2**2*k2**2
|
108 |
+
+ sqrt(4*(h2**2 + k2**2) - 7*h2*k2)*(8*(h2**3 + k2**3) -
|
109 |
+
11*h2*k2*(h2 + k2)))
|
110 |
+
return 16*pi/13125*h2*k2*res
|
111 |
+
|
112 |
+
def G34(h2, k2):
|
113 |
+
res = (6*h2**4 + 16*k2**4 - 12*h2**3*k2 - 28*h2*k2**3 + 34*h2**2*k2**2
|
114 |
+
+ sqrt(h2**2 + 4*k2**2 - h2*k2)*(-6*h2**3 - 8*k2**3 + 9*h2**2*k2 +
|
115 |
+
13*h2*k2**2))
|
116 |
+
return 16*pi/13125*h2*(k2 - h2)*res
|
117 |
+
|
118 |
+
def G33(h2, k2):
|
119 |
+
res = (6*h2**4 + 16*k2**4 - 12*h2**3*k2 - 28*h2*k2**3 + 34*h2**2*k2**2
|
120 |
+
+ sqrt(h2**2 + 4*k2**2 - h2*k2)*(6*h2**3 + 8*k2**3 - 9*h2**2*k2 -
|
121 |
+
13*h2*k2**2))
|
122 |
+
return 16*pi/13125*h2*(k2 - h2)*res
|
123 |
+
|
124 |
+
def G36(h2, k2):
|
125 |
+
res = (16*h2**4 + 6*k2**4 - 28*h2**3*k2 - 12*h2*k2**3 + 34*h2**2*k2**2
|
126 |
+
+ sqrt(4*h2**2 + k2**2 - h2*k2)*(-8*h2**3 - 6*k2**3 + 13*h2**2*k2 +
|
127 |
+
9*h2*k2**2))
|
128 |
+
return 16*pi/13125*k2*(k2 - h2)*res
|
129 |
+
|
130 |
+
def G35(h2, k2):
|
131 |
+
res = (16*h2**4 + 6*k2**4 - 28*h2**3*k2 - 12*h2*k2**3 + 34*h2**2*k2**2
|
132 |
+
+ sqrt(4*h2**2 + k2**2 - h2*k2)*(8*h2**3 + 6*k2**3 - 13*h2**2*k2 -
|
133 |
+
9*h2*k2**2))
|
134 |
+
return 16*pi/13125*k2*(k2 - h2)*res
|
135 |
+
|
136 |
+
def G37(h2, k2):
|
137 |
+
return 4*pi*h2**2*k2**2*(k2 - h2)**2/105
|
138 |
+
|
139 |
+
known_funcs = {(0, 1): G01, (1, 1): G11, (1, 2): G12, (1, 3): G13,
|
140 |
+
(2, 1): G21, (2, 2): G22, (2, 3): G23, (2, 4): G24,
|
141 |
+
(2, 5): G25, (3, 1): G31, (3, 2): G32, (3, 3): G33,
|
142 |
+
(3, 4): G34, (3, 5): G35, (3, 6): G36, (3, 7): G37}
|
143 |
+
|
144 |
+
def _ellip_norm(n, p, h2, k2):
|
145 |
+
func = known_funcs[n, p]
|
146 |
+
return func(h2, k2)
|
147 |
+
_ellip_norm = np.vectorize(_ellip_norm)
|
148 |
+
|
149 |
+
def ellip_normal_known(h2, k2, n, p):
|
150 |
+
return _ellip_norm(n, p, h2, k2)
|
151 |
+
|
152 |
+
# generate both large and small h2 < k2 pairs
|
153 |
+
np.random.seed(1234)
|
154 |
+
h2 = np.random.pareto(0.5, size=1)
|
155 |
+
k2 = h2 * (1 + np.random.pareto(0.5, size=h2.size))
|
156 |
+
|
157 |
+
points = []
|
158 |
+
for n in range(4):
|
159 |
+
for p in range(1, 2*n+2):
|
160 |
+
points.append((h2, k2, np.full(h2.size, n), np.full(h2.size, p)))
|
161 |
+
points = np.array(points)
|
162 |
+
with suppress_warnings() as sup:
|
163 |
+
sup.filter(IntegrationWarning, "The occurrence of roundoff error")
|
164 |
+
assert_func_equal(ellip_normal, ellip_normal_known, points, rtol=1e-12)
|
165 |
+
|
166 |
+
|
167 |
+
def test_ellip_harm_2():
|
168 |
+
|
169 |
+
def I1(h2, k2, s):
|
170 |
+
res = (ellip_harm_2(h2, k2, 1, 1, s)/(3 * ellip_harm(h2, k2, 1, 1, s))
|
171 |
+
+ ellip_harm_2(h2, k2, 1, 2, s)/(3 * ellip_harm(h2, k2, 1, 2, s)) +
|
172 |
+
ellip_harm_2(h2, k2, 1, 3, s)/(3 * ellip_harm(h2, k2, 1, 3, s)))
|
173 |
+
return res
|
174 |
+
|
175 |
+
with suppress_warnings() as sup:
|
176 |
+
sup.filter(IntegrationWarning, "The occurrence of roundoff error")
|
177 |
+
assert_almost_equal(I1(5, 8, 10), 1/(10*sqrt((100-5)*(100-8))))
|
178 |
+
|
179 |
+
# Values produced by code from arXiv:1204.0267
|
180 |
+
assert_almost_equal(ellip_harm_2(5, 8, 2, 1, 10), 0.00108056853382)
|
181 |
+
assert_almost_equal(ellip_harm_2(5, 8, 2, 2, 10), 0.00105820513809)
|
182 |
+
assert_almost_equal(ellip_harm_2(5, 8, 2, 3, 10), 0.00106058384743)
|
183 |
+
assert_almost_equal(ellip_harm_2(5, 8, 2, 4, 10), 0.00106774492306)
|
184 |
+
assert_almost_equal(ellip_harm_2(5, 8, 2, 5, 10), 0.00107976356454)
|
185 |
+
|
186 |
+
|
187 |
+
def test_ellip_harm():
|
188 |
+
|
189 |
+
def E01(h2, k2, s):
|
190 |
+
return 1
|
191 |
+
|
192 |
+
def E11(h2, k2, s):
|
193 |
+
return s
|
194 |
+
|
195 |
+
def E12(h2, k2, s):
|
196 |
+
return sqrt(abs(s*s - h2))
|
197 |
+
|
198 |
+
def E13(h2, k2, s):
|
199 |
+
return sqrt(abs(s*s - k2))
|
200 |
+
|
201 |
+
def E21(h2, k2, s):
|
202 |
+
return s*s - 1/3*((h2 + k2) + sqrt(abs((h2 + k2)*(h2 + k2)-3*h2*k2)))
|
203 |
+
|
204 |
+
def E22(h2, k2, s):
|
205 |
+
return s*s - 1/3*((h2 + k2) - sqrt(abs((h2 + k2)*(h2 + k2)-3*h2*k2)))
|
206 |
+
|
207 |
+
def E23(h2, k2, s):
|
208 |
+
return s * sqrt(abs(s*s - h2))
|
209 |
+
|
210 |
+
def E24(h2, k2, s):
|
211 |
+
return s * sqrt(abs(s*s - k2))
|
212 |
+
|
213 |
+
def E25(h2, k2, s):
|
214 |
+
return sqrt(abs((s*s - h2)*(s*s - k2)))
|
215 |
+
|
216 |
+
def E31(h2, k2, s):
|
217 |
+
return s*s*s - (s/5)*(2*(h2 + k2) + sqrt(4*(h2 + k2)*(h2 + k2) -
|
218 |
+
15*h2*k2))
|
219 |
+
|
220 |
+
def E32(h2, k2, s):
|
221 |
+
return s*s*s - (s/5)*(2*(h2 + k2) - sqrt(4*(h2 + k2)*(h2 + k2) -
|
222 |
+
15*h2*k2))
|
223 |
+
|
224 |
+
def E33(h2, k2, s):
|
225 |
+
return sqrt(abs(s*s - h2))*(s*s - 1/5*((h2 + 2*k2) + sqrt(abs((h2 +
|
226 |
+
2*k2)*(h2 + 2*k2) - 5*h2*k2))))
|
227 |
+
|
228 |
+
def E34(h2, k2, s):
|
229 |
+
return sqrt(abs(s*s - h2))*(s*s - 1/5*((h2 + 2*k2) - sqrt(abs((h2 +
|
230 |
+
2*k2)*(h2 + 2*k2) - 5*h2*k2))))
|
231 |
+
|
232 |
+
def E35(h2, k2, s):
|
233 |
+
return sqrt(abs(s*s - k2))*(s*s - 1/5*((2*h2 + k2) + sqrt(abs((2*h2
|
234 |
+
+ k2)*(2*h2 + k2) - 5*h2*k2))))
|
235 |
+
|
236 |
+
def E36(h2, k2, s):
|
237 |
+
return sqrt(abs(s*s - k2))*(s*s - 1/5*((2*h2 + k2) - sqrt(abs((2*h2
|
238 |
+
+ k2)*(2*h2 + k2) - 5*h2*k2))))
|
239 |
+
|
240 |
+
def E37(h2, k2, s):
|
241 |
+
return s * sqrt(abs((s*s - h2)*(s*s - k2)))
|
242 |
+
|
243 |
+
assert_equal(ellip_harm(5, 8, 1, 2, 2.5, 1, 1),
|
244 |
+
ellip_harm(5, 8, 1, 2, 2.5))
|
245 |
+
|
246 |
+
known_funcs = {(0, 1): E01, (1, 1): E11, (1, 2): E12, (1, 3): E13,
|
247 |
+
(2, 1): E21, (2, 2): E22, (2, 3): E23, (2, 4): E24,
|
248 |
+
(2, 5): E25, (3, 1): E31, (3, 2): E32, (3, 3): E33,
|
249 |
+
(3, 4): E34, (3, 5): E35, (3, 6): E36, (3, 7): E37}
|
250 |
+
|
251 |
+
point_ref = []
|
252 |
+
|
253 |
+
def ellip_harm_known(h2, k2, n, p, s):
|
254 |
+
for i in range(h2.size):
|
255 |
+
func = known_funcs[(int(n[i]), int(p[i]))]
|
256 |
+
point_ref.append(func(h2[i], k2[i], s[i]))
|
257 |
+
return point_ref
|
258 |
+
|
259 |
+
np.random.seed(1234)
|
260 |
+
h2 = np.random.pareto(0.5, size=30)
|
261 |
+
k2 = h2*(1 + np.random.pareto(0.5, size=h2.size))
|
262 |
+
s = np.random.pareto(0.5, size=h2.size)
|
263 |
+
points = []
|
264 |
+
for i in range(h2.size):
|
265 |
+
for n in range(4):
|
266 |
+
for p in range(1, 2*n+2):
|
267 |
+
points.append((h2[i], k2[i], n, p, s[i]))
|
268 |
+
points = np.array(points)
|
269 |
+
assert_func_equal(ellip_harm, ellip_harm_known, points, rtol=1e-12)
|
270 |
+
|
271 |
+
|
272 |
+
def test_ellip_harm_invalid_p():
|
273 |
+
# Regression test. This should return nan.
|
274 |
+
n = 4
|
275 |
+
# Make p > 2*n + 1.
|
276 |
+
p = 2*n + 2
|
277 |
+
result = ellip_harm(0.5, 2.0, n, p, 0.2)
|
278 |
+
assert np.isnan(result)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_erfinv.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import assert_allclose, assert_equal
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
import scipy.special as sc
|
6 |
+
|
7 |
+
|
8 |
+
class TestInverseErrorFunction:
|
9 |
+
def test_compliment(self):
|
10 |
+
# Test erfcinv(1 - x) == erfinv(x)
|
11 |
+
x = np.linspace(-1, 1, 101)
|
12 |
+
assert_allclose(sc.erfcinv(1 - x), sc.erfinv(x), rtol=0, atol=1e-15)
|
13 |
+
|
14 |
+
def test_literal_values(self):
|
15 |
+
# The expected values were calculated with mpmath:
|
16 |
+
#
|
17 |
+
# import mpmath
|
18 |
+
# mpmath.mp.dps = 200
|
19 |
+
# for y in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]:
|
20 |
+
# x = mpmath.erfinv(y)
|
21 |
+
# print(x)
|
22 |
+
#
|
23 |
+
y = np.array([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
|
24 |
+
actual = sc.erfinv(y)
|
25 |
+
expected = [
|
26 |
+
0.0,
|
27 |
+
0.08885599049425769,
|
28 |
+
0.1791434546212917,
|
29 |
+
0.2724627147267543,
|
30 |
+
0.37080715859355795,
|
31 |
+
0.4769362762044699,
|
32 |
+
0.5951160814499948,
|
33 |
+
0.7328690779592167,
|
34 |
+
0.9061938024368233,
|
35 |
+
1.1630871536766743,
|
36 |
+
]
|
37 |
+
assert_allclose(actual, expected, rtol=0, atol=1e-15)
|
38 |
+
|
39 |
+
@pytest.mark.parametrize(
|
40 |
+
'f, x, y',
|
41 |
+
[
|
42 |
+
(sc.erfinv, -1, -np.inf),
|
43 |
+
(sc.erfinv, 0, 0),
|
44 |
+
(sc.erfinv, 1, np.inf),
|
45 |
+
(sc.erfinv, -100, np.nan),
|
46 |
+
(sc.erfinv, 100, np.nan),
|
47 |
+
(sc.erfcinv, 0, np.inf),
|
48 |
+
(sc.erfcinv, 1, -0.0),
|
49 |
+
(sc.erfcinv, 2, -np.inf),
|
50 |
+
(sc.erfcinv, -100, np.nan),
|
51 |
+
(sc.erfcinv, 100, np.nan),
|
52 |
+
],
|
53 |
+
ids=[
|
54 |
+
'erfinv at lower bound',
|
55 |
+
'erfinv at midpoint',
|
56 |
+
'erfinv at upper bound',
|
57 |
+
'erfinv below lower bound',
|
58 |
+
'erfinv above upper bound',
|
59 |
+
'erfcinv at lower bound',
|
60 |
+
'erfcinv at midpoint',
|
61 |
+
'erfcinv at upper bound',
|
62 |
+
'erfcinv below lower bound',
|
63 |
+
'erfcinv above upper bound',
|
64 |
+
]
|
65 |
+
)
|
66 |
+
def test_domain_bounds(self, f, x, y):
|
67 |
+
assert_equal(f(x), y)
|
68 |
+
|
69 |
+
def test_erfinv_asympt(self):
|
70 |
+
# regression test for gh-12758: erfinv(x) loses precision at small x
|
71 |
+
# expected values precomputed with mpmath:
|
72 |
+
# >>> mpmath.mp.dps = 100
|
73 |
+
# >>> expected = [float(mpmath.erfinv(t)) for t in x]
|
74 |
+
x = np.array([1e-20, 1e-15, 1e-14, 1e-10, 1e-8, 0.9e-7, 1.1e-7, 1e-6])
|
75 |
+
expected = np.array([8.86226925452758e-21,
|
76 |
+
8.862269254527581e-16,
|
77 |
+
8.86226925452758e-15,
|
78 |
+
8.862269254527581e-11,
|
79 |
+
8.86226925452758e-09,
|
80 |
+
7.97604232907484e-08,
|
81 |
+
9.74849617998037e-08,
|
82 |
+
8.8622692545299e-07])
|
83 |
+
assert_allclose(sc.erfinv(x), expected,
|
84 |
+
rtol=1e-15)
|
85 |
+
|
86 |
+
# also test the roundtrip consistency
|
87 |
+
assert_allclose(sc.erf(sc.erfinv(x)),
|
88 |
+
x,
|
89 |
+
rtol=5e-15)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_exponential_integrals.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from numpy.testing import assert_allclose
|
5 |
+
import scipy.special as sc
|
6 |
+
|
7 |
+
|
8 |
+
class TestExp1:
|
9 |
+
|
10 |
+
def test_branch_cut(self):
|
11 |
+
assert np.isnan(sc.exp1(-1))
|
12 |
+
assert sc.exp1(complex(-1, 0)).imag == (
|
13 |
+
-sc.exp1(complex(-1, -0.0)).imag
|
14 |
+
)
|
15 |
+
|
16 |
+
assert_allclose(
|
17 |
+
sc.exp1(complex(-1, 0)),
|
18 |
+
sc.exp1(-1 + 1e-20j),
|
19 |
+
atol=0,
|
20 |
+
rtol=1e-15
|
21 |
+
)
|
22 |
+
assert_allclose(
|
23 |
+
sc.exp1(complex(-1, -0.0)),
|
24 |
+
sc.exp1(-1 - 1e-20j),
|
25 |
+
atol=0,
|
26 |
+
rtol=1e-15
|
27 |
+
)
|
28 |
+
|
29 |
+
def test_834(self):
|
30 |
+
# Regression test for #834
|
31 |
+
a = sc.exp1(-complex(19.9999990))
|
32 |
+
b = sc.exp1(-complex(19.9999991))
|
33 |
+
assert_allclose(a.imag, b.imag, atol=0, rtol=1e-15)
|
34 |
+
|
35 |
+
|
36 |
+
class TestScaledExp1:
|
37 |
+
|
38 |
+
@pytest.mark.parametrize('x, expected', [(0, 0), (np.inf, 1)])
|
39 |
+
def test_limits(self, x, expected):
|
40 |
+
y = sc._ufuncs._scaled_exp1(x)
|
41 |
+
assert y == expected
|
42 |
+
|
43 |
+
# The expected values were computed with mpmath, e.g.:
|
44 |
+
#
|
45 |
+
# from mpmath import mp
|
46 |
+
# mp.dps = 80
|
47 |
+
# x = 1e-25
|
48 |
+
# print(float(x*mp.exp(x)*np.expint(1, x)))
|
49 |
+
#
|
50 |
+
# prints 5.698741165994961e-24
|
51 |
+
#
|
52 |
+
# The method used to compute _scaled_exp1 changes at x=1
|
53 |
+
# and x=1250, so values at those inputs, and values just
|
54 |
+
# above and below them, are included in the test data.
|
55 |
+
@pytest.mark.parametrize('x, expected',
|
56 |
+
[(1e-25, 5.698741165994961e-24),
|
57 |
+
(0.1, 0.20146425447084518),
|
58 |
+
(0.9995, 0.5962509885831002),
|
59 |
+
(1.0, 0.5963473623231941),
|
60 |
+
(1.0005, 0.5964436833238044),
|
61 |
+
(2.5, 0.7588145912149602),
|
62 |
+
(10.0, 0.9156333393978808),
|
63 |
+
(100.0, 0.9901942286733019),
|
64 |
+
(500.0, 0.9980079523802055),
|
65 |
+
(1000.0, 0.9990019940238807),
|
66 |
+
(1249.5, 0.9992009578306811),
|
67 |
+
(1250.0, 0.9992012769377913),
|
68 |
+
(1250.25, 0.9992014363957858),
|
69 |
+
(2000.0, 0.9995004992514963),
|
70 |
+
(1e4, 0.9999000199940024),
|
71 |
+
(1e10, 0.9999999999),
|
72 |
+
(1e15, 0.999999999999999),
|
73 |
+
])
|
74 |
+
def test_scaled_exp1(self, x, expected):
|
75 |
+
y = sc._ufuncs._scaled_exp1(x)
|
76 |
+
assert_allclose(y, expected, rtol=2e-15)
|
77 |
+
|
78 |
+
|
79 |
+
class TestExpi:
|
80 |
+
|
81 |
+
@pytest.mark.parametrize('result', [
|
82 |
+
sc.expi(complex(-1, 0)),
|
83 |
+
sc.expi(complex(-1, -0.0)),
|
84 |
+
sc.expi(-1)
|
85 |
+
])
|
86 |
+
def test_branch_cut(self, result):
|
87 |
+
desired = -0.21938393439552027368 # Computed using Mpmath
|
88 |
+
assert_allclose(result, desired, atol=0, rtol=1e-14)
|
89 |
+
|
90 |
+
def test_near_branch_cut(self):
|
91 |
+
lim_from_above = sc.expi(-1 + 1e-20j)
|
92 |
+
lim_from_below = sc.expi(-1 - 1e-20j)
|
93 |
+
assert_allclose(
|
94 |
+
lim_from_above.real,
|
95 |
+
lim_from_below.real,
|
96 |
+
atol=0,
|
97 |
+
rtol=1e-15
|
98 |
+
)
|
99 |
+
assert_allclose(
|
100 |
+
lim_from_above.imag,
|
101 |
+
-lim_from_below.imag,
|
102 |
+
atol=0,
|
103 |
+
rtol=1e-15
|
104 |
+
)
|
105 |
+
|
106 |
+
def test_continuity_on_positive_real_axis(self):
|
107 |
+
assert_allclose(
|
108 |
+
sc.expi(complex(1, 0)),
|
109 |
+
sc.expi(complex(1, -0.0)),
|
110 |
+
atol=0,
|
111 |
+
rtol=1e-15
|
112 |
+
)
|
113 |
+
|
114 |
+
|
115 |
+
class TestExpn:
|
116 |
+
|
117 |
+
def test_out_of_domain(self):
|
118 |
+
assert all(np.isnan([sc.expn(-1, 1.0), sc.expn(1, -1.0)]))
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_faddeeva.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from numpy.testing import assert_allclose
|
5 |
+
import scipy.special as sc
|
6 |
+
from scipy.special._testutils import FuncData
|
7 |
+
|
8 |
+
|
9 |
+
class TestVoigtProfile:
|
10 |
+
|
11 |
+
@pytest.mark.parametrize('x, sigma, gamma', [
|
12 |
+
(np.nan, 1, 1),
|
13 |
+
(0, np.nan, 1),
|
14 |
+
(0, 1, np.nan),
|
15 |
+
(1, np.nan, 0),
|
16 |
+
(np.nan, 1, 0),
|
17 |
+
(1, 0, np.nan),
|
18 |
+
(np.nan, 0, 1),
|
19 |
+
(np.nan, 0, 0)
|
20 |
+
])
|
21 |
+
def test_nan(self, x, sigma, gamma):
|
22 |
+
assert np.isnan(sc.voigt_profile(x, sigma, gamma))
|
23 |
+
|
24 |
+
@pytest.mark.parametrize('x, desired', [
|
25 |
+
(-np.inf, 0),
|
26 |
+
(np.inf, 0)
|
27 |
+
])
|
28 |
+
def test_inf(self, x, desired):
|
29 |
+
assert sc.voigt_profile(x, 1, 1) == desired
|
30 |
+
|
31 |
+
def test_against_mathematica(self):
|
32 |
+
# Results obtained from Mathematica by computing
|
33 |
+
#
|
34 |
+
# PDF[VoigtDistribution[gamma, sigma], x]
|
35 |
+
#
|
36 |
+
points = np.array([
|
37 |
+
[-7.89, 45.06, 6.66, 0.0077921073660388806401],
|
38 |
+
[-0.05, 7.98, 24.13, 0.012068223646769913478],
|
39 |
+
[-13.98, 16.83, 42.37, 0.0062442236362132357833],
|
40 |
+
[-12.66, 0.21, 6.32, 0.010052516161087379402],
|
41 |
+
[11.34, 4.25, 21.96, 0.0113698923627278917805],
|
42 |
+
[-11.56, 20.40, 30.53, 0.0076332760432097464987],
|
43 |
+
[-9.17, 25.61, 8.32, 0.011646345779083005429],
|
44 |
+
[16.59, 18.05, 2.50, 0.013637768837526809181],
|
45 |
+
[9.11, 2.12, 39.33, 0.0076644040807277677585],
|
46 |
+
[-43.33, 0.30, 45.68, 0.0036680463875330150996]
|
47 |
+
])
|
48 |
+
FuncData(
|
49 |
+
sc.voigt_profile,
|
50 |
+
points,
|
51 |
+
(0, 1, 2),
|
52 |
+
3,
|
53 |
+
atol=0,
|
54 |
+
rtol=1e-15
|
55 |
+
).check()
|
56 |
+
|
57 |
+
def test_symmetry(self):
|
58 |
+
x = np.linspace(0, 10, 20)
|
59 |
+
assert_allclose(
|
60 |
+
sc.voigt_profile(x, 1, 1),
|
61 |
+
sc.voigt_profile(-x, 1, 1),
|
62 |
+
rtol=1e-15,
|
63 |
+
atol=0
|
64 |
+
)
|
65 |
+
|
66 |
+
@pytest.mark.parametrize('x, sigma, gamma, desired', [
|
67 |
+
(0, 0, 0, np.inf),
|
68 |
+
(1, 0, 0, 0)
|
69 |
+
])
|
70 |
+
def test_corner_cases(self, x, sigma, gamma, desired):
|
71 |
+
assert sc.voigt_profile(x, sigma, gamma) == desired
|
72 |
+
|
73 |
+
@pytest.mark.parametrize('sigma1, gamma1, sigma2, gamma2', [
|
74 |
+
(0, 1, 1e-16, 1),
|
75 |
+
(1, 0, 1, 1e-16),
|
76 |
+
(0, 0, 1e-16, 1e-16)
|
77 |
+
])
|
78 |
+
def test_continuity(self, sigma1, gamma1, sigma2, gamma2):
|
79 |
+
x = np.linspace(1, 10, 20)
|
80 |
+
assert_allclose(
|
81 |
+
sc.voigt_profile(x, sigma1, gamma1),
|
82 |
+
sc.voigt_profile(x, sigma2, gamma2),
|
83 |
+
rtol=1e-16,
|
84 |
+
atol=1e-16
|
85 |
+
)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_gamma.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import scipy.special as sc
|
3 |
+
|
4 |
+
|
5 |
+
class TestRgamma:
|
6 |
+
|
7 |
+
def test_gh_11315(self):
|
8 |
+
assert sc.rgamma(-35) == 0
|
9 |
+
|
10 |
+
def test_rgamma_zeros(self):
|
11 |
+
x = np.array([0, -10, -100, -1000, -10000])
|
12 |
+
assert np.all(sc.rgamma(x) == 0)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_gammainc.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
from numpy.testing import assert_allclose, assert_array_equal
|
5 |
+
|
6 |
+
import scipy.special as sc
|
7 |
+
from scipy.special._testutils import FuncData
|
8 |
+
|
9 |
+
|
10 |
+
INVALID_POINTS = [
|
11 |
+
(1, -1),
|
12 |
+
(0, 0),
|
13 |
+
(-1, 1),
|
14 |
+
(np.nan, 1),
|
15 |
+
(1, np.nan)
|
16 |
+
]
|
17 |
+
|
18 |
+
|
19 |
+
class TestGammainc:
|
20 |
+
|
21 |
+
@pytest.mark.parametrize('a, x', INVALID_POINTS)
|
22 |
+
def test_domain(self, a, x):
|
23 |
+
assert np.isnan(sc.gammainc(a, x))
|
24 |
+
|
25 |
+
def test_a_eq_0_x_gt_0(self):
|
26 |
+
assert sc.gammainc(0, 1) == 1
|
27 |
+
|
28 |
+
@pytest.mark.parametrize('a, x, desired', [
|
29 |
+
(np.inf, 1, 0),
|
30 |
+
(np.inf, 0, 0),
|
31 |
+
(np.inf, np.inf, np.nan),
|
32 |
+
(1, np.inf, 1)
|
33 |
+
])
|
34 |
+
def test_infinite_arguments(self, a, x, desired):
|
35 |
+
result = sc.gammainc(a, x)
|
36 |
+
if np.isnan(desired):
|
37 |
+
assert np.isnan(result)
|
38 |
+
else:
|
39 |
+
assert result == desired
|
40 |
+
|
41 |
+
def test_infinite_limits(self):
|
42 |
+
# Test that large arguments converge to the hard-coded limits
|
43 |
+
# at infinity.
|
44 |
+
assert_allclose(
|
45 |
+
sc.gammainc(1000, 100),
|
46 |
+
sc.gammainc(np.inf, 100),
|
47 |
+
atol=1e-200, # Use `atol` since the function converges to 0.
|
48 |
+
rtol=0
|
49 |
+
)
|
50 |
+
assert sc.gammainc(100, 1000) == sc.gammainc(100, np.inf)
|
51 |
+
|
52 |
+
def test_x_zero(self):
|
53 |
+
a = np.arange(1, 10)
|
54 |
+
assert_array_equal(sc.gammainc(a, 0), 0)
|
55 |
+
|
56 |
+
def test_limit_check(self):
|
57 |
+
result = sc.gammainc(1e-10, 1)
|
58 |
+
limit = sc.gammainc(0, 1)
|
59 |
+
assert np.isclose(result, limit)
|
60 |
+
|
61 |
+
def gammainc_line(self, x):
|
62 |
+
# The line a = x where a simpler asymptotic expansion (analog
|
63 |
+
# of DLMF 8.12.15) is available.
|
64 |
+
c = np.array([-1/3, -1/540, 25/6048, 101/155520,
|
65 |
+
-3184811/3695155200, -2745493/8151736420])
|
66 |
+
res = 0
|
67 |
+
xfac = 1
|
68 |
+
for ck in c:
|
69 |
+
res -= ck*xfac
|
70 |
+
xfac /= x
|
71 |
+
res /= np.sqrt(2*np.pi*x)
|
72 |
+
res += 0.5
|
73 |
+
return res
|
74 |
+
|
75 |
+
def test_line(self):
|
76 |
+
x = np.logspace(np.log10(25), 300, 500)
|
77 |
+
a = x
|
78 |
+
dataset = np.vstack((a, x, self.gammainc_line(x))).T
|
79 |
+
FuncData(sc.gammainc, dataset, (0, 1), 2, rtol=1e-11).check()
|
80 |
+
|
81 |
+
def test_roundtrip(self):
|
82 |
+
a = np.logspace(-5, 10, 100)
|
83 |
+
x = np.logspace(-5, 10, 100)
|
84 |
+
|
85 |
+
y = sc.gammaincinv(a, sc.gammainc(a, x))
|
86 |
+
assert_allclose(x, y, rtol=1e-10)
|
87 |
+
|
88 |
+
|
89 |
+
class TestGammaincc:
|
90 |
+
|
91 |
+
@pytest.mark.parametrize('a, x', INVALID_POINTS)
|
92 |
+
def test_domain(self, a, x):
|
93 |
+
assert np.isnan(sc.gammaincc(a, x))
|
94 |
+
|
95 |
+
def test_a_eq_0_x_gt_0(self):
|
96 |
+
assert sc.gammaincc(0, 1) == 0
|
97 |
+
|
98 |
+
@pytest.mark.parametrize('a, x, desired', [
|
99 |
+
(np.inf, 1, 1),
|
100 |
+
(np.inf, 0, 1),
|
101 |
+
(np.inf, np.inf, np.nan),
|
102 |
+
(1, np.inf, 0)
|
103 |
+
])
|
104 |
+
def test_infinite_arguments(self, a, x, desired):
|
105 |
+
result = sc.gammaincc(a, x)
|
106 |
+
if np.isnan(desired):
|
107 |
+
assert np.isnan(result)
|
108 |
+
else:
|
109 |
+
assert result == desired
|
110 |
+
|
111 |
+
def test_infinite_limits(self):
|
112 |
+
# Test that large arguments converge to the hard-coded limits
|
113 |
+
# at infinity.
|
114 |
+
assert sc.gammaincc(1000, 100) == sc.gammaincc(np.inf, 100)
|
115 |
+
assert_allclose(
|
116 |
+
sc.gammaincc(100, 1000),
|
117 |
+
sc.gammaincc(100, np.inf),
|
118 |
+
atol=1e-200, # Use `atol` since the function converges to 0.
|
119 |
+
rtol=0
|
120 |
+
)
|
121 |
+
|
122 |
+
def test_limit_check(self):
|
123 |
+
result = sc.gammaincc(1e-10,1)
|
124 |
+
limit = sc.gammaincc(0,1)
|
125 |
+
assert np.isclose(result, limit)
|
126 |
+
|
127 |
+
def test_x_zero(self):
|
128 |
+
a = np.arange(1, 10)
|
129 |
+
assert_array_equal(sc.gammaincc(a, 0), 1)
|
130 |
+
|
131 |
+
def test_roundtrip(self):
|
132 |
+
a = np.logspace(-5, 10, 100)
|
133 |
+
x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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tests for hyp2f1 for complex values.
|
2 |
+
|
3 |
+
Author: Albert Steppi, with credit to Adam Kullberg (FormerPhycisist) for
|
4 |
+
the implementation of mp_hyp2f1 below, which modifies mpmath's hyp2f1 to
|
5 |
+
return the same branch as scipy's on the standard branch cut.
|
6 |
+
"""
|
7 |
+
|
8 |
+
import sys
|
9 |
+
import pytest
|
10 |
+
import numpy as np
|
11 |
+
from typing import NamedTuple
|
12 |
+
from numpy.testing import assert_allclose
|
13 |
+
|
14 |
+
from scipy.special import hyp2f1
|
15 |
+
from scipy.special._testutils import check_version, MissingModule
|
16 |
+
|
17 |
+
|
18 |
+
try:
|
19 |
+
import mpmath
|
20 |
+
except ImportError:
|
21 |
+
mpmath = MissingModule("mpmath")
|
22 |
+
|
23 |
+
|
24 |
+
def mp_hyp2f1(a, b, c, z):
|
25 |
+
"""Return mpmath hyp2f1 calculated on same branch as scipy hyp2f1.
|
26 |
+
|
27 |
+
For most values of a,b,c mpmath returns the x - 0j branch of hyp2f1 on the
|
28 |
+
branch cut x=(1,inf) whereas scipy's hyp2f1 calculates the x + 0j branch.
|
29 |
+
Thus, to generate the right comparison values on the branch cut, we
|
30 |
+
evaluate mpmath.hyp2f1 at x + 1e-15*j.
|
31 |
+
|
32 |
+
The exception to this occurs when c-a=-m in which case both mpmath and
|
33 |
+
scipy calculate the x + 0j branch on the branch cut. When this happens
|
34 |
+
mpmath.hyp2f1 will be evaluated at the original z point.
|
35 |
+
"""
|
36 |
+
on_branch_cut = z.real > 1.0 and abs(z.imag) < 1.0e-15
|
37 |
+
cond1 = abs(c - a - round(c - a)) < 1.0e-15 and round(c - a) <= 0
|
38 |
+
cond2 = abs(c - b - round(c - b)) < 1.0e-15 and round(c - b) <= 0
|
39 |
+
# Make sure imaginary part is *exactly* zero
|
40 |
+
if on_branch_cut:
|
41 |
+
z = z.real + 0.0j
|
42 |
+
if on_branch_cut and not (cond1 or cond2):
|
43 |
+
z_mpmath = z.real + 1.0e-15j
|
44 |
+
else:
|
45 |
+
z_mpmath = z
|
46 |
+
return complex(mpmath.hyp2f1(a, b, c, z_mpmath))
|
47 |
+
|
48 |
+
|
49 |
+
class Hyp2f1TestCase(NamedTuple):
|
50 |
+
a: float
|
51 |
+
b: float
|
52 |
+
c: float
|
53 |
+
z: complex
|
54 |
+
expected: complex
|
55 |
+
rtol: float
|
56 |
+
|
57 |
+
|
58 |
+
class TestHyp2f1:
|
59 |
+
"""Tests for hyp2f1 for complex values.
|
60 |
+
|
61 |
+
Expected values for test cases were computed using mpmath. See
|
62 |
+
`scipy.special._precompute.hyp2f1_data`. The verbose style of specifying
|
63 |
+
test cases is used for readability and to make it easier to mark individual
|
64 |
+
cases as expected to fail. Expected failures are used to highlight cases
|
65 |
+
where improvements are needed. See
|
66 |
+
`scipy.special._precompute.hyp2f1_data.make_hyp2f1_test_cases` for a
|
67 |
+
function to generate the boilerplate for the test cases.
|
68 |
+
|
69 |
+
Assertions have been added to each test to ensure that the test cases match
|
70 |
+
the situations that are intended. A final test `test_test_hyp2f1` checks
|
71 |
+
that the expected values in the test cases actually match what is computed
|
72 |
+
by mpmath. This test is marked slow even though it isn't particularly slow
|
73 |
+
so that it won't run by default on continuous integration builds.
|
74 |
+
"""
|
75 |
+
@pytest.mark.parametrize(
|
76 |
+
"hyp2f1_test_case",
|
77 |
+
[
|
78 |
+
pytest.param(
|
79 |
+
Hyp2f1TestCase(
|
80 |
+
a=0.5,
|
81 |
+
b=0.2,
|
82 |
+
c=-10,
|
83 |
+
z=0.2 + 0.2j,
|
84 |
+
expected=np.inf + 0j,
|
85 |
+
rtol=0
|
86 |
+
)
|
87 |
+
),
|
88 |
+
pytest.param(
|
89 |
+
Hyp2f1TestCase(
|
90 |
+
a=0.5,
|
91 |
+
b=0.2,
|
92 |
+
c=-10,
|
93 |
+
z=0 + 0j,
|
94 |
+
expected=1 + 0j,
|
95 |
+
rtol=0
|
96 |
+
),
|
97 |
+
),
|
98 |
+
pytest.param(
|
99 |
+
Hyp2f1TestCase(
|
100 |
+
a=0.5,
|
101 |
+
b=0,
|
102 |
+
c=-10,
|
103 |
+
z=0.2 + 0.2j,
|
104 |
+
expected=1 + 0j,
|
105 |
+
rtol=0
|
106 |
+
),
|
107 |
+
),
|
108 |
+
pytest.param(
|
109 |
+
Hyp2f1TestCase(
|
110 |
+
a=0.5,
|
111 |
+
b=0,
|
112 |
+
c=0,
|
113 |
+
z=0.2 + 0.2j,
|
114 |
+
expected=1 + 0j,
|
115 |
+
rtol=0,
|
116 |
+
),
|
117 |
+
),
|
118 |
+
pytest.param(
|
119 |
+
Hyp2f1TestCase(
|
120 |
+
a=0.5,
|
121 |
+
b=0.2,
|
122 |
+
c=0,
|
123 |
+
z=0.2 + 0.2j,
|
124 |
+
expected=np.inf + 0j,
|
125 |
+
rtol=0,
|
126 |
+
),
|
127 |
+
),
|
128 |
+
pytest.param(
|
129 |
+
Hyp2f1TestCase(
|
130 |
+
a=0.5,
|
131 |
+
b=0.2,
|
132 |
+
c=0,
|
133 |
+
z=0 + 0j,
|
134 |
+
expected=np.nan + 0j,
|
135 |
+
rtol=0,
|
136 |
+
),
|
137 |
+
),
|
138 |
+
pytest.param(
|
139 |
+
Hyp2f1TestCase(
|
140 |
+
a=0.5,
|
141 |
+
b=-5,
|
142 |
+
c=-10,
|
143 |
+
z=0.2 + 0.2j,
|
144 |
+
expected=(1.0495404166666666+0.05708208333333334j),
|
145 |
+
rtol=1e-15,
|
146 |
+
),
|
147 |
+
),
|
148 |
+
pytest.param(
|
149 |
+
Hyp2f1TestCase(
|
150 |
+
a=0.5,
|
151 |
+
b=-10,
|
152 |
+
c=-10,
|
153 |
+
z=0.2 + 0.2j,
|
154 |
+
expected=(1.092966013125+0.13455014673750001j),
|
155 |
+
rtol=1e-15,
|
156 |
+
),
|
157 |
+
),
|
158 |
+
pytest.param(
|
159 |
+
Hyp2f1TestCase(
|
160 |
+
a=-10,
|
161 |
+
b=-20,
|
162 |
+
c=-10,
|
163 |
+
z=0.2 + 0.2j,
|
164 |
+
expected=(-0.07712512000000005+0.12752814080000005j),
|
165 |
+
rtol=1e-13,
|
166 |
+
),
|
167 |
+
),
|
168 |
+
pytest.param(
|
169 |
+
Hyp2f1TestCase(
|
170 |
+
a=-1,
|
171 |
+
b=3.2,
|
172 |
+
c=-1,
|
173 |
+
z=0.2 + 0.2j,
|
174 |
+
expected=(1.6400000000000001+0.6400000000000001j),
|
175 |
+
rtol=1e-13,
|
176 |
+
),
|
177 |
+
),
|
178 |
+
pytest.param(
|
179 |
+
Hyp2f1TestCase(
|
180 |
+
a=-2,
|
181 |
+
b=1.2,
|
182 |
+
c=-4,
|
183 |
+
z=1 + 0j,
|
184 |
+
expected=1.8200000000000001 + 0j,
|
185 |
+
rtol=1e-15,
|
186 |
+
),
|
187 |
+
),
|
188 |
+
]
|
189 |
+
)
|
190 |
+
def test_c_non_positive_int(self, hyp2f1_test_case):
|
191 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
192 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
193 |
+
|
194 |
+
@pytest.mark.parametrize(
|
195 |
+
"hyp2f1_test_case",
|
196 |
+
[
|
197 |
+
pytest.param(
|
198 |
+
Hyp2f1TestCase(
|
199 |
+
a=0.5,
|
200 |
+
b=0.2,
|
201 |
+
c=1.5,
|
202 |
+
z=1 + 0j,
|
203 |
+
expected=1.1496439092239847 + 0j,
|
204 |
+
rtol=1e-15
|
205 |
+
),
|
206 |
+
),
|
207 |
+
pytest.param(
|
208 |
+
Hyp2f1TestCase(
|
209 |
+
a=12.3,
|
210 |
+
b=8.0,
|
211 |
+
c=20.31,
|
212 |
+
z=1 + 0j,
|
213 |
+
expected=69280986.75273195 + 0j,
|
214 |
+
rtol=1e-15
|
215 |
+
),
|
216 |
+
),
|
217 |
+
pytest.param(
|
218 |
+
Hyp2f1TestCase(
|
219 |
+
a=290.2,
|
220 |
+
b=321.5,
|
221 |
+
c=700.1,
|
222 |
+
z=1 + 0j,
|
223 |
+
expected=1.3396562400934e117 + 0j,
|
224 |
+
rtol=1e-12,
|
225 |
+
),
|
226 |
+
),
|
227 |
+
# Note that here even mpmath produces different results for
|
228 |
+
# results that should be equivalent.
|
229 |
+
pytest.param(
|
230 |
+
Hyp2f1TestCase(
|
231 |
+
a=9.2,
|
232 |
+
b=621.5,
|
233 |
+
c=700.1,
|
234 |
+
z=(1+0j),
|
235 |
+
expected=(952726652.4158565+0j),
|
236 |
+
rtol=5e-13,
|
237 |
+
),
|
238 |
+
),
|
239 |
+
pytest.param(
|
240 |
+
Hyp2f1TestCase(
|
241 |
+
a=621.5,
|
242 |
+
b=9.2,
|
243 |
+
c=700.1,
|
244 |
+
z=(1+0j),
|
245 |
+
expected=(952726652.4160284+0j),
|
246 |
+
rtol=5e-12,
|
247 |
+
),
|
248 |
+
),
|
249 |
+
pytest.param(
|
250 |
+
Hyp2f1TestCase(
|
251 |
+
a=-101.2,
|
252 |
+
b=-400.4,
|
253 |
+
c=-172.1,
|
254 |
+
z=(1+0j),
|
255 |
+
expected=(2.2253618341394838e+37+0j),
|
256 |
+
rtol=1e-13,
|
257 |
+
),
|
258 |
+
),
|
259 |
+
pytest.param(
|
260 |
+
Hyp2f1TestCase(
|
261 |
+
a=-400.4,
|
262 |
+
b=-101.2,
|
263 |
+
c=-172.1,
|
264 |
+
z=(1+0j),
|
265 |
+
expected=(2.2253618341394838e+37+0j),
|
266 |
+
rtol=5e-13,
|
267 |
+
),
|
268 |
+
),
|
269 |
+
pytest.param(
|
270 |
+
Hyp2f1TestCase(
|
271 |
+
a=172.5,
|
272 |
+
b=-201.3,
|
273 |
+
c=151.2,
|
274 |
+
z=(1+0j),
|
275 |
+
expected=(7.072266653650905e-135+0j),
|
276 |
+
rtol=5e-13,
|
277 |
+
),
|
278 |
+
),
|
279 |
+
pytest.param(
|
280 |
+
Hyp2f1TestCase(
|
281 |
+
a=-201.3,
|
282 |
+
b=172.5,
|
283 |
+
c=151.2,
|
284 |
+
z=(1+0j),
|
285 |
+
expected=(7.072266653650905e-135+0j),
|
286 |
+
rtol=5e-13,
|
287 |
+
),
|
288 |
+
),
|
289 |
+
pytest.param(
|
290 |
+
Hyp2f1TestCase(
|
291 |
+
a=-102.1,
|
292 |
+
b=-20.3,
|
293 |
+
c=1.3,
|
294 |
+
z=1 + 0j,
|
295 |
+
expected=2.7899070752746906e22 + 0j,
|
296 |
+
rtol=3e-14,
|
297 |
+
),
|
298 |
+
),
|
299 |
+
pytest.param(
|
300 |
+
Hyp2f1TestCase(
|
301 |
+
a=-202.6,
|
302 |
+
b=60.3,
|
303 |
+
c=1.5,
|
304 |
+
z=1 + 0j,
|
305 |
+
expected=-1.3113641413099326e-56 + 0j,
|
306 |
+
rtol=1e-12,
|
307 |
+
),
|
308 |
+
),
|
309 |
+
],
|
310 |
+
)
|
311 |
+
def test_unital_argument(self, hyp2f1_test_case):
|
312 |
+
"""Tests for case z = 1, c - a - b > 0.
|
313 |
+
|
314 |
+
Expected answers computed using mpmath.
|
315 |
+
"""
|
316 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
317 |
+
assert z == 1 and c - a - b > 0 # Tests the test
|
318 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
319 |
+
|
320 |
+
@pytest.mark.parametrize(
|
321 |
+
"hyp2f1_test_case",
|
322 |
+
[
|
323 |
+
pytest.param(
|
324 |
+
Hyp2f1TestCase(
|
325 |
+
a=0.5,
|
326 |
+
b=0.2,
|
327 |
+
c=1.3,
|
328 |
+
z=-1 + 0j,
|
329 |
+
expected=0.9428846409614143 + 0j,
|
330 |
+
rtol=1e-15),
|
331 |
+
),
|
332 |
+
pytest.param(
|
333 |
+
Hyp2f1TestCase(
|
334 |
+
a=12.3,
|
335 |
+
b=8.0,
|
336 |
+
c=5.300000000000001,
|
337 |
+
z=-1 + 0j,
|
338 |
+
expected=-4.845809986595704e-06 + 0j,
|
339 |
+
rtol=1e-15
|
340 |
+
),
|
341 |
+
),
|
342 |
+
pytest.param(
|
343 |
+
Hyp2f1TestCase(
|
344 |
+
a=221.5,
|
345 |
+
b=90.2,
|
346 |
+
c=132.3,
|
347 |
+
z=-1 + 0j,
|
348 |
+
expected=2.0490488728377282e-42 + 0j,
|
349 |
+
rtol=1e-7,
|
350 |
+
),
|
351 |
+
),
|
352 |
+
pytest.param(
|
353 |
+
Hyp2f1TestCase(
|
354 |
+
a=-102.1,
|
355 |
+
b=-20.3,
|
356 |
+
c=-80.8,
|
357 |
+
z=-1 + 0j,
|
358 |
+
expected=45143784.46783885 + 0j,
|
359 |
+
rtol=1e-7,
|
360 |
+
),
|
361 |
+
marks=pytest.mark.xfail(
|
362 |
+
condition=sys.maxsize < 2**32,
|
363 |
+
reason="Fails on 32 bit.",
|
364 |
+
)
|
365 |
+
),
|
366 |
+
],
|
367 |
+
)
|
368 |
+
def test_special_case_z_near_minus_1(self, hyp2f1_test_case):
|
369 |
+
"""Tests for case z ~ -1, c ~ 1 + a - b
|
370 |
+
|
371 |
+
Expected answers computed using mpmath.
|
372 |
+
"""
|
373 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
374 |
+
assert abs(1 + a - b - c) < 1e-15 and abs(z + 1) < 1e-15
|
375 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
376 |
+
|
377 |
+
@pytest.mark.parametrize(
|
378 |
+
"hyp2f1_test_case",
|
379 |
+
[
|
380 |
+
pytest.param(
|
381 |
+
Hyp2f1TestCase(
|
382 |
+
a=-4,
|
383 |
+
b=2.02764642551431,
|
384 |
+
c=1.0561196186065624,
|
385 |
+
z=(0.9473684210526314-0.10526315789473695j),
|
386 |
+
expected=(0.0031961077109535375-0.0011313924606557173j),
|
387 |
+
rtol=1e-12,
|
388 |
+
),
|
389 |
+
),
|
390 |
+
pytest.param(
|
391 |
+
Hyp2f1TestCase(
|
392 |
+
a=-8,
|
393 |
+
b=-7.937789122896016,
|
394 |
+
c=-15.964218273004214,
|
395 |
+
z=(2-0.10526315789473695j),
|
396 |
+
expected=(0.005543763196412503-0.0025948879065698306j),
|
397 |
+
rtol=5e-13,
|
398 |
+
),
|
399 |
+
),
|
400 |
+
pytest.param(
|
401 |
+
Hyp2f1TestCase(
|
402 |
+
a=-8,
|
403 |
+
b=8.095813935368371,
|
404 |
+
c=4.0013768449590685,
|
405 |
+
z=(0.9473684210526314-0.10526315789473695j),
|
406 |
+
expected=(-0.0003054674127221263-9.261359291755414e-05j),
|
407 |
+
rtol=1e-10,
|
408 |
+
),
|
409 |
+
),
|
410 |
+
pytest.param(
|
411 |
+
Hyp2f1TestCase(
|
412 |
+
a=-4,
|
413 |
+
b=-3.956227226099288,
|
414 |
+
c=-3.9316537064827854,
|
415 |
+
z=(1.1578947368421053-0.3157894736842106j),
|
416 |
+
expected=(-0.0020809502580892937-0.0041877333232365095j),
|
417 |
+
rtol=5e-12,
|
418 |
+
),
|
419 |
+
),
|
420 |
+
pytest.param(
|
421 |
+
Hyp2f1TestCase(
|
422 |
+
a=2.02764642551431,
|
423 |
+
b=-4,
|
424 |
+
c=2.050308316530781,
|
425 |
+
z=(0.9473684210526314-0.10526315789473695j),
|
426 |
+
expected=(0.0011282435590058734+0.0002027062303465851j),
|
427 |
+
rtol=5e-13,
|
428 |
+
),
|
429 |
+
),
|
430 |
+
pytest.param(
|
431 |
+
Hyp2f1TestCase(
|
432 |
+
a=-7.937789122896016,
|
433 |
+
b=-8,
|
434 |
+
c=-15.964218273004214,
|
435 |
+
z=(1.3684210526315788+0.10526315789473673j),
|
436 |
+
expected=(-9.134907719238265e-05-0.00040219233987390723j),
|
437 |
+
rtol=5e-12,
|
438 |
+
),
|
439 |
+
),
|
440 |
+
pytest.param(
|
441 |
+
Hyp2f1TestCase(
|
442 |
+
a=4.080187217753502,
|
443 |
+
b=-4,
|
444 |
+
c=4.0013768449590685,
|
445 |
+
z=(0.9473684210526314-0.10526315789473695j),
|
446 |
+
expected=(-0.000519013062087489-0.0005855883076830948j),
|
447 |
+
rtol=5e-12,
|
448 |
+
),
|
449 |
+
),
|
450 |
+
pytest.param(
|
451 |
+
Hyp2f1TestCase(
|
452 |
+
a=-10000,
|
453 |
+
b=2.2,
|
454 |
+
c=93459345.3,
|
455 |
+
z=(2+2j),
|
456 |
+
expected=(0.9995292071559088-0.00047047067522659253j),
|
457 |
+
rtol=1e-12,
|
458 |
+
),
|
459 |
+
),
|
460 |
+
]
|
461 |
+
)
|
462 |
+
def test_a_b_negative_int(self, hyp2f1_test_case):
|
463 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
464 |
+
assert a == int(a) and a < 0 or b == int(b) and b < 0 # Tests the test
|
465 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
466 |
+
|
467 |
+
@pytest.mark.parametrize(
|
468 |
+
"hyp2f1_test_case",
|
469 |
+
[
|
470 |
+
pytest.param(
|
471 |
+
Hyp2f1TestCase(
|
472 |
+
a=-0.5,
|
473 |
+
b=-0.9629749245209605,
|
474 |
+
c=-15.5,
|
475 |
+
z=(1.1578947368421053-1.1578947368421053j),
|
476 |
+
expected=(0.9778506962676361+0.044083801141231616j),
|
477 |
+
rtol=3e-12,
|
478 |
+
),
|
479 |
+
),
|
480 |
+
pytest.param(
|
481 |
+
Hyp2f1TestCase(
|
482 |
+
a=8.5,
|
483 |
+
b=-3.9316537064827854,
|
484 |
+
c=1.5,
|
485 |
+
z=(0.9473684210526314-0.10526315789473695j),
|
486 |
+
expected=(4.0793167523167675-10.11694246310966j),
|
487 |
+
rtol=6e-12,
|
488 |
+
),
|
489 |
+
),
|
490 |
+
pytest.param(
|
491 |
+
Hyp2f1TestCase(
|
492 |
+
a=8.5,
|
493 |
+
b=-0.9629749245209605,
|
494 |
+
c=2.5,
|
495 |
+
z=(1.1578947368421053-0.10526315789473695j),
|
496 |
+
expected=(-2.9692999501916915+0.6394599899845594j),
|
497 |
+
rtol=1e-11,
|
498 |
+
),
|
499 |
+
),
|
500 |
+
pytest.param(
|
501 |
+
Hyp2f1TestCase(
|
502 |
+
a=-0.5,
|
503 |
+
b=-0.9629749245209605,
|
504 |
+
c=-15.5,
|
505 |
+
z=(1.5789473684210522-1.1578947368421053j),
|
506 |
+
expected=(0.9493076367106102-0.04316852977183447j),
|
507 |
+
rtol=1e-11,
|
508 |
+
),
|
509 |
+
),
|
510 |
+
pytest.param(
|
511 |
+
Hyp2f1TestCase(
|
512 |
+
a=-0.9220024191881196,
|
513 |
+
b=-0.5,
|
514 |
+
c=-15.5,
|
515 |
+
z=(0.5263157894736841+0.10526315789473673j),
|
516 |
+
expected=(0.9844377175631795-0.003120587561483841j),
|
517 |
+
rtol=1e-10,
|
518 |
+
),
|
519 |
+
),
|
520 |
+
],
|
521 |
+
)
|
522 |
+
def test_a_b_neg_int_after_euler_hypergeometric_transformation(
|
523 |
+
self, hyp2f1_test_case
|
524 |
+
):
|
525 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
526 |
+
assert ( # Tests the test
|
527 |
+
(abs(c - a - int(c - a)) < 1e-15 and c - a < 0) or
|
528 |
+
(abs(c - b - int(c - b)) < 1e-15 and c - b < 0)
|
529 |
+
)
|
530 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
531 |
+
|
532 |
+
@pytest.mark.parametrize(
|
533 |
+
"hyp2f1_test_case",
|
534 |
+
[
|
535 |
+
pytest.param(
|
536 |
+
Hyp2f1TestCase(
|
537 |
+
a=-0.9220024191881196,
|
538 |
+
b=-0.9629749245209605,
|
539 |
+
c=-15.963511401609862,
|
540 |
+
z=(0.10526315789473673-0.3157894736842106j),
|
541 |
+
expected=(0.9941449585778349+0.01756335047931358j),
|
542 |
+
rtol=1e-14,
|
543 |
+
),
|
544 |
+
),
|
545 |
+
pytest.param(
|
546 |
+
Hyp2f1TestCase(
|
547 |
+
a=1.0272592605282642,
|
548 |
+
b=-0.9629749245209605,
|
549 |
+
c=-15.963511401609862,
|
550 |
+
z=(0.5263157894736841+0.5263157894736841j),
|
551 |
+
expected=(1.0388722293372104-0.09549450380041416j),
|
552 |
+
rtol=5e-11,
|
553 |
+
),
|
554 |
+
),
|
555 |
+
pytest.param(
|
556 |
+
Hyp2f1TestCase(
|
557 |
+
a=2.02764642551431,
|
558 |
+
b=1.0561196186065624,
|
559 |
+
c=-7.93846038215665,
|
560 |
+
z=(0.10526315789473673+0.7368421052631575j),
|
561 |
+
expected=(2.1948378809826434+24.934157235172222j),
|
562 |
+
rtol=5e-15,
|
563 |
+
),
|
564 |
+
),
|
565 |
+
pytest.param(
|
566 |
+
Hyp2f1TestCase(
|
567 |
+
a=2.02764642551431,
|
568 |
+
b=16.088264119063613,
|
569 |
+
c=8.031683612216888,
|
570 |
+
z=(0.3157894736842106-0.736842105263158j),
|
571 |
+
expected=(-0.4075277891264672-0.06819344579666956j),
|
572 |
+
rtol=2e-12,
|
573 |
+
),
|
574 |
+
),
|
575 |
+
pytest.param(
|
576 |
+
Hyp2f1TestCase(
|
577 |
+
a=4.080187217753502,
|
578 |
+
b=2.050308316530781,
|
579 |
+
c=8.031683612216888,
|
580 |
+
z=(0.7368421052631575-0.10526315789473695j),
|
581 |
+
expected=(2.833535530740603-0.6925373701408158j),
|
582 |
+
rtol=5e-15,
|
583 |
+
),
|
584 |
+
),
|
585 |
+
pytest.param(
|
586 |
+
Hyp2f1TestCase(
|
587 |
+
a=2.02764642551431,
|
588 |
+
b=2.050308316530781,
|
589 |
+
c=4.078873014294075,
|
590 |
+
z=(0.10526315789473673-0.3157894736842106j),
|
591 |
+
expected=(1.005347176329683-0.3580736009337313j),
|
592 |
+
rtol=5e-16,
|
593 |
+
),
|
594 |
+
),
|
595 |
+
pytest.param(
|
596 |
+
Hyp2f1TestCase(
|
597 |
+
a=-0.9220024191881196,
|
598 |
+
b=-0.9629749245209605,
|
599 |
+
c=-15.963511401609862,
|
600 |
+
z=(0.3157894736842106-0.5263157894736843j),
|
601 |
+
expected=(0.9824353641135369+0.029271018868990268j),
|
602 |
+
rtol=5e-13,
|
603 |
+
),
|
604 |
+
),
|
605 |
+
pytest.param(
|
606 |
+
Hyp2f1TestCase(
|
607 |
+
a=-0.9220024191881196,
|
608 |
+
b=-0.9629749245209605,
|
609 |
+
c=-159.63511401609862,
|
610 |
+
z=(0.3157894736842106-0.5263157894736843j),
|
611 |
+
expected=(0.9982436200365834+0.002927268199671111j),
|
612 |
+
rtol=1e-7,
|
613 |
+
),
|
614 |
+
marks=pytest.mark.xfail(reason="Poor convergence.")
|
615 |
+
),
|
616 |
+
pytest.param(
|
617 |
+
Hyp2f1TestCase(
|
618 |
+
a=2.02764642551431,
|
619 |
+
b=16.088264119063613,
|
620 |
+
c=8.031683612216888,
|
621 |
+
z=(0.5263157894736841-0.5263157894736843j),
|
622 |
+
expected=(-0.6906825165778091+0.8176575137504892j),
|
623 |
+
rtol=5e-13,
|
624 |
+
),
|
625 |
+
),
|
626 |
+
]
|
627 |
+
)
|
628 |
+
def test_region1(self, hyp2f1_test_case):
|
629 |
+
"""|z| < 0.9 and real(z) >= 0."""
|
630 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
631 |
+
assert abs(z) < 0.9 and z.real >= 0 # Tests the test
|
632 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
633 |
+
|
634 |
+
@pytest.mark.parametrize(
|
635 |
+
"hyp2f1_test_case",
|
636 |
+
[
|
637 |
+
pytest.param(
|
638 |
+
Hyp2f1TestCase(
|
639 |
+
a=2.02764642551431,
|
640 |
+
b=1.0561196186065624,
|
641 |
+
c=4.078873014294075,
|
642 |
+
z=(-0.3157894736842106+0.7368421052631575j),
|
643 |
+
expected=(0.7751915029081136+0.24068493258607315j),
|
644 |
+
rtol=5e-15,
|
645 |
+
),
|
646 |
+
),
|
647 |
+
pytest.param(
|
648 |
+
Hyp2f1TestCase(
|
649 |
+
a=16.087593263474208,
|
650 |
+
b=16.088264119063613,
|
651 |
+
c=2.0397202577726152,
|
652 |
+
z=(-0.9473684210526316-0.3157894736842106j),
|
653 |
+
expected=(6.564549348474962e-07+1.6761570598334562e-06j),
|
654 |
+
rtol=5e-09,
|
655 |
+
),
|
656 |
+
),
|
657 |
+
pytest.param(
|
658 |
+
Hyp2f1TestCase(
|
659 |
+
a=1.0272592605282642,
|
660 |
+
b=2.050308316530781,
|
661 |
+
c=16.056809865262608,
|
662 |
+
z=(-0.10526315789473695-0.10526315789473695j),
|
663 |
+
expected=(0.9862043298997204-0.013293151372712681j),
|
664 |
+
rtol=5e-15,
|
665 |
+
),
|
666 |
+
),
|
667 |
+
pytest.param(
|
668 |
+
Hyp2f1TestCase(
|
669 |
+
a=4.080187217753502,
|
670 |
+
b=8.077282662161238,
|
671 |
+
c=16.056809865262608,
|
672 |
+
z=(-0.3157894736842106-0.736842105263158j),
|
673 |
+
expected=(0.16163826638754716-0.41378530376373734j),
|
674 |
+
rtol=5e-15,
|
675 |
+
),
|
676 |
+
),
|
677 |
+
pytest.param(
|
678 |
+
Hyp2f1TestCase(
|
679 |
+
a=2.02764642551431,
|
680 |
+
b=2.050308316530781,
|
681 |
+
c=-0.906685989801748,
|
682 |
+
z=(-0.5263157894736843+0.3157894736842106j),
|
683 |
+
expected=(-6.256871535165936+0.13824973858225484j),
|
684 |
+
rtol=1e-15,
|
685 |
+
),
|
686 |
+
),
|
687 |
+
pytest.param(
|
688 |
+
Hyp2f1TestCase(
|
689 |
+
a=2.02764642551431,
|
690 |
+
b=8.077282662161238,
|
691 |
+
c=-3.9924618758357022,
|
692 |
+
z=(-0.9473684210526316-0.3157894736842106j),
|
693 |
+
expected=(75.54672526086316+50.56157041797548j),
|
694 |
+
rtol=5e-12,
|
695 |
+
),
|
696 |
+
),
|
697 |
+
pytest.param(
|
698 |
+
Hyp2f1TestCase(
|
699 |
+
a=16.087593263474208,
|
700 |
+
b=8.077282662161238,
|
701 |
+
c=-1.9631175993998025,
|
702 |
+
z=(-0.5263157894736843+0.5263157894736841j),
|
703 |
+
expected=(282.0602536306534-82.31597306936214j),
|
704 |
+
rtol=5e-13,
|
705 |
+
),
|
706 |
+
),
|
707 |
+
pytest.param(
|
708 |
+
Hyp2f1TestCase(
|
709 |
+
a=8.095813935368371,
|
710 |
+
b=-3.9316537064827854,
|
711 |
+
c=8.031683612216888,
|
712 |
+
z=(-0.5263157894736843-0.10526315789473695j),
|
713 |
+
expected=(5.179603735575851+1.4445374002099813j),
|
714 |
+
rtol=5e-14,
|
715 |
+
),
|
716 |
+
),
|
717 |
+
pytest.param(
|
718 |
+
Hyp2f1TestCase(
|
719 |
+
a=4.080187217753502,
|
720 |
+
b=-7.949900487447654,
|
721 |
+
c=1.0651378143226575,
|
722 |
+
z=(-0.3157894736842106-0.9473684210526316j),
|
723 |
+
expected=(2317.623517606141-269.51476321010324j),
|
724 |
+
rtol=5e-13,
|
725 |
+
),
|
726 |
+
),
|
727 |
+
pytest.param(
|
728 |
+
Hyp2f1TestCase(
|
729 |
+
a=16.087593263474208,
|
730 |
+
b=-1.92872979730171,
|
731 |
+
c=2.0397202577726152,
|
732 |
+
z=(-0.736842105263158-0.3157894736842106j),
|
733 |
+
expected=(29.179154096175836+22.126690357535043j),
|
734 |
+
rtol=5e-15,
|
735 |
+
),
|
736 |
+
),
|
737 |
+
pytest.param(
|
738 |
+
Hyp2f1TestCase(
|
739 |
+
a=8.095813935368371,
|
740 |
+
b=-3.9316537064827854,
|
741 |
+
c=-15.963511401609862,
|
742 |
+
z=(-0.736842105263158-0.10526315789473695j),
|
743 |
+
expected=(0.20820247892032057-0.04763956711248794j),
|
744 |
+
rtol=5e-14,
|
745 |
+
),
|
746 |
+
),
|
747 |
+
pytest.param(
|
748 |
+
Hyp2f1TestCase(
|
749 |
+
a=1.0272592605282642,
|
750 |
+
b=-15.964218273004214,
|
751 |
+
c=-1.9631175993998025,
|
752 |
+
z=(-0.3157894736842106-0.5263157894736843j),
|
753 |
+
expected=(-157471.63920142158+991294.0587828817j),
|
754 |
+
rtol=5e-14,
|
755 |
+
),
|
756 |
+
),
|
757 |
+
pytest.param(
|
758 |
+
Hyp2f1TestCase(
|
759 |
+
a=8.095813935368371,
|
760 |
+
b=-7.949900487447654,
|
761 |
+
c=-7.93846038215665,
|
762 |
+
z=(-0.10526315789473695-0.10526315789473695j),
|
763 |
+
expected=(0.30765349653210194-0.2979706363594157j),
|
764 |
+
rtol=1e-15,
|
765 |
+
),
|
766 |
+
),
|
767 |
+
pytest.param(
|
768 |
+
Hyp2f1TestCase(
|
769 |
+
a=-3.956227226099288,
|
770 |
+
b=1.0561196186065624,
|
771 |
+
c=8.031683612216888,
|
772 |
+
z=(-0.9473684210526316-0.10526315789473695j),
|
773 |
+
expected=(1.6787607400597109+0.10056620134616838j),
|
774 |
+
rtol=5e-14,
|
775 |
+
),
|
776 |
+
),
|
777 |
+
pytest.param(
|
778 |
+
Hyp2f1TestCase(
|
779 |
+
a=-7.937789122896016,
|
780 |
+
b=16.088264119063613,
|
781 |
+
c=4.078873014294075,
|
782 |
+
z=(-0.5263157894736843-0.736842105263158j),
|
783 |
+
expected=(7062.07842506049-12768.77955655703j),
|
784 |
+
rtol=5e-15,
|
785 |
+
),
|
786 |
+
),
|
787 |
+
pytest.param(
|
788 |
+
Hyp2f1TestCase(
|
789 |
+
a=-7.937789122896016,
|
790 |
+
b=16.088264119063613,
|
791 |
+
c=2.0397202577726152,
|
792 |
+
z=(-0.3157894736842106+0.7368421052631575j),
|
793 |
+
expected=(54749.216391029935-23078.144720887536j),
|
794 |
+
rtol=1e-15,
|
795 |
+
),
|
796 |
+
),
|
797 |
+
pytest.param(
|
798 |
+
Hyp2f1TestCase(
|
799 |
+
a=-3.956227226099288,
|
800 |
+
b=1.0561196186065624,
|
801 |
+
c=-0.906685989801748,
|
802 |
+
z=(-0.10526315789473695-0.10526315789473695j),
|
803 |
+
expected=(1.21521766411428-4.449385173946672j),
|
804 |
+
rtol=5e-15,
|
805 |
+
),
|
806 |
+
),
|
807 |
+
pytest.param(
|
808 |
+
Hyp2f1TestCase(
|
809 |
+
a=-15.980848054962111,
|
810 |
+
b=4.0013768449590685,
|
811 |
+
c=-1.9631175993998025,
|
812 |
+
z=(-0.736842105263158+0.5263157894736841j),
|
813 |
+
expected=(19234693144.196907+1617913967.7294445j),
|
814 |
+
rtol=5e-14,
|
815 |
+
),
|
816 |
+
),
|
817 |
+
pytest.param(
|
818 |
+
Hyp2f1TestCase(
|
819 |
+
a=-1.9214641416286231,
|
820 |
+
b=1.0561196186065624,
|
821 |
+
c=-15.963511401609862,
|
822 |
+
z=(-0.5263157894736843+0.3157894736842106j),
|
823 |
+
expected=(0.9345201094534371+0.03745712558992195j),
|
824 |
+
rtol=5e-15,
|
825 |
+
),
|
826 |
+
),
|
827 |
+
pytest.param(
|
828 |
+
Hyp2f1TestCase(
|
829 |
+
a=-7.937789122896016,
|
830 |
+
b=-0.9629749245209605,
|
831 |
+
c=2.0397202577726152,
|
832 |
+
z=(-0.10526315789473695+0.10526315789473673j),
|
833 |
+
expected=(0.605732446296829+0.398171533680972j),
|
834 |
+
rtol=5e-15,
|
835 |
+
),
|
836 |
+
),
|
837 |
+
pytest.param(
|
838 |
+
Hyp2f1TestCase(
|
839 |
+
a=-1.9214641416286231,
|
840 |
+
b=-15.964218273004214,
|
841 |
+
c=2.0397202577726152,
|
842 |
+
z=(-0.10526315789473695-0.5263157894736843j),
|
843 |
+
expected=(-9.753761888305416-4.590126012666959j),
|
844 |
+
rtol=5e-15,
|
845 |
+
),
|
846 |
+
),
|
847 |
+
pytest.param(
|
848 |
+
Hyp2f1TestCase(
|
849 |
+
a=-3.956227226099288,
|
850 |
+
b=-1.92872979730171,
|
851 |
+
c=2.0397202577726152,
|
852 |
+
z=(-0.10526315789473695+0.3157894736842106j),
|
853 |
+
expected=(0.45587226291120714+1.0694545265819797j),
|
854 |
+
rtol=5e-15,
|
855 |
+
),
|
856 |
+
),
|
857 |
+
pytest.param(
|
858 |
+
Hyp2f1TestCase(
|
859 |
+
a=-0.9220024191881196,
|
860 |
+
b=-7.949900487447654,
|
861 |
+
c=-0.906685989801748,
|
862 |
+
z=(-0.736842105263158+0.3157894736842106j),
|
863 |
+
expected=(12.334808243233418-76.26089051819054j),
|
864 |
+
rtol=5e-14,
|
865 |
+
),
|
866 |
+
),
|
867 |
+
pytest.param(
|
868 |
+
Hyp2f1TestCase(
|
869 |
+
a=-0.9220024191881196,
|
870 |
+
b=-7.949900487447654,
|
871 |
+
c=-15.963511401609862,
|
872 |
+
z=(-0.5263157894736843+0.10526315789473673j),
|
873 |
+
expected=(1.2396019687632678-0.047507973161146286j),
|
874 |
+
rtol=1e-14,
|
875 |
+
),
|
876 |
+
),
|
877 |
+
pytest.param(
|
878 |
+
Hyp2f1TestCase(
|
879 |
+
a=-15.980848054962111,
|
880 |
+
b=-0.9629749245209605,
|
881 |
+
c=-0.906685989801748,
|
882 |
+
z=(-0.3157894736842106-0.5263157894736843j),
|
883 |
+
expected=(97.7889554372208-18.999754543400016j),
|
884 |
+
rtol=5e-13,
|
885 |
+
),
|
886 |
+
),
|
887 |
+
]
|
888 |
+
)
|
889 |
+
def test_region2(self, hyp2f1_test_case):
|
890 |
+
"""|z| < 1 and real(z) < 0."""
|
891 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
892 |
+
assert abs(z) < 1 and z.real < 0 # Tests the test
|
893 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
894 |
+
|
895 |
+
@pytest.mark.parametrize(
|
896 |
+
"hyp2f1_test_case",
|
897 |
+
[
|
898 |
+
pytest.param(
|
899 |
+
Hyp2f1TestCase(
|
900 |
+
a=16.25,
|
901 |
+
b=4.25,
|
902 |
+
c=2.5,
|
903 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
904 |
+
expected=(38.41207903409937-30.510151276075792j),
|
905 |
+
rtol=5e-14,
|
906 |
+
),
|
907 |
+
),
|
908 |
+
pytest.param(
|
909 |
+
Hyp2f1TestCase(
|
910 |
+
a=2.0,
|
911 |
+
b=16.087593263474208,
|
912 |
+
c=16.088264119063613,
|
913 |
+
z=(0.5689655172413794-0.7965517241379311j),
|
914 |
+
expected=(-0.6667857912761286-1.0206224321443573j),
|
915 |
+
rtol=1e-15,
|
916 |
+
),
|
917 |
+
),
|
918 |
+
pytest.param(
|
919 |
+
Hyp2f1TestCase(
|
920 |
+
a=8.0,
|
921 |
+
b=1.0272592605282642,
|
922 |
+
c=-7.949900487447654,
|
923 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
924 |
+
expected=(1679024.1647997478-2748129.775857212j),
|
925 |
+
rtol=5e-14,
|
926 |
+
),
|
927 |
+
),
|
928 |
+
pytest.param(
|
929 |
+
Hyp2f1TestCase(
|
930 |
+
a=4.080187217753502,
|
931 |
+
b=16.0,
|
932 |
+
c=-7.949900487447654,
|
933 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
934 |
+
expected=(424747226301.16986-1245539049327.2856j),
|
935 |
+
rtol=1e-14,
|
936 |
+
),
|
937 |
+
),
|
938 |
+
pytest.param(
|
939 |
+
Hyp2f1TestCase(
|
940 |
+
a=2.02764642551431,
|
941 |
+
b=-15.964218273004214,
|
942 |
+
c=4.0,
|
943 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
944 |
+
expected=(-0.0057826199201757595+0.026359861999025885j),
|
945 |
+
rtol=5e-06,
|
946 |
+
),
|
947 |
+
),
|
948 |
+
pytest.param(
|
949 |
+
Hyp2f1TestCase(
|
950 |
+
a=2.02764642551431,
|
951 |
+
b=-0.9629749245209605,
|
952 |
+
c=2.0397202577726152,
|
953 |
+
z=(0.5689655172413794-0.7965517241379311j),
|
954 |
+
expected=(0.4671901063492606+0.7769632229834897j),
|
955 |
+
rtol=5e-14,
|
956 |
+
),
|
957 |
+
),
|
958 |
+
pytest.param(
|
959 |
+
Hyp2f1TestCase(
|
960 |
+
a=2.0,
|
961 |
+
b=-3.956227226099288,
|
962 |
+
c=-7.949900487447654,
|
963 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
964 |
+
expected=(0.9422283708145973+1.3476905754773343j),
|
965 |
+
rtol=5e-15,
|
966 |
+
),
|
967 |
+
),
|
968 |
+
pytest.param(
|
969 |
+
Hyp2f1TestCase(
|
970 |
+
a=1.0,
|
971 |
+
b=-15.980848054962111,
|
972 |
+
c=-15.964218273004214,
|
973 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
974 |
+
expected=(0.4168719497319604-0.9770953555235625j),
|
975 |
+
rtol=5e-10,
|
976 |
+
),
|
977 |
+
),
|
978 |
+
pytest.param(
|
979 |
+
Hyp2f1TestCase(
|
980 |
+
a=-0.5,
|
981 |
+
b=16.088264119063613,
|
982 |
+
c=2.5,
|
983 |
+
z=(0.5689655172413794+0.7965517241379312j),
|
984 |
+
expected=(1.279096377550619-2.173827694297929j),
|
985 |
+
rtol=5e-12,
|
986 |
+
),
|
987 |
+
),
|
988 |
+
pytest.param(
|
989 |
+
Hyp2f1TestCase(
|
990 |
+
a=-1.9214641416286231,
|
991 |
+
b=4.0013768449590685,
|
992 |
+
c=2.0397202577726152,
|
993 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
994 |
+
expected=(-2.071520656161738-0.7846098268395909j),
|
995 |
+
rtol=5e-14,
|
996 |
+
),
|
997 |
+
),
|
998 |
+
pytest.param(
|
999 |
+
Hyp2f1TestCase(
|
1000 |
+
a=-0.9220024191881196,
|
1001 |
+
b=8.0,
|
1002 |
+
c=-0.9629749245209605,
|
1003 |
+
z=(0.5689655172413794-0.7965517241379311j),
|
1004 |
+
expected=(-7.740015495862889+3.386766435696699j),
|
1005 |
+
rtol=5e-12,
|
1006 |
+
),
|
1007 |
+
),
|
1008 |
+
pytest.param(
|
1009 |
+
Hyp2f1TestCase(
|
1010 |
+
a=-1.9214641416286231,
|
1011 |
+
b=16.088264119063613,
|
1012 |
+
c=-7.93846038215665,
|
1013 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
1014 |
+
expected=(-6318.553685853241-7133.416085202879j),
|
1015 |
+
rtol=1e-10,
|
1016 |
+
),
|
1017 |
+
),
|
1018 |
+
pytest.param(
|
1019 |
+
Hyp2f1TestCase(
|
1020 |
+
a=-15.980848054962111,
|
1021 |
+
b=-3.9316537064827854,
|
1022 |
+
c=16.056809865262608,
|
1023 |
+
z=(0.5689655172413794+0.7965517241379312j),
|
1024 |
+
expected=(-0.8854577905547399+8.135089099967278j),
|
1025 |
+
rtol=5e-14,
|
1026 |
+
),
|
1027 |
+
),
|
1028 |
+
pytest.param(
|
1029 |
+
Hyp2f1TestCase(
|
1030 |
+
a=-1.9214641416286231,
|
1031 |
+
b=-0.9629749245209605,
|
1032 |
+
c=4.078873014294075,
|
1033 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
1034 |
+
expected=(1.224291301521487+0.36014711766402485j),
|
1035 |
+
rtol=1e-15,
|
1036 |
+
),
|
1037 |
+
),
|
1038 |
+
pytest.param(
|
1039 |
+
Hyp2f1TestCase(
|
1040 |
+
a=-15.75,
|
1041 |
+
b=-0.75,
|
1042 |
+
c=-1.5,
|
1043 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
1044 |
+
expected=(-1.5765685855028473-3.9399766961046323j),
|
1045 |
+
rtol=1e-3,
|
1046 |
+
),
|
1047 |
+
marks=pytest.mark.xfail(
|
1048 |
+
reason="Unhandled parameters."
|
1049 |
+
)
|
1050 |
+
),
|
1051 |
+
pytest.param(
|
1052 |
+
Hyp2f1TestCase(
|
1053 |
+
a=-15.980848054962111,
|
1054 |
+
b=-1.92872979730171,
|
1055 |
+
c=-7.93846038215665,
|
1056 |
+
z=(0.5689655172413794-0.7965517241379311j),
|
1057 |
+
expected=(56.794588688231194+4.556286783533971j),
|
1058 |
+
rtol=5e-14,
|
1059 |
+
),
|
1060 |
+
),
|
1061 |
+
pytest.param(
|
1062 |
+
Hyp2f1TestCase(
|
1063 |
+
a=4.5,
|
1064 |
+
b=4.5,
|
1065 |
+
c=2.050308316530781,
|
1066 |
+
z=(0.5689655172413794+0.7965517241379312j),
|
1067 |
+
expected=(-4.251456563455306+6.737837111569671j),
|
1068 |
+
rtol=5e-14,
|
1069 |
+
),
|
1070 |
+
),
|
1071 |
+
pytest.param(
|
1072 |
+
Hyp2f1TestCase(
|
1073 |
+
a=4.5,
|
1074 |
+
b=8.5,
|
1075 |
+
c=-1.92872979730171,
|
1076 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
1077 |
+
expected=(2177143.9156599627-3313617.2748088865j),
|
1078 |
+
rtol=5e-14,
|
1079 |
+
),
|
1080 |
+
),
|
1081 |
+
pytest.param(
|
1082 |
+
Hyp2f1TestCase(
|
1083 |
+
a=2.5,
|
1084 |
+
b=-1.5,
|
1085 |
+
c=4.0013768449590685,
|
1086 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
1087 |
+
expected=(0.45563554481603946+0.6212000158060831j),
|
1088 |
+
rtol=5e-14,
|
1089 |
+
),
|
1090 |
+
),
|
1091 |
+
pytest.param(
|
1092 |
+
Hyp2f1TestCase(
|
1093 |
+
a=8.5,
|
1094 |
+
b=-7.5,
|
1095 |
+
c=-15.964218273004214,
|
1096 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
1097 |
+
expected=(61.03201617828073-37.185626416756214j),
|
1098 |
+
rtol=5e-14,
|
1099 |
+
),
|
1100 |
+
),
|
1101 |
+
pytest.param(
|
1102 |
+
Hyp2f1TestCase(
|
1103 |
+
a=-15.5,
|
1104 |
+
b=16.5,
|
1105 |
+
c=4.0013768449590685,
|
1106 |
+
z=(0.4931034482758623+0.7965517241379312j),
|
1107 |
+
expected=(-33143.425963520735+20790.608514722644j),
|
1108 |
+
rtol=1e-14,
|
1109 |
+
),
|
1110 |
+
),
|
1111 |
+
pytest.param(
|
1112 |
+
Hyp2f1TestCase(
|
1113 |
+
a=-0.5,
|
1114 |
+
b=4.5,
|
1115 |
+
c=-0.9629749245209605,
|
1116 |
+
z=(0.5689655172413794+0.7965517241379312j),
|
1117 |
+
expected=(30.778600270824423-26.65160354466787j),
|
1118 |
+
rtol=5e-13,
|
1119 |
+
),
|
1120 |
+
),
|
1121 |
+
pytest.param(
|
1122 |
+
Hyp2f1TestCase(
|
1123 |
+
a=-0.5,
|
1124 |
+
b=-3.5,
|
1125 |
+
c=16.088264119063613,
|
1126 |
+
z=(0.5689655172413794-0.7965517241379311j),
|
1127 |
+
expected=(1.0629792615560487-0.08308454486044772j),
|
1128 |
+
rtol=1e-15,
|
1129 |
+
),
|
1130 |
+
),
|
1131 |
+
pytest.param(
|
1132 |
+
Hyp2f1TestCase(
|
1133 |
+
a=-3.5,
|
1134 |
+
b=-7.5,
|
1135 |
+
c=-0.9629749245209605,
|
1136 |
+
z=(0.4931034482758623-0.7965517241379311j),
|
1137 |
+
expected=(17431.571802591767+3553.7129767034507j),
|
1138 |
+
rtol=5e-14,
|
1139 |
+
),
|
1140 |
+
),
|
1141 |
+
pytest.param(
|
1142 |
+
Hyp2f1TestCase(
|
1143 |
+
a=2.25,
|
1144 |
+
b=8.25,
|
1145 |
+
c=16.5,
|
1146 |
+
z=(0.11379310344827598+0.9482758620689657j),
|
1147 |
+
expected=(0.4468600750211926+0.7313214934036885j),
|
1148 |
+
rtol=1e-3,
|
1149 |
+
),
|
1150 |
+
marks=pytest.mark.xfail(
|
1151 |
+
reason="Unhandled parameters."
|
1152 |
+
)
|
1153 |
+
),
|
1154 |
+
pytest.param(
|
1155 |
+
Hyp2f1TestCase(
|
1156 |
+
a=8.25,
|
1157 |
+
b=16.25,
|
1158 |
+
c=4.5,
|
1159 |
+
z=(0.3413793103448277+0.8724137931034486j),
|
1160 |
+
expected=(-3.905704438293991+3.693347860329299j),
|
1161 |
+
rtol=5e-14,
|
1162 |
+
),
|
1163 |
+
),
|
1164 |
+
pytest.param(
|
1165 |
+
Hyp2f1TestCase(
|
1166 |
+
a=4.25,
|
1167 |
+
b=4.25,
|
1168 |
+
c=-0.5,
|
1169 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1170 |
+
expected=(-40.31777941834244-89.89852492432011j),
|
1171 |
+
rtol=5e-15,
|
1172 |
+
),
|
1173 |
+
),
|
1174 |
+
pytest.param(
|
1175 |
+
Hyp2f1TestCase(
|
1176 |
+
a=1.0272592605282642,
|
1177 |
+
b=8.0,
|
1178 |
+
c=-15.964218273004214,
|
1179 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1180 |
+
expected=(52584.347773055284-109197.86244309516j),
|
1181 |
+
rtol=5e-14,
|
1182 |
+
),
|
1183 |
+
),
|
1184 |
+
pytest.param(
|
1185 |
+
Hyp2f1TestCase(
|
1186 |
+
a=8.095813935368371,
|
1187 |
+
b=-15.964218273004214,
|
1188 |
+
c=16.056809865262608,
|
1189 |
+
z=(0.03793103448275881+0.9482758620689657j),
|
1190 |
+
expected=(-1.187733570412592-1.5147865053584582j),
|
1191 |
+
rtol=5e-10,
|
1192 |
+
),
|
1193 |
+
),
|
1194 |
+
pytest.param(
|
1195 |
+
Hyp2f1TestCase(
|
1196 |
+
a=4.080187217753502,
|
1197 |
+
b=-3.9316537064827854,
|
1198 |
+
c=1.0651378143226575,
|
1199 |
+
z=(0.26551724137931054+0.9482758620689657j),
|
1200 |
+
expected=(13.077494677898947+35.071599628224966j),
|
1201 |
+
rtol=5e-13,
|
1202 |
+
),
|
1203 |
+
),
|
1204 |
+
pytest.param(
|
1205 |
+
Hyp2f1TestCase(
|
1206 |
+
a=4.080187217753502,
|
1207 |
+
b=-3.5,
|
1208 |
+
c=-3.5,
|
1209 |
+
z=(0.26551724137931054+0.8724137931034486j),
|
1210 |
+
expected=(-0.5359656237994614-0.2344483936591811j),
|
1211 |
+
rtol=5e-15,
|
1212 |
+
),
|
1213 |
+
),
|
1214 |
+
pytest.param(
|
1215 |
+
Hyp2f1TestCase(
|
1216 |
+
a=4.25,
|
1217 |
+
b=-3.75,
|
1218 |
+
c=-1.5,
|
1219 |
+
z=(0.26551724137931054+0.9482758620689657j),
|
1220 |
+
expected=(1204.8114871663133+64.41022826840198j),
|
1221 |
+
rtol=5e-13,
|
1222 |
+
),
|
1223 |
+
),
|
1224 |
+
pytest.param(
|
1225 |
+
Hyp2f1TestCase(
|
1226 |
+
a=-1.9214641416286231,
|
1227 |
+
b=16.0,
|
1228 |
+
c=4.0013768449590685,
|
1229 |
+
z=(0.03793103448275881-0.9482758620689655j),
|
1230 |
+
expected=(-9.85268872413994+7.011107558429154j),
|
1231 |
+
rtol=5e-15,
|
1232 |
+
),
|
1233 |
+
),
|
1234 |
+
pytest.param(
|
1235 |
+
Hyp2f1TestCase(
|
1236 |
+
a=-7.937789122896016,
|
1237 |
+
b=16.0,
|
1238 |
+
c=4.0013768449590685,
|
1239 |
+
z=(0.3413793103448277-0.8724137931034484j),
|
1240 |
+
expected=(528.5522951158454-1412.21630264791j),
|
1241 |
+
rtol=1e-15,
|
1242 |
+
),
|
1243 |
+
),
|
1244 |
+
pytest.param(
|
1245 |
+
Hyp2f1TestCase(
|
1246 |
+
a=-15.5,
|
1247 |
+
b=1.0561196186065624,
|
1248 |
+
c=-7.5,
|
1249 |
+
z=(0.4172413793103451+0.8724137931034486j),
|
1250 |
+
expected=(133306.45260685298+256510.7045225382j),
|
1251 |
+
rtol=5e-15,
|
1252 |
+
),
|
1253 |
+
),
|
1254 |
+
pytest.param(
|
1255 |
+
Hyp2f1TestCase(
|
1256 |
+
a=-7.937789122896016,
|
1257 |
+
b=8.077282662161238,
|
1258 |
+
c=-15.963511401609862,
|
1259 |
+
z=(0.3413793103448277-0.8724137931034484j),
|
1260 |
+
expected=(-0.998555715276967+2.774198742229889j),
|
1261 |
+
rtol=5e-11,
|
1262 |
+
),
|
1263 |
+
),
|
1264 |
+
pytest.param(
|
1265 |
+
Hyp2f1TestCase(
|
1266 |
+
a=-7.75,
|
1267 |
+
b=-0.75,
|
1268 |
+
c=1.5,
|
1269 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1270 |
+
expected=(2.072445019723025-2.9793504811373515j),
|
1271 |
+
rtol=5e-14,
|
1272 |
+
),
|
1273 |
+
),
|
1274 |
+
pytest.param(
|
1275 |
+
Hyp2f1TestCase(
|
1276 |
+
a=-15.5,
|
1277 |
+
b=-1.92872979730171,
|
1278 |
+
c=1.5,
|
1279 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1280 |
+
expected=(-41.87581944176649-32.52980303527139j),
|
1281 |
+
rtol=5e-13,
|
1282 |
+
),
|
1283 |
+
),
|
1284 |
+
pytest.param(
|
1285 |
+
Hyp2f1TestCase(
|
1286 |
+
a=-3.75,
|
1287 |
+
b=-15.75,
|
1288 |
+
c=-0.5,
|
1289 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1290 |
+
expected=(-3729.6214864209774-30627.510509112635j),
|
1291 |
+
rtol=5e-15,
|
1292 |
+
),
|
1293 |
+
),
|
1294 |
+
pytest.param(
|
1295 |
+
Hyp2f1TestCase(
|
1296 |
+
a=-3.956227226099288,
|
1297 |
+
b=-15.964218273004214,
|
1298 |
+
c=-0.906685989801748,
|
1299 |
+
z=(0.03793103448275881+0.9482758620689657j),
|
1300 |
+
expected=(-131615.07820609974+145596.13384245415j),
|
1301 |
+
rtol=5e-15,
|
1302 |
+
),
|
1303 |
+
),
|
1304 |
+
pytest.param(
|
1305 |
+
Hyp2f1TestCase(
|
1306 |
+
a=1.5,
|
1307 |
+
b=16.5,
|
1308 |
+
c=16.088264119063613,
|
1309 |
+
z=(0.26551724137931054+0.8724137931034486j),
|
1310 |
+
expected=(0.18981844071070744+0.7855036242583742j),
|
1311 |
+
rtol=1e-15,
|
1312 |
+
),
|
1313 |
+
),
|
1314 |
+
pytest.param(
|
1315 |
+
Hyp2f1TestCase(
|
1316 |
+
a=16.5,
|
1317 |
+
b=8.5,
|
1318 |
+
c=-3.9316537064827854,
|
1319 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1320 |
+
expected=(110224529.2376068+128287212.04290268j),
|
1321 |
+
rtol=5e-13,
|
1322 |
+
),
|
1323 |
+
),
|
1324 |
+
pytest.param(
|
1325 |
+
Hyp2f1TestCase(
|
1326 |
+
a=2.5,
|
1327 |
+
b=-7.5,
|
1328 |
+
c=4.0013768449590685,
|
1329 |
+
z=(0.3413793103448277-0.8724137931034484j),
|
1330 |
+
expected=(0.2722302180888523-0.21790187837266162j),
|
1331 |
+
rtol=1e-12,
|
1332 |
+
),
|
1333 |
+
),
|
1334 |
+
pytest.param(
|
1335 |
+
Hyp2f1TestCase(
|
1336 |
+
a=8.5,
|
1337 |
+
b=-7.5,
|
1338 |
+
c=-15.964218273004214,
|
1339 |
+
z=(0.11379310344827598-0.9482758620689655j),
|
1340 |
+
expected=(-2.8252338010989035+2.430661949756161j),
|
1341 |
+
rtol=5e-14,
|
1342 |
+
),
|
1343 |
+
),
|
1344 |
+
pytest.param(
|
1345 |
+
Hyp2f1TestCase(
|
1346 |
+
a=-3.5,
|
1347 |
+
b=16.5,
|
1348 |
+
c=4.0013768449590685,
|
1349 |
+
z=(0.03793103448275881+0.9482758620689657j),
|
1350 |
+
expected=(-20.604894257647945+74.5109432558078j),
|
1351 |
+
rtol=5e-15,
|
1352 |
+
),
|
1353 |
+
),
|
1354 |
+
pytest.param(
|
1355 |
+
Hyp2f1TestCase(
|
1356 |
+
a=-7.5,
|
1357 |
+
b=8.5,
|
1358 |
+
c=-0.9629749245209605,
|
1359 |
+
z=(0.3413793103448277+0.8724137931034486j),
|
1360 |
+
expected=(-2764422.521269463-3965966.9965808876j),
|
1361 |
+
rtol=1e-15,
|
1362 |
+
),
|
1363 |
+
),
|
1364 |
+
pytest.param(
|
1365 |
+
Hyp2f1TestCase(
|
1366 |
+
a=-1.5,
|
1367 |
+
b=-0.5,
|
1368 |
+
c=1.0561196186065624,
|
1369 |
+
z=(0.26551724137931054+0.9482758620689657j),
|
1370 |
+
expected=(1.2262338560994905+0.6545051266925549j),
|
1371 |
+
rtol=1e-15,
|
1372 |
+
),
|
1373 |
+
),
|
1374 |
+
pytest.param(
|
1375 |
+
Hyp2f1TestCase(
|
1376 |
+
a=-0.5,
|
1377 |
+
b=-15.5,
|
1378 |
+
c=-7.949900487447654,
|
1379 |
+
z=(0.4172413793103451-0.8724137931034484j),
|
1380 |
+
expected=(-2258.1590330318213+8860.193389158803j),
|
1381 |
+
rtol=1.4e-10,
|
1382 |
+
),
|
1383 |
+
),
|
1384 |
+
]
|
1385 |
+
)
|
1386 |
+
def test_region4(self, hyp2f1_test_case):
|
1387 |
+
"""0.9 <= |z| <= 1 and |1 - z| >= 1.
|
1388 |
+
|
1389 |
+
This region is unhandled by of the standard transformations and
|
1390 |
+
needs special care.
|
1391 |
+
"""
|
1392 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
1393 |
+
assert 0.9 <= abs(z) <= 1 and abs(1 - z) >= 0.9 # Tests the test
|
1394 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
1395 |
+
|
1396 |
+
@pytest.mark.parametrize(
|
1397 |
+
"hyp2f1_test_case",
|
1398 |
+
[
|
1399 |
+
pytest.param(
|
1400 |
+
Hyp2f1TestCase(
|
1401 |
+
a=4.5,
|
1402 |
+
b=16.088264119063613,
|
1403 |
+
c=8.5,
|
1404 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1405 |
+
expected=(0.018601324701770394-0.07618420586062377j),
|
1406 |
+
rtol=5e-08,
|
1407 |
+
),
|
1408 |
+
),
|
1409 |
+
pytest.param(
|
1410 |
+
Hyp2f1TestCase(
|
1411 |
+
a=8.25,
|
1412 |
+
b=4.25,
|
1413 |
+
c=4.5,
|
1414 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1415 |
+
expected=(-1.391549471425551-0.118036604903893j),
|
1416 |
+
rtol=5e-15,
|
1417 |
+
),
|
1418 |
+
),
|
1419 |
+
pytest.param(
|
1420 |
+
Hyp2f1TestCase(
|
1421 |
+
a=2.02764642551431,
|
1422 |
+
b=2.050308316530781,
|
1423 |
+
c=-1.9631175993998025,
|
1424 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1425 |
+
expected=(-2309.178768155151-1932.7247727595172j),
|
1426 |
+
rtol=5e-15,
|
1427 |
+
),
|
1428 |
+
),
|
1429 |
+
pytest.param(
|
1430 |
+
Hyp2f1TestCase(
|
1431 |
+
a=16.087593263474208,
|
1432 |
+
b=1.0,
|
1433 |
+
c=-15.964218273004214,
|
1434 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1435 |
+
expected=(85592537010.05054-8061416766688.324j),
|
1436 |
+
rtol=2e-14,
|
1437 |
+
),
|
1438 |
+
),
|
1439 |
+
pytest.param(
|
1440 |
+
Hyp2f1TestCase(
|
1441 |
+
a=8.095813935368371,
|
1442 |
+
b=-0.5,
|
1443 |
+
c=1.5,
|
1444 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1445 |
+
expected=(1.2334498208515172-2.1639498536219732j),
|
1446 |
+
rtol=5e-11,
|
1447 |
+
),
|
1448 |
+
),
|
1449 |
+
pytest.param(
|
1450 |
+
Hyp2f1TestCase(
|
1451 |
+
a=16.087593263474208,
|
1452 |
+
b=-15.964218273004214,
|
1453 |
+
c=4.0,
|
1454 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1455 |
+
expected=(102266.35398605966-44976.97828737755j),
|
1456 |
+
rtol=1e-3,
|
1457 |
+
),
|
1458 |
+
marks=pytest.mark.xfail(
|
1459 |
+
reason="Unhandled parameters."
|
1460 |
+
)
|
1461 |
+
),
|
1462 |
+
pytest.param(
|
1463 |
+
Hyp2f1TestCase(
|
1464 |
+
a=4.0,
|
1465 |
+
b=-3.956227226099288,
|
1466 |
+
c=-15.964218273004214,
|
1467 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1468 |
+
expected=(-2.9590030930007236-4.190770764773225j),
|
1469 |
+
rtol=5e-13,
|
1470 |
+
),
|
1471 |
+
),
|
1472 |
+
pytest.param(
|
1473 |
+
Hyp2f1TestCase(
|
1474 |
+
a=4.080187217753502,
|
1475 |
+
b=-15.5,
|
1476 |
+
c=-7.5,
|
1477 |
+
z=(0.5689655172413794-0.8724137931034484j),
|
1478 |
+
expected=(-112554838.92074208+174941462.9202412j),
|
1479 |
+
rtol=5e-05,
|
1480 |
+
),
|
1481 |
+
),
|
1482 |
+
pytest.param(
|
1483 |
+
Hyp2f1TestCase(
|
1484 |
+
a=-15.980848054962111,
|
1485 |
+
b=2.050308316530781,
|
1486 |
+
c=1.0,
|
1487 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1488 |
+
expected=(3.7519882374080145+7.360753798667486j),
|
1489 |
+
rtol=5e-13,
|
1490 |
+
),
|
1491 |
+
),
|
1492 |
+
pytest.param(
|
1493 |
+
Hyp2f1TestCase(
|
1494 |
+
a=-7.937789122896016,
|
1495 |
+
b=2.050308316530781,
|
1496 |
+
c=4.0,
|
1497 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1498 |
+
expected=(0.000181132943964693+0.07742903103815582j),
|
1499 |
+
rtol=5e-14,
|
1500 |
+
),
|
1501 |
+
),
|
1502 |
+
pytest.param(
|
1503 |
+
Hyp2f1TestCase(
|
1504 |
+
a=-7.937789122896016,
|
1505 |
+
b=4.0013768449590685,
|
1506 |
+
c=-1.9631175993998025,
|
1507 |
+
z=(0.5689655172413794+0.8724137931034486j),
|
1508 |
+
expected=(386338.760913596-386166.51762171905j),
|
1509 |
+
rtol=5e-15,
|
1510 |
+
),
|
1511 |
+
),
|
1512 |
+
pytest.param(
|
1513 |
+
Hyp2f1TestCase(
|
1514 |
+
a=-15.980848054962111,
|
1515 |
+
b=8.0,
|
1516 |
+
c=-1.92872979730171,
|
1517 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1518 |
+
expected=(1348667126.3444858-2375132427.158893j),
|
1519 |
+
rtol=5e-14,
|
1520 |
+
),
|
1521 |
+
),
|
1522 |
+
pytest.param(
|
1523 |
+
Hyp2f1TestCase(
|
1524 |
+
a=-3.5,
|
1525 |
+
b=-0.9629749245209605,
|
1526 |
+
c=4.5,
|
1527 |
+
z=(0.5689655172413794+0.8724137931034486j),
|
1528 |
+
expected=(1.428353429538678+0.6472718120804372j),
|
1529 |
+
rtol=5e-15,
|
1530 |
+
),
|
1531 |
+
),
|
1532 |
+
pytest.param(
|
1533 |
+
Hyp2f1TestCase(
|
1534 |
+
a=-7.937789122896016,
|
1535 |
+
b=-0.9629749245209605,
|
1536 |
+
c=2.0397202577726152,
|
1537 |
+
z=(0.5689655172413794-0.8724137931034484j),
|
1538 |
+
expected=(3.1439267526119643-3.145305240375117j),
|
1539 |
+
rtol=5e-14,
|
1540 |
+
),
|
1541 |
+
),
|
1542 |
+
pytest.param(
|
1543 |
+
Hyp2f1TestCase(
|
1544 |
+
a=-1.9214641416286231,
|
1545 |
+
b=-15.964218273004214,
|
1546 |
+
c=-7.93846038215665,
|
1547 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1548 |
+
expected=(75.27467675681773+144.0946946292215j),
|
1549 |
+
rtol=1e-07,
|
1550 |
+
),
|
1551 |
+
),
|
1552 |
+
pytest.param(
|
1553 |
+
Hyp2f1TestCase(
|
1554 |
+
a=-3.75,
|
1555 |
+
b=-7.75,
|
1556 |
+
c=-7.5,
|
1557 |
+
z=(0.5689655172413794+0.8724137931034486j),
|
1558 |
+
expected=(-0.3699450626264222+0.8732812475910993j),
|
1559 |
+
rtol=1e-15,
|
1560 |
+
),
|
1561 |
+
),
|
1562 |
+
pytest.param(
|
1563 |
+
Hyp2f1TestCase(
|
1564 |
+
a=1.5,
|
1565 |
+
b=16.5,
|
1566 |
+
c=1.0561196186065624,
|
1567 |
+
z=(0.5689655172413794-0.8724137931034484j),
|
1568 |
+
expected=(5.5361025821300665-2.4709693474656285j),
|
1569 |
+
rtol=5e-09,
|
1570 |
+
),
|
1571 |
+
),
|
1572 |
+
pytest.param(
|
1573 |
+
Hyp2f1TestCase(
|
1574 |
+
a=1.5,
|
1575 |
+
b=8.5,
|
1576 |
+
c=-3.9316537064827854,
|
1577 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1578 |
+
expected=(-782805.6699207705-537192.581278909j),
|
1579 |
+
rtol=5e-14,
|
1580 |
+
),
|
1581 |
+
),
|
1582 |
+
pytest.param(
|
1583 |
+
Hyp2f1TestCase(
|
1584 |
+
a=2.5,
|
1585 |
+
b=-15.5,
|
1586 |
+
c=1.0561196186065624,
|
1587 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1588 |
+
expected=(12.345113400639693-14.993248992902007j),
|
1589 |
+
rtol=0.0005,
|
1590 |
+
),
|
1591 |
+
),
|
1592 |
+
pytest.param(
|
1593 |
+
Hyp2f1TestCase(
|
1594 |
+
a=1.5,
|
1595 |
+
b=-0.5,
|
1596 |
+
c=-15.964218273004214,
|
1597 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1598 |
+
expected=(23.698109392667842+97.15002033534108j),
|
1599 |
+
rtol=5e-14,
|
1600 |
+
),
|
1601 |
+
),
|
1602 |
+
pytest.param(
|
1603 |
+
Hyp2f1TestCase(
|
1604 |
+
a=-7.5,
|
1605 |
+
b=16.5,
|
1606 |
+
c=4.0013768449590685,
|
1607 |
+
z=(0.6448275862068968-0.8724137931034484j),
|
1608 |
+
expected=(1115.2978631811834+915.9212658718577j),
|
1609 |
+
rtol=5e-15,
|
1610 |
+
),
|
1611 |
+
),
|
1612 |
+
pytest.param(
|
1613 |
+
Hyp2f1TestCase(
|
1614 |
+
a=-15.5,
|
1615 |
+
b=16.5,
|
1616 |
+
c=-0.9629749245209605,
|
1617 |
+
z=(0.6448275862068968+0.8724137931034486j),
|
1618 |
+
expected=(642077722221.6489+535274495398.21027j),
|
1619 |
+
rtol=5e-15,
|
1620 |
+
),
|
1621 |
+
),
|
1622 |
+
pytest.param(
|
1623 |
+
Hyp2f1TestCase(
|
1624 |
+
a=-7.5,
|
1625 |
+
b=-3.5,
|
1626 |
+
c=4.0013768449590685,
|
1627 |
+
z=(0.5689655172413794+0.8724137931034486j),
|
1628 |
+
expected=(-5.689219222945697+16.877463062787143j),
|
1629 |
+
rtol=5e-15,
|
1630 |
+
),
|
1631 |
+
),
|
1632 |
+
pytest.param(
|
1633 |
+
Hyp2f1TestCase(
|
1634 |
+
a=-15.5,
|
1635 |
+
b=-1.5,
|
1636 |
+
c=-0.9629749245209605,
|
1637 |
+
z=(0.5689655172413794-0.8724137931034484j),
|
1638 |
+
expected=(-44.32070290703576+1026.9127058617403j),
|
1639 |
+
rtol=5e-14,
|
1640 |
+
),
|
1641 |
+
),
|
1642 |
+
pytest.param(
|
1643 |
+
Hyp2f1TestCase(
|
1644 |
+
a=16.25,
|
1645 |
+
b=2.25,
|
1646 |
+
c=4.5,
|
1647 |
+
z=(0.11379310344827598-1.024137931034483j),
|
1648 |
+
expected=(-0.021965227124574663+0.009908300237809064j),
|
1649 |
+
rtol=1e-3,
|
1650 |
+
),
|
1651 |
+
marks=pytest.mark.xfail(
|
1652 |
+
reason="Unhandled parameters."
|
1653 |
+
)
|
1654 |
+
),
|
1655 |
+
pytest.param(
|
1656 |
+
Hyp2f1TestCase(
|
1657 |
+
a=2.02764642551431,
|
1658 |
+
b=1.5,
|
1659 |
+
c=16.5,
|
1660 |
+
z=(0.26551724137931054+1.024137931034483j),
|
1661 |
+
expected=(1.0046072901244183+0.19945500134119992j),
|
1662 |
+
rtol=5e-14,
|
1663 |
+
),
|
1664 |
+
),
|
1665 |
+
pytest.param(
|
1666 |
+
Hyp2f1TestCase(
|
1667 |
+
a=16.087593263474208,
|
1668 |
+
b=1.0,
|
1669 |
+
c=-3.9316537064827854,
|
1670 |
+
z=(0.3413793103448277+0.9482758620689657j),
|
1671 |
+
expected=(21022.30133421465+49175.98317370489j),
|
1672 |
+
rtol=5e-13,
|
1673 |
+
),
|
1674 |
+
),
|
1675 |
+
pytest.param(
|
1676 |
+
Hyp2f1TestCase(
|
1677 |
+
a=4.080187217753502,
|
1678 |
+
b=16.088264119063613,
|
1679 |
+
c=-1.9631175993998025,
|
1680 |
+
z=(0.4172413793103451-0.9482758620689655j),
|
1681 |
+
expected=(-7024239.358547302+2481375.02681063j),
|
1682 |
+
rtol=5e-14,
|
1683 |
+
),
|
1684 |
+
),
|
1685 |
+
pytest.param(
|
1686 |
+
Hyp2f1TestCase(
|
1687 |
+
a=16.25,
|
1688 |
+
b=-15.75,
|
1689 |
+
c=1.5,
|
1690 |
+
z=(0.18965517241379315+1.024137931034483j),
|
1691 |
+
expected=(92371704.94848-403546832.548352j),
|
1692 |
+
rtol=5e-06,
|
1693 |
+
),
|
1694 |
+
),
|
1695 |
+
pytest.param(
|
1696 |
+
Hyp2f1TestCase(
|
1697 |
+
a=8.5,
|
1698 |
+
b=-7.949900487447654,
|
1699 |
+
c=8.5,
|
1700 |
+
z=(0.26551724137931054-1.024137931034483j),
|
1701 |
+
expected=(1.9335109845308265+5.986542524829654j),
|
1702 |
+
rtol=5e-10,
|
1703 |
+
),
|
1704 |
+
),
|
1705 |
+
pytest.param(
|
1706 |
+
Hyp2f1TestCase(
|
1707 |
+
a=8.095813935368371,
|
1708 |
+
b=-1.92872979730171,
|
1709 |
+
c=-7.93846038215665,
|
1710 |
+
z=(0.4931034482758623+0.8724137931034486j),
|
1711 |
+
expected=(-122.52639696039328-59.72428067512221j),
|
1712 |
+
rtol=5e-14,
|
1713 |
+
),
|
1714 |
+
),
|
1715 |
+
pytest.param(
|
1716 |
+
Hyp2f1TestCase(
|
1717 |
+
a=16.25,
|
1718 |
+
b=-1.75,
|
1719 |
+
c=-1.5,
|
1720 |
+
z=(0.4931034482758623+0.9482758620689657j),
|
1721 |
+
expected=(-90.40642053579428+50.50649180047921j),
|
1722 |
+
rtol=5e-08,
|
1723 |
+
),
|
1724 |
+
),
|
1725 |
+
pytest.param(
|
1726 |
+
Hyp2f1TestCase(
|
1727 |
+
a=-3.5,
|
1728 |
+
b=8.077282662161238,
|
1729 |
+
c=16.5,
|
1730 |
+
z=(0.4931034482758623+0.9482758620689657j),
|
1731 |
+
expected=(-0.2155745818150323-0.564628986876639j),
|
1732 |
+
rtol=5e-15,
|
1733 |
+
),
|
1734 |
+
),
|
1735 |
+
pytest.param(
|
1736 |
+
Hyp2f1TestCase(
|
1737 |
+
a=-0.9220024191881196,
|
1738 |
+
b=1.0561196186065624,
|
1739 |
+
c=8.031683612216888,
|
1740 |
+
z=(0.4172413793103451-0.9482758620689655j),
|
1741 |
+
expected=(0.9503140488280465+0.11574960074292677j),
|
1742 |
+
rtol=5e-15,
|
1743 |
+
),
|
1744 |
+
),
|
1745 |
+
pytest.param(
|
1746 |
+
Hyp2f1TestCase(
|
1747 |
+
a=-0.75,
|
1748 |
+
b=2.25,
|
1749 |
+
c=-15.5,
|
1750 |
+
z=(0.4172413793103451+0.9482758620689657j),
|
1751 |
+
expected=(0.9285862488442175+0.8203699266719692j),
|
1752 |
+
rtol=5e-13,
|
1753 |
+
),
|
1754 |
+
),
|
1755 |
+
pytest.param(
|
1756 |
+
Hyp2f1TestCase(
|
1757 |
+
a=-7.75,
|
1758 |
+
b=4.25,
|
1759 |
+
c=-15.5,
|
1760 |
+
z=(0.3413793103448277-0.9482758620689655j),
|
1761 |
+
expected=(-1.0509834850116921-1.1145522325486075j),
|
1762 |
+
rtol=1e-14,
|
1763 |
+
),
|
1764 |
+
),
|
1765 |
+
pytest.param(
|
1766 |
+
Hyp2f1TestCase(
|
1767 |
+
a=-7.937789122896016,
|
1768 |
+
b=-0.9629749245209605,
|
1769 |
+
c=2.0397202577726152,
|
1770 |
+
z=(0.4931034482758623-0.9482758620689655j),
|
1771 |
+
expected=(2.88119116536769-3.4249933450696806j),
|
1772 |
+
rtol=5e-15,
|
1773 |
+
),
|
1774 |
+
),
|
1775 |
+
pytest.param(
|
1776 |
+
Hyp2f1TestCase(
|
1777 |
+
a=-15.5,
|
1778 |
+
b=-15.964218273004214,
|
1779 |
+
c=16.5,
|
1780 |
+
z=(0.18965517241379315+1.024137931034483j),
|
1781 |
+
expected=(199.65868451496038+347.79384207302877j),
|
1782 |
+
rtol=1e-13,
|
1783 |
+
),
|
1784 |
+
),
|
1785 |
+
pytest.param(
|
1786 |
+
Hyp2f1TestCase(
|
1787 |
+
a=-15.75,
|
1788 |
+
b=-15.75,
|
1789 |
+
c=-3.5,
|
1790 |
+
z=(0.4931034482758623-0.8724137931034484j),
|
1791 |
+
expected=(-208138312553.07013+58631611809.026955j),
|
1792 |
+
rtol=5e-14,
|
1793 |
+
),
|
1794 |
+
),
|
1795 |
+
pytest.param(
|
1796 |
+
Hyp2f1TestCase(
|
1797 |
+
a=-7.937789122896016,
|
1798 |
+
b=-15.5,
|
1799 |
+
c=-7.5,
|
1800 |
+
z=(0.3413793103448277+0.9482758620689657j),
|
1801 |
+
expected=(-23032.90519856288-18256.94050457296j),
|
1802 |
+
rtol=5e-15,
|
1803 |
+
),
|
1804 |
+
),
|
1805 |
+
pytest.param(
|
1806 |
+
Hyp2f1TestCase(
|
1807 |
+
a=4.5,
|
1808 |
+
b=1.5,
|
1809 |
+
c=1.0561196186065624,
|
1810 |
+
z=(0.4931034482758623-0.8724137931034484j),
|
1811 |
+
expected=(1.507342459587056+1.2332023580148403j),
|
1812 |
+
rtol=1e-15,
|
1813 |
+
),
|
1814 |
+
),
|
1815 |
+
pytest.param(
|
1816 |
+
Hyp2f1TestCase(
|
1817 |
+
a=2.5,
|
1818 |
+
b=4.5,
|
1819 |
+
c=-3.9316537064827854,
|
1820 |
+
z=(0.4172413793103451+0.9482758620689657j),
|
1821 |
+
expected=(7044.766127108853-40210.365567285575j),
|
1822 |
+
rtol=5e-14,
|
1823 |
+
),
|
1824 |
+
),
|
1825 |
+
pytest.param(
|
1826 |
+
Hyp2f1TestCase(
|
1827 |
+
a=1.5,
|
1828 |
+
b=-1.5,
|
1829 |
+
c=1.0561196186065624,
|
1830 |
+
z=(0.03793103448275881+1.024137931034483j),
|
1831 |
+
expected=(0.2725347741628333-2.247314875514784j),
|
1832 |
+
rtol=1e-15,
|
1833 |
+
),
|
1834 |
+
),
|
1835 |
+
pytest.param(
|
1836 |
+
Hyp2f1TestCase(
|
1837 |
+
a=4.5,
|
1838 |
+
b=-1.5,
|
1839 |
+
c=-7.949900487447654,
|
1840 |
+
z=(0.26551724137931054+1.024137931034483j),
|
1841 |
+
expected=(-11.250200011017546+12.597393659160472j),
|
1842 |
+
rtol=5e-14,
|
1843 |
+
),
|
1844 |
+
),
|
1845 |
+
pytest.param(
|
1846 |
+
Hyp2f1TestCase(
|
1847 |
+
a=-7.5,
|
1848 |
+
b=8.5,
|
1849 |
+
c=16.088264119063613,
|
1850 |
+
z=(0.26551724137931054+1.024137931034483j),
|
1851 |
+
expected=(-0.18515160890991517+0.7959014164484782j),
|
1852 |
+
rtol=1e-15,
|
1853 |
+
),
|
1854 |
+
),
|
1855 |
+
pytest.param(
|
1856 |
+
Hyp2f1TestCase(
|
1857 |
+
a=-7.5,
|
1858 |
+
b=16.5,
|
1859 |
+
c=-3.9316537064827854,
|
1860 |
+
z=(0.3413793103448277-1.024137931034483j),
|
1861 |
+
expected=(998246378.8556538+1112032928.103645j),
|
1862 |
+
rtol=5e-14,
|
1863 |
+
),
|
1864 |
+
),
|
1865 |
+
pytest.param(
|
1866 |
+
Hyp2f1TestCase(
|
1867 |
+
a=-1.5,
|
1868 |
+
b=-3.5,
|
1869 |
+
c=2.050308316530781,
|
1870 |
+
z=(0.03793103448275881+1.024137931034483j),
|
1871 |
+
expected=(0.5527670397711952+2.697662715303637j),
|
1872 |
+
rtol=1.2e-15, # rtol bumped from 1e-15 in gh18414
|
1873 |
+
),
|
1874 |
+
),
|
1875 |
+
pytest.param(
|
1876 |
+
Hyp2f1TestCase(
|
1877 |
+
a=-15.5,
|
1878 |
+
b=-1.5,
|
1879 |
+
c=-0.9629749245209605,
|
1880 |
+
z=(0.4931034482758623-0.8724137931034484j),
|
1881 |
+
expected=(55.396931662136886+968.467463806326j),
|
1882 |
+
rtol=5e-14,
|
1883 |
+
),
|
1884 |
+
),
|
1885 |
+
]
|
1886 |
+
)
|
1887 |
+
def test_region5(self, hyp2f1_test_case):
|
1888 |
+
"""1 < |z| < 1.1 and |1 - z| >= 0.9 and real(z) >= 0"""
|
1889 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
1890 |
+
assert 1 < abs(z) < 1.1 and abs(1 - z) >= 0.9 and z.real >= 0
|
1891 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
1892 |
+
|
1893 |
+
@pytest.mark.parametrize(
|
1894 |
+
"hyp2f1_test_case",
|
1895 |
+
[
|
1896 |
+
pytest.param(
|
1897 |
+
Hyp2f1TestCase(
|
1898 |
+
a=8.095813935368371,
|
1899 |
+
b=4.0013768449590685,
|
1900 |
+
c=4.078873014294075,
|
1901 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
1902 |
+
expected=(-0.0018093573941378783+0.003481887377423739j),
|
1903 |
+
rtol=5e-15,
|
1904 |
+
),
|
1905 |
+
),
|
1906 |
+
pytest.param(
|
1907 |
+
Hyp2f1TestCase(
|
1908 |
+
a=16.087593263474208,
|
1909 |
+
b=2.050308316530781,
|
1910 |
+
c=1.0651378143226575,
|
1911 |
+
z=(-0.736842105263158-0.736842105263158j),
|
1912 |
+
expected=(-0.00023401243818780545-1.7983496305603562e-05j),
|
1913 |
+
rtol=1e-15,
|
1914 |
+
),
|
1915 |
+
),
|
1916 |
+
pytest.param(
|
1917 |
+
Hyp2f1TestCase(
|
1918 |
+
a=1.0272592605282642,
|
1919 |
+
b=8.077282662161238,
|
1920 |
+
c=4.078873014294075,
|
1921 |
+
z=(-0.5263157894736843-0.9473684210526316j),
|
1922 |
+
expected=(0.22359773002226846-0.24092487123993353j),
|
1923 |
+
rtol=1e-15,
|
1924 |
+
),
|
1925 |
+
),
|
1926 |
+
pytest.param(
|
1927 |
+
Hyp2f1TestCase(
|
1928 |
+
a=1.0272592605282642,
|
1929 |
+
b=2.050308316530781,
|
1930 |
+
c=-15.963511401609862,
|
1931 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
1932 |
+
expected=(1.191573745740011+0.14347394589721466j),
|
1933 |
+
rtol=5e-14,
|
1934 |
+
),
|
1935 |
+
),
|
1936 |
+
pytest.param(
|
1937 |
+
Hyp2f1TestCase(
|
1938 |
+
a=4.080187217753502,
|
1939 |
+
b=4.0013768449590685,
|
1940 |
+
c=-15.963511401609862,
|
1941 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
1942 |
+
expected=(31.822620756901784-66.09094396747611j),
|
1943 |
+
rtol=5e-14,
|
1944 |
+
),
|
1945 |
+
),
|
1946 |
+
pytest.param(
|
1947 |
+
Hyp2f1TestCase(
|
1948 |
+
a=4.080187217753502,
|
1949 |
+
b=8.077282662161238,
|
1950 |
+
c=-7.93846038215665,
|
1951 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
1952 |
+
expected=(207.16750179245952+34.80478274924269j),
|
1953 |
+
rtol=5e-12,
|
1954 |
+
),
|
1955 |
+
),
|
1956 |
+
pytest.param(
|
1957 |
+
Hyp2f1TestCase(
|
1958 |
+
a=8.095813935368371,
|
1959 |
+
b=-7.949900487447654,
|
1960 |
+
c=8.031683612216888,
|
1961 |
+
z=(-0.736842105263158+0.7368421052631575j),
|
1962 |
+
expected=(-159.62429364277145+9.154224290644898j),
|
1963 |
+
rtol=5e-14,
|
1964 |
+
),
|
1965 |
+
),
|
1966 |
+
pytest.param(
|
1967 |
+
Hyp2f1TestCase(
|
1968 |
+
a=1.0272592605282642,
|
1969 |
+
b=-1.92872979730171,
|
1970 |
+
c=16.056809865262608,
|
1971 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
1972 |
+
expected=(1.121122351247184-0.07170260470126685j),
|
1973 |
+
rtol=5e-15,
|
1974 |
+
),
|
1975 |
+
),
|
1976 |
+
pytest.param(
|
1977 |
+
Hyp2f1TestCase(
|
1978 |
+
a=16.087593263474208,
|
1979 |
+
b=-0.9629749245209605,
|
1980 |
+
c=16.056809865262608,
|
1981 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
1982 |
+
expected=(1.9040596681316053-0.4951799449960107j),
|
1983 |
+
rtol=5e-14,
|
1984 |
+
),
|
1985 |
+
),
|
1986 |
+
pytest.param(
|
1987 |
+
Hyp2f1TestCase(
|
1988 |
+
a=1.0272592605282642,
|
1989 |
+
b=-1.92872979730171,
|
1990 |
+
c=-0.906685989801748,
|
1991 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
1992 |
+
expected=(-14.496623497780739-21.897524523299875j),
|
1993 |
+
rtol=5e-14,
|
1994 |
+
),
|
1995 |
+
),
|
1996 |
+
pytest.param(
|
1997 |
+
Hyp2f1TestCase(
|
1998 |
+
a=4.080187217753502,
|
1999 |
+
b=-3.9316537064827854,
|
2000 |
+
c=-3.9924618758357022,
|
2001 |
+
z=(-0.5263157894736843-0.9473684210526316j),
|
2002 |
+
expected=(36.33473466026878+253.88728442029577j),
|
2003 |
+
rtol=5e-14,
|
2004 |
+
),
|
2005 |
+
),
|
2006 |
+
pytest.param(
|
2007 |
+
Hyp2f1TestCase(
|
2008 |
+
a=1.0272592605282642,
|
2009 |
+
b=-15.964218273004214,
|
2010 |
+
c=-0.906685989801748,
|
2011 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
2012 |
+
expected=(1505052.5653144997-50820766.81043443j),
|
2013 |
+
rtol=1e-14,
|
2014 |
+
),
|
2015 |
+
),
|
2016 |
+
pytest.param(
|
2017 |
+
Hyp2f1TestCase(
|
2018 |
+
a=-3.956227226099288,
|
2019 |
+
b=4.0013768449590685,
|
2020 |
+
c=1.0651378143226575,
|
2021 |
+
z=(-0.5263157894736843+0.9473684210526314j),
|
2022 |
+
expected=(-127.79407519260877-28.69899444941112j),
|
2023 |
+
rtol=5e-15,
|
2024 |
+
),
|
2025 |
+
),
|
2026 |
+
pytest.param(
|
2027 |
+
Hyp2f1TestCase(
|
2028 |
+
a=-1.9214641416286231,
|
2029 |
+
b=8.077282662161238,
|
2030 |
+
c=16.056809865262608,
|
2031 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
2032 |
+
expected=(2.0623331933754976+0.741234463565458j),
|
2033 |
+
rtol=5e-15,
|
2034 |
+
),
|
2035 |
+
),
|
2036 |
+
pytest.param(
|
2037 |
+
Hyp2f1TestCase(
|
2038 |
+
a=-3.956227226099288,
|
2039 |
+
b=8.077282662161238,
|
2040 |
+
c=2.0397202577726152,
|
2041 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
2042 |
+
expected=(30.729193458862525-292.5700835046965j),
|
2043 |
+
rtol=1e-15,
|
2044 |
+
),
|
2045 |
+
),
|
2046 |
+
pytest.param(
|
2047 |
+
Hyp2f1TestCase(
|
2048 |
+
a=-1.9214641416286231,
|
2049 |
+
b=1.0561196186065624,
|
2050 |
+
c=-1.9631175993998025,
|
2051 |
+
z=(-0.5263157894736843-0.9473684210526316j),
|
2052 |
+
expected=(1.1285917906203495-0.735264575450189j),
|
2053 |
+
rtol=5e-15,
|
2054 |
+
),
|
2055 |
+
),
|
2056 |
+
pytest.param(
|
2057 |
+
Hyp2f1TestCase(
|
2058 |
+
a=-0.9220024191881196,
|
2059 |
+
b=1.0561196186065624,
|
2060 |
+
c=-3.9924618758357022,
|
2061 |
+
z=(-0.736842105263158+0.7368421052631575j),
|
2062 |
+
expected=(0.6356474446678052-0.02429663008952248j),
|
2063 |
+
rtol=5e-14,
|
2064 |
+
),
|
2065 |
+
),
|
2066 |
+
pytest.param(
|
2067 |
+
Hyp2f1TestCase(
|
2068 |
+
a=-1.9214641416286231,
|
2069 |
+
b=16.088264119063613,
|
2070 |
+
c=-7.93846038215665,
|
2071 |
+
z=(-0.736842105263158+0.7368421052631575j),
|
2072 |
+
expected=(0.4718880510273174+0.655083067736377j),
|
2073 |
+
rtol=1e-11,
|
2074 |
+
),
|
2075 |
+
),
|
2076 |
+
pytest.param(
|
2077 |
+
Hyp2f1TestCase(
|
2078 |
+
a=-7.937789122896016,
|
2079 |
+
b=-3.9316537064827854,
|
2080 |
+
c=16.056809865262608,
|
2081 |
+
z=(-0.9473684210526316+0.5263157894736841j),
|
2082 |
+
expected=(-0.14681550942352714+0.16092206364265146j),
|
2083 |
+
rtol=5e-11,
|
2084 |
+
),
|
2085 |
+
),
|
2086 |
+
pytest.param(
|
2087 |
+
Hyp2f1TestCase(
|
2088 |
+
a=-0.9220024191881196,
|
2089 |
+
b=-15.964218273004214,
|
2090 |
+
c=1.0651378143226575,
|
2091 |
+
z=(-0.5263157894736843+0.9473684210526314j),
|
2092 |
+
expected=(-6.436835190526225+22.883156700606182j),
|
2093 |
+
rtol=5e-14,
|
2094 |
+
),
|
2095 |
+
),
|
2096 |
+
pytest.param(
|
2097 |
+
Hyp2f1TestCase(
|
2098 |
+
a=-0.9220024191881196,
|
2099 |
+
b=-7.949900487447654,
|
2100 |
+
c=4.078873014294075,
|
2101 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
2102 |
+
expected=(-0.7505682955068583-1.1026583264249945j),
|
2103 |
+
rtol=1e-15,
|
2104 |
+
),
|
2105 |
+
),
|
2106 |
+
pytest.param(
|
2107 |
+
Hyp2f1TestCase(
|
2108 |
+
a=-3.956227226099288,
|
2109 |
+
b=-3.9316537064827854,
|
2110 |
+
c=-7.93846038215665,
|
2111 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
2112 |
+
expected=(3.6247814989198166+2.596041360148318j),
|
2113 |
+
rtol=5e-15,
|
2114 |
+
),
|
2115 |
+
),
|
2116 |
+
pytest.param(
|
2117 |
+
Hyp2f1TestCase(
|
2118 |
+
a=-3.956227226099288,
|
2119 |
+
b=-15.964218273004214,
|
2120 |
+
c=-1.9631175993998025,
|
2121 |
+
z=(-0.5263157894736843-0.9473684210526316j),
|
2122 |
+
expected=(-59537.65287927933-669074.4342539902j),
|
2123 |
+
rtol=5e-15,
|
2124 |
+
),
|
2125 |
+
),
|
2126 |
+
pytest.param(
|
2127 |
+
Hyp2f1TestCase(
|
2128 |
+
a=-3.956227226099288,
|
2129 |
+
b=-15.964218273004214,
|
2130 |
+
c=-1.9631175993998025,
|
2131 |
+
z=(-0.9473684210526316-0.5263157894736843j),
|
2132 |
+
expected=(-433084.9970266166+431088.393918521j),
|
2133 |
+
rtol=5e-14,
|
2134 |
+
),
|
2135 |
+
),
|
2136 |
+
]
|
2137 |
+
)
|
2138 |
+
def test_region6(self, hyp2f1_test_case):
|
2139 |
+
"""|z| > 1 but not in region 5."""
|
2140 |
+
a, b, c, z, expected, rtol = hyp2f1_test_case
|
2141 |
+
assert (
|
2142 |
+
abs(z) > 1 and
|
2143 |
+
not (1 < abs(z) < 1.1 and abs(1 - z) >= 0.9 and z.real >= 0)
|
2144 |
+
)
|
2145 |
+
assert_allclose(hyp2f1(a, b, c, z), expected, rtol=rtol)
|
2146 |
+
|
2147 |
+
@pytest.mark.slow
|
2148 |
+
@check_version(mpmath, "1.0.0")
|
2149 |
+
def test_test_hyp2f1(self):
|
2150 |
+
"""Test that expected values match what is computed by mpmath.
|
2151 |
+
|
2152 |
+
This gathers the parameters for the test cases out of the pytest marks.
|
2153 |
+
The parameters are a, b, c, z, expected, rtol, where expected should
|
2154 |
+
be the value of hyp2f1(a, b, c, z) computed with mpmath. The test
|
2155 |
+
recomputes hyp2f1(a, b, c, z) using mpmath and verifies that expected
|
2156 |
+
actually is the correct value. This allows the data for the tests to
|
2157 |
+
live within the test code instead of an external datafile, while
|
2158 |
+
avoiding having to compute the results with mpmath during the test,
|
2159 |
+
except for when slow tests are being run.
|
2160 |
+
"""
|
2161 |
+
test_methods = [
|
2162 |
+
test_method for test_method in dir(self)
|
2163 |
+
if test_method.startswith('test') and
|
2164 |
+
# Filter properties and attributes (futureproofing).
|
2165 |
+
callable(getattr(self, test_method)) and
|
2166 |
+
# Filter out this test
|
2167 |
+
test_method != 'test_test_hyp2f1'
|
2168 |
+
]
|
2169 |
+
for test_method in test_methods:
|
2170 |
+
params = self._get_test_parameters(getattr(self, test_method))
|
2171 |
+
for a, b, c, z, expected, _ in params:
|
2172 |
+
assert_allclose(mp_hyp2f1(a, b, c, z), expected, rtol=2.25e-16)
|
2173 |
+
|
2174 |
+
def _get_test_parameters(self, test_method):
|
2175 |
+
"""Get pytest.mark parameters for a test in this class."""
|
2176 |
+
return [
|
2177 |
+
case.values[0] for mark in test_method.pytestmark
|
2178 |
+
if mark.name == 'parametrize'
|
2179 |
+
for case in mark.args[1]
|
2180 |
+
]
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_hypergeometric.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
import numpy as np
|
3 |
+
from numpy.testing import assert_allclose, assert_equal
|
4 |
+
import scipy.special as sc
|
5 |
+
|
6 |
+
|
7 |
+
class TestHyperu:
|
8 |
+
|
9 |
+
def test_negative_x(self):
|
10 |
+
a, b, x = np.meshgrid(
|
11 |
+
[-1, -0.5, 0, 0.5, 1],
|
12 |
+
[-1, -0.5, 0, 0.5, 1],
|
13 |
+
np.linspace(-100, -1, 10),
|
14 |
+
)
|
15 |
+
assert np.all(np.isnan(sc.hyperu(a, b, x)))
|
16 |
+
|
17 |
+
def test_special_cases(self):
|
18 |
+
assert sc.hyperu(0, 1, 1) == 1.0
|
19 |
+
|
20 |
+
@pytest.mark.parametrize('a', [0.5, 1, np.nan])
|
21 |
+
@pytest.mark.parametrize('b', [1, 2, np.nan])
|
22 |
+
@pytest.mark.parametrize('x', [0.25, 3, np.nan])
|
23 |
+
def test_nan_inputs(self, a, b, x):
|
24 |
+
assert np.isnan(sc.hyperu(a, b, x)) == np.any(np.isnan([a, b, x]))
|
25 |
+
|
26 |
+
|
27 |
+
class TestHyp1f1:
|
28 |
+
|
29 |
+
@pytest.mark.parametrize('a, b, x', [
|
30 |
+
(np.nan, 1, 1),
|
31 |
+
(1, np.nan, 1),
|
32 |
+
(1, 1, np.nan)
|
33 |
+
])
|
34 |
+
def test_nan_inputs(self, a, b, x):
|
35 |
+
assert np.isnan(sc.hyp1f1(a, b, x))
|
36 |
+
|
37 |
+
def test_poles(self):
|
38 |
+
assert_equal(sc.hyp1f1(1, [0, -1, -2, -3, -4], 0.5), np.inf)
|
39 |
+
|
40 |
+
@pytest.mark.parametrize('a, b, x, result', [
|
41 |
+
(-1, 1, 0.5, 0.5),
|
42 |
+
(1, 1, 0.5, 1.6487212707001281468),
|
43 |
+
(2, 1, 0.5, 2.4730819060501922203),
|
44 |
+
(1, 2, 0.5, 1.2974425414002562937),
|
45 |
+
(-10, 1, 0.5, -0.38937441413785204475)
|
46 |
+
])
|
47 |
+
def test_special_cases(self, a, b, x, result):
|
48 |
+
# Hit all the special case branches at the beginning of the
|
49 |
+
# function. Desired answers computed using Mpmath.
|
50 |
+
assert_allclose(sc.hyp1f1(a, b, x), result, atol=0, rtol=1e-15)
|
51 |
+
|
52 |
+
@pytest.mark.parametrize('a, b, x, result', [
|
53 |
+
(1, 1, 0.44, 1.5527072185113360455),
|
54 |
+
(-1, 1, 0.44, 0.55999999999999999778),
|
55 |
+
(100, 100, 0.89, 2.4351296512898745592),
|
56 |
+
(-100, 100, 0.89, 0.40739062490768104667),
|
57 |
+
(1.5, 100, 59.99, 3.8073513625965598107),
|
58 |
+
(-1.5, 100, 59.99, 0.25099240047125826943)
|
59 |
+
])
|
60 |
+
def test_geometric_convergence(self, a, b, x, result):
|
61 |
+
# Test the region where we are relying on the ratio of
|
62 |
+
#
|
63 |
+
# (|a| + 1) * |x| / |b|
|
64 |
+
#
|
65 |
+
# being small. Desired answers computed using Mpmath
|
66 |
+
assert_allclose(sc.hyp1f1(a, b, x), result, atol=0, rtol=1e-15)
|
67 |
+
|
68 |
+
@pytest.mark.parametrize('a, b, x, result', [
|
69 |
+
(-1, 1, 1.5, -0.5),
|
70 |
+
(-10, 1, 1.5, 0.41801777430943080357),
|
71 |
+
(-25, 1, 1.5, 0.25114491646037839809),
|
72 |
+
(-50, 1, 1.5, -0.25683643975194756115),
|
73 |
+
(-80, 1, 1.5, -0.24554329325751503601),
|
74 |
+
(-150, 1, 1.5, -0.173364795515420454496),
|
75 |
+
])
|
76 |
+
def test_a_negative_integer(self, a, b, x, result):
|
77 |
+
# Desired answers computed using Mpmath.
|
78 |
+
assert_allclose(sc.hyp1f1(a, b, x), result, atol=0, rtol=2e-14)
|
79 |
+
|
80 |
+
@pytest.mark.parametrize('a, b, x, expected', [
|
81 |
+
(0.01, 150, -4, 0.99973683897677527773), # gh-3492
|
82 |
+
(1, 5, 0.01, 1.0020033381011970966), # gh-3593
|
83 |
+
(50, 100, 0.01, 1.0050126452421463411), # gh-3593
|
84 |
+
(1, 0.3, -1e3, -7.011932249442947651455e-04), # gh-14149
|
85 |
+
(1, 0.3, -1e4, -7.001190321418937164734e-05), # gh-14149
|
86 |
+
(9, 8.5, -350, -5.224090831922378361082e-20), # gh-17120
|
87 |
+
(9, 8.5, -355, -4.595407159813368193322e-20), # gh-17120
|
88 |
+
(75, -123.5, 15, 3.425753920814889017493e+06),
|
89 |
+
])
|
90 |
+
def test_assorted_cases(self, a, b, x, expected):
|
91 |
+
# Expected values were computed with mpmath.hyp1f1(a, b, x).
|
92 |
+
assert_allclose(sc.hyp1f1(a, b, x), expected, atol=0, rtol=1e-14)
|
93 |
+
|
94 |
+
def test_a_neg_int_and_b_equal_x(self):
|
95 |
+
# This is a case where the Boost wrapper will call hypergeometric_pFq
|
96 |
+
# instead of hypergeometric_1F1. When we use a version of Boost in
|
97 |
+
# which https://github.com/boostorg/math/issues/833 is fixed, this
|
98 |
+
# test case can probably be moved into test_assorted_cases.
|
99 |
+
# The expected value was computed with mpmath.hyp1f1(a, b, x).
|
100 |
+
a = -10.0
|
101 |
+
b = 2.5
|
102 |
+
x = 2.5
|
103 |
+
expected = 0.0365323664364104338721
|
104 |
+
computed = sc.hyp1f1(a, b, x)
|
105 |
+
assert_allclose(computed, expected, atol=0, rtol=1e-13)
|
106 |
+
|
107 |
+
@pytest.mark.parametrize('a, b, x, desired', [
|
108 |
+
(-1, -2, 2, 2),
|
109 |
+
(-1, -4, 10, 3.5),
|
110 |
+
(-2, -2, 1, 2.5)
|
111 |
+
])
|
112 |
+
def test_gh_11099(self, a, b, x, desired):
|
113 |
+
# All desired results computed using Mpmath
|
114 |
+
assert sc.hyp1f1(a, b, x) == desired
|
115 |
+
|
116 |
+
@pytest.mark.parametrize('a', [-3, -2])
|
117 |
+
def test_x_zero_a_and_b_neg_ints_and_a_ge_b(self, a):
|
118 |
+
assert sc.hyp1f1(a, -3, 0) == 1
|
119 |
+
|
120 |
+
# The "legacy edge cases" mentioned in the comments in the following
|
121 |
+
# tests refers to the behavior of hyp1f1(a, b, x) when b is a nonpositive
|
122 |
+
# integer. In some subcases, the behavior of SciPy does not match that
|
123 |
+
# of Boost (1.81+), mpmath and Mathematica (via Wolfram Alpha online).
|
124 |
+
# If the handling of these edges cases is changed to agree with those
|
125 |
+
# libraries, these test will have to be updated.
|
126 |
+
|
127 |
+
@pytest.mark.parametrize('b', [0, -1, -5])
|
128 |
+
def test_legacy_case1(self, b):
|
129 |
+
# Test results of hyp1f1(0, n, x) for n <= 0.
|
130 |
+
# This is a legacy edge case.
|
131 |
+
# Boost (versions greater than 1.80), Mathematica (via Wolfram Alpha
|
132 |
+
# online) and mpmath all return 1 in this case, but SciPy's hyp1f1
|
133 |
+
# returns inf.
|
134 |
+
assert_equal(sc.hyp1f1(0, b, [-1.5, 0, 1.5]), [np.inf, np.inf, np.inf])
|
135 |
+
|
136 |
+
def test_legacy_case2(self):
|
137 |
+
# This is a legacy edge case.
|
138 |
+
# In software such as boost (1.81+), mpmath and Mathematica,
|
139 |
+
# the value is 1.
|
140 |
+
assert sc.hyp1f1(-4, -3, 0) == np.inf
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_kolmogorov.py
ADDED
@@ -0,0 +1,495 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import itertools
|
2 |
+
import sys
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
from numpy.testing import assert_
|
7 |
+
from scipy.special._testutils import FuncData
|
8 |
+
|
9 |
+
from scipy.special import kolmogorov, kolmogi, smirnov, smirnovi
|
10 |
+
from scipy.special._ufuncs import (_kolmogc, _kolmogci, _kolmogp,
|
11 |
+
_smirnovc, _smirnovci, _smirnovp)
|
12 |
+
|
13 |
+
_rtol = 1e-10
|
14 |
+
|
15 |
+
class TestSmirnov:
|
16 |
+
def test_nan(self):
|
17 |
+
assert_(np.isnan(smirnov(1, np.nan)))
|
18 |
+
|
19 |
+
def test_basic(self):
|
20 |
+
dataset = [(1, 0.1, 0.9),
|
21 |
+
(1, 0.875, 0.125),
|
22 |
+
(2, 0.875, 0.125 * 0.125),
|
23 |
+
(3, 0.875, 0.125 * 0.125 * 0.125)]
|
24 |
+
|
25 |
+
dataset = np.asarray(dataset)
|
26 |
+
FuncData(
|
27 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
28 |
+
).check(dtypes=[int, float, float])
|
29 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
30 |
+
FuncData(
|
31 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
32 |
+
).check(dtypes=[int, float, float])
|
33 |
+
|
34 |
+
def test_x_equals_0(self):
|
35 |
+
dataset = [(n, 0, 1) for n in itertools.chain(range(2, 20), range(1010, 1020))]
|
36 |
+
dataset = np.asarray(dataset)
|
37 |
+
FuncData(
|
38 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
39 |
+
).check(dtypes=[int, float, float])
|
40 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
41 |
+
FuncData(
|
42 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
43 |
+
).check(dtypes=[int, float, float])
|
44 |
+
|
45 |
+
def test_x_equals_1(self):
|
46 |
+
dataset = [(n, 1, 0) for n in itertools.chain(range(2, 20), range(1010, 1020))]
|
47 |
+
dataset = np.asarray(dataset)
|
48 |
+
FuncData(
|
49 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
50 |
+
).check(dtypes=[int, float, float])
|
51 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
52 |
+
FuncData(
|
53 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
54 |
+
).check(dtypes=[int, float, float])
|
55 |
+
|
56 |
+
def test_x_equals_0point5(self):
|
57 |
+
dataset = [(1, 0.5, 0.5),
|
58 |
+
(2, 0.5, 0.25),
|
59 |
+
(3, 0.5, 0.166666666667),
|
60 |
+
(4, 0.5, 0.09375),
|
61 |
+
(5, 0.5, 0.056),
|
62 |
+
(6, 0.5, 0.0327932098765),
|
63 |
+
(7, 0.5, 0.0191958707681),
|
64 |
+
(8, 0.5, 0.0112953186035),
|
65 |
+
(9, 0.5, 0.00661933257355),
|
66 |
+
(10, 0.5, 0.003888705)]
|
67 |
+
|
68 |
+
dataset = np.asarray(dataset)
|
69 |
+
FuncData(
|
70 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
71 |
+
).check(dtypes=[int, float, float])
|
72 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
73 |
+
FuncData(
|
74 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
75 |
+
).check(dtypes=[int, float, float])
|
76 |
+
|
77 |
+
def test_n_equals_1(self):
|
78 |
+
x = np.linspace(0, 1, 101, endpoint=True)
|
79 |
+
dataset = np.column_stack([[1]*len(x), x, 1-x])
|
80 |
+
FuncData(
|
81 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
82 |
+
).check(dtypes=[int, float, float])
|
83 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
84 |
+
FuncData(
|
85 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
86 |
+
).check(dtypes=[int, float, float])
|
87 |
+
|
88 |
+
def test_n_equals_2(self):
|
89 |
+
x = np.linspace(0.5, 1, 101, endpoint=True)
|
90 |
+
p = np.power(1-x, 2)
|
91 |
+
n = np.array([2] * len(x))
|
92 |
+
dataset = np.column_stack([n, x, p])
|
93 |
+
FuncData(
|
94 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
95 |
+
).check(dtypes=[int, float, float])
|
96 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
97 |
+
FuncData(
|
98 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
99 |
+
).check(dtypes=[int, float, float])
|
100 |
+
|
101 |
+
def test_n_equals_3(self):
|
102 |
+
x = np.linspace(0.7, 1, 31, endpoint=True)
|
103 |
+
p = np.power(1-x, 3)
|
104 |
+
n = np.array([3] * len(x))
|
105 |
+
dataset = np.column_stack([n, x, p])
|
106 |
+
FuncData(
|
107 |
+
smirnov, dataset, (0, 1), 2, rtol=_rtol
|
108 |
+
).check(dtypes=[int, float, float])
|
109 |
+
dataset[:, -1] = 1 - dataset[:, -1]
|
110 |
+
FuncData(
|
111 |
+
_smirnovc, dataset, (0, 1), 2, rtol=_rtol
|
112 |
+
).check(dtypes=[int, float, float])
|
113 |
+
|
114 |
+
def test_n_large(self):
|
115 |
+
# test for large values of n
|
116 |
+
# Probabilities should go down as n goes up
|
117 |
+
x = 0.4
|
118 |
+
pvals = np.array([smirnov(n, x) for n in range(400, 1100, 20)])
|
119 |
+
dfs = np.diff(pvals)
|
120 |
+
assert_(np.all(dfs <= 0), msg='Not all diffs negative %s' % dfs)
|
121 |
+
|
122 |
+
|
123 |
+
class TestSmirnovi:
|
124 |
+
def test_nan(self):
|
125 |
+
assert_(np.isnan(smirnovi(1, np.nan)))
|
126 |
+
|
127 |
+
def test_basic(self):
|
128 |
+
dataset = [(1, 0.4, 0.6),
|
129 |
+
(1, 0.6, 0.4),
|
130 |
+
(1, 0.99, 0.01),
|
131 |
+
(1, 0.01, 0.99),
|
132 |
+
(2, 0.125 * 0.125, 0.875),
|
133 |
+
(3, 0.125 * 0.125 * 0.125, 0.875),
|
134 |
+
(10, 1.0 / 16 ** 10, 1 - 1.0 / 16)]
|
135 |
+
|
136 |
+
dataset = np.asarray(dataset)
|
137 |
+
FuncData(
|
138 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
139 |
+
).check(dtypes=[int, float, float])
|
140 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
141 |
+
FuncData(
|
142 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
143 |
+
).check(dtypes=[int, float, float])
|
144 |
+
|
145 |
+
def test_x_equals_0(self):
|
146 |
+
dataset = [(n, 0, 1) for n in itertools.chain(range(2, 20), range(1010, 1020))]
|
147 |
+
dataset = np.asarray(dataset)
|
148 |
+
FuncData(
|
149 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
150 |
+
).check(dtypes=[int, float, float])
|
151 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
152 |
+
FuncData(
|
153 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
154 |
+
).check(dtypes=[int, float, float])
|
155 |
+
|
156 |
+
def test_x_equals_1(self):
|
157 |
+
dataset = [(n, 1, 0) for n in itertools.chain(range(2, 20), range(1010, 1020))]
|
158 |
+
dataset = np.asarray(dataset)
|
159 |
+
FuncData(
|
160 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
161 |
+
).check(dtypes=[int, float, float])
|
162 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
163 |
+
FuncData(
|
164 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
165 |
+
).check(dtypes=[int, float, float])
|
166 |
+
|
167 |
+
def test_n_equals_1(self):
|
168 |
+
pp = np.linspace(0, 1, 101, endpoint=True)
|
169 |
+
# dataset = np.array([(1, p, 1-p) for p in pp])
|
170 |
+
dataset = np.column_stack([[1]*len(pp), pp, 1-pp])
|
171 |
+
FuncData(
|
172 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
173 |
+
).check(dtypes=[int, float, float])
|
174 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
175 |
+
FuncData(
|
176 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
177 |
+
).check(dtypes=[int, float, float])
|
178 |
+
|
179 |
+
def test_n_equals_2(self):
|
180 |
+
x = np.linspace(0.5, 1, 101, endpoint=True)
|
181 |
+
p = np.power(1-x, 2)
|
182 |
+
n = np.array([2] * len(x))
|
183 |
+
dataset = np.column_stack([n, p, x])
|
184 |
+
FuncData(
|
185 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
186 |
+
).check(dtypes=[int, float, float])
|
187 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
188 |
+
FuncData(
|
189 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
190 |
+
).check(dtypes=[int, float, float])
|
191 |
+
|
192 |
+
def test_n_equals_3(self):
|
193 |
+
x = np.linspace(0.7, 1, 31, endpoint=True)
|
194 |
+
p = np.power(1-x, 3)
|
195 |
+
n = np.array([3] * len(x))
|
196 |
+
dataset = np.column_stack([n, p, x])
|
197 |
+
FuncData(
|
198 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
199 |
+
).check(dtypes=[int, float, float])
|
200 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
201 |
+
FuncData(
|
202 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
203 |
+
).check(dtypes=[int, float, float])
|
204 |
+
|
205 |
+
def test_round_trip(self):
|
206 |
+
def _sm_smi(n, p):
|
207 |
+
return smirnov(n, smirnovi(n, p))
|
208 |
+
|
209 |
+
def _smc_smci(n, p):
|
210 |
+
return _smirnovc(n, _smirnovci(n, p))
|
211 |
+
|
212 |
+
dataset = [(1, 0.4, 0.4),
|
213 |
+
(1, 0.6, 0.6),
|
214 |
+
(2, 0.875, 0.875),
|
215 |
+
(3, 0.875, 0.875),
|
216 |
+
(3, 0.125, 0.125),
|
217 |
+
(10, 0.999, 0.999),
|
218 |
+
(10, 0.0001, 0.0001)]
|
219 |
+
|
220 |
+
dataset = np.asarray(dataset)
|
221 |
+
FuncData(
|
222 |
+
_sm_smi, dataset, (0, 1), 2, rtol=_rtol
|
223 |
+
).check(dtypes=[int, float, float])
|
224 |
+
FuncData(
|
225 |
+
_smc_smci, dataset, (0, 1), 2, rtol=_rtol
|
226 |
+
).check(dtypes=[int, float, float])
|
227 |
+
|
228 |
+
def test_x_equals_0point5(self):
|
229 |
+
dataset = [(1, 0.5, 0.5),
|
230 |
+
(2, 0.5, 0.366025403784),
|
231 |
+
(2, 0.25, 0.5),
|
232 |
+
(3, 0.5, 0.297156508177),
|
233 |
+
(4, 0.5, 0.255520481121),
|
234 |
+
(5, 0.5, 0.234559536069),
|
235 |
+
(6, 0.5, 0.21715965898),
|
236 |
+
(7, 0.5, 0.202722580034),
|
237 |
+
(8, 0.5, 0.190621765256),
|
238 |
+
(9, 0.5, 0.180363501362),
|
239 |
+
(10, 0.5, 0.17157867006)]
|
240 |
+
|
241 |
+
dataset = np.asarray(dataset)
|
242 |
+
FuncData(
|
243 |
+
smirnovi, dataset, (0, 1), 2, rtol=_rtol
|
244 |
+
).check(dtypes=[int, float, float])
|
245 |
+
dataset[:, 1] = 1 - dataset[:, 1]
|
246 |
+
FuncData(
|
247 |
+
_smirnovci, dataset, (0, 1), 2, rtol=_rtol
|
248 |
+
).check(dtypes=[int, float, float])
|
249 |
+
|
250 |
+
|
251 |
+
class TestSmirnovp:
|
252 |
+
def test_nan(self):
|
253 |
+
assert_(np.isnan(_smirnovp(1, np.nan)))
|
254 |
+
|
255 |
+
def test_basic(self):
|
256 |
+
# Check derivative at endpoints
|
257 |
+
n1_10 = np.arange(1, 10)
|
258 |
+
dataset0 = np.column_stack([n1_10,
|
259 |
+
np.full_like(n1_10, 0),
|
260 |
+
np.full_like(n1_10, -1)])
|
261 |
+
FuncData(
|
262 |
+
_smirnovp, dataset0, (0, 1), 2, rtol=_rtol
|
263 |
+
).check(dtypes=[int, float, float])
|
264 |
+
|
265 |
+
n2_10 = np.arange(2, 10)
|
266 |
+
dataset1 = np.column_stack([n2_10,
|
267 |
+
np.full_like(n2_10, 1.0),
|
268 |
+
np.full_like(n2_10, 0)])
|
269 |
+
FuncData(
|
270 |
+
_smirnovp, dataset1, (0, 1), 2, rtol=_rtol
|
271 |
+
).check(dtypes=[int, float, float])
|
272 |
+
|
273 |
+
def test_oneminusoneovern(self):
|
274 |
+
# Check derivative at x=1-1/n
|
275 |
+
n = np.arange(1, 20)
|
276 |
+
x = 1.0/n
|
277 |
+
xm1 = 1-1.0/n
|
278 |
+
pp1 = -n * x**(n-1)
|
279 |
+
pp1 -= (1-np.sign(n-2)**2) * 0.5 # n=2, x=0.5, 1-1/n = 0.5, need to adjust
|
280 |
+
dataset1 = np.column_stack([n, xm1, pp1])
|
281 |
+
FuncData(
|
282 |
+
_smirnovp, dataset1, (0, 1), 2, rtol=_rtol
|
283 |
+
).check(dtypes=[int, float, float])
|
284 |
+
|
285 |
+
def test_oneovertwon(self):
|
286 |
+
# Check derivative at x=1/2n (Discontinuous at x=1/n, so check at x=1/2n)
|
287 |
+
n = np.arange(1, 20)
|
288 |
+
x = 1.0/2/n
|
289 |
+
pp = -(n*x+1) * (1+x)**(n-2)
|
290 |
+
dataset0 = np.column_stack([n, x, pp])
|
291 |
+
FuncData(
|
292 |
+
_smirnovp, dataset0, (0, 1), 2, rtol=_rtol
|
293 |
+
).check(dtypes=[int, float, float])
|
294 |
+
|
295 |
+
def test_oneovern(self):
|
296 |
+
# Check derivative at x=1/n
|
297 |
+
# (Discontinuous at x=1/n, hard to tell if x==1/n, only use n=power of 2)
|
298 |
+
n = 2**np.arange(1, 10)
|
299 |
+
x = 1.0/n
|
300 |
+
pp = -(n*x+1) * (1+x)**(n-2) + 0.5
|
301 |
+
dataset0 = np.column_stack([n, x, pp])
|
302 |
+
FuncData(
|
303 |
+
_smirnovp, dataset0, (0, 1), 2, rtol=_rtol
|
304 |
+
).check(dtypes=[int, float, float])
|
305 |
+
|
306 |
+
@pytest.mark.xfail(sys.maxsize <= 2**32,
|
307 |
+
reason="requires 64-bit platform")
|
308 |
+
def test_oneovernclose(self):
|
309 |
+
# Check derivative at x=1/n
|
310 |
+
# (Discontinuous at x=1/n, test on either side: x=1/n +/- 2epsilon)
|
311 |
+
n = np.arange(3, 20)
|
312 |
+
|
313 |
+
x = 1.0/n - 2*np.finfo(float).eps
|
314 |
+
pp = -(n*x+1) * (1+x)**(n-2)
|
315 |
+
dataset0 = np.column_stack([n, x, pp])
|
316 |
+
FuncData(
|
317 |
+
_smirnovp, dataset0, (0, 1), 2, rtol=_rtol
|
318 |
+
).check(dtypes=[int, float, float])
|
319 |
+
|
320 |
+
x = 1.0/n + 2*np.finfo(float).eps
|
321 |
+
pp = -(n*x+1) * (1+x)**(n-2) + 1
|
322 |
+
dataset1 = np.column_stack([n, x, pp])
|
323 |
+
FuncData(
|
324 |
+
_smirnovp, dataset1, (0, 1), 2, rtol=_rtol
|
325 |
+
).check(dtypes=[int, float, float])
|
326 |
+
|
327 |
+
|
328 |
+
class TestKolmogorov:
|
329 |
+
def test_nan(self):
|
330 |
+
assert_(np.isnan(kolmogorov(np.nan)))
|
331 |
+
|
332 |
+
def test_basic(self):
|
333 |
+
dataset = [(0, 1.0),
|
334 |
+
(0.5, 0.96394524366487511),
|
335 |
+
(0.8275735551899077, 0.5000000000000000),
|
336 |
+
(1, 0.26999967167735456),
|
337 |
+
(2, 0.00067092525577969533)]
|
338 |
+
|
339 |
+
dataset = np.asarray(dataset)
|
340 |
+
FuncData(kolmogorov, dataset, (0,), 1, rtol=_rtol).check()
|
341 |
+
|
342 |
+
def test_linspace(self):
|
343 |
+
x = np.linspace(0, 2.0, 21)
|
344 |
+
dataset = [1.0000000000000000, 1.0000000000000000, 0.9999999999994950,
|
345 |
+
0.9999906941986655, 0.9971923267772983, 0.9639452436648751,
|
346 |
+
0.8642827790506042, 0.7112351950296890, 0.5441424115741981,
|
347 |
+
0.3927307079406543, 0.2699996716773546, 0.1777181926064012,
|
348 |
+
0.1122496666707249, 0.0680922218447664, 0.0396818795381144,
|
349 |
+
0.0222179626165251, 0.0119520432391966, 0.0061774306344441,
|
350 |
+
0.0030676213475797, 0.0014636048371873, 0.0006709252557797]
|
351 |
+
|
352 |
+
dataset_c = [0.0000000000000000, 6.609305242245699e-53, 5.050407338670114e-13,
|
353 |
+
9.305801334566668e-06, 0.0028076732227017, 0.0360547563351249,
|
354 |
+
0.1357172209493958, 0.2887648049703110, 0.4558575884258019,
|
355 |
+
0.6072692920593457, 0.7300003283226455, 0.8222818073935988,
|
356 |
+
0.8877503333292751, 0.9319077781552336, 0.9603181204618857,
|
357 |
+
0.9777820373834749, 0.9880479567608034, 0.9938225693655559,
|
358 |
+
0.9969323786524203, 0.9985363951628127, 0.9993290747442203]
|
359 |
+
|
360 |
+
dataset = np.column_stack([x, dataset])
|
361 |
+
FuncData(kolmogorov, dataset, (0,), 1, rtol=_rtol).check()
|
362 |
+
dataset_c = np.column_stack([x, dataset_c])
|
363 |
+
FuncData(_kolmogc, dataset_c, (0,), 1, rtol=_rtol).check()
|
364 |
+
|
365 |
+
def test_linspacei(self):
|
366 |
+
p = np.linspace(0, 1.0, 21, endpoint=True)
|
367 |
+
dataset = [np.inf, 1.3580986393225507, 1.2238478702170823,
|
368 |
+
1.1379465424937751, 1.0727491749396481, 1.0191847202536859,
|
369 |
+
0.9730633753323726, 0.9320695842357622, 0.8947644549851197,
|
370 |
+
0.8601710725555463, 0.8275735551899077, 0.7964065373291559,
|
371 |
+
0.7661855555617682, 0.7364542888171910, 0.7067326523068980,
|
372 |
+
0.6764476915028201, 0.6448126061663567, 0.6105590999244391,
|
373 |
+
0.5711732651063401, 0.5196103791686224, 0.0000000000000000]
|
374 |
+
|
375 |
+
dataset_c = [0.0000000000000000, 0.5196103791686225, 0.5711732651063401,
|
376 |
+
0.6105590999244391, 0.6448126061663567, 0.6764476915028201,
|
377 |
+
0.7067326523068980, 0.7364542888171910, 0.7661855555617682,
|
378 |
+
0.7964065373291559, 0.8275735551899077, 0.8601710725555463,
|
379 |
+
0.8947644549851196, 0.9320695842357622, 0.9730633753323727,
|
380 |
+
1.0191847202536859, 1.0727491749396481, 1.1379465424937754,
|
381 |
+
1.2238478702170825, 1.3580986393225509, np.inf]
|
382 |
+
|
383 |
+
dataset = np.column_stack([p[1:], dataset[1:]])
|
384 |
+
FuncData(kolmogi, dataset, (0,), 1, rtol=_rtol).check()
|
385 |
+
dataset_c = np.column_stack([p[:-1], dataset_c[:-1]])
|
386 |
+
FuncData(_kolmogci, dataset_c, (0,), 1, rtol=_rtol).check()
|
387 |
+
|
388 |
+
def test_smallx(self):
|
389 |
+
epsilon = 0.1 ** np.arange(1, 14)
|
390 |
+
x = np.array([0.571173265106, 0.441027698518, 0.374219690278, 0.331392659217,
|
391 |
+
0.300820537459, 0.277539353999, 0.259023494805, 0.243829561254,
|
392 |
+
0.231063086389, 0.220135543236, 0.210641372041, 0.202290283658,
|
393 |
+
0.19487060742])
|
394 |
+
|
395 |
+
dataset = np.column_stack([x, 1-epsilon])
|
396 |
+
FuncData(kolmogorov, dataset, (0,), 1, rtol=_rtol).check()
|
397 |
+
|
398 |
+
def test_round_trip(self):
|
399 |
+
def _ki_k(_x):
|
400 |
+
return kolmogi(kolmogorov(_x))
|
401 |
+
|
402 |
+
def _kci_kc(_x):
|
403 |
+
return _kolmogci(_kolmogc(_x))
|
404 |
+
|
405 |
+
x = np.linspace(0.0, 2.0, 21, endpoint=True)
|
406 |
+
# Exclude 0.1, 0.2. 0.2 almost makes succeeds, but 0.1 has no chance.
|
407 |
+
x02 = x[(x == 0) | (x > 0.21)]
|
408 |
+
dataset02 = np.column_stack([x02, x02])
|
409 |
+
FuncData(_ki_k, dataset02, (0,), 1, rtol=_rtol).check()
|
410 |
+
|
411 |
+
dataset = np.column_stack([x, x])
|
412 |
+
FuncData(_kci_kc, dataset, (0,), 1, rtol=_rtol).check()
|
413 |
+
|
414 |
+
|
415 |
+
class TestKolmogi:
|
416 |
+
def test_nan(self):
|
417 |
+
assert_(np.isnan(kolmogi(np.nan)))
|
418 |
+
|
419 |
+
def test_basic(self):
|
420 |
+
dataset = [(1.0, 0),
|
421 |
+
(0.96394524366487511, 0.5),
|
422 |
+
(0.9, 0.571173265106),
|
423 |
+
(0.5000000000000000, 0.8275735551899077),
|
424 |
+
(0.26999967167735456, 1),
|
425 |
+
(0.00067092525577969533, 2)]
|
426 |
+
|
427 |
+
dataset = np.asarray(dataset)
|
428 |
+
FuncData(kolmogi, dataset, (0,), 1, rtol=_rtol).check()
|
429 |
+
|
430 |
+
def test_smallpcdf(self):
|
431 |
+
epsilon = 0.5 ** np.arange(1, 55, 3)
|
432 |
+
# kolmogi(1-p) == _kolmogci(p) if 1-(1-p) == p, but not necessarily otherwise
|
433 |
+
# Use epsilon s.t. 1-(1-epsilon)) == epsilon,
|
434 |
+
# so can use same x-array for both results
|
435 |
+
|
436 |
+
x = np.array([0.8275735551899077, 0.5345255069097583, 0.4320114038786941,
|
437 |
+
0.3736868442620478, 0.3345161714909591, 0.3057833329315859,
|
438 |
+
0.2835052890528936, 0.2655578150208676, 0.2506869966107999,
|
439 |
+
0.2380971058736669, 0.2272549289962079, 0.2177876361600040,
|
440 |
+
0.2094254686862041, 0.2019676748836232, 0.1952612948137504,
|
441 |
+
0.1891874239646641, 0.1836520225050326, 0.1785795904846466])
|
442 |
+
|
443 |
+
dataset = np.column_stack([1-epsilon, x])
|
444 |
+
FuncData(kolmogi, dataset, (0,), 1, rtol=_rtol).check()
|
445 |
+
|
446 |
+
dataset = np.column_stack([epsilon, x])
|
447 |
+
FuncData(_kolmogci, dataset, (0,), 1, rtol=_rtol).check()
|
448 |
+
|
449 |
+
def test_smallpsf(self):
|
450 |
+
epsilon = 0.5 ** np.arange(1, 55, 3)
|
451 |
+
# kolmogi(p) == _kolmogci(1-p) if 1-(1-p) == p, but not necessarily otherwise
|
452 |
+
# Use epsilon s.t. 1-(1-epsilon)) == epsilon,
|
453 |
+
# so can use same x-array for both results
|
454 |
+
|
455 |
+
x = np.array([0.8275735551899077, 1.3163786275161036, 1.6651092133663343,
|
456 |
+
1.9525136345289607, 2.2027324540033235, 2.4272929437460848,
|
457 |
+
2.6327688477341593, 2.8233300509220260, 3.0018183401530627,
|
458 |
+
3.1702735084088891, 3.3302184446307912, 3.4828258153113318,
|
459 |
+
3.6290214150152051, 3.7695513262825959, 3.9050272690877326,
|
460 |
+
4.0359582187082550, 4.1627730557884890, 4.2858371743264527])
|
461 |
+
|
462 |
+
dataset = np.column_stack([epsilon, x])
|
463 |
+
FuncData(kolmogi, dataset, (0,), 1, rtol=_rtol).check()
|
464 |
+
|
465 |
+
dataset = np.column_stack([1-epsilon, x])
|
466 |
+
FuncData(_kolmogci, dataset, (0,), 1, rtol=_rtol).check()
|
467 |
+
|
468 |
+
def test_round_trip(self):
|
469 |
+
def _k_ki(_p):
|
470 |
+
return kolmogorov(kolmogi(_p))
|
471 |
+
|
472 |
+
p = np.linspace(0.1, 1.0, 10, endpoint=True)
|
473 |
+
dataset = np.column_stack([p, p])
|
474 |
+
FuncData(_k_ki, dataset, (0,), 1, rtol=_rtol).check()
|
475 |
+
|
476 |
+
|
477 |
+
class TestKolmogp:
|
478 |
+
def test_nan(self):
|
479 |
+
assert_(np.isnan(_kolmogp(np.nan)))
|
480 |
+
|
481 |
+
def test_basic(self):
|
482 |
+
dataset = [(0.000000, -0.0),
|
483 |
+
(0.200000, -1.532420541338916e-10),
|
484 |
+
(0.400000, -0.1012254419260496),
|
485 |
+
(0.600000, -1.324123244249925),
|
486 |
+
(0.800000, -1.627024345636592),
|
487 |
+
(1.000000, -1.071948558356941),
|
488 |
+
(1.200000, -0.538512430720529),
|
489 |
+
(1.400000, -0.2222133182429472),
|
490 |
+
(1.600000, -0.07649302775520538),
|
491 |
+
(1.800000, -0.02208687346347873),
|
492 |
+
(2.000000, -0.005367402045629683)]
|
493 |
+
|
494 |
+
dataset = np.asarray(dataset)
|
495 |
+
FuncData(_kolmogp, dataset, (0,), 1, rtol=_rtol).check()
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_lambertw.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
# Tests for the lambertw function,
|
3 |
+
# Adapted from the MPMath tests [1] by Yosef Meller, [email protected]
|
4 |
+
# Distributed under the same license as SciPy itself.
|
5 |
+
#
|
6 |
+
# [1] mpmath source code, Subversion revision 992
|
7 |
+
# http://code.google.com/p/mpmath/source/browse/trunk/mpmath/tests/test_functions2.py?spec=svn994&r=992
|
8 |
+
|
9 |
+
import pytest
|
10 |
+
import numpy as np
|
11 |
+
from numpy.testing import assert_, assert_equal, assert_array_almost_equal
|
12 |
+
from scipy.special import lambertw
|
13 |
+
from numpy import nan, inf, pi, e, isnan, log, r_, array, complex128
|
14 |
+
|
15 |
+
from scipy.special._testutils import FuncData
|
16 |
+
|
17 |
+
|
18 |
+
def test_values():
|
19 |
+
assert_(isnan(lambertw(nan)))
|
20 |
+
assert_equal(lambertw(inf,1).real, inf)
|
21 |
+
assert_equal(lambertw(inf,1).imag, 2*pi)
|
22 |
+
assert_equal(lambertw(-inf,1).real, inf)
|
23 |
+
assert_equal(lambertw(-inf,1).imag, 3*pi)
|
24 |
+
|
25 |
+
assert_equal(lambertw(1.), lambertw(1., 0))
|
26 |
+
|
27 |
+
data = [
|
28 |
+
(0,0, 0),
|
29 |
+
(0+0j,0, 0),
|
30 |
+
(inf,0, inf),
|
31 |
+
(0,-1, -inf),
|
32 |
+
(0,1, -inf),
|
33 |
+
(0,3, -inf),
|
34 |
+
(e,0, 1),
|
35 |
+
(1,0, 0.567143290409783873),
|
36 |
+
(-pi/2,0, 1j*pi/2),
|
37 |
+
(-log(2)/2,0, -log(2)),
|
38 |
+
(0.25,0, 0.203888354702240164),
|
39 |
+
(-0.25,0, -0.357402956181388903),
|
40 |
+
(-1./10000,0, -0.000100010001500266719),
|
41 |
+
(-0.25,-1, -2.15329236411034965),
|
42 |
+
(0.25,-1, -3.00899800997004620-4.07652978899159763j),
|
43 |
+
(-0.25,-1, -2.15329236411034965),
|
44 |
+
(0.25,1, -3.00899800997004620+4.07652978899159763j),
|
45 |
+
(-0.25,1, -3.48973228422959210+7.41405453009603664j),
|
46 |
+
(-4,0, 0.67881197132094523+1.91195078174339937j),
|
47 |
+
(-4,1, -0.66743107129800988+7.76827456802783084j),
|
48 |
+
(-4,-1, 0.67881197132094523-1.91195078174339937j),
|
49 |
+
(1000,0, 5.24960285240159623),
|
50 |
+
(1000,1, 4.91492239981054535+5.44652615979447070j),
|
51 |
+
(1000,-1, 4.91492239981054535-5.44652615979447070j),
|
52 |
+
(1000,5, 3.5010625305312892+29.9614548941181328j),
|
53 |
+
(3+4j,0, 1.281561806123775878+0.533095222020971071j),
|
54 |
+
(-0.4+0.4j,0, -0.10396515323290657+0.61899273315171632j),
|
55 |
+
(3+4j,1, -0.11691092896595324+5.61888039871282334j),
|
56 |
+
(3+4j,-1, 0.25856740686699742-3.85211668616143559j),
|
57 |
+
(-0.5,-1, -0.794023632344689368-0.770111750510379110j),
|
58 |
+
(-1./10000,1, -11.82350837248724344+6.80546081842002101j),
|
59 |
+
(-1./10000,-1, -11.6671145325663544),
|
60 |
+
(-1./10000,-2, -11.82350837248724344-6.80546081842002101j),
|
61 |
+
(-1./100000,4, -14.9186890769540539+26.1856750178782046j),
|
62 |
+
(-1./100000,5, -15.0931437726379218666+32.5525721210262290086j),
|
63 |
+
((2+1j)/10,0, 0.173704503762911669+0.071781336752835511j),
|
64 |
+
((2+1j)/10,1, -3.21746028349820063+4.56175438896292539j),
|
65 |
+
((2+1j)/10,-1, -3.03781405002993088-3.53946629633505737j),
|
66 |
+
((2+1j)/10,4, -4.6878509692773249+23.8313630697683291j),
|
67 |
+
(-(2+1j)/10,0, -0.226933772515757933-0.164986470020154580j),
|
68 |
+
(-(2+1j)/10,1, -2.43569517046110001+0.76974067544756289j),
|
69 |
+
(-(2+1j)/10,-1, -3.54858738151989450-6.91627921869943589j),
|
70 |
+
(-(2+1j)/10,4, -4.5500846928118151+20.6672982215434637j),
|
71 |
+
(pi,0, 1.073658194796149172092178407024821347547745350410314531),
|
72 |
+
|
73 |
+
# Former bug in generated branch,
|
74 |
+
(-0.5+0.002j,0, -0.78917138132659918344 + 0.76743539379990327749j),
|
75 |
+
(-0.5-0.002j,0, -0.78917138132659918344 - 0.76743539379990327749j),
|
76 |
+
(-0.448+0.4j,0, -0.11855133765652382241 + 0.66570534313583423116j),
|
77 |
+
(-0.448-0.4j,0, -0.11855133765652382241 - 0.66570534313583423116j),
|
78 |
+
]
|
79 |
+
data = array(data, dtype=complex128)
|
80 |
+
|
81 |
+
def w(x, y):
|
82 |
+
return lambertw(x, y.real.astype(int))
|
83 |
+
with np.errstate(all='ignore'):
|
84 |
+
FuncData(w, data, (0,1), 2, rtol=1e-10, atol=1e-13).check()
|
85 |
+
|
86 |
+
|
87 |
+
def test_ufunc():
|
88 |
+
assert_array_almost_equal(
|
89 |
+
lambertw(r_[0., e, 1.]), r_[0., 1., 0.567143290409783873])
|
90 |
+
|
91 |
+
|
92 |
+
def test_lambertw_ufunc_loop_selection():
|
93 |
+
# see https://github.com/scipy/scipy/issues/4895
|
94 |
+
dt = np.dtype(np.complex128)
|
95 |
+
assert_equal(lambertw(0, 0, 0).dtype, dt)
|
96 |
+
assert_equal(lambertw([0], 0, 0).dtype, dt)
|
97 |
+
assert_equal(lambertw(0, [0], 0).dtype, dt)
|
98 |
+
assert_equal(lambertw(0, 0, [0]).dtype, dt)
|
99 |
+
assert_equal(lambertw([0], [0], [0]).dtype, dt)
|
100 |
+
|
101 |
+
|
102 |
+
@pytest.mark.parametrize('z', [1e-316, -2e-320j, -5e-318+1e-320j])
|
103 |
+
def test_lambertw_subnormal_k0(z):
|
104 |
+
# Verify that subnormal inputs are handled correctly on
|
105 |
+
# the branch k=0 (regression test for gh-16291).
|
106 |
+
w = lambertw(z)
|
107 |
+
# For values this small, we can be sure that numerically,
|
108 |
+
# lambertw(z) is z.
|
109 |
+
assert w == z
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_log_softmax.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import assert_allclose
|
3 |
+
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import scipy.special as sc
|
7 |
+
|
8 |
+
|
9 |
+
@pytest.mark.parametrize('x, expected', [
|
10 |
+
(np.array([1000, 1]), np.array([0, -999])),
|
11 |
+
|
12 |
+
# Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
|
13 |
+
# converted to float.
|
14 |
+
(np.arange(4), np.array([-3.4401896985611953,
|
15 |
+
-2.4401896985611953,
|
16 |
+
-1.4401896985611953,
|
17 |
+
-0.44018969856119533]))
|
18 |
+
])
|
19 |
+
def test_log_softmax(x, expected):
|
20 |
+
assert_allclose(sc.log_softmax(x), expected, rtol=1e-13)
|
21 |
+
|
22 |
+
|
23 |
+
@pytest.fixture
|
24 |
+
def log_softmax_x():
|
25 |
+
x = np.arange(4)
|
26 |
+
return x
|
27 |
+
|
28 |
+
|
29 |
+
@pytest.fixture
|
30 |
+
def log_softmax_expected():
|
31 |
+
# Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
|
32 |
+
# converted to float.
|
33 |
+
expected = np.array([-3.4401896985611953,
|
34 |
+
-2.4401896985611953,
|
35 |
+
-1.4401896985611953,
|
36 |
+
-0.44018969856119533])
|
37 |
+
return expected
|
38 |
+
|
39 |
+
|
40 |
+
def test_log_softmax_translation(log_softmax_x, log_softmax_expected):
|
41 |
+
# Translation property. If all the values are changed by the same amount,
|
42 |
+
# the softmax result does not change.
|
43 |
+
x = log_softmax_x + 100
|
44 |
+
expected = log_softmax_expected
|
45 |
+
assert_allclose(sc.log_softmax(x), expected, rtol=1e-13)
|
46 |
+
|
47 |
+
|
48 |
+
def test_log_softmax_noneaxis(log_softmax_x, log_softmax_expected):
|
49 |
+
# When axis=None, softmax operates on the entire array, and preserves
|
50 |
+
# the shape.
|
51 |
+
x = log_softmax_x.reshape(2, 2)
|
52 |
+
expected = log_softmax_expected.reshape(2, 2)
|
53 |
+
assert_allclose(sc.log_softmax(x), expected, rtol=1e-13)
|
54 |
+
|
55 |
+
|
56 |
+
@pytest.mark.parametrize('axis_2d, expected_2d', [
|
57 |
+
(0, np.log(0.5) * np.ones((2, 2))),
|
58 |
+
(1, np.array([[0, -999], [0, -999]]))
|
59 |
+
])
|
60 |
+
def test_axes(axis_2d, expected_2d):
|
61 |
+
assert_allclose(
|
62 |
+
sc.log_softmax([[1000, 1], [1000, 1]], axis=axis_2d),
|
63 |
+
expected_2d,
|
64 |
+
rtol=1e-13,
|
65 |
+
)
|
66 |
+
|
67 |
+
|
68 |
+
@pytest.fixture
|
69 |
+
def log_softmax_2d_x():
|
70 |
+
x = np.arange(8).reshape(2, 4)
|
71 |
+
return x
|
72 |
+
|
73 |
+
|
74 |
+
@pytest.fixture
|
75 |
+
def log_softmax_2d_expected():
|
76 |
+
# Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
|
77 |
+
# converted to float.
|
78 |
+
expected = np.array([[-3.4401896985611953,
|
79 |
+
-2.4401896985611953,
|
80 |
+
-1.4401896985611953,
|
81 |
+
-0.44018969856119533],
|
82 |
+
[-3.4401896985611953,
|
83 |
+
-2.4401896985611953,
|
84 |
+
-1.4401896985611953,
|
85 |
+
-0.44018969856119533]])
|
86 |
+
return expected
|
87 |
+
|
88 |
+
|
89 |
+
def test_log_softmax_2d_axis1(log_softmax_2d_x, log_softmax_2d_expected):
|
90 |
+
x = log_softmax_2d_x
|
91 |
+
expected = log_softmax_2d_expected
|
92 |
+
assert_allclose(sc.log_softmax(x, axis=1), expected, rtol=1e-13)
|
93 |
+
|
94 |
+
|
95 |
+
def test_log_softmax_2d_axis0(log_softmax_2d_x, log_softmax_2d_expected):
|
96 |
+
x = log_softmax_2d_x.T
|
97 |
+
expected = log_softmax_2d_expected.T
|
98 |
+
assert_allclose(sc.log_softmax(x, axis=0), expected, rtol=1e-13)
|
99 |
+
|
100 |
+
|
101 |
+
def test_log_softmax_3d(log_softmax_2d_x, log_softmax_2d_expected):
|
102 |
+
# 3-d input, with a tuple for the axis.
|
103 |
+
x_3d = log_softmax_2d_x.reshape(2, 2, 2)
|
104 |
+
expected_3d = log_softmax_2d_expected.reshape(2, 2, 2)
|
105 |
+
assert_allclose(sc.log_softmax(x_3d, axis=(1, 2)), expected_3d, rtol=1e-13)
|
106 |
+
|
107 |
+
|
108 |
+
def test_log_softmax_scalar():
|
109 |
+
assert_allclose(sc.log_softmax(1.0), 0.0, rtol=1e-13)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_loggamma.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import assert_allclose, assert_
|
3 |
+
|
4 |
+
from scipy.special._testutils import FuncData
|
5 |
+
from scipy.special import gamma, gammaln, loggamma
|
6 |
+
|
7 |
+
|
8 |
+
def test_identities1():
|
9 |
+
# test the identity exp(loggamma(z)) = gamma(z)
|
10 |
+
x = np.array([-99.5, -9.5, -0.5, 0.5, 9.5, 99.5])
|
11 |
+
y = x.copy()
|
12 |
+
x, y = np.meshgrid(x, y)
|
13 |
+
z = (x + 1J*y).flatten()
|
14 |
+
dataset = np.vstack((z, gamma(z))).T
|
15 |
+
|
16 |
+
def f(z):
|
17 |
+
return np.exp(loggamma(z))
|
18 |
+
|
19 |
+
FuncData(f, dataset, 0, 1, rtol=1e-14, atol=1e-14).check()
|
20 |
+
|
21 |
+
|
22 |
+
def test_identities2():
|
23 |
+
# test the identity loggamma(z + 1) = log(z) + loggamma(z)
|
24 |
+
x = np.array([-99.5, -9.5, -0.5, 0.5, 9.5, 99.5])
|
25 |
+
y = x.copy()
|
26 |
+
x, y = np.meshgrid(x, y)
|
27 |
+
z = (x + 1J*y).flatten()
|
28 |
+
dataset = np.vstack((z, np.log(z) + loggamma(z))).T
|
29 |
+
|
30 |
+
def f(z):
|
31 |
+
return loggamma(z + 1)
|
32 |
+
|
33 |
+
FuncData(f, dataset, 0, 1, rtol=1e-14, atol=1e-14).check()
|
34 |
+
|
35 |
+
|
36 |
+
def test_complex_dispatch_realpart():
|
37 |
+
# Test that the real parts of loggamma and gammaln agree on the
|
38 |
+
# real axis.
|
39 |
+
x = np.r_[-np.logspace(10, -10), np.logspace(-10, 10)] + 0.5
|
40 |
+
|
41 |
+
dataset = np.vstack((x, gammaln(x))).T
|
42 |
+
|
43 |
+
def f(z):
|
44 |
+
z = np.array(z, dtype='complex128')
|
45 |
+
return loggamma(z).real
|
46 |
+
|
47 |
+
FuncData(f, dataset, 0, 1, rtol=1e-14, atol=1e-14).check()
|
48 |
+
|
49 |
+
|
50 |
+
def test_real_dispatch():
|
51 |
+
x = np.logspace(-10, 10) + 0.5
|
52 |
+
dataset = np.vstack((x, gammaln(x))).T
|
53 |
+
|
54 |
+
FuncData(loggamma, dataset, 0, 1, rtol=1e-14, atol=1e-14).check()
|
55 |
+
assert_(loggamma(0) == np.inf)
|
56 |
+
assert_(np.isnan(loggamma(-1)))
|
57 |
+
|
58 |
+
|
59 |
+
def test_gh_6536():
|
60 |
+
z = loggamma(complex(-3.4, +0.0))
|
61 |
+
zbar = loggamma(complex(-3.4, -0.0))
|
62 |
+
assert_allclose(z, zbar.conjugate(), rtol=1e-15, atol=0)
|
63 |
+
|
64 |
+
|
65 |
+
def test_branch_cut():
|
66 |
+
# Make sure negative zero is treated correctly
|
67 |
+
x = -np.logspace(300, -30, 100)
|
68 |
+
z = np.asarray([complex(x0, 0.0) for x0 in x])
|
69 |
+
zbar = np.asarray([complex(x0, -0.0) for x0 in x])
|
70 |
+
assert_allclose(z, zbar.conjugate(), rtol=1e-15, atol=0)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_logit.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import (assert_equal, assert_almost_equal,
|
3 |
+
assert_allclose)
|
4 |
+
from scipy.special import logit, expit, log_expit
|
5 |
+
|
6 |
+
|
7 |
+
class TestLogit:
|
8 |
+
def check_logit_out(self, dtype, expected):
|
9 |
+
a = np.linspace(0, 1, 10)
|
10 |
+
a = np.array(a, dtype=dtype)
|
11 |
+
with np.errstate(divide='ignore'):
|
12 |
+
actual = logit(a)
|
13 |
+
|
14 |
+
assert_almost_equal(actual, expected)
|
15 |
+
|
16 |
+
assert_equal(actual.dtype, np.dtype(dtype))
|
17 |
+
|
18 |
+
def test_float32(self):
|
19 |
+
expected = np.array([-np.inf, -2.07944155,
|
20 |
+
-1.25276291, -0.69314718,
|
21 |
+
-0.22314353, 0.22314365,
|
22 |
+
0.6931473, 1.25276303,
|
23 |
+
2.07944155, np.inf], dtype=np.float32)
|
24 |
+
self.check_logit_out('f4', expected)
|
25 |
+
|
26 |
+
def test_float64(self):
|
27 |
+
expected = np.array([-np.inf, -2.07944154,
|
28 |
+
-1.25276297, -0.69314718,
|
29 |
+
-0.22314355, 0.22314355,
|
30 |
+
0.69314718, 1.25276297,
|
31 |
+
2.07944154, np.inf])
|
32 |
+
self.check_logit_out('f8', expected)
|
33 |
+
|
34 |
+
def test_nan(self):
|
35 |
+
expected = np.array([np.nan]*4)
|
36 |
+
with np.errstate(invalid='ignore'):
|
37 |
+
actual = logit(np.array([-3., -2., 2., 3.]))
|
38 |
+
|
39 |
+
assert_equal(expected, actual)
|
40 |
+
|
41 |
+
|
42 |
+
class TestExpit:
|
43 |
+
def check_expit_out(self, dtype, expected):
|
44 |
+
a = np.linspace(-4, 4, 10)
|
45 |
+
a = np.array(a, dtype=dtype)
|
46 |
+
actual = expit(a)
|
47 |
+
assert_almost_equal(actual, expected)
|
48 |
+
assert_equal(actual.dtype, np.dtype(dtype))
|
49 |
+
|
50 |
+
def test_float32(self):
|
51 |
+
expected = np.array([0.01798621, 0.04265125,
|
52 |
+
0.09777259, 0.20860852,
|
53 |
+
0.39068246, 0.60931754,
|
54 |
+
0.79139149, 0.9022274,
|
55 |
+
0.95734876, 0.98201376], dtype=np.float32)
|
56 |
+
self.check_expit_out('f4', expected)
|
57 |
+
|
58 |
+
def test_float64(self):
|
59 |
+
expected = np.array([0.01798621, 0.04265125,
|
60 |
+
0.0977726, 0.20860853,
|
61 |
+
0.39068246, 0.60931754,
|
62 |
+
0.79139147, 0.9022274,
|
63 |
+
0.95734875, 0.98201379])
|
64 |
+
self.check_expit_out('f8', expected)
|
65 |
+
|
66 |
+
def test_large(self):
|
67 |
+
for dtype in (np.float32, np.float64, np.longdouble):
|
68 |
+
for n in (88, 89, 709, 710, 11356, 11357):
|
69 |
+
n = np.array(n, dtype=dtype)
|
70 |
+
assert_allclose(expit(n), 1.0, atol=1e-20)
|
71 |
+
assert_allclose(expit(-n), 0.0, atol=1e-20)
|
72 |
+
assert_equal(expit(n).dtype, dtype)
|
73 |
+
assert_equal(expit(-n).dtype, dtype)
|
74 |
+
|
75 |
+
|
76 |
+
class TestLogExpit:
|
77 |
+
|
78 |
+
def test_large_negative(self):
|
79 |
+
x = np.array([-10000.0, -750.0, -500.0, -35.0])
|
80 |
+
y = log_expit(x)
|
81 |
+
assert_equal(y, x)
|
82 |
+
|
83 |
+
def test_large_positive(self):
|
84 |
+
x = np.array([750.0, 1000.0, 10000.0])
|
85 |
+
y = log_expit(x)
|
86 |
+
# y will contain -0.0, and -0.0 is used in the expected value,
|
87 |
+
# but assert_equal does not check the sign of zeros, and I don't
|
88 |
+
# think the sign is an essential part of the test (i.e. it would
|
89 |
+
# probably be OK if log_expit(1000) returned 0.0 instead of -0.0).
|
90 |
+
assert_equal(y, np.array([-0.0, -0.0, -0.0]))
|
91 |
+
|
92 |
+
def test_basic_float64(self):
|
93 |
+
x = np.array([-32, -20, -10, -3, -1, -0.1, -1e-9,
|
94 |
+
0, 1e-9, 0.1, 1, 10, 100, 500, 710, 725, 735])
|
95 |
+
y = log_expit(x)
|
96 |
+
#
|
97 |
+
# Expected values were computed with mpmath:
|
98 |
+
#
|
99 |
+
# import mpmath
|
100 |
+
#
|
101 |
+
# mpmath.mp.dps = 100
|
102 |
+
#
|
103 |
+
# def mp_log_expit(x):
|
104 |
+
# return -mpmath.log1p(mpmath.exp(-x))
|
105 |
+
#
|
106 |
+
# expected = [float(mp_log_expit(t)) for t in x]
|
107 |
+
#
|
108 |
+
expected = [-32.000000000000014, -20.000000002061153,
|
109 |
+
-10.000045398899218, -3.048587351573742,
|
110 |
+
-1.3132616875182228, -0.7443966600735709,
|
111 |
+
-0.6931471810599453, -0.6931471805599453,
|
112 |
+
-0.6931471800599454, -0.6443966600735709,
|
113 |
+
-0.3132616875182228, -4.539889921686465e-05,
|
114 |
+
-3.720075976020836e-44, -7.124576406741286e-218,
|
115 |
+
-4.47628622567513e-309, -1.36930634e-315,
|
116 |
+
-6.217e-320]
|
117 |
+
|
118 |
+
# When tested locally, only one value in y was not exactly equal to
|
119 |
+
# expected. That was for x=1, and the y value differed from the
|
120 |
+
# expected by 1 ULP. For this test, however, I'll use rtol=1e-15.
|
121 |
+
assert_allclose(y, expected, rtol=1e-15)
|
122 |
+
|
123 |
+
def test_basic_float32(self):
|
124 |
+
x = np.array([-32, -20, -10, -3, -1, -0.1, -1e-9,
|
125 |
+
0, 1e-9, 0.1, 1, 10, 100], dtype=np.float32)
|
126 |
+
y = log_expit(x)
|
127 |
+
#
|
128 |
+
# Expected values were computed with mpmath:
|
129 |
+
#
|
130 |
+
# import mpmath
|
131 |
+
#
|
132 |
+
# mpmath.mp.dps = 100
|
133 |
+
#
|
134 |
+
# def mp_log_expit(x):
|
135 |
+
# return -mpmath.log1p(mpmath.exp(-x))
|
136 |
+
#
|
137 |
+
# expected = [np.float32(mp_log_expit(t)) for t in x]
|
138 |
+
#
|
139 |
+
expected = np.array([-32.0, -20.0, -10.000046, -3.0485873,
|
140 |
+
-1.3132616, -0.7443967, -0.6931472,
|
141 |
+
-0.6931472, -0.6931472, -0.64439666,
|
142 |
+
-0.3132617, -4.5398898e-05, -3.8e-44],
|
143 |
+
dtype=np.float32)
|
144 |
+
|
145 |
+
assert_allclose(y, expected, rtol=5e-7)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_logsumexp.py
ADDED
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.testing import (assert_almost_equal, assert_equal, assert_allclose,
|
3 |
+
assert_array_almost_equal, assert_)
|
4 |
+
|
5 |
+
from scipy.special import logsumexp, softmax
|
6 |
+
|
7 |
+
|
8 |
+
def test_logsumexp():
|
9 |
+
# Test whether logsumexp() function correctly handles large inputs.
|
10 |
+
a = np.arange(200)
|
11 |
+
desired = np.log(np.sum(np.exp(a)))
|
12 |
+
assert_almost_equal(logsumexp(a), desired)
|
13 |
+
|
14 |
+
# Now test with large numbers
|
15 |
+
b = [1000, 1000]
|
16 |
+
desired = 1000.0 + np.log(2.0)
|
17 |
+
assert_almost_equal(logsumexp(b), desired)
|
18 |
+
|
19 |
+
n = 1000
|
20 |
+
b = np.full(n, 10000, dtype='float64')
|
21 |
+
desired = 10000.0 + np.log(n)
|
22 |
+
assert_almost_equal(logsumexp(b), desired)
|
23 |
+
|
24 |
+
x = np.array([1e-40] * 1000000)
|
25 |
+
logx = np.log(x)
|
26 |
+
|
27 |
+
X = np.vstack([x, x])
|
28 |
+
logX = np.vstack([logx, logx])
|
29 |
+
assert_array_almost_equal(np.exp(logsumexp(logX)), X.sum())
|
30 |
+
assert_array_almost_equal(np.exp(logsumexp(logX, axis=0)), X.sum(axis=0))
|
31 |
+
assert_array_almost_equal(np.exp(logsumexp(logX, axis=1)), X.sum(axis=1))
|
32 |
+
|
33 |
+
# Handling special values properly
|
34 |
+
assert_equal(logsumexp(np.inf), np.inf)
|
35 |
+
assert_equal(logsumexp(-np.inf), -np.inf)
|
36 |
+
assert_equal(logsumexp(np.nan), np.nan)
|
37 |
+
assert_equal(logsumexp([-np.inf, -np.inf]), -np.inf)
|
38 |
+
|
39 |
+
# Handling an array with different magnitudes on the axes
|
40 |
+
assert_array_almost_equal(logsumexp([[1e10, 1e-10],
|
41 |
+
[-1e10, -np.inf]], axis=-1),
|
42 |
+
[1e10, -1e10])
|
43 |
+
|
44 |
+
# Test keeping dimensions
|
45 |
+
assert_array_almost_equal(logsumexp([[1e10, 1e-10],
|
46 |
+
[-1e10, -np.inf]],
|
47 |
+
axis=-1,
|
48 |
+
keepdims=True),
|
49 |
+
[[1e10], [-1e10]])
|
50 |
+
|
51 |
+
# Test multiple axes
|
52 |
+
assert_array_almost_equal(logsumexp([[1e10, 1e-10],
|
53 |
+
[-1e10, -np.inf]],
|
54 |
+
axis=(-1,-2)),
|
55 |
+
1e10)
|
56 |
+
|
57 |
+
|
58 |
+
def test_logsumexp_b():
|
59 |
+
a = np.arange(200)
|
60 |
+
b = np.arange(200, 0, -1)
|
61 |
+
desired = np.log(np.sum(b*np.exp(a)))
|
62 |
+
assert_almost_equal(logsumexp(a, b=b), desired)
|
63 |
+
|
64 |
+
a = [1000, 1000]
|
65 |
+
b = [1.2, 1.2]
|
66 |
+
desired = 1000 + np.log(2 * 1.2)
|
67 |
+
assert_almost_equal(logsumexp(a, b=b), desired)
|
68 |
+
|
69 |
+
x = np.array([1e-40] * 100000)
|
70 |
+
b = np.linspace(1, 1000, 100000)
|
71 |
+
logx = np.log(x)
|
72 |
+
|
73 |
+
X = np.vstack((x, x))
|
74 |
+
logX = np.vstack((logx, logx))
|
75 |
+
B = np.vstack((b, b))
|
76 |
+
assert_array_almost_equal(np.exp(logsumexp(logX, b=B)), (B * X).sum())
|
77 |
+
assert_array_almost_equal(np.exp(logsumexp(logX, b=B, axis=0)),
|
78 |
+
(B * X).sum(axis=0))
|
79 |
+
assert_array_almost_equal(np.exp(logsumexp(logX, b=B, axis=1)),
|
80 |
+
(B * X).sum(axis=1))
|
81 |
+
|
82 |
+
|
83 |
+
def test_logsumexp_sign():
|
84 |
+
a = [1,1,1]
|
85 |
+
b = [1,-1,-1]
|
86 |
+
|
87 |
+
r, s = logsumexp(a, b=b, return_sign=True)
|
88 |
+
assert_almost_equal(r,1)
|
89 |
+
assert_equal(s,-1)
|
90 |
+
|
91 |
+
|
92 |
+
def test_logsumexp_sign_zero():
|
93 |
+
a = [1,1]
|
94 |
+
b = [1,-1]
|
95 |
+
|
96 |
+
r, s = logsumexp(a, b=b, return_sign=True)
|
97 |
+
assert_(not np.isfinite(r))
|
98 |
+
assert_(not np.isnan(r))
|
99 |
+
assert_(r < 0)
|
100 |
+
assert_equal(s,0)
|
101 |
+
|
102 |
+
|
103 |
+
def test_logsumexp_sign_shape():
|
104 |
+
a = np.ones((1,2,3,4))
|
105 |
+
b = np.ones_like(a)
|
106 |
+
|
107 |
+
r, s = logsumexp(a, axis=2, b=b, return_sign=True)
|
108 |
+
|
109 |
+
assert_equal(r.shape, s.shape)
|
110 |
+
assert_equal(r.shape, (1,2,4))
|
111 |
+
|
112 |
+
r, s = logsumexp(a, axis=(1,3), b=b, return_sign=True)
|
113 |
+
|
114 |
+
assert_equal(r.shape, s.shape)
|
115 |
+
assert_equal(r.shape, (1,3))
|
116 |
+
|
117 |
+
|
118 |
+
def test_logsumexp_complex_sign():
|
119 |
+
a = np.array([1 + 1j, 2 - 1j, -2 + 3j])
|
120 |
+
|
121 |
+
r, s = logsumexp(a, return_sign=True)
|
122 |
+
|
123 |
+
expected_sumexp = np.exp(a).sum()
|
124 |
+
# This is the numpy>=2.0 convention for np.sign
|
125 |
+
expected_sign = expected_sumexp / abs(expected_sumexp)
|
126 |
+
|
127 |
+
assert_allclose(s, expected_sign)
|
128 |
+
assert_allclose(s * np.exp(r), expected_sumexp)
|
129 |
+
|
130 |
+
|
131 |
+
def test_logsumexp_shape():
|
132 |
+
a = np.ones((1, 2, 3, 4))
|
133 |
+
b = np.ones_like(a)
|
134 |
+
|
135 |
+
r = logsumexp(a, axis=2, b=b)
|
136 |
+
assert_equal(r.shape, (1, 2, 4))
|
137 |
+
|
138 |
+
r = logsumexp(a, axis=(1, 3), b=b)
|
139 |
+
assert_equal(r.shape, (1, 3))
|
140 |
+
|
141 |
+
|
142 |
+
def test_logsumexp_b_zero():
|
143 |
+
a = [1,10000]
|
144 |
+
b = [1,0]
|
145 |
+
|
146 |
+
assert_almost_equal(logsumexp(a, b=b), 1)
|
147 |
+
|
148 |
+
|
149 |
+
def test_logsumexp_b_shape():
|
150 |
+
a = np.zeros((4,1,2,1))
|
151 |
+
b = np.ones((3,1,5))
|
152 |
+
|
153 |
+
logsumexp(a, b=b)
|
154 |
+
|
155 |
+
|
156 |
+
def test_softmax_fixtures():
|
157 |
+
assert_allclose(softmax([1000, 0, 0, 0]), np.array([1, 0, 0, 0]),
|
158 |
+
rtol=1e-13)
|
159 |
+
assert_allclose(softmax([1, 1]), np.array([.5, .5]), rtol=1e-13)
|
160 |
+
assert_allclose(softmax([0, 1]), np.array([1, np.e])/(1 + np.e),
|
161 |
+
rtol=1e-13)
|
162 |
+
|
163 |
+
# Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
|
164 |
+
# converted to float.
|
165 |
+
x = np.arange(4)
|
166 |
+
expected = np.array([0.03205860328008499,
|
167 |
+
0.08714431874203256,
|
168 |
+
0.23688281808991013,
|
169 |
+
0.6439142598879722])
|
170 |
+
|
171 |
+
assert_allclose(softmax(x), expected, rtol=1e-13)
|
172 |
+
|
173 |
+
# Translation property. If all the values are changed by the same amount,
|
174 |
+
# the softmax result does not change.
|
175 |
+
assert_allclose(softmax(x + 100), expected, rtol=1e-13)
|
176 |
+
|
177 |
+
# When axis=None, softmax operates on the entire array, and preserves
|
178 |
+
# the shape.
|
179 |
+
assert_allclose(softmax(x.reshape(2, 2)), expected.reshape(2, 2),
|
180 |
+
rtol=1e-13)
|
181 |
+
|
182 |
+
|
183 |
+
def test_softmax_multi_axes():
|
184 |
+
assert_allclose(softmax([[1000, 0], [1000, 0]], axis=0),
|
185 |
+
np.array([[.5, .5], [.5, .5]]), rtol=1e-13)
|
186 |
+
assert_allclose(softmax([[1000, 0], [1000, 0]], axis=1),
|
187 |
+
np.array([[1, 0], [1, 0]]), rtol=1e-13)
|
188 |
+
|
189 |
+
# Expected value computed using mpmath (with mpmath.mp.dps = 200) and then
|
190 |
+
# converted to float.
|
191 |
+
x = np.array([[-25, 0, 25, 50],
|
192 |
+
[1, 325, 749, 750]])
|
193 |
+
expected = np.array([[2.678636961770877e-33,
|
194 |
+
1.9287498479371314e-22,
|
195 |
+
1.3887943864771144e-11,
|
196 |
+
0.999999999986112],
|
197 |
+
[0.0,
|
198 |
+
1.9444526359919372e-185,
|
199 |
+
0.2689414213699951,
|
200 |
+
0.7310585786300048]])
|
201 |
+
assert_allclose(softmax(x, axis=1), expected, rtol=1e-13)
|
202 |
+
assert_allclose(softmax(x.T, axis=0), expected.T, rtol=1e-13)
|
203 |
+
|
204 |
+
# 3-d input, with a tuple for the axis.
|
205 |
+
x3d = x.reshape(2, 2, 2)
|
206 |
+
assert_allclose(softmax(x3d, axis=(1, 2)), expected.reshape(2, 2, 2),
|
207 |
+
rtol=1e-13)
|
venv/lib/python3.10/site-packages/scipy/special/tests/test_mpmath.py
ADDED
@@ -0,0 +1,2272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Test SciPy functions versus mpmath, if available.
|
3 |
+
|
4 |
+
"""
|
5 |
+
import numpy as np
|
6 |
+
from numpy.testing import assert_, assert_allclose
|
7 |
+
from numpy import pi
|
8 |
+
import pytest
|
9 |
+
import itertools
|
10 |
+
|
11 |
+
from scipy._lib import _pep440
|
12 |
+
|
13 |
+
import scipy.special as sc
|
14 |
+
from scipy.special._testutils import (
|
15 |
+
MissingModule, check_version, FuncData,
|
16 |
+
assert_func_equal)
|
17 |
+
from scipy.special._mptestutils import (
|
18 |
+
Arg, FixedArg, ComplexArg, IntArg, assert_mpmath_equal,
|
19 |
+
nonfunctional_tooslow, trace_args, time_limited, exception_to_nan,
|
20 |
+
inf_to_nan)
|
21 |
+
from scipy.special._ufuncs import (
|
22 |
+
_sinpi, _cospi, _lgam1p, _lanczos_sum_expg_scaled, _log1pmx,
|
23 |
+
_igam_fac)
|
24 |
+
|
25 |
+
try:
|
26 |
+
import mpmath
|
27 |
+
except ImportError:
|
28 |
+
mpmath = MissingModule('mpmath')
|
29 |
+
|
30 |
+
|
31 |
+
# ------------------------------------------------------------------------------
|
32 |
+
# expi
|
33 |
+
# ------------------------------------------------------------------------------
|
34 |
+
|
35 |
+
@check_version(mpmath, '0.10')
|
36 |
+
def test_expi_complex():
|
37 |
+
dataset = []
|
38 |
+
for r in np.logspace(-99, 2, 10):
|
39 |
+
for p in np.linspace(0, 2*np.pi, 30):
|
40 |
+
z = r*np.exp(1j*p)
|
41 |
+
dataset.append((z, complex(mpmath.ei(z))))
|
42 |
+
dataset = np.array(dataset, dtype=np.cdouble)
|
43 |
+
|
44 |
+
FuncData(sc.expi, dataset, 0, 1).check()
|
45 |
+
|
46 |
+
|
47 |
+
# ------------------------------------------------------------------------------
|
48 |
+
# expn
|
49 |
+
# ------------------------------------------------------------------------------
|
50 |
+
|
51 |
+
@check_version(mpmath, '0.19')
|
52 |
+
def test_expn_large_n():
|
53 |
+
# Test the transition to the asymptotic regime of n.
|
54 |
+
dataset = []
|
55 |
+
for n in [50, 51]:
|
56 |
+
for x in np.logspace(0, 4, 200):
|
57 |
+
with mpmath.workdps(100):
|
58 |
+
dataset.append((n, x, float(mpmath.expint(n, x))))
|
59 |
+
dataset = np.asarray(dataset)
|
60 |
+
|
61 |
+
FuncData(sc.expn, dataset, (0, 1), 2, rtol=1e-13).check()
|
62 |
+
|
63 |
+
# ------------------------------------------------------------------------------
|
64 |
+
# hyp0f1
|
65 |
+
# ------------------------------------------------------------------------------
|
66 |
+
|
67 |
+
|
68 |
+
@check_version(mpmath, '0.19')
|
69 |
+
def test_hyp0f1_gh5764():
|
70 |
+
# Do a small and somewhat systematic test that runs quickly
|
71 |
+
dataset = []
|
72 |
+
axis = [-99.5, -9.5, -0.5, 0.5, 9.5, 99.5]
|
73 |
+
for v in axis:
|
74 |
+
for x in axis:
|
75 |
+
for y in axis:
|
76 |
+
z = x + 1j*y
|
77 |
+
# mpmath computes the answer correctly at dps ~ 17 but
|
78 |
+
# fails for 20 < dps < 120 (uses a different method);
|
79 |
+
# set the dps high enough that this isn't an issue
|
80 |
+
with mpmath.workdps(120):
|
81 |
+
res = complex(mpmath.hyp0f1(v, z))
|
82 |
+
dataset.append((v, z, res))
|
83 |
+
dataset = np.array(dataset)
|
84 |
+
|
85 |
+
FuncData(lambda v, z: sc.hyp0f1(v.real, z), dataset, (0, 1), 2,
|
86 |
+
rtol=1e-13).check()
|
87 |
+
|
88 |
+
|
89 |
+
@check_version(mpmath, '0.19')
|
90 |
+
def test_hyp0f1_gh_1609():
|
91 |
+
# this is a regression test for gh-1609
|
92 |
+
vv = np.linspace(150, 180, 21)
|
93 |
+
af = sc.hyp0f1(vv, 0.5)
|
94 |
+
mf = np.array([mpmath.hyp0f1(v, 0.5) for v in vv])
|
95 |
+
assert_allclose(af, mf.astype(float), rtol=1e-12)
|
96 |
+
|
97 |
+
|
98 |
+
# ------------------------------------------------------------------------------
|
99 |
+
# hyperu
|
100 |
+
# ------------------------------------------------------------------------------
|
101 |
+
|
102 |
+
@check_version(mpmath, '1.1.0')
|
103 |
+
def test_hyperu_around_0():
|
104 |
+
dataset = []
|
105 |
+
# DLMF 13.2.14-15 test points.
|
106 |
+
for n in np.arange(-5, 5):
|
107 |
+
for b in np.linspace(-5, 5, 20):
|
108 |
+
a = -n
|
109 |
+
dataset.append((a, b, 0, float(mpmath.hyperu(a, b, 0))))
|
110 |
+
a = -n + b - 1
|
111 |
+
dataset.append((a, b, 0, float(mpmath.hyperu(a, b, 0))))
|
112 |
+
# DLMF 13.2.16-22 test points.
|
113 |
+
for a in [-10.5, -1.5, -0.5, 0, 0.5, 1, 10]:
|
114 |
+
for b in [-1.0, -0.5, 0, 0.5, 1, 1.5, 2, 2.5]:
|
115 |
+
dataset.append((a, b, 0, float(mpmath.hyperu(a, b, 0))))
|
116 |
+
dataset = np.array(dataset)
|
117 |
+
|
118 |
+
FuncData(sc.hyperu, dataset, (0, 1, 2), 3, rtol=1e-15, atol=5e-13).check()
|
119 |
+
|
120 |
+
|
121 |
+
# ------------------------------------------------------------------------------
|
122 |
+
# hyp2f1
|
123 |
+
# ------------------------------------------------------------------------------
|
124 |
+
|
125 |
+
@check_version(mpmath, '1.0.0')
|
126 |
+
def test_hyp2f1_strange_points():
|
127 |
+
pts = [
|
128 |
+
(2, -1, -1, 0.7), # expected: 2.4
|
129 |
+
(2, -2, -2, 0.7), # expected: 3.87
|
130 |
+
]
|
131 |
+
pts += list(itertools.product([2, 1, -0.7, -1000], repeat=4))
|
132 |
+
pts = [
|
133 |
+
(a, b, c, x) for a, b, c, x in pts
|
134 |
+
if b == c and round(b) == b and b < 0 and b != -1000
|
135 |
+
]
|
136 |
+
kw = dict(eliminate=True)
|
137 |
+
dataset = [p + (float(mpmath.hyp2f1(*p, **kw)),) for p in pts]
|
138 |
+
dataset = np.array(dataset, dtype=np.float64)
|
139 |
+
|
140 |
+
FuncData(sc.hyp2f1, dataset, (0,1,2,3), 4, rtol=1e-10).check()
|
141 |
+
|
142 |
+
|
143 |
+
@check_version(mpmath, '0.13')
|
144 |
+
def test_hyp2f1_real_some_points():
|
145 |
+
pts = [
|
146 |
+
(1, 2, 3, 0),
|
147 |
+
(1./3, 2./3, 5./6, 27./32),
|
148 |
+
(1./4, 1./2, 3./4, 80./81),
|
149 |
+
(2,-2, -3, 3),
|
150 |
+
(2, -3, -2, 3),
|
151 |
+
(2, -1.5, -1.5, 3),
|
152 |
+
(1, 2, 3, 0),
|
153 |
+
(0.7235, -1, -5, 0.3),
|
154 |
+
(0.25, 1./3, 2, 0.999),
|
155 |
+
(0.25, 1./3, 2, -1),
|
156 |
+
(2, 3, 5, 0.99),
|
157 |
+
(3./2, -0.5, 3, 0.99),
|
158 |
+
(2, 2.5, -3.25, 0.999),
|
159 |
+
(-8, 18.016500331508873, 10.805295997850628, 0.90875647507000001),
|
160 |
+
(-10, 900, -10.5, 0.99),
|
161 |
+
(-10, 900, 10.5, 0.99),
|
162 |
+
(-1, 2, 1, 1.0),
|
163 |
+
(-1, 2, 1, -1.0),
|
164 |
+
(-3, 13, 5, 1.0),
|
165 |
+
(-3, 13, 5, -1.0),
|
166 |
+
(0.5, 1 - 270.5, 1.5, 0.999**2), # from issue 1561
|
167 |
+
]
|
168 |
+
dataset = [p + (float(mpmath.hyp2f1(*p)),) for p in pts]
|
169 |
+
dataset = np.array(dataset, dtype=np.float64)
|
170 |
+
|
171 |
+
with np.errstate(invalid='ignore'):
|
172 |
+
FuncData(sc.hyp2f1, dataset, (0,1,2,3), 4, rtol=1e-10).check()
|
173 |
+
|
174 |
+
|
175 |
+
@check_version(mpmath, '0.14')
|
176 |
+
def test_hyp2f1_some_points_2():
|
177 |
+
# Taken from mpmath unit tests -- this point failed for mpmath 0.13 but
|
178 |
+
# was fixed in their SVN since then
|
179 |
+
pts = [
|
180 |
+
(112, (51,10), (-9,10), -0.99999),
|
181 |
+
(10,-900,10.5,0.99),
|
182 |
+
(10,-900,-10.5,0.99),
|
183 |
+
]
|
184 |
+
|
185 |
+
def fev(x):
|
186 |
+
if isinstance(x, tuple):
|
187 |
+
return float(x[0]) / x[1]
|
188 |
+
else:
|
189 |
+
return x
|
190 |
+
|
191 |
+
dataset = [tuple(map(fev, p)) + (float(mpmath.hyp2f1(*p)),) for p in pts]
|
192 |
+
dataset = np.array(dataset, dtype=np.float64)
|
193 |
+
|
194 |
+
FuncData(sc.hyp2f1, dataset, (0,1,2,3), 4, rtol=1e-10).check()
|
195 |
+
|
196 |
+
|
197 |
+
@check_version(mpmath, '0.13')
|
198 |
+
def test_hyp2f1_real_some():
|
199 |
+
dataset = []
|
200 |
+
for a in [-10, -5, -1.8, 1.8, 5, 10]:
|
201 |
+
for b in [-2.5, -1, 1, 7.4]:
|
202 |
+
for c in [-9, -1.8, 5, 20.4]:
|
203 |
+
for z in [-10, -1.01, -0.99, 0, 0.6, 0.95, 1.5, 10]:
|
204 |
+
try:
|
205 |
+
v = float(mpmath.hyp2f1(a, b, c, z))
|
206 |
+
except Exception:
|
207 |
+
continue
|
208 |
+
dataset.append((a, b, c, z, v))
|
209 |
+
dataset = np.array(dataset, dtype=np.float64)
|
210 |
+
|
211 |
+
with np.errstate(invalid='ignore'):
|
212 |
+
FuncData(sc.hyp2f1, dataset, (0,1,2,3), 4, rtol=1e-9,
|
213 |
+
ignore_inf_sign=True).check()
|
214 |
+
|
215 |
+
|
216 |
+
@check_version(mpmath, '0.12')
|
217 |
+
@pytest.mark.slow
|
218 |
+
def test_hyp2f1_real_random():
|
219 |
+
npoints = 500
|
220 |
+
dataset = np.zeros((npoints, 5), np.float64)
|
221 |
+
|
222 |
+
np.random.seed(1234)
|
223 |
+
dataset[:, 0] = np.random.pareto(1.5, npoints)
|
224 |
+
dataset[:, 1] = np.random.pareto(1.5, npoints)
|
225 |
+
dataset[:, 2] = np.random.pareto(1.5, npoints)
|
226 |
+
dataset[:, 3] = 2*np.random.rand(npoints) - 1
|
227 |
+
|
228 |
+
dataset[:, 0] *= (-1)**np.random.randint(2, npoints)
|
229 |
+
dataset[:, 1] *= (-1)**np.random.randint(2, npoints)
|
230 |
+
dataset[:, 2] *= (-1)**np.random.randint(2, npoints)
|
231 |
+
|
232 |
+
for ds in dataset:
|
233 |
+
if mpmath.__version__ < '0.14':
|
234 |
+
# mpmath < 0.14 fails for c too much smaller than a, b
|
235 |
+
if abs(ds[:2]).max() > abs(ds[2]):
|
236 |
+
ds[2] = abs(ds[:2]).max()
|
237 |
+
ds[4] = float(mpmath.hyp2f1(*tuple(ds[:4])))
|
238 |
+
|
239 |
+
FuncData(sc.hyp2f1, dataset, (0, 1, 2, 3), 4, rtol=1e-9).check()
|
240 |
+
|
241 |
+
|
242 |
+
# ------------------------------------------------------------------------------
|
243 |
+
# erf (complex)
|
244 |
+
# ------------------------------------------------------------------------------
|
245 |
+
|
246 |
+
@check_version(mpmath, '0.14')
|
247 |
+
def test_erf_complex():
|
248 |
+
# need to increase mpmath precision for this test
|
249 |
+
old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec
|
250 |
+
try:
|
251 |
+
mpmath.mp.dps = 70
|
252 |
+
x1, y1 = np.meshgrid(np.linspace(-10, 1, 31), np.linspace(-10, 1, 11))
|
253 |
+
x2, y2 = np.meshgrid(np.logspace(-80, .8, 31), np.logspace(-80, .8, 11))
|
254 |
+
points = np.r_[x1.ravel(),x2.ravel()] + 1j*np.r_[y1.ravel(), y2.ravel()]
|
255 |
+
|
256 |
+
assert_func_equal(sc.erf, lambda x: complex(mpmath.erf(x)), points,
|
257 |
+
vectorized=False, rtol=1e-13)
|
258 |
+
assert_func_equal(sc.erfc, lambda x: complex(mpmath.erfc(x)), points,
|
259 |
+
vectorized=False, rtol=1e-13)
|
260 |
+
finally:
|
261 |
+
mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
|
262 |
+
|
263 |
+
|
264 |
+
# ------------------------------------------------------------------------------
|
265 |
+
# lpmv
|
266 |
+
# ------------------------------------------------------------------------------
|
267 |
+
|
268 |
+
@check_version(mpmath, '0.15')
|
269 |
+
def test_lpmv():
|
270 |
+
pts = []
|
271 |
+
for x in [-0.99, -0.557, 1e-6, 0.132, 1]:
|
272 |
+
pts.extend([
|
273 |
+
(1, 1, x),
|
274 |
+
(1, -1, x),
|
275 |
+
(-1, 1, x),
|
276 |
+
(-1, -2, x),
|
277 |
+
(1, 1.7, x),
|
278 |
+
(1, -1.7, x),
|
279 |
+
(-1, 1.7, x),
|
280 |
+
(-1, -2.7, x),
|
281 |
+
(1, 10, x),
|
282 |
+
(1, 11, x),
|
283 |
+
(3, 8, x),
|
284 |
+
(5, 11, x),
|
285 |
+
(-3, 8, x),
|
286 |
+
(-5, 11, x),
|
287 |
+
(3, -8, x),
|
288 |
+
(5, -11, x),
|
289 |
+
(-3, -8, x),
|
290 |
+
(-5, -11, x),
|
291 |
+
(3, 8.3, x),
|
292 |
+
(5, 11.3, x),
|
293 |
+
(-3, 8.3, x),
|
294 |
+
(-5, 11.3, x),
|
295 |
+
(3, -8.3, x),
|
296 |
+
(5, -11.3, x),
|
297 |
+
(-3, -8.3, x),
|
298 |
+
(-5, -11.3, x),
|
299 |
+
])
|
300 |
+
|
301 |
+
def mplegenp(nu, mu, x):
|
302 |
+
if mu == int(mu) and x == 1:
|
303 |
+
# mpmath 0.17 gets this wrong
|
304 |
+
if mu == 0:
|
305 |
+
return 1
|
306 |
+
else:
|
307 |
+
return 0
|
308 |
+
return mpmath.legenp(nu, mu, x)
|
309 |
+
|
310 |
+
dataset = [p + (mplegenp(p[1], p[0], p[2]),) for p in pts]
|
311 |
+
dataset = np.array(dataset, dtype=np.float64)
|
312 |
+
|
313 |
+
def evf(mu, nu, x):
|
314 |
+
return sc.lpmv(mu.astype(int), nu, x)
|
315 |
+
|
316 |
+
with np.errstate(invalid='ignore'):
|
317 |
+
FuncData(evf, dataset, (0,1,2), 3, rtol=1e-10, atol=1e-14).check()
|
318 |
+
|
319 |
+
|
320 |
+
# ------------------------------------------------------------------------------
|
321 |
+
# beta
|
322 |
+
# ------------------------------------------------------------------------------
|
323 |
+
|
324 |
+
@check_version(mpmath, '0.15')
|
325 |
+
def test_beta():
|
326 |
+
np.random.seed(1234)
|
327 |
+
|
328 |
+
b = np.r_[np.logspace(-200, 200, 4),
|
329 |
+
np.logspace(-10, 10, 4),
|
330 |
+
np.logspace(-1, 1, 4),
|
331 |
+
np.arange(-10, 11, 1),
|
332 |
+
np.arange(-10, 11, 1) + 0.5,
|
333 |
+
-1, -2.3, -3, -100.3, -10003.4]
|
334 |
+
a = b
|
335 |
+
|
336 |
+
ab = np.array(np.broadcast_arrays(a[:,None], b[None,:])).reshape(2, -1).T
|
337 |
+
|
338 |
+
old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec
|
339 |
+
try:
|
340 |
+
mpmath.mp.dps = 400
|
341 |
+
|
342 |
+
assert_func_equal(sc.beta,
|
343 |
+
lambda a, b: float(mpmath.beta(a, b)),
|
344 |
+
ab,
|
345 |
+
vectorized=False,
|
346 |
+
rtol=1e-10,
|
347 |
+
ignore_inf_sign=True)
|
348 |
+
|
349 |
+
assert_func_equal(
|
350 |
+
sc.betaln,
|
351 |
+
lambda a, b: float(mpmath.log(abs(mpmath.beta(a, b)))),
|
352 |
+
ab,
|
353 |
+
vectorized=False,
|
354 |
+
rtol=1e-10)
|
355 |
+
finally:
|
356 |
+
mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
|
357 |
+
|
358 |
+
|
359 |
+
# ------------------------------------------------------------------------------
|
360 |
+
# loggamma
|
361 |
+
# ------------------------------------------------------------------------------
|
362 |
+
|
363 |
+
LOGGAMMA_TAYLOR_RADIUS = 0.2
|
364 |
+
|
365 |
+
|
366 |
+
@check_version(mpmath, '0.19')
|
367 |
+
def test_loggamma_taylor_transition():
|
368 |
+
# Make sure there isn't a big jump in accuracy when we move from
|
369 |
+
# using the Taylor series to using the recurrence relation.
|
370 |
+
|
371 |
+
r = LOGGAMMA_TAYLOR_RADIUS + np.array([-0.1, -0.01, 0, 0.01, 0.1])
|
372 |
+
theta = np.linspace(0, 2*np.pi, 20)
|
373 |
+
r, theta = np.meshgrid(r, theta)
|
374 |
+
dz = r*np.exp(1j*theta)
|
375 |
+
z = np.r_[1 + dz, 2 + dz].flatten()
|
376 |
+
|
377 |
+
dataset = [(z0, complex(mpmath.loggamma(z0))) for z0 in z]
|
378 |
+
dataset = np.array(dataset)
|
379 |
+
|
380 |
+
FuncData(sc.loggamma, dataset, 0, 1, rtol=5e-14).check()
|
381 |
+
|
382 |
+
|
383 |
+
@check_version(mpmath, '0.19')
|
384 |
+
def test_loggamma_taylor():
|
385 |
+
# Test around the zeros at z = 1, 2.
|
386 |
+
|
387 |
+
r = np.logspace(-16, np.log10(LOGGAMMA_TAYLOR_RADIUS), 10)
|
388 |
+
theta = np.linspace(0, 2*np.pi, 20)
|
389 |
+
r, theta = np.meshgrid(r, theta)
|
390 |
+
dz = r*np.exp(1j*theta)
|
391 |
+
z = np.r_[1 + dz, 2 + dz].flatten()
|
392 |
+
|
393 |
+
dataset = [(z0, complex(mpmath.loggamma(z0))) for z0 in z]
|
394 |
+
dataset = np.array(dataset)
|
395 |
+
|
396 |
+
FuncData(sc.loggamma, dataset, 0, 1, rtol=5e-14).check()
|
397 |
+
|
398 |
+
|
399 |
+
# ------------------------------------------------------------------------------
|
400 |
+
# rgamma
|
401 |
+
# ------------------------------------------------------------------------------
|
402 |
+
|
403 |
+
@check_version(mpmath, '0.19')
|
404 |
+
@pytest.mark.slow
|
405 |
+
def test_rgamma_zeros():
|
406 |
+
# Test around the zeros at z = 0, -1, -2, ..., -169. (After -169 we
|
407 |
+
# get values that are out of floating point range even when we're
|
408 |
+
# within 0.1 of the zero.)
|
409 |
+
|
410 |
+
# Can't use too many points here or the test takes forever.
|
411 |
+
dx = np.r_[-np.logspace(-1, -13, 3), 0, np.logspace(-13, -1, 3)]
|
412 |
+
dy = dx.copy()
|
413 |
+
dx, dy = np.meshgrid(dx, dy)
|
414 |
+
dz = dx + 1j*dy
|
415 |
+
zeros = np.arange(0, -170, -1).reshape(1, 1, -1)
|
416 |
+
z = (zeros + np.dstack((dz,)*zeros.size)).flatten()
|
417 |
+
with mpmath.workdps(100):
|
418 |
+
dataset = [(z0, complex(mpmath.rgamma(z0))) for z0 in z]
|
419 |
+
|
420 |
+
dataset = np.array(dataset)
|
421 |
+
FuncData(sc.rgamma, dataset, 0, 1, rtol=1e-12).check()
|
422 |
+
|
423 |
+
|
424 |
+
# ------------------------------------------------------------------------------
|
425 |
+
# digamma
|
426 |
+
# ------------------------------------------------------------------------------
|
427 |
+
|
428 |
+
@check_version(mpmath, '0.19')
|
429 |
+
@pytest.mark.slow
|
430 |
+
def test_digamma_roots():
|
431 |
+
# Test the special-cased roots for digamma.
|
432 |
+
root = mpmath.findroot(mpmath.digamma, 1.5)
|
433 |
+
roots = [float(root)]
|
434 |
+
root = mpmath.findroot(mpmath.digamma, -0.5)
|
435 |
+
roots.append(float(root))
|
436 |
+
roots = np.array(roots)
|
437 |
+
|
438 |
+
# If we test beyond a radius of 0.24 mpmath will take forever.
|
439 |
+
dx = np.r_[-0.24, -np.logspace(-1, -15, 10), 0, np.logspace(-15, -1, 10), 0.24]
|
440 |
+
dy = dx.copy()
|
441 |
+
dx, dy = np.meshgrid(dx, dy)
|
442 |
+
dz = dx + 1j*dy
|
443 |
+
z = (roots + np.dstack((dz,)*roots.size)).flatten()
|
444 |
+
with mpmath.workdps(30):
|
445 |
+
dataset = [(z0, complex(mpmath.digamma(z0))) for z0 in z]
|
446 |
+
|
447 |
+
dataset = np.array(dataset)
|
448 |
+
FuncData(sc.digamma, dataset, 0, 1, rtol=1e-14).check()
|
449 |
+
|
450 |
+
|
451 |
+
@check_version(mpmath, '0.19')
|
452 |
+
def test_digamma_negreal():
|
453 |
+
# Test digamma around the negative real axis. Don't do this in
|
454 |
+
# TestSystematic because the points need some jiggering so that
|
455 |
+
# mpmath doesn't take forever.
|
456 |
+
|
457 |
+
digamma = exception_to_nan(mpmath.digamma)
|
458 |
+
|
459 |
+
x = -np.logspace(300, -30, 100)
|
460 |
+
y = np.r_[-np.logspace(0, -3, 5), 0, np.logspace(-3, 0, 5)]
|
461 |
+
x, y = np.meshgrid(x, y)
|
462 |
+
z = (x + 1j*y).flatten()
|
463 |
+
|
464 |
+
with mpmath.workdps(40):
|
465 |
+
dataset = [(z0, complex(digamma(z0))) for z0 in z]
|
466 |
+
dataset = np.asarray(dataset)
|
467 |
+
|
468 |
+
FuncData(sc.digamma, dataset, 0, 1, rtol=1e-13).check()
|
469 |
+
|
470 |
+
|
471 |
+
@check_version(mpmath, '0.19')
|
472 |
+
def test_digamma_boundary():
|
473 |
+
# Check that there isn't a jump in accuracy when we switch from
|
474 |
+
# using the asymptotic series to the reflection formula.
|
475 |
+
|
476 |
+
x = -np.logspace(300, -30, 100)
|
477 |
+
y = np.array([-6.1, -5.9, 5.9, 6.1])
|
478 |
+
x, y = np.meshgrid(x, y)
|
479 |
+
z = (x + 1j*y).flatten()
|
480 |
+
|
481 |
+
with mpmath.workdps(30):
|
482 |
+
dataset = [(z0, complex(mpmath.digamma(z0))) for z0 in z]
|
483 |
+
dataset = np.asarray(dataset)
|
484 |
+
|
485 |
+
FuncData(sc.digamma, dataset, 0, 1, rtol=1e-13).check()
|
486 |
+
|
487 |
+
|
488 |
+
# ------------------------------------------------------------------------------
|
489 |
+
# gammainc
|
490 |
+
# ------------------------------------------------------------------------------
|
491 |
+
|
492 |
+
@check_version(mpmath, '0.19')
|
493 |
+
@pytest.mark.slow
|
494 |
+
def test_gammainc_boundary():
|
495 |
+
# Test the transition to the asymptotic series.
|
496 |
+
small = 20
|
497 |
+
a = np.linspace(0.5*small, 2*small, 50)
|
498 |
+
x = a.copy()
|
499 |
+
a, x = np.meshgrid(a, x)
|
500 |
+
a, x = a.flatten(), x.flatten()
|
501 |
+
with mpmath.workdps(100):
|
502 |
+
dataset = [(a0, x0, float(mpmath.gammainc(a0, b=x0, regularized=True)))
|
503 |
+
for a0, x0 in zip(a, x)]
|
504 |
+
dataset = np.array(dataset)
|
505 |
+
|
506 |
+
FuncData(sc.gammainc, dataset, (0, 1), 2, rtol=1e-12).check()
|
507 |
+
|
508 |
+
|
509 |
+
# ------------------------------------------------------------------------------
|
510 |
+
# spence
|
511 |
+
# ------------------------------------------------------------------------------
|
512 |
+
|
513 |
+
@check_version(mpmath, '0.19')
|
514 |
+
@pytest.mark.slow
|
515 |
+
def test_spence_circle():
|
516 |
+
# The trickiest region for spence is around the circle |z - 1| = 1,
|
517 |
+
# so test that region carefully.
|
518 |
+
|
519 |
+
def spence(z):
|
520 |
+
return complex(mpmath.polylog(2, 1 - z))
|
521 |
+
|
522 |
+
r = np.linspace(0.5, 1.5)
|
523 |
+
theta = np.linspace(0, 2*pi)
|
524 |
+
z = (1 + np.outer(r, np.exp(1j*theta))).flatten()
|
525 |
+
dataset = np.asarray([(z0, spence(z0)) for z0 in z])
|
526 |
+
|
527 |
+
FuncData(sc.spence, dataset, 0, 1, rtol=1e-14).check()
|
528 |
+
|
529 |
+
|
530 |
+
# ------------------------------------------------------------------------------
|
531 |
+
# sinpi and cospi
|
532 |
+
# ------------------------------------------------------------------------------
|
533 |
+
|
534 |
+
@check_version(mpmath, '0.19')
|
535 |
+
def test_sinpi_zeros():
|
536 |
+
eps = np.finfo(float).eps
|
537 |
+
dx = np.r_[-np.logspace(0, -13, 3), 0, np.logspace(-13, 0, 3)]
|
538 |
+
dy = dx.copy()
|
539 |
+
dx, dy = np.meshgrid(dx, dy)
|
540 |
+
dz = dx + 1j*dy
|
541 |
+
zeros = np.arange(-100, 100, 1).reshape(1, 1, -1)
|
542 |
+
z = (zeros + np.dstack((dz,)*zeros.size)).flatten()
|
543 |
+
dataset = np.asarray([(z0, complex(mpmath.sinpi(z0)))
|
544 |
+
for z0 in z])
|
545 |
+
FuncData(_sinpi, dataset, 0, 1, rtol=2*eps).check()
|
546 |
+
|
547 |
+
|
548 |
+
@check_version(mpmath, '0.19')
|
549 |
+
def test_cospi_zeros():
|
550 |
+
eps = np.finfo(float).eps
|
551 |
+
dx = np.r_[-np.logspace(0, -13, 3), 0, np.logspace(-13, 0, 3)]
|
552 |
+
dy = dx.copy()
|
553 |
+
dx, dy = np.meshgrid(dx, dy)
|
554 |
+
dz = dx + 1j*dy
|
555 |
+
zeros = (np.arange(-100, 100, 1) + 0.5).reshape(1, 1, -1)
|
556 |
+
z = (zeros + np.dstack((dz,)*zeros.size)).flatten()
|
557 |
+
dataset = np.asarray([(z0, complex(mpmath.cospi(z0)))
|
558 |
+
for z0 in z])
|
559 |
+
|
560 |
+
FuncData(_cospi, dataset, 0, 1, rtol=2*eps).check()
|
561 |
+
|
562 |
+
|
563 |
+
# ------------------------------------------------------------------------------
|
564 |
+
# ellipj
|
565 |
+
# ------------------------------------------------------------------------------
|
566 |
+
|
567 |
+
@check_version(mpmath, '0.19')
|
568 |
+
def test_dn_quarter_period():
|
569 |
+
def dn(u, m):
|
570 |
+
return sc.ellipj(u, m)[2]
|
571 |
+
|
572 |
+
def mpmath_dn(u, m):
|
573 |
+
return float(mpmath.ellipfun("dn", u=u, m=m))
|
574 |
+
|
575 |
+
m = np.linspace(0, 1, 20)
|
576 |
+
du = np.r_[-np.logspace(-1, -15, 10), 0, np.logspace(-15, -1, 10)]
|
577 |
+
dataset = []
|
578 |
+
for m0 in m:
|
579 |
+
u0 = float(mpmath.ellipk(m0))
|
580 |
+
for du0 in du:
|
581 |
+
p = u0 + du0
|
582 |
+
dataset.append((p, m0, mpmath_dn(p, m0)))
|
583 |
+
dataset = np.asarray(dataset)
|
584 |
+
|
585 |
+
FuncData(dn, dataset, (0, 1), 2, rtol=1e-10).check()
|
586 |
+
|
587 |
+
|
588 |
+
# ------------------------------------------------------------------------------
|
589 |
+
# Wright Omega
|
590 |
+
# ------------------------------------------------------------------------------
|
591 |
+
|
592 |
+
def _mpmath_wrightomega(z, dps):
|
593 |
+
with mpmath.workdps(dps):
|
594 |
+
z = mpmath.mpc(z)
|
595 |
+
unwind = mpmath.ceil((z.imag - mpmath.pi)/(2*mpmath.pi))
|
596 |
+
res = mpmath.lambertw(mpmath.exp(z), unwind)
|
597 |
+
return res
|
598 |
+
|
599 |
+
|
600 |
+
@pytest.mark.slow
|
601 |
+
@check_version(mpmath, '0.19')
|
602 |
+
def test_wrightomega_branch():
|
603 |
+
x = -np.logspace(10, 0, 25)
|
604 |
+
picut_above = [np.nextafter(np.pi, np.inf)]
|
605 |
+
picut_below = [np.nextafter(np.pi, -np.inf)]
|
606 |
+
npicut_above = [np.nextafter(-np.pi, np.inf)]
|
607 |
+
npicut_below = [np.nextafter(-np.pi, -np.inf)]
|
608 |
+
for i in range(50):
|
609 |
+
picut_above.append(np.nextafter(picut_above[-1], np.inf))
|
610 |
+
picut_below.append(np.nextafter(picut_below[-1], -np.inf))
|
611 |
+
npicut_above.append(np.nextafter(npicut_above[-1], np.inf))
|
612 |
+
npicut_below.append(np.nextafter(npicut_below[-1], -np.inf))
|
613 |
+
y = np.hstack((picut_above, picut_below, npicut_above, npicut_below))
|
614 |
+
x, y = np.meshgrid(x, y)
|
615 |
+
z = (x + 1j*y).flatten()
|
616 |
+
|
617 |
+
dataset = np.asarray([(z0, complex(_mpmath_wrightomega(z0, 25)))
|
618 |
+
for z0 in z])
|
619 |
+
|
620 |
+
FuncData(sc.wrightomega, dataset, 0, 1, rtol=1e-8).check()
|
621 |
+
|
622 |
+
|
623 |
+
@pytest.mark.slow
|
624 |
+
@check_version(mpmath, '0.19')
|
625 |
+
def test_wrightomega_region1():
|
626 |
+
# This region gets less coverage in the TestSystematic test
|
627 |
+
x = np.linspace(-2, 1)
|
628 |
+
y = np.linspace(1, 2*np.pi)
|
629 |
+
x, y = np.meshgrid(x, y)
|
630 |
+
z = (x + 1j*y).flatten()
|
631 |
+
|
632 |
+
dataset = np.asarray([(z0, complex(_mpmath_wrightomega(z0, 25)))
|
633 |
+
for z0 in z])
|
634 |
+
|
635 |
+
FuncData(sc.wrightomega, dataset, 0, 1, rtol=1e-15).check()
|
636 |
+
|
637 |
+
|
638 |
+
@pytest.mark.slow
|
639 |
+
@check_version(mpmath, '0.19')
|
640 |
+
def test_wrightomega_region2():
|
641 |
+
# This region gets less coverage in the TestSystematic test
|
642 |
+
x = np.linspace(-2, 1)
|
643 |
+
y = np.linspace(-2*np.pi, -1)
|
644 |
+
x, y = np.meshgrid(x, y)
|
645 |
+
z = (x + 1j*y).flatten()
|
646 |
+
|
647 |
+
dataset = np.asarray([(z0, complex(_mpmath_wrightomega(z0, 25)))
|
648 |
+
for z0 in z])
|
649 |
+
|
650 |
+
FuncData(sc.wrightomega, dataset, 0, 1, rtol=1e-15).check()
|
651 |
+
|
652 |
+
|
653 |
+
# ------------------------------------------------------------------------------
|
654 |
+
# lambertw
|
655 |
+
# ------------------------------------------------------------------------------
|
656 |
+
|
657 |
+
@pytest.mark.slow
|
658 |
+
@check_version(mpmath, '0.19')
|
659 |
+
def test_lambertw_smallz():
|
660 |
+
x, y = np.linspace(-1, 1, 25), np.linspace(-1, 1, 25)
|
661 |
+
x, y = np.meshgrid(x, y)
|
662 |
+
z = (x + 1j*y).flatten()
|
663 |
+
|
664 |
+
dataset = np.asarray([(z0, complex(mpmath.lambertw(z0)))
|
665 |
+
for z0 in z])
|
666 |
+
|
667 |
+
FuncData(sc.lambertw, dataset, 0, 1, rtol=1e-13).check()
|
668 |
+
|
669 |
+
|
670 |
+
# ------------------------------------------------------------------------------
|
671 |
+
# Systematic tests
|
672 |
+
# ------------------------------------------------------------------------------
|
673 |
+
|
674 |
+
HYPERKW = dict(maxprec=200, maxterms=200)
|
675 |
+
|
676 |
+
|
677 |
+
@pytest.mark.slow
|
678 |
+
@check_version(mpmath, '0.17')
|
679 |
+
class TestSystematic:
|
680 |
+
|
681 |
+
def test_airyai(self):
|
682 |
+
# oscillating function, limit range
|
683 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[0],
|
684 |
+
mpmath.airyai,
|
685 |
+
[Arg(-1e8, 1e8)],
|
686 |
+
rtol=1e-5)
|
687 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[0],
|
688 |
+
mpmath.airyai,
|
689 |
+
[Arg(-1e3, 1e3)])
|
690 |
+
|
691 |
+
def test_airyai_complex(self):
|
692 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[0],
|
693 |
+
mpmath.airyai,
|
694 |
+
[ComplexArg()])
|
695 |
+
|
696 |
+
def test_airyai_prime(self):
|
697 |
+
# oscillating function, limit range
|
698 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[1], lambda z:
|
699 |
+
mpmath.airyai(z, derivative=1),
|
700 |
+
[Arg(-1e8, 1e8)],
|
701 |
+
rtol=1e-5)
|
702 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[1], lambda z:
|
703 |
+
mpmath.airyai(z, derivative=1),
|
704 |
+
[Arg(-1e3, 1e3)])
|
705 |
+
|
706 |
+
def test_airyai_prime_complex(self):
|
707 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[1], lambda z:
|
708 |
+
mpmath.airyai(z, derivative=1),
|
709 |
+
[ComplexArg()])
|
710 |
+
|
711 |
+
def test_airybi(self):
|
712 |
+
# oscillating function, limit range
|
713 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[2], lambda z:
|
714 |
+
mpmath.airybi(z),
|
715 |
+
[Arg(-1e8, 1e8)],
|
716 |
+
rtol=1e-5)
|
717 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[2], lambda z:
|
718 |
+
mpmath.airybi(z),
|
719 |
+
[Arg(-1e3, 1e3)])
|
720 |
+
|
721 |
+
def test_airybi_complex(self):
|
722 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[2], lambda z:
|
723 |
+
mpmath.airybi(z),
|
724 |
+
[ComplexArg()])
|
725 |
+
|
726 |
+
def test_airybi_prime(self):
|
727 |
+
# oscillating function, limit range
|
728 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[3], lambda z:
|
729 |
+
mpmath.airybi(z, derivative=1),
|
730 |
+
[Arg(-1e8, 1e8)],
|
731 |
+
rtol=1e-5)
|
732 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[3], lambda z:
|
733 |
+
mpmath.airybi(z, derivative=1),
|
734 |
+
[Arg(-1e3, 1e3)])
|
735 |
+
|
736 |
+
def test_airybi_prime_complex(self):
|
737 |
+
assert_mpmath_equal(lambda z: sc.airy(z)[3], lambda z:
|
738 |
+
mpmath.airybi(z, derivative=1),
|
739 |
+
[ComplexArg()])
|
740 |
+
|
741 |
+
def test_bei(self):
|
742 |
+
assert_mpmath_equal(sc.bei,
|
743 |
+
exception_to_nan(lambda z: mpmath.bei(0, z, **HYPERKW)),
|
744 |
+
[Arg(-1e3, 1e3)])
|
745 |
+
|
746 |
+
def test_ber(self):
|
747 |
+
assert_mpmath_equal(sc.ber,
|
748 |
+
exception_to_nan(lambda z: mpmath.ber(0, z, **HYPERKW)),
|
749 |
+
[Arg(-1e3, 1e3)])
|
750 |
+
|
751 |
+
def test_bernoulli(self):
|
752 |
+
assert_mpmath_equal(lambda n: sc.bernoulli(int(n))[int(n)],
|
753 |
+
lambda n: float(mpmath.bernoulli(int(n))),
|
754 |
+
[IntArg(0, 13000)],
|
755 |
+
rtol=1e-9, n=13000)
|
756 |
+
|
757 |
+
def test_besseli(self):
|
758 |
+
assert_mpmath_equal(
|
759 |
+
sc.iv,
|
760 |
+
exception_to_nan(lambda v, z: mpmath.besseli(v, z, **HYPERKW)),
|
761 |
+
[Arg(-1e100, 1e100), Arg()],
|
762 |
+
atol=1e-270,
|
763 |
+
)
|
764 |
+
|
765 |
+
def test_besseli_complex(self):
|
766 |
+
assert_mpmath_equal(
|
767 |
+
lambda v, z: sc.iv(v.real, z),
|
768 |
+
exception_to_nan(lambda v, z: mpmath.besseli(v, z, **HYPERKW)),
|
769 |
+
[Arg(-1e100, 1e100), ComplexArg()],
|
770 |
+
)
|
771 |
+
|
772 |
+
def test_besselj(self):
|
773 |
+
assert_mpmath_equal(
|
774 |
+
sc.jv,
|
775 |
+
exception_to_nan(lambda v, z: mpmath.besselj(v, z, **HYPERKW)),
|
776 |
+
[Arg(-1e100, 1e100), Arg(-1e3, 1e3)],
|
777 |
+
ignore_inf_sign=True,
|
778 |
+
)
|
779 |
+
|
780 |
+
# loss of precision at large arguments due to oscillation
|
781 |
+
assert_mpmath_equal(
|
782 |
+
sc.jv,
|
783 |
+
exception_to_nan(lambda v, z: mpmath.besselj(v, z, **HYPERKW)),
|
784 |
+
[Arg(-1e100, 1e100), Arg(-1e8, 1e8)],
|
785 |
+
ignore_inf_sign=True,
|
786 |
+
rtol=1e-5,
|
787 |
+
)
|
788 |
+
|
789 |
+
def test_besselj_complex(self):
|
790 |
+
assert_mpmath_equal(
|
791 |
+
lambda v, z: sc.jv(v.real, z),
|
792 |
+
exception_to_nan(lambda v, z: mpmath.besselj(v, z, **HYPERKW)),
|
793 |
+
[Arg(), ComplexArg()]
|
794 |
+
)
|
795 |
+
|
796 |
+
def test_besselk(self):
|
797 |
+
assert_mpmath_equal(
|
798 |
+
sc.kv,
|
799 |
+
mpmath.besselk,
|
800 |
+
[Arg(-200, 200), Arg(0, np.inf)],
|
801 |
+
nan_ok=False,
|
802 |
+
rtol=1e-12,
|
803 |
+
)
|
804 |
+
|
805 |
+
def test_besselk_int(self):
|
806 |
+
assert_mpmath_equal(
|
807 |
+
sc.kn,
|
808 |
+
mpmath.besselk,
|
809 |
+
[IntArg(-200, 200), Arg(0, np.inf)],
|
810 |
+
nan_ok=False,
|
811 |
+
rtol=1e-12,
|
812 |
+
)
|
813 |
+
|
814 |
+
def test_besselk_complex(self):
|
815 |
+
assert_mpmath_equal(
|
816 |
+
lambda v, z: sc.kv(v.real, z),
|
817 |
+
exception_to_nan(lambda v, z: mpmath.besselk(v, z, **HYPERKW)),
|
818 |
+
[Arg(-1e100, 1e100), ComplexArg()],
|
819 |
+
)
|
820 |
+
|
821 |
+
def test_bessely(self):
|
822 |
+
def mpbessely(v, x):
|
823 |
+
r = float(mpmath.bessely(v, x, **HYPERKW))
|
824 |
+
if abs(r) > 1e305:
|
825 |
+
# overflowing to inf a bit earlier is OK
|
826 |
+
r = np.inf * np.sign(r)
|
827 |
+
if abs(r) == 0 and x == 0:
|
828 |
+
# invalid result from mpmath, point x=0 is a divergence
|
829 |
+
return np.nan
|
830 |
+
return r
|
831 |
+
assert_mpmath_equal(
|
832 |
+
sc.yv,
|
833 |
+
exception_to_nan(mpbessely),
|
834 |
+
[Arg(-1e100, 1e100), Arg(-1e8, 1e8)],
|
835 |
+
n=5000,
|
836 |
+
)
|
837 |
+
|
838 |
+
def test_bessely_complex(self):
|
839 |
+
def mpbessely(v, x):
|
840 |
+
r = complex(mpmath.bessely(v, x, **HYPERKW))
|
841 |
+
if abs(r) > 1e305:
|
842 |
+
# overflowing to inf a bit earlier is OK
|
843 |
+
with np.errstate(invalid='ignore'):
|
844 |
+
r = np.inf * np.sign(r)
|
845 |
+
return r
|
846 |
+
assert_mpmath_equal(
|
847 |
+
lambda v, z: sc.yv(v.real, z),
|
848 |
+
exception_to_nan(mpbessely),
|
849 |
+
[Arg(), ComplexArg()],
|
850 |
+
n=15000,
|
851 |
+
)
|
852 |
+
|
853 |
+
def test_bessely_int(self):
|
854 |
+
def mpbessely(v, x):
|
855 |
+
r = float(mpmath.bessely(v, x))
|
856 |
+
if abs(r) == 0 and x == 0:
|
857 |
+
# invalid result from mpmath, point x=0 is a divergence
|
858 |
+
return np.nan
|
859 |
+
return r
|
860 |
+
assert_mpmath_equal(
|
861 |
+
lambda v, z: sc.yn(int(v), z),
|
862 |
+
exception_to_nan(mpbessely),
|
863 |
+
[IntArg(-1000, 1000), Arg(-1e8, 1e8)],
|
864 |
+
)
|
865 |
+
|
866 |
+
def test_beta(self):
|
867 |
+
bad_points = []
|
868 |
+
|
869 |
+
def beta(a, b, nonzero=False):
|
870 |
+
if a < -1e12 or b < -1e12:
|
871 |
+
# Function is defined here only at integers, but due
|
872 |
+
# to loss of precision this is numerically
|
873 |
+
# ill-defined. Don't compare values here.
|
874 |
+
return np.nan
|
875 |
+
if (a < 0 or b < 0) and (abs(float(a + b)) % 1) == 0:
|
876 |
+
# close to a zero of the function: mpmath and scipy
|
877 |
+
# will not round here the same, so the test needs to be
|
878 |
+
# run with an absolute tolerance
|
879 |
+
if nonzero:
|
880 |
+
bad_points.append((float(a), float(b)))
|
881 |
+
return np.nan
|
882 |
+
return mpmath.beta(a, b)
|
883 |
+
|
884 |
+
assert_mpmath_equal(
|
885 |
+
sc.beta,
|
886 |
+
lambda a, b: beta(a, b, nonzero=True),
|
887 |
+
[Arg(), Arg()],
|
888 |
+
dps=400,
|
889 |
+
ignore_inf_sign=True,
|
890 |
+
)
|
891 |
+
|
892 |
+
assert_mpmath_equal(
|
893 |
+
sc.beta,
|
894 |
+
beta,
|
895 |
+
np.array(bad_points),
|
896 |
+
dps=400,
|
897 |
+
ignore_inf_sign=True,
|
898 |
+
atol=1e-11,
|
899 |
+
)
|
900 |
+
|
901 |
+
def test_betainc(self):
|
902 |
+
assert_mpmath_equal(
|
903 |
+
sc.betainc,
|
904 |
+
time_limited()(
|
905 |
+
exception_to_nan(
|
906 |
+
lambda a, b, x: mpmath.betainc(a, b, 0, x, regularized=True)
|
907 |
+
)
|
908 |
+
),
|
909 |
+
[Arg(), Arg(), Arg()],
|
910 |
+
)
|
911 |
+
|
912 |
+
def test_betaincc(self):
|
913 |
+
assert_mpmath_equal(
|
914 |
+
sc.betaincc,
|
915 |
+
time_limited()(
|
916 |
+
exception_to_nan(
|
917 |
+
lambda a, b, x: mpmath.betainc(a, b, x, 1, regularized=True)
|
918 |
+
)
|
919 |
+
),
|
920 |
+
[Arg(), Arg(), Arg()],
|
921 |
+
dps=400,
|
922 |
+
)
|
923 |
+
|
924 |
+
def test_binom(self):
|
925 |
+
bad_points = []
|
926 |
+
|
927 |
+
def binomial(n, k, nonzero=False):
|
928 |
+
if abs(k) > 1e8*(abs(n) + 1):
|
929 |
+
# The binomial is rapidly oscillating in this region,
|
930 |
+
# and the function is numerically ill-defined. Don't
|
931 |
+
# compare values here.
|
932 |
+
return np.nan
|
933 |
+
if n < k and abs(float(n-k) - np.round(float(n-k))) < 1e-15:
|
934 |
+
# close to a zero of the function: mpmath and scipy
|
935 |
+
# will not round here the same, so the test needs to be
|
936 |
+
# run with an absolute tolerance
|
937 |
+
if nonzero:
|
938 |
+
bad_points.append((float(n), float(k)))
|
939 |
+
return np.nan
|
940 |
+
return mpmath.binomial(n, k)
|
941 |
+
|
942 |
+
assert_mpmath_equal(
|
943 |
+
sc.binom,
|
944 |
+
lambda n, k: binomial(n, k, nonzero=True),
|
945 |
+
[Arg(), Arg()],
|
946 |
+
dps=400,
|
947 |
+
)
|
948 |
+
|
949 |
+
assert_mpmath_equal(
|
950 |
+
sc.binom,
|
951 |
+
binomial,
|
952 |
+
np.array(bad_points),
|
953 |
+
dps=400,
|
954 |
+
atol=1e-14,
|
955 |
+
)
|
956 |
+
|
957 |
+
def test_chebyt_int(self):
|
958 |
+
assert_mpmath_equal(
|
959 |
+
lambda n, x: sc.eval_chebyt(int(n), x),
|
960 |
+
exception_to_nan(lambda n, x: mpmath.chebyt(n, x, **HYPERKW)),
|
961 |
+
[IntArg(), Arg()],
|
962 |
+
dps=50,
|
963 |
+
)
|
964 |
+
|
965 |
+
@pytest.mark.xfail(run=False, reason="some cases in hyp2f1 not fully accurate")
|
966 |
+
def test_chebyt(self):
|
967 |
+
assert_mpmath_equal(
|
968 |
+
sc.eval_chebyt,
|
969 |
+
lambda n, x: time_limited()(
|
970 |
+
exception_to_nan(mpmath.chebyt)
|
971 |
+
)(n, x, **HYPERKW),
|
972 |
+
[Arg(-101, 101), Arg()],
|
973 |
+
n=10000,
|
974 |
+
)
|
975 |
+
|
976 |
+
def test_chebyu_int(self):
|
977 |
+
assert_mpmath_equal(
|
978 |
+
lambda n, x: sc.eval_chebyu(int(n), x),
|
979 |
+
exception_to_nan(lambda n, x: mpmath.chebyu(n, x, **HYPERKW)),
|
980 |
+
[IntArg(), Arg()],
|
981 |
+
dps=50,
|
982 |
+
)
|
983 |
+
|
984 |
+
@pytest.mark.xfail(run=False, reason="some cases in hyp2f1 not fully accurate")
|
985 |
+
def test_chebyu(self):
|
986 |
+
assert_mpmath_equal(
|
987 |
+
sc.eval_chebyu,
|
988 |
+
lambda n, x: time_limited()(
|
989 |
+
exception_to_nan(mpmath.chebyu)
|
990 |
+
)(n, x, **HYPERKW),
|
991 |
+
[Arg(-101, 101), Arg()],
|
992 |
+
)
|
993 |
+
|
994 |
+
def test_chi(self):
|
995 |
+
def chi(x):
|
996 |
+
return sc.shichi(x)[1]
|
997 |
+
assert_mpmath_equal(chi, mpmath.chi, [Arg()])
|
998 |
+
# check asymptotic series cross-over
|
999 |
+
assert_mpmath_equal(chi, mpmath.chi, [FixedArg([88 - 1e-9, 88, 88 + 1e-9])])
|
1000 |
+
|
1001 |
+
def test_chi_complex(self):
|
1002 |
+
def chi(z):
|
1003 |
+
return sc.shichi(z)[1]
|
1004 |
+
# chi oscillates as Im[z] -> +- inf, so limit range
|
1005 |
+
assert_mpmath_equal(
|
1006 |
+
chi,
|
1007 |
+
mpmath.chi,
|
1008 |
+
[ComplexArg(complex(-np.inf, -1e8), complex(np.inf, 1e8))],
|
1009 |
+
rtol=1e-12,
|
1010 |
+
)
|
1011 |
+
|
1012 |
+
def test_ci(self):
|
1013 |
+
def ci(x):
|
1014 |
+
return sc.sici(x)[1]
|
1015 |
+
# oscillating function: limit range
|
1016 |
+
assert_mpmath_equal(ci, mpmath.ci, [Arg(-1e8, 1e8)])
|
1017 |
+
|
1018 |
+
def test_ci_complex(self):
|
1019 |
+
def ci(z):
|
1020 |
+
return sc.sici(z)[1]
|
1021 |
+
# ci oscillates as Re[z] -> +- inf, so limit range
|
1022 |
+
assert_mpmath_equal(
|
1023 |
+
ci,
|
1024 |
+
mpmath.ci,
|
1025 |
+
[ComplexArg(complex(-1e8, -np.inf), complex(1e8, np.inf))],
|
1026 |
+
rtol=1e-8,
|
1027 |
+
)
|
1028 |
+
|
1029 |
+
def test_cospi(self):
|
1030 |
+
eps = np.finfo(float).eps
|
1031 |
+
assert_mpmath_equal(_cospi, mpmath.cospi, [Arg()], nan_ok=False, rtol=2*eps)
|
1032 |
+
|
1033 |
+
def test_cospi_complex(self):
|
1034 |
+
assert_mpmath_equal(
|
1035 |
+
_cospi,
|
1036 |
+
mpmath.cospi,
|
1037 |
+
[ComplexArg()],
|
1038 |
+
nan_ok=False,
|
1039 |
+
rtol=1e-13,
|
1040 |
+
)
|
1041 |
+
|
1042 |
+
def test_digamma(self):
|
1043 |
+
assert_mpmath_equal(
|
1044 |
+
sc.digamma,
|
1045 |
+
exception_to_nan(mpmath.digamma),
|
1046 |
+
[Arg()],
|
1047 |
+
rtol=1e-12,
|
1048 |
+
dps=50,
|
1049 |
+
)
|
1050 |
+
|
1051 |
+
def test_digamma_complex(self):
|
1052 |
+
# Test on a cut plane because mpmath will hang. See
|
1053 |
+
# test_digamma_negreal for tests on the negative real axis.
|
1054 |
+
def param_filter(z):
|
1055 |
+
return np.where((z.real < 0) & (np.abs(z.imag) < 1.12), False, True)
|
1056 |
+
|
1057 |
+
assert_mpmath_equal(
|
1058 |
+
sc.digamma,
|
1059 |
+
exception_to_nan(mpmath.digamma),
|
1060 |
+
[ComplexArg()],
|
1061 |
+
rtol=1e-13,
|
1062 |
+
dps=40,
|
1063 |
+
param_filter=param_filter
|
1064 |
+
)
|
1065 |
+
|
1066 |
+
def test_e1(self):
|
1067 |
+
assert_mpmath_equal(
|
1068 |
+
sc.exp1,
|
1069 |
+
mpmath.e1,
|
1070 |
+
[Arg()],
|
1071 |
+
rtol=1e-14,
|
1072 |
+
)
|
1073 |
+
|
1074 |
+
def test_e1_complex(self):
|
1075 |
+
# E_1 oscillates as Im[z] -> +- inf, so limit range
|
1076 |
+
assert_mpmath_equal(
|
1077 |
+
sc.exp1,
|
1078 |
+
mpmath.e1,
|
1079 |
+
[ComplexArg(complex(-np.inf, -1e8), complex(np.inf, 1e8))],
|
1080 |
+
rtol=1e-11,
|
1081 |
+
)
|
1082 |
+
|
1083 |
+
# Check cross-over region
|
1084 |
+
assert_mpmath_equal(
|
1085 |
+
sc.exp1,
|
1086 |
+
mpmath.e1,
|
1087 |
+
(np.linspace(-50, 50, 171)[:, None]
|
1088 |
+
+ np.r_[0, np.logspace(-3, 2, 61), -np.logspace(-3, 2, 11)]*1j).ravel(),
|
1089 |
+
rtol=1e-11,
|
1090 |
+
)
|
1091 |
+
assert_mpmath_equal(
|
1092 |
+
sc.exp1,
|
1093 |
+
mpmath.e1,
|
1094 |
+
(np.linspace(-50, -35, 10000) + 0j),
|
1095 |
+
rtol=1e-11,
|
1096 |
+
)
|
1097 |
+
|
1098 |
+
def test_exprel(self):
|
1099 |
+
assert_mpmath_equal(
|
1100 |
+
sc.exprel,
|
1101 |
+
lambda x: mpmath.expm1(x)/x if x != 0 else mpmath.mpf('1.0'),
|
1102 |
+
[Arg(a=-np.log(np.finfo(np.float64).max),
|
1103 |
+
b=np.log(np.finfo(np.float64).max))],
|
1104 |
+
)
|
1105 |
+
assert_mpmath_equal(
|
1106 |
+
sc.exprel,
|
1107 |
+
lambda x: mpmath.expm1(x)/x if x != 0 else mpmath.mpf('1.0'),
|
1108 |
+
np.array([1e-12, 1e-24, 0, 1e12, 1e24, np.inf]),
|
1109 |
+
rtol=1e-11,
|
1110 |
+
)
|
1111 |
+
assert_(np.isinf(sc.exprel(np.inf)))
|
1112 |
+
assert_(sc.exprel(-np.inf) == 0)
|
1113 |
+
|
1114 |
+
def test_expm1_complex(self):
|
1115 |
+
# Oscillates as a function of Im[z], so limit range to avoid loss of precision
|
1116 |
+
assert_mpmath_equal(
|
1117 |
+
sc.expm1,
|
1118 |
+
mpmath.expm1,
|
1119 |
+
[ComplexArg(complex(-np.inf, -1e7), complex(np.inf, 1e7))],
|
1120 |
+
)
|
1121 |
+
|
1122 |
+
def test_log1p_complex(self):
|
1123 |
+
assert_mpmath_equal(
|
1124 |
+
sc.log1p,
|
1125 |
+
lambda x: mpmath.log(x+1),
|
1126 |
+
[ComplexArg()],
|
1127 |
+
dps=60,
|
1128 |
+
)
|
1129 |
+
|
1130 |
+
def test_log1pmx(self):
|
1131 |
+
assert_mpmath_equal(
|
1132 |
+
_log1pmx,
|
1133 |
+
lambda x: mpmath.log(x + 1) - x,
|
1134 |
+
[Arg()],
|
1135 |
+
dps=60,
|
1136 |
+
rtol=1e-14,
|
1137 |
+
)
|
1138 |
+
|
1139 |
+
def test_ei(self):
|
1140 |
+
assert_mpmath_equal(sc.expi, mpmath.ei, [Arg()], rtol=1e-11)
|
1141 |
+
|
1142 |
+
def test_ei_complex(self):
|
1143 |
+
# Ei oscillates as Im[z] -> +- inf, so limit range
|
1144 |
+
assert_mpmath_equal(
|
1145 |
+
sc.expi,
|
1146 |
+
mpmath.ei,
|
1147 |
+
[ComplexArg(complex(-np.inf, -1e8), complex(np.inf, 1e8))],
|
1148 |
+
rtol=1e-9,
|
1149 |
+
)
|
1150 |
+
|
1151 |
+
def test_ellipe(self):
|
1152 |
+
assert_mpmath_equal(sc.ellipe, mpmath.ellipe, [Arg(b=1.0)])
|
1153 |
+
|
1154 |
+
def test_ellipeinc(self):
|
1155 |
+
assert_mpmath_equal(sc.ellipeinc, mpmath.ellipe, [Arg(-1e3, 1e3), Arg(b=1.0)])
|
1156 |
+
|
1157 |
+
def test_ellipeinc_largephi(self):
|
1158 |
+
assert_mpmath_equal(sc.ellipeinc, mpmath.ellipe, [Arg(), Arg()])
|
1159 |
+
|
1160 |
+
def test_ellipf(self):
|
1161 |
+
assert_mpmath_equal(sc.ellipkinc, mpmath.ellipf, [Arg(-1e3, 1e3), Arg()])
|
1162 |
+
|
1163 |
+
def test_ellipf_largephi(self):
|
1164 |
+
assert_mpmath_equal(sc.ellipkinc, mpmath.ellipf, [Arg(), Arg()])
|
1165 |
+
|
1166 |
+
def test_ellipk(self):
|
1167 |
+
assert_mpmath_equal(sc.ellipk, mpmath.ellipk, [Arg(b=1.0)])
|
1168 |
+
assert_mpmath_equal(
|
1169 |
+
sc.ellipkm1,
|
1170 |
+
lambda m: mpmath.ellipk(1 - m),
|
1171 |
+
[Arg(a=0.0)],
|
1172 |
+
dps=400,
|
1173 |
+
)
|
1174 |
+
|
1175 |
+
def test_ellipkinc(self):
|
1176 |
+
def ellipkinc(phi, m):
|
1177 |
+
return mpmath.ellippi(0, phi, m)
|
1178 |
+
assert_mpmath_equal(
|
1179 |
+
sc.ellipkinc,
|
1180 |
+
ellipkinc,
|
1181 |
+
[Arg(-1e3, 1e3), Arg(b=1.0)],
|
1182 |
+
ignore_inf_sign=True,
|
1183 |
+
)
|
1184 |
+
|
1185 |
+
def test_ellipkinc_largephi(self):
|
1186 |
+
def ellipkinc(phi, m):
|
1187 |
+
return mpmath.ellippi(0, phi, m)
|
1188 |
+
assert_mpmath_equal(
|
1189 |
+
sc.ellipkinc,
|
1190 |
+
ellipkinc,
|
1191 |
+
[Arg(), Arg(b=1.0)],
|
1192 |
+
ignore_inf_sign=True,
|
1193 |
+
)
|
1194 |
+
|
1195 |
+
def test_ellipfun_sn(self):
|
1196 |
+
def sn(u, m):
|
1197 |
+
# mpmath doesn't get the zero at u = 0--fix that
|
1198 |
+
if u == 0:
|
1199 |
+
return 0
|
1200 |
+
else:
|
1201 |
+
return mpmath.ellipfun("sn", u=u, m=m)
|
1202 |
+
|
1203 |
+
# Oscillating function --- limit range of first argument; the
|
1204 |
+
# loss of precision there is an expected numerical feature
|
1205 |
+
# rather than an actual bug
|
1206 |
+
assert_mpmath_equal(
|
1207 |
+
lambda u, m: sc.ellipj(u, m)[0],
|
1208 |
+
sn,
|
1209 |
+
[Arg(-1e6, 1e6), Arg(a=0, b=1)],
|
1210 |
+
rtol=1e-8,
|
1211 |
+
)
|
1212 |
+
|
1213 |
+
def test_ellipfun_cn(self):
|
1214 |
+
# see comment in ellipfun_sn
|
1215 |
+
assert_mpmath_equal(
|
1216 |
+
lambda u, m: sc.ellipj(u, m)[1],
|
1217 |
+
lambda u, m: mpmath.ellipfun("cn", u=u, m=m),
|
1218 |
+
[Arg(-1e6, 1e6), Arg(a=0, b=1)],
|
1219 |
+
rtol=1e-8,
|
1220 |
+
)
|
1221 |
+
|
1222 |
+
def test_ellipfun_dn(self):
|
1223 |
+
# see comment in ellipfun_sn
|
1224 |
+
assert_mpmath_equal(
|
1225 |
+
lambda u, m: sc.ellipj(u, m)[2],
|
1226 |
+
lambda u, m: mpmath.ellipfun("dn", u=u, m=m),
|
1227 |
+
[Arg(-1e6, 1e6), Arg(a=0, b=1)],
|
1228 |
+
rtol=1e-8,
|
1229 |
+
)
|
1230 |
+
|
1231 |
+
def test_erf(self):
|
1232 |
+
assert_mpmath_equal(sc.erf, lambda z: mpmath.erf(z), [Arg()])
|
1233 |
+
|
1234 |
+
def test_erf_complex(self):
|
1235 |
+
assert_mpmath_equal(sc.erf, lambda z: mpmath.erf(z), [ComplexArg()], n=200)
|
1236 |
+
|
1237 |
+
def test_erfc(self):
|
1238 |
+
assert_mpmath_equal(
|
1239 |
+
sc.erfc,
|
1240 |
+
exception_to_nan(lambda z: mpmath.erfc(z)),
|
1241 |
+
[Arg()],
|
1242 |
+
rtol=1e-13,
|
1243 |
+
)
|
1244 |
+
|
1245 |
+
def test_erfc_complex(self):
|
1246 |
+
assert_mpmath_equal(
|
1247 |
+
sc.erfc,
|
1248 |
+
exception_to_nan(lambda z: mpmath.erfc(z)),
|
1249 |
+
[ComplexArg()],
|
1250 |
+
n=200,
|
1251 |
+
)
|
1252 |
+
|
1253 |
+
def test_erfi(self):
|
1254 |
+
assert_mpmath_equal(sc.erfi, mpmath.erfi, [Arg()], n=200)
|
1255 |
+
|
1256 |
+
def test_erfi_complex(self):
|
1257 |
+
assert_mpmath_equal(sc.erfi, mpmath.erfi, [ComplexArg()], n=200)
|
1258 |
+
|
1259 |
+
def test_ndtr(self):
|
1260 |
+
assert_mpmath_equal(
|
1261 |
+
sc.ndtr,
|
1262 |
+
exception_to_nan(lambda z: mpmath.ncdf(z)),
|
1263 |
+
[Arg()],
|
1264 |
+
n=200,
|
1265 |
+
)
|
1266 |
+
|
1267 |
+
def test_ndtr_complex(self):
|
1268 |
+
assert_mpmath_equal(
|
1269 |
+
sc.ndtr,
|
1270 |
+
lambda z: mpmath.erfc(-z/np.sqrt(2.))/2.,
|
1271 |
+
[ComplexArg(a=complex(-10000, -10000), b=complex(10000, 10000))],
|
1272 |
+
n=400,
|
1273 |
+
)
|
1274 |
+
|
1275 |
+
def test_log_ndtr(self):
|
1276 |
+
assert_mpmath_equal(
|
1277 |
+
sc.log_ndtr,
|
1278 |
+
exception_to_nan(lambda z: mpmath.log(mpmath.ncdf(z))),
|
1279 |
+
[Arg()], n=600, dps=300, rtol=1e-13,
|
1280 |
+
)
|
1281 |
+
|
1282 |
+
def test_log_ndtr_complex(self):
|
1283 |
+
assert_mpmath_equal(
|
1284 |
+
sc.log_ndtr,
|
1285 |
+
exception_to_nan(lambda z: mpmath.log(mpmath.erfc(-z/np.sqrt(2.))/2.)),
|
1286 |
+
[ComplexArg(a=complex(-10000, -100), b=complex(10000, 100))],
|
1287 |
+
n=200, dps=300,
|
1288 |
+
)
|
1289 |
+
|
1290 |
+
def test_eulernum(self):
|
1291 |
+
assert_mpmath_equal(
|
1292 |
+
lambda n: sc.euler(n)[-1],
|
1293 |
+
mpmath.eulernum,
|
1294 |
+
[IntArg(1, 10000)],
|
1295 |
+
n=10000,
|
1296 |
+
)
|
1297 |
+
|
1298 |
+
def test_expint(self):
|
1299 |
+
assert_mpmath_equal(
|
1300 |
+
sc.expn,
|
1301 |
+
mpmath.expint,
|
1302 |
+
[IntArg(0, 200), Arg(0, np.inf)],
|
1303 |
+
rtol=1e-13,
|
1304 |
+
dps=160,
|
1305 |
+
)
|
1306 |
+
|
1307 |
+
def test_fresnels(self):
|
1308 |
+
def fresnels(x):
|
1309 |
+
return sc.fresnel(x)[0]
|
1310 |
+
assert_mpmath_equal(fresnels, mpmath.fresnels, [Arg()])
|
1311 |
+
|
1312 |
+
def test_fresnelc(self):
|
1313 |
+
def fresnelc(x):
|
1314 |
+
return sc.fresnel(x)[1]
|
1315 |
+
assert_mpmath_equal(fresnelc, mpmath.fresnelc, [Arg()])
|
1316 |
+
|
1317 |
+
def test_gamma(self):
|
1318 |
+
assert_mpmath_equal(sc.gamma, exception_to_nan(mpmath.gamma), [Arg()])
|
1319 |
+
|
1320 |
+
def test_gamma_complex(self):
|
1321 |
+
assert_mpmath_equal(
|
1322 |
+
sc.gamma,
|
1323 |
+
exception_to_nan(mpmath.gamma),
|
1324 |
+
[ComplexArg()],
|
1325 |
+
rtol=5e-13,
|
1326 |
+
)
|
1327 |
+
|
1328 |
+
def test_gammainc(self):
|
1329 |
+
# Larger arguments are tested in test_data.py:test_local
|
1330 |
+
assert_mpmath_equal(
|
1331 |
+
sc.gammainc,
|
1332 |
+
lambda z, b: mpmath.gammainc(z, b=b, regularized=True),
|
1333 |
+
[Arg(0, 1e4, inclusive_a=False), Arg(0, 1e4)],
|
1334 |
+
nan_ok=False,
|
1335 |
+
rtol=1e-11,
|
1336 |
+
)
|
1337 |
+
|
1338 |
+
def test_gammaincc(self):
|
1339 |
+
# Larger arguments are tested in test_data.py:test_local
|
1340 |
+
assert_mpmath_equal(
|
1341 |
+
sc.gammaincc,
|
1342 |
+
lambda z, a: mpmath.gammainc(z, a=a, regularized=True),
|
1343 |
+
[Arg(0, 1e4, inclusive_a=False), Arg(0, 1e4)],
|
1344 |
+
nan_ok=False,
|
1345 |
+
rtol=1e-11,
|
1346 |
+
)
|
1347 |
+
|
1348 |
+
def test_gammaln(self):
|
1349 |
+
# The real part of loggamma is log(|gamma(z)|).
|
1350 |
+
def f(z):
|
1351 |
+
return mpmath.loggamma(z).real
|
1352 |
+
|
1353 |
+
assert_mpmath_equal(sc.gammaln, exception_to_nan(f), [Arg()])
|
1354 |
+
|
1355 |
+
@pytest.mark.xfail(run=False)
|
1356 |
+
def test_gegenbauer(self):
|
1357 |
+
assert_mpmath_equal(
|
1358 |
+
sc.eval_gegenbauer,
|
1359 |
+
exception_to_nan(mpmath.gegenbauer),
|
1360 |
+
[Arg(-1e3, 1e3), Arg(), Arg()],
|
1361 |
+
)
|
1362 |
+
|
1363 |
+
def test_gegenbauer_int(self):
|
1364 |
+
# Redefine functions to deal with numerical + mpmath issues
|
1365 |
+
def gegenbauer(n, a, x):
|
1366 |
+
# Avoid overflow at large `a` (mpmath would need an even larger
|
1367 |
+
# dps to handle this correctly, so just skip this region)
|
1368 |
+
if abs(a) > 1e100:
|
1369 |
+
return np.nan
|
1370 |
+
|
1371 |
+
# Deal with n=0, n=1 correctly; mpmath 0.17 doesn't do these
|
1372 |
+
# always correctly
|
1373 |
+
if n == 0:
|
1374 |
+
r = 1.0
|
1375 |
+
elif n == 1:
|
1376 |
+
r = 2*a*x
|
1377 |
+
else:
|
1378 |
+
r = mpmath.gegenbauer(n, a, x)
|
1379 |
+
|
1380 |
+
# Mpmath 0.17 gives wrong results (spurious zero) in some cases, so
|
1381 |
+
# compute the value by perturbing the result
|
1382 |
+
if float(r) == 0 and a < -1 and float(a) == int(float(a)):
|
1383 |
+
r = mpmath.gegenbauer(n, a + mpmath.mpf('1e-50'), x)
|
1384 |
+
if abs(r) < mpmath.mpf('1e-50'):
|
1385 |
+
r = mpmath.mpf('0.0')
|
1386 |
+
|
1387 |
+
# Differing overflow thresholds in scipy vs. mpmath
|
1388 |
+
if abs(r) > 1e270:
|
1389 |
+
return np.inf
|
1390 |
+
return r
|
1391 |
+
|
1392 |
+
def sc_gegenbauer(n, a, x):
|
1393 |
+
r = sc.eval_gegenbauer(int(n), a, x)
|
1394 |
+
# Differing overflow thresholds in scipy vs. mpmath
|
1395 |
+
if abs(r) > 1e270:
|
1396 |
+
return np.inf
|
1397 |
+
return r
|
1398 |
+
assert_mpmath_equal(
|
1399 |
+
sc_gegenbauer,
|
1400 |
+
exception_to_nan(gegenbauer),
|
1401 |
+
[IntArg(0, 100), Arg(-1e9, 1e9), Arg()],
|
1402 |
+
n=40000, dps=100, ignore_inf_sign=True, rtol=1e-6,
|
1403 |
+
)
|
1404 |
+
|
1405 |
+
# Check the small-x expansion
|
1406 |
+
assert_mpmath_equal(
|
1407 |
+
sc_gegenbauer,
|
1408 |
+
exception_to_nan(gegenbauer),
|
1409 |
+
[IntArg(0, 100), Arg(), FixedArg(np.logspace(-30, -4, 30))],
|
1410 |
+
dps=100, ignore_inf_sign=True,
|
1411 |
+
)
|
1412 |
+
|
1413 |
+
@pytest.mark.xfail(run=False)
|
1414 |
+
def test_gegenbauer_complex(self):
|
1415 |
+
assert_mpmath_equal(
|
1416 |
+
lambda n, a, x: sc.eval_gegenbauer(int(n), a.real, x),
|
1417 |
+
exception_to_nan(mpmath.gegenbauer),
|
1418 |
+
[IntArg(0, 100), Arg(), ComplexArg()],
|
1419 |
+
)
|
1420 |
+
|
1421 |
+
@nonfunctional_tooslow
|
1422 |
+
def test_gegenbauer_complex_general(self):
|
1423 |
+
assert_mpmath_equal(
|
1424 |
+
lambda n, a, x: sc.eval_gegenbauer(n.real, a.real, x),
|
1425 |
+
exception_to_nan(mpmath.gegenbauer),
|
1426 |
+
[Arg(-1e3, 1e3), Arg(), ComplexArg()],
|
1427 |
+
)
|
1428 |
+
|
1429 |
+
def test_hankel1(self):
|
1430 |
+
assert_mpmath_equal(
|
1431 |
+
sc.hankel1,
|
1432 |
+
exception_to_nan(lambda v, x: mpmath.hankel1(v, x, **HYPERKW)),
|
1433 |
+
[Arg(-1e20, 1e20), Arg()],
|
1434 |
+
)
|
1435 |
+
|
1436 |
+
def test_hankel2(self):
|
1437 |
+
assert_mpmath_equal(
|
1438 |
+
sc.hankel2,
|
1439 |
+
exception_to_nan(lambda v, x: mpmath.hankel2(v, x, **HYPERKW)),
|
1440 |
+
[Arg(-1e20, 1e20), Arg()],
|
1441 |
+
)
|
1442 |
+
|
1443 |
+
@pytest.mark.xfail(run=False, reason="issues at intermediately large orders")
|
1444 |
+
def test_hermite(self):
|
1445 |
+
assert_mpmath_equal(
|
1446 |
+
lambda n, x: sc.eval_hermite(int(n), x),
|
1447 |
+
exception_to_nan(mpmath.hermite),
|
1448 |
+
[IntArg(0, 10000), Arg()],
|
1449 |
+
)
|
1450 |
+
|
1451 |
+
# hurwitz: same as zeta
|
1452 |
+
|
1453 |
+
def test_hyp0f1(self):
|
1454 |
+
# mpmath reports no convergence unless maxterms is large enough
|
1455 |
+
KW = dict(maxprec=400, maxterms=1500)
|
1456 |
+
# n=500 (non-xslow default) fails for one bad point
|
1457 |
+
assert_mpmath_equal(
|
1458 |
+
sc.hyp0f1,
|
1459 |
+
lambda a, x: mpmath.hyp0f1(a, x, **KW),
|
1460 |
+
[Arg(-1e7, 1e7), Arg(0, 1e5)],
|
1461 |
+
n=5000,
|
1462 |
+
)
|
1463 |
+
# NB: The range of the second parameter ("z") is limited from below
|
1464 |
+
# because of an overflow in the intermediate calculations. The way
|
1465 |
+
# for fix it is to implement an asymptotic expansion for Bessel J
|
1466 |
+
# (similar to what is implemented for Bessel I here).
|
1467 |
+
|
1468 |
+
def test_hyp0f1_complex(self):
|
1469 |
+
assert_mpmath_equal(
|
1470 |
+
lambda a, z: sc.hyp0f1(a.real, z),
|
1471 |
+
exception_to_nan(lambda a, x: mpmath.hyp0f1(a, x, **HYPERKW)),
|
1472 |
+
[Arg(-10, 10), ComplexArg(complex(-120, -120), complex(120, 120))],
|
1473 |
+
)
|
1474 |
+
# NB: The range of the first parameter ("v") are limited by an overflow
|
1475 |
+
# in the intermediate calculations. Can be fixed by implementing an
|
1476 |
+
# asymptotic expansion for Bessel functions for large order.
|
1477 |
+
|
1478 |
+
def test_hyp1f1(self):
|
1479 |
+
def mpmath_hyp1f1(a, b, x):
|
1480 |
+
try:
|
1481 |
+
return mpmath.hyp1f1(a, b, x)
|
1482 |
+
except ZeroDivisionError:
|
1483 |
+
return np.inf
|
1484 |
+
|
1485 |
+
assert_mpmath_equal(
|
1486 |
+
sc.hyp1f1,
|
1487 |
+
mpmath_hyp1f1,
|
1488 |
+
[Arg(-50, 50), Arg(1, 50, inclusive_a=False), Arg(-50, 50)],
|
1489 |
+
n=500,
|
1490 |
+
nan_ok=False,
|
1491 |
+
)
|
1492 |
+
|
1493 |
+
@pytest.mark.xfail(run=False)
|
1494 |
+
def test_hyp1f1_complex(self):
|
1495 |
+
assert_mpmath_equal(
|
1496 |
+
inf_to_nan(lambda a, b, x: sc.hyp1f1(a.real, b.real, x)),
|
1497 |
+
exception_to_nan(lambda a, b, x: mpmath.hyp1f1(a, b, x, **HYPERKW)),
|
1498 |
+
[Arg(-1e3, 1e3), Arg(-1e3, 1e3), ComplexArg()],
|
1499 |
+
n=2000,
|
1500 |
+
)
|
1501 |
+
|
1502 |
+
@nonfunctional_tooslow
|
1503 |
+
def test_hyp2f1_complex(self):
|
1504 |
+
# SciPy's hyp2f1 seems to have performance and accuracy problems
|
1505 |
+
assert_mpmath_equal(
|
1506 |
+
lambda a, b, c, x: sc.hyp2f1(a.real, b.real, c.real, x),
|
1507 |
+
exception_to_nan(lambda a, b, c, x: mpmath.hyp2f1(a, b, c, x, **HYPERKW)),
|
1508 |
+
[Arg(-1e2, 1e2), Arg(-1e2, 1e2), Arg(-1e2, 1e2), ComplexArg()],
|
1509 |
+
n=10,
|
1510 |
+
)
|
1511 |
+
|
1512 |
+
@pytest.mark.xfail(run=False)
|
1513 |
+
def test_hyperu(self):
|
1514 |
+
assert_mpmath_equal(
|
1515 |
+
sc.hyperu,
|
1516 |
+
exception_to_nan(lambda a, b, x: mpmath.hyperu(a, b, x, **HYPERKW)),
|
1517 |
+
[Arg(), Arg(), Arg()],
|
1518 |
+
)
|
1519 |
+
|
1520 |
+
@pytest.mark.xfail_on_32bit("mpmath issue gh-342: "
|
1521 |
+
"unsupported operand mpz, long for pow")
|
1522 |
+
def test_igam_fac(self):
|
1523 |
+
def mp_igam_fac(a, x):
|
1524 |
+
return mpmath.power(x, a)*mpmath.exp(-x)/mpmath.gamma(a)
|
1525 |
+
|
1526 |
+
assert_mpmath_equal(
|
1527 |
+
_igam_fac,
|
1528 |
+
mp_igam_fac,
|
1529 |
+
[Arg(0, 1e14, inclusive_a=False), Arg(0, 1e14)],
|
1530 |
+
rtol=1e-10,
|
1531 |
+
)
|
1532 |
+
|
1533 |
+
def test_j0(self):
|
1534 |
+
# The Bessel function at large arguments is j0(x) ~ cos(x + phi)/sqrt(x)
|
1535 |
+
# and at large arguments the phase of the cosine loses precision.
|
1536 |
+
#
|
1537 |
+
# This is numerically expected behavior, so we compare only up to
|
1538 |
+
# 1e8 = 1e15 * 1e-7
|
1539 |
+
assert_mpmath_equal(sc.j0, mpmath.j0, [Arg(-1e3, 1e3)])
|
1540 |
+
assert_mpmath_equal(sc.j0, mpmath.j0, [Arg(-1e8, 1e8)], rtol=1e-5)
|
1541 |
+
|
1542 |
+
def test_j1(self):
|
1543 |
+
# See comment in test_j0
|
1544 |
+
assert_mpmath_equal(sc.j1, mpmath.j1, [Arg(-1e3, 1e3)])
|
1545 |
+
assert_mpmath_equal(sc.j1, mpmath.j1, [Arg(-1e8, 1e8)], rtol=1e-5)
|
1546 |
+
|
1547 |
+
@pytest.mark.xfail(run=False)
|
1548 |
+
def test_jacobi(self):
|
1549 |
+
assert_mpmath_equal(
|
1550 |
+
sc.eval_jacobi,
|
1551 |
+
exception_to_nan(lambda a, b, c, x: mpmath.jacobi(a, b, c, x, **HYPERKW)),
|
1552 |
+
[Arg(), Arg(), Arg(), Arg()],
|
1553 |
+
)
|
1554 |
+
assert_mpmath_equal(
|
1555 |
+
lambda n, b, c, x: sc.eval_jacobi(int(n), b, c, x),
|
1556 |
+
exception_to_nan(lambda a, b, c, x: mpmath.jacobi(a, b, c, x, **HYPERKW)),
|
1557 |
+
[IntArg(), Arg(), Arg(), Arg()],
|
1558 |
+
)
|
1559 |
+
|
1560 |
+
def test_jacobi_int(self):
|
1561 |
+
# Redefine functions to deal with numerical + mpmath issues
|
1562 |
+
def jacobi(n, a, b, x):
|
1563 |
+
# Mpmath does not handle n=0 case always correctly
|
1564 |
+
if n == 0:
|
1565 |
+
return 1.0
|
1566 |
+
return mpmath.jacobi(n, a, b, x)
|
1567 |
+
assert_mpmath_equal(
|
1568 |
+
lambda n, a, b, x: sc.eval_jacobi(int(n), a, b, x),
|
1569 |
+
lambda n, a, b, x: exception_to_nan(jacobi)(n, a, b, x, **HYPERKW),
|
1570 |
+
[IntArg(), Arg(), Arg(), Arg()],
|
1571 |
+
n=20000,
|
1572 |
+
dps=50,
|
1573 |
+
)
|
1574 |
+
|
1575 |
+
def test_kei(self):
|
1576 |
+
def kei(x):
|
1577 |
+
if x == 0:
|
1578 |
+
# work around mpmath issue at x=0
|
1579 |
+
return -pi/4
|
1580 |
+
return exception_to_nan(mpmath.kei)(0, x, **HYPERKW)
|
1581 |
+
assert_mpmath_equal(sc.kei, kei, [Arg(-1e30, 1e30)], n=1000)
|
1582 |
+
|
1583 |
+
def test_ker(self):
|
1584 |
+
assert_mpmath_equal(
|
1585 |
+
sc.ker,
|
1586 |
+
exception_to_nan(lambda x: mpmath.ker(0, x, **HYPERKW)),
|
1587 |
+
[Arg(-1e30, 1e30)],
|
1588 |
+
n=1000,
|
1589 |
+
)
|
1590 |
+
|
1591 |
+
@nonfunctional_tooslow
|
1592 |
+
def test_laguerre(self):
|
1593 |
+
assert_mpmath_equal(
|
1594 |
+
trace_args(sc.eval_laguerre),
|
1595 |
+
lambda n, x: exception_to_nan(mpmath.laguerre)(n, x, **HYPERKW),
|
1596 |
+
[Arg(), Arg()],
|
1597 |
+
)
|
1598 |
+
|
1599 |
+
def test_laguerre_int(self):
|
1600 |
+
assert_mpmath_equal(
|
1601 |
+
lambda n, x: sc.eval_laguerre(int(n), x),
|
1602 |
+
lambda n, x: exception_to_nan(mpmath.laguerre)(n, x, **HYPERKW),
|
1603 |
+
[IntArg(), Arg()],
|
1604 |
+
n=20000,
|
1605 |
+
)
|
1606 |
+
|
1607 |
+
@pytest.mark.xfail_on_32bit("see gh-3551 for bad points")
|
1608 |
+
def test_lambertw_real(self):
|
1609 |
+
assert_mpmath_equal(
|
1610 |
+
lambda x, k: sc.lambertw(x, int(k.real)),
|
1611 |
+
lambda x, k: mpmath.lambertw(x, int(k.real)),
|
1612 |
+
[ComplexArg(-np.inf, np.inf), IntArg(0, 10)],
|
1613 |
+
rtol=1e-13, nan_ok=False,
|
1614 |
+
)
|
1615 |
+
|
1616 |
+
def test_lanczos_sum_expg_scaled(self):
|
1617 |
+
maxgamma = 171.624376956302725
|
1618 |
+
e = np.exp(1)
|
1619 |
+
g = 6.024680040776729583740234375
|
1620 |
+
|
1621 |
+
def gamma(x):
|
1622 |
+
with np.errstate(over='ignore'):
|
1623 |
+
fac = ((x + g - 0.5)/e)**(x - 0.5)
|
1624 |
+
if fac != np.inf:
|
1625 |
+
res = fac*_lanczos_sum_expg_scaled(x)
|
1626 |
+
else:
|
1627 |
+
fac = ((x + g - 0.5)/e)**(0.5*(x - 0.5))
|
1628 |
+
res = fac*_lanczos_sum_expg_scaled(x)
|
1629 |
+
res *= fac
|
1630 |
+
return res
|
1631 |
+
|
1632 |
+
assert_mpmath_equal(
|
1633 |
+
gamma,
|
1634 |
+
mpmath.gamma,
|
1635 |
+
[Arg(0, maxgamma, inclusive_a=False)],
|
1636 |
+
rtol=1e-13,
|
1637 |
+
)
|
1638 |
+
|
1639 |
+
@nonfunctional_tooslow
|
1640 |
+
def test_legendre(self):
|
1641 |
+
assert_mpmath_equal(sc.eval_legendre, mpmath.legendre, [Arg(), Arg()])
|
1642 |
+
|
1643 |
+
def test_legendre_int(self):
|
1644 |
+
assert_mpmath_equal(
|
1645 |
+
lambda n, x: sc.eval_legendre(int(n), x),
|
1646 |
+
lambda n, x: exception_to_nan(mpmath.legendre)(n, x, **HYPERKW),
|
1647 |
+
[IntArg(), Arg()],
|
1648 |
+
n=20000,
|
1649 |
+
)
|
1650 |
+
|
1651 |
+
# Check the small-x expansion
|
1652 |
+
assert_mpmath_equal(
|
1653 |
+
lambda n, x: sc.eval_legendre(int(n), x),
|
1654 |
+
lambda n, x: exception_to_nan(mpmath.legendre)(n, x, **HYPERKW),
|
1655 |
+
[IntArg(), FixedArg(np.logspace(-30, -4, 20))],
|
1656 |
+
)
|
1657 |
+
|
1658 |
+
def test_legenp(self):
|
1659 |
+
def lpnm(n, m, z):
|
1660 |
+
try:
|
1661 |
+
v = sc.lpmn(m, n, z)[0][-1,-1]
|
1662 |
+
except ValueError:
|
1663 |
+
return np.nan
|
1664 |
+
if abs(v) > 1e306:
|
1665 |
+
# harmonize overflow to inf
|
1666 |
+
v = np.inf * np.sign(v.real)
|
1667 |
+
return v
|
1668 |
+
|
1669 |
+
def lpnm_2(n, m, z):
|
1670 |
+
v = sc.lpmv(m, n, z)
|
1671 |
+
if abs(v) > 1e306:
|
1672 |
+
# harmonize overflow to inf
|
1673 |
+
v = np.inf * np.sign(v.real)
|
1674 |
+
return v
|
1675 |
+
|
1676 |
+
def legenp(n, m, z):
|
1677 |
+
if (z == 1 or z == -1) and int(n) == n:
|
1678 |
+
# Special case (mpmath may give inf, we take the limit by
|
1679 |
+
# continuity)
|
1680 |
+
if m == 0:
|
1681 |
+
if n < 0:
|
1682 |
+
n = -n - 1
|
1683 |
+
return mpmath.power(mpmath.sign(z), n)
|
1684 |
+
else:
|
1685 |
+
return 0
|
1686 |
+
|
1687 |
+
if abs(z) < 1e-15:
|
1688 |
+
# mpmath has bad performance here
|
1689 |
+
return np.nan
|
1690 |
+
|
1691 |
+
typ = 2 if abs(z) < 1 else 3
|
1692 |
+
v = exception_to_nan(mpmath.legenp)(n, m, z, type=typ)
|
1693 |
+
|
1694 |
+
if abs(v) > 1e306:
|
1695 |
+
# harmonize overflow to inf
|
1696 |
+
v = mpmath.inf * mpmath.sign(v.real)
|
1697 |
+
|
1698 |
+
return v
|
1699 |
+
|
1700 |
+
assert_mpmath_equal(lpnm, legenp, [IntArg(-100, 100), IntArg(-100, 100), Arg()])
|
1701 |
+
|
1702 |
+
assert_mpmath_equal(
|
1703 |
+
lpnm_2,
|
1704 |
+
legenp,
|
1705 |
+
[IntArg(-100, 100), Arg(-100, 100), Arg(-1, 1)],
|
1706 |
+
atol=1e-10,
|
1707 |
+
)
|
1708 |
+
|
1709 |
+
def test_legenp_complex_2(self):
|
1710 |
+
def clpnm(n, m, z):
|
1711 |
+
try:
|
1712 |
+
return sc.clpmn(m.real, n.real, z, type=2)[0][-1,-1]
|
1713 |
+
except ValueError:
|
1714 |
+
return np.nan
|
1715 |
+
|
1716 |
+
def legenp(n, m, z):
|
1717 |
+
if abs(z) < 1e-15:
|
1718 |
+
# mpmath has bad performance here
|
1719 |
+
return np.nan
|
1720 |
+
return exception_to_nan(mpmath.legenp)(int(n.real), int(m.real), z, type=2)
|
1721 |
+
|
1722 |
+
# mpmath is quite slow here
|
1723 |
+
x = np.array([-2, -0.99, -0.5, 0, 1e-5, 0.5, 0.99, 20, 2e3])
|
1724 |
+
y = np.array([-1e3, -0.5, 0.5, 1.3])
|
1725 |
+
z = (x[:,None] + 1j*y[None,:]).ravel()
|
1726 |
+
|
1727 |
+
assert_mpmath_equal(
|
1728 |
+
clpnm,
|
1729 |
+
legenp,
|
1730 |
+
[FixedArg([-2, -1, 0, 1, 2, 10]),
|
1731 |
+
FixedArg([-2, -1, 0, 1, 2, 10]),
|
1732 |
+
FixedArg(z)],
|
1733 |
+
rtol=1e-6,
|
1734 |
+
n=500,
|
1735 |
+
)
|
1736 |
+
|
1737 |
+
def test_legenp_complex_3(self):
|
1738 |
+
def clpnm(n, m, z):
|
1739 |
+
try:
|
1740 |
+
return sc.clpmn(m.real, n.real, z, type=3)[0][-1,-1]
|
1741 |
+
except ValueError:
|
1742 |
+
return np.nan
|
1743 |
+
|
1744 |
+
def legenp(n, m, z):
|
1745 |
+
if abs(z) < 1e-15:
|
1746 |
+
# mpmath has bad performance here
|
1747 |
+
return np.nan
|
1748 |
+
return exception_to_nan(mpmath.legenp)(int(n.real), int(m.real), z, type=3)
|
1749 |
+
|
1750 |
+
# mpmath is quite slow here
|
1751 |
+
x = np.array([-2, -0.99, -0.5, 0, 1e-5, 0.5, 0.99, 20, 2e3])
|
1752 |
+
y = np.array([-1e3, -0.5, 0.5, 1.3])
|
1753 |
+
z = (x[:,None] + 1j*y[None,:]).ravel()
|
1754 |
+
|
1755 |
+
assert_mpmath_equal(
|
1756 |
+
clpnm,
|
1757 |
+
legenp,
|
1758 |
+
[FixedArg([-2, -1, 0, 1, 2, 10]),
|
1759 |
+
FixedArg([-2, -1, 0, 1, 2, 10]),
|
1760 |
+
FixedArg(z)],
|
1761 |
+
rtol=1e-6,
|
1762 |
+
n=500,
|
1763 |
+
)
|
1764 |
+
|
1765 |
+
@pytest.mark.xfail(run=False, reason="apparently picks wrong function at |z| > 1")
|
1766 |
+
def test_legenq(self):
|
1767 |
+
def lqnm(n, m, z):
|
1768 |
+
return sc.lqmn(m, n, z)[0][-1,-1]
|
1769 |
+
|
1770 |
+
def legenq(n, m, z):
|
1771 |
+
if abs(z) < 1e-15:
|
1772 |
+
# mpmath has bad performance here
|
1773 |
+
return np.nan
|
1774 |
+
return exception_to_nan(mpmath.legenq)(n, m, z, type=2)
|
1775 |
+
|
1776 |
+
assert_mpmath_equal(
|
1777 |
+
lqnm,
|
1778 |
+
legenq,
|
1779 |
+
[IntArg(0, 100), IntArg(0, 100), Arg()],
|
1780 |
+
)
|
1781 |
+
|
1782 |
+
@nonfunctional_tooslow
|
1783 |
+
def test_legenq_complex(self):
|
1784 |
+
def lqnm(n, m, z):
|
1785 |
+
return sc.lqmn(int(m.real), int(n.real), z)[0][-1,-1]
|
1786 |
+
|
1787 |
+
def legenq(n, m, z):
|
1788 |
+
if abs(z) < 1e-15:
|
1789 |
+
# mpmath has bad performance here
|
1790 |
+
return np.nan
|
1791 |
+
return exception_to_nan(mpmath.legenq)(int(n.real), int(m.real), z, type=2)
|
1792 |
+
|
1793 |
+
assert_mpmath_equal(
|
1794 |
+
lqnm,
|
1795 |
+
legenq,
|
1796 |
+
[IntArg(0, 100), IntArg(0, 100), ComplexArg()],
|
1797 |
+
n=100,
|
1798 |
+
)
|
1799 |
+
|
1800 |
+
def test_lgam1p(self):
|
1801 |
+
def param_filter(x):
|
1802 |
+
# Filter the poles
|
1803 |
+
return np.where((np.floor(x) == x) & (x <= 0), False, True)
|
1804 |
+
|
1805 |
+
def mp_lgam1p(z):
|
1806 |
+
# The real part of loggamma is log(|gamma(z)|)
|
1807 |
+
return mpmath.loggamma(1 + z).real
|
1808 |
+
|
1809 |
+
assert_mpmath_equal(
|
1810 |
+
_lgam1p,
|
1811 |
+
mp_lgam1p,
|
1812 |
+
[Arg()],
|
1813 |
+
rtol=1e-13,
|
1814 |
+
dps=100,
|
1815 |
+
param_filter=param_filter,
|
1816 |
+
)
|
1817 |
+
|
1818 |
+
def test_loggamma(self):
|
1819 |
+
def mpmath_loggamma(z):
|
1820 |
+
try:
|
1821 |
+
res = mpmath.loggamma(z)
|
1822 |
+
except ValueError:
|
1823 |
+
res = complex(np.nan, np.nan)
|
1824 |
+
return res
|
1825 |
+
|
1826 |
+
assert_mpmath_equal(
|
1827 |
+
sc.loggamma,
|
1828 |
+
mpmath_loggamma,
|
1829 |
+
[ComplexArg()],
|
1830 |
+
nan_ok=False,
|
1831 |
+
distinguish_nan_and_inf=False,
|
1832 |
+
rtol=5e-14,
|
1833 |
+
)
|
1834 |
+
|
1835 |
+
@pytest.mark.xfail(run=False)
|
1836 |
+
def test_pcfd(self):
|
1837 |
+
def pcfd(v, x):
|
1838 |
+
return sc.pbdv(v, x)[0]
|
1839 |
+
assert_mpmath_equal(
|
1840 |
+
pcfd,
|
1841 |
+
exception_to_nan(lambda v, x: mpmath.pcfd(v, x, **HYPERKW)),
|
1842 |
+
[Arg(), Arg()],
|
1843 |
+
)
|
1844 |
+
|
1845 |
+
@pytest.mark.xfail(run=False, reason="it's not the same as the mpmath function --- "
|
1846 |
+
"maybe different definition?")
|
1847 |
+
def test_pcfv(self):
|
1848 |
+
def pcfv(v, x):
|
1849 |
+
return sc.pbvv(v, x)[0]
|
1850 |
+
assert_mpmath_equal(
|
1851 |
+
pcfv,
|
1852 |
+
lambda v, x: time_limited()(exception_to_nan(mpmath.pcfv))(v, x, **HYPERKW),
|
1853 |
+
[Arg(), Arg()],
|
1854 |
+
n=1000,
|
1855 |
+
)
|
1856 |
+
|
1857 |
+
def test_pcfw(self):
|
1858 |
+
def pcfw(a, x):
|
1859 |
+
return sc.pbwa(a, x)[0]
|
1860 |
+
|
1861 |
+
def dpcfw(a, x):
|
1862 |
+
return sc.pbwa(a, x)[1]
|
1863 |
+
|
1864 |
+
def mpmath_dpcfw(a, x):
|
1865 |
+
return mpmath.diff(mpmath.pcfw, (a, x), (0, 1))
|
1866 |
+
|
1867 |
+
# The Zhang and Jin implementation only uses Taylor series and
|
1868 |
+
# is thus accurate in only a very small range.
|
1869 |
+
assert_mpmath_equal(
|
1870 |
+
pcfw,
|
1871 |
+
mpmath.pcfw,
|
1872 |
+
[Arg(-5, 5), Arg(-5, 5)],
|
1873 |
+
rtol=2e-8,
|
1874 |
+
n=100,
|
1875 |
+
)
|
1876 |
+
|
1877 |
+
assert_mpmath_equal(
|
1878 |
+
dpcfw,
|
1879 |
+
mpmath_dpcfw,
|
1880 |
+
[Arg(-5, 5), Arg(-5, 5)],
|
1881 |
+
rtol=2e-9,
|
1882 |
+
n=100,
|
1883 |
+
)
|
1884 |
+
|
1885 |
+
@pytest.mark.xfail(run=False,
|
1886 |
+
reason="issues at large arguments (atol OK, rtol not) "
|
1887 |
+
"and <eps-close to z=0")
|
1888 |
+
def test_polygamma(self):
|
1889 |
+
assert_mpmath_equal(
|
1890 |
+
sc.polygamma,
|
1891 |
+
time_limited()(exception_to_nan(mpmath.polygamma)),
|
1892 |
+
[IntArg(0, 1000), Arg()],
|
1893 |
+
)
|
1894 |
+
|
1895 |
+
def test_rgamma(self):
|
1896 |
+
assert_mpmath_equal(
|
1897 |
+
sc.rgamma,
|
1898 |
+
mpmath.rgamma,
|
1899 |
+
[Arg(-8000, np.inf)],
|
1900 |
+
n=5000,
|
1901 |
+
nan_ok=False,
|
1902 |
+
ignore_inf_sign=True,
|
1903 |
+
)
|
1904 |
+
|
1905 |
+
def test_rgamma_complex(self):
|
1906 |
+
assert_mpmath_equal(
|
1907 |
+
sc.rgamma,
|
1908 |
+
exception_to_nan(mpmath.rgamma),
|
1909 |
+
[ComplexArg()],
|
1910 |
+
rtol=5e-13,
|
1911 |
+
)
|
1912 |
+
|
1913 |
+
@pytest.mark.xfail(reason=("see gh-3551 for bad points on 32 bit "
|
1914 |
+
"systems and gh-8095 for another bad "
|
1915 |
+
"point"))
|
1916 |
+
def test_rf(self):
|
1917 |
+
if _pep440.parse(mpmath.__version__) >= _pep440.Version("1.0.0"):
|
1918 |
+
# no workarounds needed
|
1919 |
+
mppoch = mpmath.rf
|
1920 |
+
else:
|
1921 |
+
def mppoch(a, m):
|
1922 |
+
# deal with cases where the result in double precision
|
1923 |
+
# hits exactly a non-positive integer, but the
|
1924 |
+
# corresponding extended-precision mpf floats don't
|
1925 |
+
if float(a + m) == int(a + m) and float(a + m) <= 0:
|
1926 |
+
a = mpmath.mpf(a)
|
1927 |
+
m = int(a + m) - a
|
1928 |
+
return mpmath.rf(a, m)
|
1929 |
+
|
1930 |
+
assert_mpmath_equal(sc.poch, mppoch, [Arg(), Arg()], dps=400)
|
1931 |
+
|
1932 |
+
def test_sinpi(self):
|
1933 |
+
eps = np.finfo(float).eps
|
1934 |
+
assert_mpmath_equal(
|
1935 |
+
_sinpi,
|
1936 |
+
mpmath.sinpi,
|
1937 |
+
[Arg()],
|
1938 |
+
nan_ok=False,
|
1939 |
+
rtol=2*eps,
|
1940 |
+
)
|
1941 |
+
|
1942 |
+
def test_sinpi_complex(self):
|
1943 |
+
assert_mpmath_equal(
|
1944 |
+
_sinpi,
|
1945 |
+
mpmath.sinpi,
|
1946 |
+
[ComplexArg()],
|
1947 |
+
nan_ok=False,
|
1948 |
+
rtol=2e-14,
|
1949 |
+
)
|
1950 |
+
|
1951 |
+
def test_shi(self):
|
1952 |
+
def shi(x):
|
1953 |
+
return sc.shichi(x)[0]
|
1954 |
+
assert_mpmath_equal(shi, mpmath.shi, [Arg()])
|
1955 |
+
# check asymptotic series cross-over
|
1956 |
+
assert_mpmath_equal(shi, mpmath.shi, [FixedArg([88 - 1e-9, 88, 88 + 1e-9])])
|
1957 |
+
|
1958 |
+
def test_shi_complex(self):
|
1959 |
+
def shi(z):
|
1960 |
+
return sc.shichi(z)[0]
|
1961 |
+
# shi oscillates as Im[z] -> +- inf, so limit range
|
1962 |
+
assert_mpmath_equal(
|
1963 |
+
shi,
|
1964 |
+
mpmath.shi,
|
1965 |
+
[ComplexArg(complex(-np.inf, -1e8), complex(np.inf, 1e8))],
|
1966 |
+
rtol=1e-12,
|
1967 |
+
)
|
1968 |
+
|
1969 |
+
def test_si(self):
|
1970 |
+
def si(x):
|
1971 |
+
return sc.sici(x)[0]
|
1972 |
+
assert_mpmath_equal(si, mpmath.si, [Arg()])
|
1973 |
+
|
1974 |
+
def test_si_complex(self):
|
1975 |
+
def si(z):
|
1976 |
+
return sc.sici(z)[0]
|
1977 |
+
# si oscillates as Re[z] -> +- inf, so limit range
|
1978 |
+
assert_mpmath_equal(
|
1979 |
+
si,
|
1980 |
+
mpmath.si,
|
1981 |
+
[ComplexArg(complex(-1e8, -np.inf), complex(1e8, np.inf))],
|
1982 |
+
rtol=1e-12,
|
1983 |
+
)
|
1984 |
+
|
1985 |
+
def test_spence(self):
|
1986 |
+
# mpmath uses a different convention for the dilogarithm
|
1987 |
+
def dilog(x):
|
1988 |
+
return mpmath.polylog(2, 1 - x)
|
1989 |
+
# Spence has a branch cut on the negative real axis
|
1990 |
+
assert_mpmath_equal(
|
1991 |
+
sc.spence,
|
1992 |
+
exception_to_nan(dilog),
|
1993 |
+
[Arg(0, np.inf)],
|
1994 |
+
rtol=1e-14,
|
1995 |
+
)
|
1996 |
+
|
1997 |
+
def test_spence_complex(self):
|
1998 |
+
def dilog(z):
|
1999 |
+
return mpmath.polylog(2, 1 - z)
|
2000 |
+
assert_mpmath_equal(
|
2001 |
+
sc.spence,
|
2002 |
+
exception_to_nan(dilog),
|
2003 |
+
[ComplexArg()],
|
2004 |
+
rtol=1e-14,
|
2005 |
+
)
|
2006 |
+
|
2007 |
+
def test_spherharm(self):
|
2008 |
+
def spherharm(l, m, theta, phi):
|
2009 |
+
if m > l:
|
2010 |
+
return np.nan
|
2011 |
+
return sc.sph_harm(m, l, phi, theta)
|
2012 |
+
assert_mpmath_equal(
|
2013 |
+
spherharm,
|
2014 |
+
mpmath.spherharm,
|
2015 |
+
[IntArg(0, 100), IntArg(0, 100), Arg(a=0, b=pi), Arg(a=0, b=2*pi)],
|
2016 |
+
atol=1e-8,
|
2017 |
+
n=6000,
|
2018 |
+
dps=150,
|
2019 |
+
)
|
2020 |
+
|
2021 |
+
def test_struveh(self):
|
2022 |
+
assert_mpmath_equal(
|
2023 |
+
sc.struve,
|
2024 |
+
exception_to_nan(mpmath.struveh),
|
2025 |
+
[Arg(-1e4, 1e4), Arg(0, 1e4)],
|
2026 |
+
rtol=5e-10,
|
2027 |
+
)
|
2028 |
+
|
2029 |
+
def test_struvel(self):
|
2030 |
+
def mp_struvel(v, z):
|
2031 |
+
if v < 0 and z < -v and abs(v) > 1000:
|
2032 |
+
# larger DPS needed for correct results
|
2033 |
+
old_dps = mpmath.mp.dps
|
2034 |
+
try:
|
2035 |
+
mpmath.mp.dps = 300
|
2036 |
+
return mpmath.struvel(v, z)
|
2037 |
+
finally:
|
2038 |
+
mpmath.mp.dps = old_dps
|
2039 |
+
return mpmath.struvel(v, z)
|
2040 |
+
|
2041 |
+
assert_mpmath_equal(
|
2042 |
+
sc.modstruve,
|
2043 |
+
exception_to_nan(mp_struvel),
|
2044 |
+
[Arg(-1e4, 1e4), Arg(0, 1e4)],
|
2045 |
+
rtol=5e-10,
|
2046 |
+
ignore_inf_sign=True,
|
2047 |
+
)
|
2048 |
+
|
2049 |
+
def test_wrightomega_real(self):
|
2050 |
+
def mpmath_wrightomega_real(x):
|
2051 |
+
return mpmath.lambertw(mpmath.exp(x), mpmath.mpf('-0.5'))
|
2052 |
+
|
2053 |
+
# For x < -1000 the Wright Omega function is just 0 to double
|
2054 |
+
# precision, and for x > 1e21 it is just x to double
|
2055 |
+
# precision.
|
2056 |
+
assert_mpmath_equal(
|
2057 |
+
sc.wrightomega,
|
2058 |
+
mpmath_wrightomega_real,
|
2059 |
+
[Arg(-1000, 1e21)],
|
2060 |
+
rtol=5e-15,
|
2061 |
+
atol=0,
|
2062 |
+
nan_ok=False,
|
2063 |
+
)
|
2064 |
+
|
2065 |
+
def test_wrightomega(self):
|
2066 |
+
assert_mpmath_equal(
|
2067 |
+
sc.wrightomega,
|
2068 |
+
lambda z: _mpmath_wrightomega(z, 25),
|
2069 |
+
[ComplexArg()],
|
2070 |
+
rtol=1e-14,
|
2071 |
+
nan_ok=False,
|
2072 |
+
)
|
2073 |
+
|
2074 |
+
def test_hurwitz_zeta(self):
|
2075 |
+
assert_mpmath_equal(
|
2076 |
+
sc.zeta,
|
2077 |
+
exception_to_nan(mpmath.zeta),
|
2078 |
+
[Arg(a=1, b=1e10, inclusive_a=False), Arg(a=0, inclusive_a=False)],
|
2079 |
+
)
|
2080 |
+
|
2081 |
+
def test_riemann_zeta(self):
|
2082 |
+
assert_mpmath_equal(
|
2083 |
+
sc.zeta,
|
2084 |
+
lambda x: mpmath.zeta(x) if x != 1 else mpmath.inf,
|
2085 |
+
[Arg(-100, 100)],
|
2086 |
+
nan_ok=False,
|
2087 |
+
rtol=5e-13,
|
2088 |
+
)
|
2089 |
+
|
2090 |
+
def test_zetac(self):
|
2091 |
+
assert_mpmath_equal(
|
2092 |
+
sc.zetac,
|
2093 |
+
lambda x: mpmath.zeta(x) - 1 if x != 1 else mpmath.inf,
|
2094 |
+
[Arg(-100, 100)],
|
2095 |
+
nan_ok=False,
|
2096 |
+
dps=45,
|
2097 |
+
rtol=5e-13,
|
2098 |
+
)
|
2099 |
+
|
2100 |
+
def test_boxcox(self):
|
2101 |
+
|
2102 |
+
def mp_boxcox(x, lmbda):
|
2103 |
+
x = mpmath.mp.mpf(x)
|
2104 |
+
lmbda = mpmath.mp.mpf(lmbda)
|
2105 |
+
if lmbda == 0:
|
2106 |
+
return mpmath.mp.log(x)
|
2107 |
+
else:
|
2108 |
+
return mpmath.mp.powm1(x, lmbda) / lmbda
|
2109 |
+
|
2110 |
+
assert_mpmath_equal(
|
2111 |
+
sc.boxcox,
|
2112 |
+
exception_to_nan(mp_boxcox),
|
2113 |
+
[Arg(a=0, inclusive_a=False), Arg()],
|
2114 |
+
n=200,
|
2115 |
+
dps=60,
|
2116 |
+
rtol=1e-13,
|
2117 |
+
)
|
2118 |
+
|
2119 |
+
def test_boxcox1p(self):
|
2120 |
+
|
2121 |
+
def mp_boxcox1p(x, lmbda):
|
2122 |
+
x = mpmath.mp.mpf(x)
|
2123 |
+
lmbda = mpmath.mp.mpf(lmbda)
|
2124 |
+
one = mpmath.mp.mpf(1)
|
2125 |
+
if lmbda == 0:
|
2126 |
+
return mpmath.mp.log(one + x)
|
2127 |
+
else:
|
2128 |
+
return mpmath.mp.powm1(one + x, lmbda) / lmbda
|
2129 |
+
|
2130 |
+
assert_mpmath_equal(
|
2131 |
+
sc.boxcox1p,
|
2132 |
+
exception_to_nan(mp_boxcox1p),
|
2133 |
+
[Arg(a=-1, inclusive_a=False), Arg()],
|
2134 |
+
n=200,
|
2135 |
+
dps=60,
|
2136 |
+
rtol=1e-13,
|
2137 |
+
)
|
2138 |
+
|
2139 |
+
def test_spherical_jn(self):
|
2140 |
+
def mp_spherical_jn(n, z):
|
2141 |
+
arg = mpmath.mpmathify(z)
|
2142 |
+
out = (mpmath.besselj(n + mpmath.mpf(1)/2, arg) /
|
2143 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2144 |
+
if arg.imag == 0:
|
2145 |
+
return out.real
|
2146 |
+
else:
|
2147 |
+
return out
|
2148 |
+
|
2149 |
+
assert_mpmath_equal(
|
2150 |
+
lambda n, z: sc.spherical_jn(int(n), z),
|
2151 |
+
exception_to_nan(mp_spherical_jn),
|
2152 |
+
[IntArg(0, 200), Arg(-1e8, 1e8)],
|
2153 |
+
dps=300,
|
2154 |
+
)
|
2155 |
+
|
2156 |
+
def test_spherical_jn_complex(self):
|
2157 |
+
def mp_spherical_jn(n, z):
|
2158 |
+
arg = mpmath.mpmathify(z)
|
2159 |
+
out = (mpmath.besselj(n + mpmath.mpf(1)/2, arg) /
|
2160 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2161 |
+
if arg.imag == 0:
|
2162 |
+
return out.real
|
2163 |
+
else:
|
2164 |
+
return out
|
2165 |
+
|
2166 |
+
assert_mpmath_equal(
|
2167 |
+
lambda n, z: sc.spherical_jn(int(n.real), z),
|
2168 |
+
exception_to_nan(mp_spherical_jn),
|
2169 |
+
[IntArg(0, 200), ComplexArg()]
|
2170 |
+
)
|
2171 |
+
|
2172 |
+
def test_spherical_yn(self):
|
2173 |
+
def mp_spherical_yn(n, z):
|
2174 |
+
arg = mpmath.mpmathify(z)
|
2175 |
+
out = (mpmath.bessely(n + mpmath.mpf(1)/2, arg) /
|
2176 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2177 |
+
if arg.imag == 0:
|
2178 |
+
return out.real
|
2179 |
+
else:
|
2180 |
+
return out
|
2181 |
+
|
2182 |
+
assert_mpmath_equal(
|
2183 |
+
lambda n, z: sc.spherical_yn(int(n), z),
|
2184 |
+
exception_to_nan(mp_spherical_yn),
|
2185 |
+
[IntArg(0, 200), Arg(-1e10, 1e10)],
|
2186 |
+
dps=100,
|
2187 |
+
)
|
2188 |
+
|
2189 |
+
def test_spherical_yn_complex(self):
|
2190 |
+
def mp_spherical_yn(n, z):
|
2191 |
+
arg = mpmath.mpmathify(z)
|
2192 |
+
out = (mpmath.bessely(n + mpmath.mpf(1)/2, arg) /
|
2193 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2194 |
+
if arg.imag == 0:
|
2195 |
+
return out.real
|
2196 |
+
else:
|
2197 |
+
return out
|
2198 |
+
|
2199 |
+
assert_mpmath_equal(
|
2200 |
+
lambda n, z: sc.spherical_yn(int(n.real), z),
|
2201 |
+
exception_to_nan(mp_spherical_yn),
|
2202 |
+
[IntArg(0, 200), ComplexArg()],
|
2203 |
+
)
|
2204 |
+
|
2205 |
+
def test_spherical_in(self):
|
2206 |
+
def mp_spherical_in(n, z):
|
2207 |
+
arg = mpmath.mpmathify(z)
|
2208 |
+
out = (mpmath.besseli(n + mpmath.mpf(1)/2, arg) /
|
2209 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2210 |
+
if arg.imag == 0:
|
2211 |
+
return out.real
|
2212 |
+
else:
|
2213 |
+
return out
|
2214 |
+
|
2215 |
+
assert_mpmath_equal(
|
2216 |
+
lambda n, z: sc.spherical_in(int(n), z),
|
2217 |
+
exception_to_nan(mp_spherical_in),
|
2218 |
+
[IntArg(0, 200), Arg()],
|
2219 |
+
dps=200,
|
2220 |
+
atol=10**(-278),
|
2221 |
+
)
|
2222 |
+
|
2223 |
+
def test_spherical_in_complex(self):
|
2224 |
+
def mp_spherical_in(n, z):
|
2225 |
+
arg = mpmath.mpmathify(z)
|
2226 |
+
out = (mpmath.besseli(n + mpmath.mpf(1)/2, arg) /
|
2227 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2228 |
+
if arg.imag == 0:
|
2229 |
+
return out.real
|
2230 |
+
else:
|
2231 |
+
return out
|
2232 |
+
|
2233 |
+
assert_mpmath_equal(
|
2234 |
+
lambda n, z: sc.spherical_in(int(n.real), z),
|
2235 |
+
exception_to_nan(mp_spherical_in),
|
2236 |
+
[IntArg(0, 200), ComplexArg()],
|
2237 |
+
)
|
2238 |
+
|
2239 |
+
def test_spherical_kn(self):
|
2240 |
+
def mp_spherical_kn(n, z):
|
2241 |
+
out = (mpmath.besselk(n + mpmath.mpf(1)/2, z) *
|
2242 |
+
mpmath.sqrt(mpmath.pi/(2*mpmath.mpmathify(z))))
|
2243 |
+
if mpmath.mpmathify(z).imag == 0:
|
2244 |
+
return out.real
|
2245 |
+
else:
|
2246 |
+
return out
|
2247 |
+
|
2248 |
+
assert_mpmath_equal(
|
2249 |
+
lambda n, z: sc.spherical_kn(int(n), z),
|
2250 |
+
exception_to_nan(mp_spherical_kn),
|
2251 |
+
[IntArg(0, 150), Arg()],
|
2252 |
+
dps=100,
|
2253 |
+
)
|
2254 |
+
|
2255 |
+
@pytest.mark.xfail(run=False,
|
2256 |
+
reason="Accuracy issues near z = -1 inherited from kv.")
|
2257 |
+
def test_spherical_kn_complex(self):
|
2258 |
+
def mp_spherical_kn(n, z):
|
2259 |
+
arg = mpmath.mpmathify(z)
|
2260 |
+
out = (mpmath.besselk(n + mpmath.mpf(1)/2, arg) /
|
2261 |
+
mpmath.sqrt(2*arg/mpmath.pi))
|
2262 |
+
if arg.imag == 0:
|
2263 |
+
return out.real
|
2264 |
+
else:
|
2265 |
+
return out
|
2266 |
+
|
2267 |
+
assert_mpmath_equal(
|
2268 |
+
lambda n, z: sc.spherical_kn(int(n.real), z),
|
2269 |
+
exception_to_nan(mp_spherical_kn),
|
2270 |
+
[IntArg(0, 200), ComplexArg()],
|
2271 |
+
dps=200,
|
2272 |
+
)
|