Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- parrot/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_round.cpython-310.pyc +0 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_boxcox.py +125 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_cython_special.py +365 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_ellip_harm.py +278 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_erfinv.py +89 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_gamma.py +12 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_hypergeometric.py +172 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_mpmath.py +2272 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_nan_inputs.py +64 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_orthogonal.py +804 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_pdtr.py +48 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_precompute_expn_asy.py +24 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_trig.py +72 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_wrightomega.py +117 -0
- parrot/lib/python3.10/site-packages/scipy/special/tests/test_zeta.py +49 -0
- vllm/lib/python3.10/site-packages/opencensus/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/__init__.py +1 -0
- vllm/lib/python3.10/site-packages/opencensus/common/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/backports/__init__.py +79 -0
- vllm/lib/python3.10/site-packages/opencensus/common/backports/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/configuration/__init__.py +46 -0
- vllm/lib/python3.10/site-packages/opencensus/common/configuration/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/http_handler/__init__.py +43 -0
- vllm/lib/python3.10/site-packages/opencensus/common/http_handler/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__init__.py +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/aws_identity_doc_utils.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/gcp_metadata_config.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/k8s_utils.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/monitored_resource.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/aws_identity_doc_utils.py +95 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/gcp_metadata_config.py +115 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/k8s_utils.py +64 -0
- vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/monitored_resource.py +68 -0
- vllm/lib/python3.10/site-packages/opencensus/common/resource/__init__.py +218 -0
- vllm/lib/python3.10/site-packages/opencensus/common/resource/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/__init__.py +177 -0
- vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/__pycache__/version.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/version.py +15 -0
- vllm/lib/python3.10/site-packages/opencensus/common/schedule/__init__.py +149 -0
- vllm/lib/python3.10/site-packages/opencensus/common/schedule/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/__init__.py +13 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/async_.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/base.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/sync.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/async_.py +231 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/base.py +31 -0
- vllm/lib/python3.10/site-packages/opencensus/common/transports/sync.py +28 -0
parrot/lib/python3.10/site-packages/scipy/special/tests/__pycache__/test_round.cpython-310.pyc
ADDED
|
Binary file (715 Bytes). View file
|
|
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_boxcox.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# There are more tests of boxcox and boxcox1p in test_mpmath.py.
|
| 8 |
+
|
| 9 |
+
def test_boxcox_basic():
|
| 10 |
+
x = np.array([0.5, 1, 2, 4])
|
| 11 |
+
|
| 12 |
+
# lambda = 0 => y = log(x)
|
| 13 |
+
y = boxcox(x, 0)
|
| 14 |
+
assert_almost_equal(y, np.log(x))
|
| 15 |
+
|
| 16 |
+
# lambda = 1 => y = x - 1
|
| 17 |
+
y = boxcox(x, 1)
|
| 18 |
+
assert_almost_equal(y, x - 1)
|
| 19 |
+
|
| 20 |
+
# lambda = 2 => y = 0.5*(x**2 - 1)
|
| 21 |
+
y = boxcox(x, 2)
|
| 22 |
+
assert_almost_equal(y, 0.5*(x**2 - 1))
|
| 23 |
+
|
| 24 |
+
# x = 0 and lambda > 0 => y = -1 / lambda
|
| 25 |
+
lam = np.array([0.5, 1, 2])
|
| 26 |
+
y = boxcox(0, lam)
|
| 27 |
+
assert_almost_equal(y, -1.0 / lam)
|
| 28 |
+
|
| 29 |
+
def test_boxcox_underflow():
|
| 30 |
+
x = 1 + 1e-15
|
| 31 |
+
lmbda = 1e-306
|
| 32 |
+
y = boxcox(x, lmbda)
|
| 33 |
+
assert_allclose(y, np.log(x), rtol=1e-14)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_boxcox_nonfinite():
|
| 37 |
+
# x < 0 => y = nan
|
| 38 |
+
x = np.array([-1, -1, -0.5])
|
| 39 |
+
y = boxcox(x, [0.5, 2.0, -1.5])
|
| 40 |
+
assert_equal(y, np.array([np.nan, np.nan, np.nan]))
|
| 41 |
+
|
| 42 |
+
# x = 0 and lambda <= 0 => y = -inf
|
| 43 |
+
x = 0
|
| 44 |
+
y = boxcox(x, [-2.5, 0])
|
| 45 |
+
assert_equal(y, np.array([-np.inf, -np.inf]))
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_boxcox1p_basic():
|
| 49 |
+
x = np.array([-0.25, -1e-20, 0, 1e-20, 0.25, 1, 3])
|
| 50 |
+
|
| 51 |
+
# lambda = 0 => y = log(1+x)
|
| 52 |
+
y = boxcox1p(x, 0)
|
| 53 |
+
assert_almost_equal(y, np.log1p(x))
|
| 54 |
+
|
| 55 |
+
# lambda = 1 => y = x
|
| 56 |
+
y = boxcox1p(x, 1)
|
| 57 |
+
assert_almost_equal(y, x)
|
| 58 |
+
|
| 59 |
+
# lambda = 2 => y = 0.5*((1+x)**2 - 1) = 0.5*x*(2 + x)
|
| 60 |
+
y = boxcox1p(x, 2)
|
| 61 |
+
assert_almost_equal(y, 0.5*x*(2 + x))
|
| 62 |
+
|
| 63 |
+
# x = -1 and lambda > 0 => y = -1 / lambda
|
| 64 |
+
lam = np.array([0.5, 1, 2])
|
| 65 |
+
y = boxcox1p(-1, lam)
|
| 66 |
+
assert_almost_equal(y, -1.0 / lam)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_boxcox1p_underflow():
|
| 70 |
+
x = np.array([1e-15, 1e-306])
|
| 71 |
+
lmbda = np.array([1e-306, 1e-18])
|
| 72 |
+
y = boxcox1p(x, lmbda)
|
| 73 |
+
assert_allclose(y, np.log1p(x), rtol=1e-14)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_boxcox1p_nonfinite():
|
| 77 |
+
# x < -1 => y = nan
|
| 78 |
+
x = np.array([-2, -2, -1.5])
|
| 79 |
+
y = boxcox1p(x, [0.5, 2.0, -1.5])
|
| 80 |
+
assert_equal(y, np.array([np.nan, np.nan, np.nan]))
|
| 81 |
+
|
| 82 |
+
# x = -1 and lambda <= 0 => y = -inf
|
| 83 |
+
x = -1
|
| 84 |
+
y = boxcox1p(x, [-2.5, 0])
|
| 85 |
+
assert_equal(y, np.array([-np.inf, -np.inf]))
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_inv_boxcox():
|
| 89 |
+
x = np.array([0., 1., 2.])
|
| 90 |
+
lam = np.array([0., 1., 2.])
|
| 91 |
+
y = boxcox(x, lam)
|
| 92 |
+
x2 = inv_boxcox(y, lam)
|
| 93 |
+
assert_almost_equal(x, x2)
|
| 94 |
+
|
| 95 |
+
x = np.array([0., 1., 2.])
|
| 96 |
+
lam = np.array([0., 1., 2.])
|
| 97 |
+
y = boxcox1p(x, lam)
|
| 98 |
+
x2 = inv_boxcox1p(y, lam)
|
| 99 |
+
assert_almost_equal(x, x2)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_inv_boxcox1p_underflow():
|
| 103 |
+
x = 1e-15
|
| 104 |
+
lam = 1e-306
|
| 105 |
+
y = inv_boxcox1p(x, lam)
|
| 106 |
+
assert_allclose(y, x, rtol=1e-14)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@pytest.mark.parametrize(
|
| 110 |
+
"x, lmb",
|
| 111 |
+
[[100, 155],
|
| 112 |
+
[0.01, -155]]
|
| 113 |
+
)
|
| 114 |
+
def test_boxcox_premature_overflow(x, lmb):
|
| 115 |
+
# test boxcox & inv_boxcox
|
| 116 |
+
y = boxcox(x, lmb)
|
| 117 |
+
assert np.isfinite(y)
|
| 118 |
+
x_inv = inv_boxcox(y, lmb)
|
| 119 |
+
assert_allclose(x, x_inv)
|
| 120 |
+
|
| 121 |
+
# test boxcox1p & inv_boxcox1p
|
| 122 |
+
y1p = boxcox1p(x-1, lmb)
|
| 123 |
+
assert np.isfinite(y1p)
|
|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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.log_wright_bessel, cython_special.log_wright_bessel, ('ddd',), None),
|
| 199 |
+
(special.ndtri_exp, cython_special.ndtri_exp, ('d',), None),
|
| 200 |
+
(special.loggamma, cython_special.loggamma, ('D',), None),
|
| 201 |
+
(special.logit, cython_special.logit, ('f', 'd', 'g'), None),
|
| 202 |
+
(special.lpmv, cython_special.lpmv, ('ddd',), None),
|
| 203 |
+
(special.mathieu_a, cython_special.mathieu_a, ('dd',), None),
|
| 204 |
+
(special.mathieu_b, cython_special.mathieu_b, ('dd',), None),
|
| 205 |
+
(special.mathieu_cem, cython_special._mathieu_cem_pywrap, ('ddd',), None),
|
| 206 |
+
(special.mathieu_modcem1, cython_special._mathieu_modcem1_pywrap, ('ddd',), None),
|
| 207 |
+
(special.mathieu_modcem2, cython_special._mathieu_modcem2_pywrap, ('ddd',), None),
|
| 208 |
+
(special.mathieu_modsem1, cython_special._mathieu_modsem1_pywrap, ('ddd',), None),
|
| 209 |
+
(special.mathieu_modsem2, cython_special._mathieu_modsem2_pywrap, ('ddd',), None),
|
| 210 |
+
(special.mathieu_sem, cython_special._mathieu_sem_pywrap, ('ddd',), None),
|
| 211 |
+
(special.modfresnelm, cython_special._modfresnelm_pywrap, ('d',), None),
|
| 212 |
+
(special.modfresnelp, cython_special._modfresnelp_pywrap, ('d',), None),
|
| 213 |
+
(special.modstruve, cython_special.modstruve, ('dd',), None),
|
| 214 |
+
(special.nbdtr, cython_special.nbdtr, ('lld', 'ddd'), None),
|
| 215 |
+
(special.nbdtrc, cython_special.nbdtrc, ('lld', 'ddd'), None),
|
| 216 |
+
(special.nbdtri, cython_special.nbdtri, ('lld', 'ddd'), None),
|
| 217 |
+
(special.nbdtrik, cython_special.nbdtrik, ('ddd',), None),
|
| 218 |
+
(special.nbdtrin, cython_special.nbdtrin, ('ddd',), None),
|
| 219 |
+
(special.ncfdtr, cython_special.ncfdtr, ('dddd',), None),
|
| 220 |
+
(special.ncfdtri, cython_special.ncfdtri, ('dddd',), None),
|
| 221 |
+
(special.ncfdtridfd, cython_special.ncfdtridfd, ('dddd',), None),
|
| 222 |
+
(special.ncfdtridfn, cython_special.ncfdtridfn, ('dddd',), None),
|
| 223 |
+
(special.ncfdtrinc, cython_special.ncfdtrinc, ('dddd',), None),
|
| 224 |
+
(special.nctdtr, cython_special.nctdtr, ('ddd',), None),
|
| 225 |
+
(special.nctdtridf, cython_special.nctdtridf, ('ddd',), None),
|
| 226 |
+
(special.nctdtrinc, cython_special.nctdtrinc, ('ddd',), None),
|
| 227 |
+
(special.nctdtrit, cython_special.nctdtrit, ('ddd',), None),
|
| 228 |
+
(special.ndtr, cython_special.ndtr, ('d', 'D'), None),
|
| 229 |
+
(special.ndtri, cython_special.ndtri, ('d',), None),
|
| 230 |
+
(special.nrdtrimn, cython_special.nrdtrimn, ('ddd',), None),
|
| 231 |
+
(special.nrdtrisd, cython_special.nrdtrisd, ('ddd',), None),
|
| 232 |
+
(special.obl_ang1, cython_special._obl_ang1_pywrap, ('dddd',), None),
|
| 233 |
+
(special.obl_ang1_cv, cython_special._obl_ang1_cv_pywrap, ('ddddd',), None),
|
| 234 |
+
(special.obl_cv, cython_special.obl_cv, ('ddd',), None),
|
| 235 |
+
(special.obl_rad1, cython_special._obl_rad1_pywrap, ('dddd',), "see gh-6211"),
|
| 236 |
+
(special.obl_rad1_cv, cython_special._obl_rad1_cv_pywrap, ('ddddd',),
|
| 237 |
+
"see gh-6211"),
|
| 238 |
+
(special.obl_rad2, cython_special._obl_rad2_pywrap, ('dddd',), "see gh-6211"),
|
| 239 |
+
(special.obl_rad2_cv, cython_special._obl_rad2_cv_pywrap, ('ddddd',),
|
| 240 |
+
"see gh-6211"),
|
| 241 |
+
(special.pbdv, cython_special._pbdv_pywrap, ('dd',), None),
|
| 242 |
+
(special.pbvv, cython_special._pbvv_pywrap, ('dd',), None),
|
| 243 |
+
(special.pbwa, cython_special._pbwa_pywrap, ('dd',), None),
|
| 244 |
+
(special.pdtr, cython_special.pdtr, ('dd', 'dd'), None),
|
| 245 |
+
(special.pdtrc, cython_special.pdtrc, ('dd', 'dd'), None),
|
| 246 |
+
(special.pdtri, cython_special.pdtri, ('ld', 'dd'), None),
|
| 247 |
+
(special.pdtrik, cython_special.pdtrik, ('dd',), None),
|
| 248 |
+
(special.poch, cython_special.poch, ('dd',), None),
|
| 249 |
+
(special.powm1, cython_special.powm1, ('dd',), None),
|
| 250 |
+
(special.pro_ang1, cython_special._pro_ang1_pywrap, ('dddd',), None),
|
| 251 |
+
(special.pro_ang1_cv, cython_special._pro_ang1_cv_pywrap, ('ddddd',), None),
|
| 252 |
+
(special.pro_cv, cython_special.pro_cv, ('ddd',), None),
|
| 253 |
+
(special.pro_rad1, cython_special._pro_rad1_pywrap, ('dddd',), "see gh-6211"),
|
| 254 |
+
(special.pro_rad1_cv, cython_special._pro_rad1_cv_pywrap, ('ddddd',),
|
| 255 |
+
"see gh-6211"),
|
| 256 |
+
(special.pro_rad2, cython_special._pro_rad2_pywrap, ('dddd',), "see gh-6211"),
|
| 257 |
+
(special.pro_rad2_cv, cython_special._pro_rad2_cv_pywrap, ('ddddd',),
|
| 258 |
+
"see gh-6211"),
|
| 259 |
+
(special.pseudo_huber, cython_special.pseudo_huber, ('dd',), None),
|
| 260 |
+
(special.psi, cython_special.psi, ('d', 'D'), None),
|
| 261 |
+
(special.radian, cython_special.radian, ('ddd',), None),
|
| 262 |
+
(special.rel_entr, cython_special.rel_entr, ('dd',), None),
|
| 263 |
+
(special.rgamma, cython_special.rgamma, ('d', 'D'), None),
|
| 264 |
+
(special.round, cython_special.round, ('d',), None),
|
| 265 |
+
(special.spherical_jn, cython_special.spherical_jn, ('ld', 'ldb', 'lD', 'lDb'),
|
| 266 |
+
None),
|
| 267 |
+
(special.spherical_yn, cython_special.spherical_yn, ('ld', 'ldb', 'lD', 'lDb'),
|
| 268 |
+
None),
|
| 269 |
+
(special.spherical_in, cython_special.spherical_in, ('ld', 'ldb', 'lD', 'lDb'),
|
| 270 |
+
None),
|
| 271 |
+
(special.spherical_kn, cython_special.spherical_kn, ('ld', 'ldb', 'lD', 'lDb'),
|
| 272 |
+
None),
|
| 273 |
+
(special.shichi, cython_special._shichi_pywrap, ('d', 'D'), None),
|
| 274 |
+
(special.sici, cython_special._sici_pywrap, ('d', 'D'), None),
|
| 275 |
+
(special.sindg, cython_special.sindg, ('d',), None),
|
| 276 |
+
(special.smirnov, cython_special.smirnov, ('ld', 'dd'), None),
|
| 277 |
+
(special.smirnovi, cython_special.smirnovi, ('ld', 'dd'), None),
|
| 278 |
+
(special.spence, cython_special.spence, ('d', 'D'), None),
|
| 279 |
+
(special.sph_harm, cython_special.sph_harm, ('lldd', 'dddd'), None),
|
| 280 |
+
(special.stdtr, cython_special.stdtr, ('dd',), None),
|
| 281 |
+
(special.stdtridf, cython_special.stdtridf, ('dd',), None),
|
| 282 |
+
(special.stdtrit, cython_special.stdtrit, ('dd',), None),
|
| 283 |
+
(special.struve, cython_special.struve, ('dd',), None),
|
| 284 |
+
(special.tandg, cython_special.tandg, ('d',), None),
|
| 285 |
+
(special.tklmbda, cython_special.tklmbda, ('dd',), None),
|
| 286 |
+
(special.voigt_profile, cython_special.voigt_profile, ('ddd',), None),
|
| 287 |
+
(special.wofz, cython_special.wofz, ('D',), None),
|
| 288 |
+
(special.wright_bessel, cython_special.wright_bessel, ('ddd',), None),
|
| 289 |
+
(special.wrightomega, cython_special.wrightomega, ('D',), None),
|
| 290 |
+
(special.xlog1py, cython_special.xlog1py, ('dd', 'DD'), None),
|
| 291 |
+
(special.xlogy, cython_special.xlogy, ('dd', 'DD'), None),
|
| 292 |
+
(special.y0, cython_special.y0, ('d',), None),
|
| 293 |
+
(special.y1, cython_special.y1, ('d',), None),
|
| 294 |
+
(special.yn, cython_special.yn, ('ld', 'dd'), None),
|
| 295 |
+
(special.yv, cython_special.yv, ('dd', 'dD'), None),
|
| 296 |
+
(special.yve, cython_special.yve, ('dd', 'dD'), None),
|
| 297 |
+
(special.zetac, cython_special.zetac, ('d',), None),
|
| 298 |
+
(special.owens_t, cython_special.owens_t, ('dd',), None)
|
| 299 |
+
]
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
IDS = [x[0].__name__ for x in PARAMS]
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def _generate_test_points(typecodes):
|
| 306 |
+
axes = tuple(TEST_POINTS[x] for x in typecodes)
|
| 307 |
+
pts = list(product(*axes))
|
| 308 |
+
return pts
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def test_cython_api_completeness():
|
| 312 |
+
# Check that everything is tested
|
| 313 |
+
for name in dir(cython_special):
|
| 314 |
+
func = getattr(cython_special, name)
|
| 315 |
+
if callable(func) and not name.startswith('_'):
|
| 316 |
+
for _, cyfun, _, _ in PARAMS:
|
| 317 |
+
if cyfun is func:
|
| 318 |
+
break
|
| 319 |
+
else:
|
| 320 |
+
raise RuntimeError(f"{name} missing from tests!")
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
@pytest.mark.fail_slow(5)
|
| 324 |
+
@pytest.mark.parametrize("param", PARAMS, ids=IDS)
|
| 325 |
+
def test_cython_api(param):
|
| 326 |
+
pyfunc, cyfunc, specializations, knownfailure = param
|
| 327 |
+
if knownfailure:
|
| 328 |
+
pytest.xfail(reason=knownfailure)
|
| 329 |
+
|
| 330 |
+
# Check which parameters are expected to be fused types
|
| 331 |
+
max_params = max(len(spec) for spec in specializations)
|
| 332 |
+
values = [set() for _ in range(max_params)]
|
| 333 |
+
for typecodes in specializations:
|
| 334 |
+
for j, v in enumerate(typecodes):
|
| 335 |
+
values[j].add(v)
|
| 336 |
+
seen = set()
|
| 337 |
+
is_fused_code = [False] * len(values)
|
| 338 |
+
for j, v in enumerate(values):
|
| 339 |
+
vv = tuple(sorted(v))
|
| 340 |
+
if vv in seen:
|
| 341 |
+
continue
|
| 342 |
+
is_fused_code[j] = (len(v) > 1)
|
| 343 |
+
seen.add(vv)
|
| 344 |
+
|
| 345 |
+
# Check results
|
| 346 |
+
for typecodes in specializations:
|
| 347 |
+
# Pick the correct specialized function
|
| 348 |
+
signature = [CYTHON_SIGNATURE_MAP[code]
|
| 349 |
+
for j, code in enumerate(typecodes)
|
| 350 |
+
if is_fused_code[j]]
|
| 351 |
+
|
| 352 |
+
if signature:
|
| 353 |
+
cy_spec_func = cyfunc[tuple(signature)]
|
| 354 |
+
else:
|
| 355 |
+
signature = None
|
| 356 |
+
cy_spec_func = cyfunc
|
| 357 |
+
|
| 358 |
+
# Test it
|
| 359 |
+
pts = _generate_test_points(typecodes)
|
| 360 |
+
for pt in pts:
|
| 361 |
+
with suppress_warnings() as sup:
|
| 362 |
+
sup.filter(DeprecationWarning)
|
| 363 |
+
pyval = pyfunc(*pt)
|
| 364 |
+
cyval = cy_spec_func(*pt)
|
| 365 |
+
assert_allclose(cyval, pyval, err_msg=f"{pt} {typecodes} {signature}")
|
parrot/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)
|
parrot/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)
|
parrot/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)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_hypergeometric.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 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 |
+
# In the following tests with complex z, the reference values
|
| 121 |
+
# were computed with mpmath.hyp1f1(a, b, z), and verified with
|
| 122 |
+
# Wolfram Alpha Hypergeometric1F1(a, b, z), except for the
|
| 123 |
+
# case a=0.1, b=1, z=7-24j, where Wolfram Alpha reported
|
| 124 |
+
# "Standard computation time exceeded". That reference value
|
| 125 |
+
# was confirmed in an online Matlab session, with the commands
|
| 126 |
+
#
|
| 127 |
+
# > format long
|
| 128 |
+
# > hypergeom(0.1, 1, 7-24i)
|
| 129 |
+
# ans =
|
| 130 |
+
# -3.712349651834209 + 4.554636556672912i
|
| 131 |
+
#
|
| 132 |
+
@pytest.mark.parametrize(
|
| 133 |
+
'a, b, z, ref',
|
| 134 |
+
[(-0.25, 0.5, 1+2j, 1.1814553180903435-1.2792130661292984j),
|
| 135 |
+
(0.25, 0.5, 1+2j, 0.24636797405707597+1.293434354945675j),
|
| 136 |
+
(25, 1.5, -2j, -516.1771262822523+407.04142751922024j),
|
| 137 |
+
(12, -1.5, -10+20j, -5098507.422706547-1341962.8043508842j),
|
| 138 |
+
pytest.param(
|
| 139 |
+
10, 250, 10-15j, 1.1985998416598884-0.8613474402403436j,
|
| 140 |
+
marks=pytest.mark.xfail,
|
| 141 |
+
),
|
| 142 |
+
pytest.param(
|
| 143 |
+
0.1, 1, 7-24j, -3.712349651834209+4.554636556672913j,
|
| 144 |
+
marks=pytest.mark.xfail,
|
| 145 |
+
)
|
| 146 |
+
],
|
| 147 |
+
)
|
| 148 |
+
def test_complex_z(self, a, b, z, ref):
|
| 149 |
+
h = sc.hyp1f1(a, b, z)
|
| 150 |
+
assert_allclose(h, ref, rtol=4e-15)
|
| 151 |
+
|
| 152 |
+
# The "legacy edge cases" mentioned in the comments in the following
|
| 153 |
+
# tests refers to the behavior of hyp1f1(a, b, x) when b is a nonpositive
|
| 154 |
+
# integer. In some subcases, the behavior of SciPy does not match that
|
| 155 |
+
# of Boost (1.81+), mpmath and Mathematica (via Wolfram Alpha online).
|
| 156 |
+
# If the handling of these edges cases is changed to agree with those
|
| 157 |
+
# libraries, these test will have to be updated.
|
| 158 |
+
|
| 159 |
+
@pytest.mark.parametrize('b', [0, -1, -5])
|
| 160 |
+
def test_legacy_case1(self, b):
|
| 161 |
+
# Test results of hyp1f1(0, n, x) for n <= 0.
|
| 162 |
+
# This is a legacy edge case.
|
| 163 |
+
# Boost (versions greater than 1.80), Mathematica (via Wolfram Alpha
|
| 164 |
+
# online) and mpmath all return 1 in this case, but SciPy's hyp1f1
|
| 165 |
+
# returns inf.
|
| 166 |
+
assert_equal(sc.hyp1f1(0, b, [-1.5, 0, 1.5]), [np.inf, np.inf, np.inf])
|
| 167 |
+
|
| 168 |
+
def test_legacy_case2(self):
|
| 169 |
+
# This is a legacy edge case.
|
| 170 |
+
# In software such as boost (1.81+), mpmath and Mathematica,
|
| 171 |
+
# the value is 1.
|
| 172 |
+
assert sc.hyp1f1(-4, -3, 0) == np.inf
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_mpmath.py
ADDED
|
@@ -0,0 +1,2272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
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 |
+
)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_nan_inputs.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test how the ufuncs in special handle nan inputs.
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
from typing import Callable
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
from numpy.testing import assert_array_equal, assert_, suppress_warnings
|
| 8 |
+
import pytest
|
| 9 |
+
import scipy.special as sc
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
KNOWNFAILURES: dict[str, Callable] = {}
|
| 13 |
+
|
| 14 |
+
POSTPROCESSING: dict[str, Callable] = {}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _get_ufuncs():
|
| 18 |
+
ufuncs = []
|
| 19 |
+
ufunc_names = []
|
| 20 |
+
for name in sorted(sc.__dict__):
|
| 21 |
+
obj = sc.__dict__[name]
|
| 22 |
+
if not isinstance(obj, np.ufunc):
|
| 23 |
+
continue
|
| 24 |
+
msg = KNOWNFAILURES.get(obj)
|
| 25 |
+
if msg is None:
|
| 26 |
+
ufuncs.append(obj)
|
| 27 |
+
ufunc_names.append(name)
|
| 28 |
+
else:
|
| 29 |
+
fail = pytest.mark.xfail(run=False, reason=msg)
|
| 30 |
+
ufuncs.append(pytest.param(obj, marks=fail))
|
| 31 |
+
ufunc_names.append(name)
|
| 32 |
+
return ufuncs, ufunc_names
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
UFUNCS, UFUNC_NAMES = _get_ufuncs()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@pytest.mark.parametrize("func", UFUNCS, ids=UFUNC_NAMES)
|
| 39 |
+
def test_nan_inputs(func):
|
| 40 |
+
args = (np.nan,)*func.nin
|
| 41 |
+
with suppress_warnings() as sup:
|
| 42 |
+
# Ignore warnings about unsafe casts from legacy wrappers
|
| 43 |
+
sup.filter(RuntimeWarning,
|
| 44 |
+
"floating point number truncated to an integer")
|
| 45 |
+
try:
|
| 46 |
+
with suppress_warnings() as sup:
|
| 47 |
+
sup.filter(DeprecationWarning)
|
| 48 |
+
res = func(*args)
|
| 49 |
+
except TypeError:
|
| 50 |
+
# One of the arguments doesn't take real inputs
|
| 51 |
+
return
|
| 52 |
+
if func in POSTPROCESSING:
|
| 53 |
+
res = POSTPROCESSING[func](*res)
|
| 54 |
+
|
| 55 |
+
msg = f"got {res} instead of nan"
|
| 56 |
+
assert_array_equal(np.isnan(res), True, err_msg=msg)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_legacy_cast():
|
| 60 |
+
with suppress_warnings() as sup:
|
| 61 |
+
sup.filter(RuntimeWarning,
|
| 62 |
+
"floating point number truncated to an integer")
|
| 63 |
+
res = sc.bdtrc(np.nan, 1, 0.5)
|
| 64 |
+
assert_(np.isnan(res))
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_orthogonal.py
ADDED
|
@@ -0,0 +1,804 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy import array, sqrt
|
| 3 |
+
from numpy.testing import (assert_array_almost_equal, assert_equal,
|
| 4 |
+
assert_almost_equal, assert_allclose)
|
| 5 |
+
from pytest import raises as assert_raises
|
| 6 |
+
|
| 7 |
+
from scipy import integrate
|
| 8 |
+
import scipy.special as sc
|
| 9 |
+
from scipy.special import gamma
|
| 10 |
+
import scipy.special._orthogonal as orth
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestCheby:
|
| 14 |
+
def test_chebyc(self):
|
| 15 |
+
C0 = orth.chebyc(0)
|
| 16 |
+
C1 = orth.chebyc(1)
|
| 17 |
+
with np.errstate(all='ignore'):
|
| 18 |
+
C2 = orth.chebyc(2)
|
| 19 |
+
C3 = orth.chebyc(3)
|
| 20 |
+
C4 = orth.chebyc(4)
|
| 21 |
+
C5 = orth.chebyc(5)
|
| 22 |
+
|
| 23 |
+
assert_array_almost_equal(C0.c,[2],13)
|
| 24 |
+
assert_array_almost_equal(C1.c,[1,0],13)
|
| 25 |
+
assert_array_almost_equal(C2.c,[1,0,-2],13)
|
| 26 |
+
assert_array_almost_equal(C3.c,[1,0,-3,0],13)
|
| 27 |
+
assert_array_almost_equal(C4.c,[1,0,-4,0,2],13)
|
| 28 |
+
assert_array_almost_equal(C5.c,[1,0,-5,0,5,0],13)
|
| 29 |
+
|
| 30 |
+
def test_chebys(self):
|
| 31 |
+
S0 = orth.chebys(0)
|
| 32 |
+
S1 = orth.chebys(1)
|
| 33 |
+
S2 = orth.chebys(2)
|
| 34 |
+
S3 = orth.chebys(3)
|
| 35 |
+
S4 = orth.chebys(4)
|
| 36 |
+
S5 = orth.chebys(5)
|
| 37 |
+
assert_array_almost_equal(S0.c,[1],13)
|
| 38 |
+
assert_array_almost_equal(S1.c,[1,0],13)
|
| 39 |
+
assert_array_almost_equal(S2.c,[1,0,-1],13)
|
| 40 |
+
assert_array_almost_equal(S3.c,[1,0,-2,0],13)
|
| 41 |
+
assert_array_almost_equal(S4.c,[1,0,-3,0,1],13)
|
| 42 |
+
assert_array_almost_equal(S5.c,[1,0,-4,0,3,0],13)
|
| 43 |
+
|
| 44 |
+
def test_chebyt(self):
|
| 45 |
+
T0 = orth.chebyt(0)
|
| 46 |
+
T1 = orth.chebyt(1)
|
| 47 |
+
T2 = orth.chebyt(2)
|
| 48 |
+
T3 = orth.chebyt(3)
|
| 49 |
+
T4 = orth.chebyt(4)
|
| 50 |
+
T5 = orth.chebyt(5)
|
| 51 |
+
assert_array_almost_equal(T0.c,[1],13)
|
| 52 |
+
assert_array_almost_equal(T1.c,[1,0],13)
|
| 53 |
+
assert_array_almost_equal(T2.c,[2,0,-1],13)
|
| 54 |
+
assert_array_almost_equal(T3.c,[4,0,-3,0],13)
|
| 55 |
+
assert_array_almost_equal(T4.c,[8,0,-8,0,1],13)
|
| 56 |
+
assert_array_almost_equal(T5.c,[16,0,-20,0,5,0],13)
|
| 57 |
+
|
| 58 |
+
def test_chebyu(self):
|
| 59 |
+
U0 = orth.chebyu(0)
|
| 60 |
+
U1 = orth.chebyu(1)
|
| 61 |
+
U2 = orth.chebyu(2)
|
| 62 |
+
U3 = orth.chebyu(3)
|
| 63 |
+
U4 = orth.chebyu(4)
|
| 64 |
+
U5 = orth.chebyu(5)
|
| 65 |
+
assert_array_almost_equal(U0.c,[1],13)
|
| 66 |
+
assert_array_almost_equal(U1.c,[2,0],13)
|
| 67 |
+
assert_array_almost_equal(U2.c,[4,0,-1],13)
|
| 68 |
+
assert_array_almost_equal(U3.c,[8,0,-4,0],13)
|
| 69 |
+
assert_array_almost_equal(U4.c,[16,0,-12,0,1],13)
|
| 70 |
+
assert_array_almost_equal(U5.c,[32,0,-32,0,6,0],13)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class TestGegenbauer:
|
| 74 |
+
|
| 75 |
+
def test_gegenbauer(self):
|
| 76 |
+
a = 5*np.random.random() - 0.5
|
| 77 |
+
if np.any(a == 0):
|
| 78 |
+
a = -0.2
|
| 79 |
+
Ca0 = orth.gegenbauer(0,a)
|
| 80 |
+
Ca1 = orth.gegenbauer(1,a)
|
| 81 |
+
Ca2 = orth.gegenbauer(2,a)
|
| 82 |
+
Ca3 = orth.gegenbauer(3,a)
|
| 83 |
+
Ca4 = orth.gegenbauer(4,a)
|
| 84 |
+
Ca5 = orth.gegenbauer(5,a)
|
| 85 |
+
|
| 86 |
+
assert_array_almost_equal(Ca0.c,array([1]),13)
|
| 87 |
+
assert_array_almost_equal(Ca1.c,array([2*a,0]),13)
|
| 88 |
+
assert_array_almost_equal(Ca2.c,array([2*a*(a+1),0,-a]),13)
|
| 89 |
+
assert_array_almost_equal(Ca3.c,array([4*sc.poch(a,3),0,-6*a*(a+1),
|
| 90 |
+
0])/3.0,11)
|
| 91 |
+
assert_array_almost_equal(Ca4.c,array([4*sc.poch(a,4),0,-12*sc.poch(a,3),
|
| 92 |
+
0,3*a*(a+1)])/6.0,11)
|
| 93 |
+
assert_array_almost_equal(Ca5.c,array([4*sc.poch(a,5),0,-20*sc.poch(a,4),
|
| 94 |
+
0,15*sc.poch(a,3),0])/15.0,11)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class TestHermite:
|
| 98 |
+
def test_hermite(self):
|
| 99 |
+
H0 = orth.hermite(0)
|
| 100 |
+
H1 = orth.hermite(1)
|
| 101 |
+
H2 = orth.hermite(2)
|
| 102 |
+
H3 = orth.hermite(3)
|
| 103 |
+
H4 = orth.hermite(4)
|
| 104 |
+
H5 = orth.hermite(5)
|
| 105 |
+
assert_array_almost_equal(H0.c,[1],13)
|
| 106 |
+
assert_array_almost_equal(H1.c,[2,0],13)
|
| 107 |
+
assert_array_almost_equal(H2.c,[4,0,-2],13)
|
| 108 |
+
assert_array_almost_equal(H3.c,[8,0,-12,0],13)
|
| 109 |
+
assert_array_almost_equal(H4.c,[16,0,-48,0,12],12)
|
| 110 |
+
assert_array_almost_equal(H5.c,[32,0,-160,0,120,0],12)
|
| 111 |
+
|
| 112 |
+
def test_hermitenorm(self):
|
| 113 |
+
# He_n(x) = 2**(-n/2) H_n(x/sqrt(2))
|
| 114 |
+
psub = np.poly1d([1.0/sqrt(2),0])
|
| 115 |
+
H0 = orth.hermitenorm(0)
|
| 116 |
+
H1 = orth.hermitenorm(1)
|
| 117 |
+
H2 = orth.hermitenorm(2)
|
| 118 |
+
H3 = orth.hermitenorm(3)
|
| 119 |
+
H4 = orth.hermitenorm(4)
|
| 120 |
+
H5 = orth.hermitenorm(5)
|
| 121 |
+
he0 = orth.hermite(0)(psub)
|
| 122 |
+
he1 = orth.hermite(1)(psub) / sqrt(2)
|
| 123 |
+
he2 = orth.hermite(2)(psub) / 2.0
|
| 124 |
+
he3 = orth.hermite(3)(psub) / (2*sqrt(2))
|
| 125 |
+
he4 = orth.hermite(4)(psub) / 4.0
|
| 126 |
+
he5 = orth.hermite(5)(psub) / (4.0*sqrt(2))
|
| 127 |
+
|
| 128 |
+
assert_array_almost_equal(H0.c,he0.c,13)
|
| 129 |
+
assert_array_almost_equal(H1.c,he1.c,13)
|
| 130 |
+
assert_array_almost_equal(H2.c,he2.c,13)
|
| 131 |
+
assert_array_almost_equal(H3.c,he3.c,13)
|
| 132 |
+
assert_array_almost_equal(H4.c,he4.c,13)
|
| 133 |
+
assert_array_almost_equal(H5.c,he5.c,13)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class TestShLegendre:
|
| 137 |
+
def test_sh_legendre(self):
|
| 138 |
+
# P*_n(x) = P_n(2x-1)
|
| 139 |
+
psub = np.poly1d([2,-1])
|
| 140 |
+
Ps0 = orth.sh_legendre(0)
|
| 141 |
+
Ps1 = orth.sh_legendre(1)
|
| 142 |
+
Ps2 = orth.sh_legendre(2)
|
| 143 |
+
Ps3 = orth.sh_legendre(3)
|
| 144 |
+
Ps4 = orth.sh_legendre(4)
|
| 145 |
+
Ps5 = orth.sh_legendre(5)
|
| 146 |
+
pse0 = orth.legendre(0)(psub)
|
| 147 |
+
pse1 = orth.legendre(1)(psub)
|
| 148 |
+
pse2 = orth.legendre(2)(psub)
|
| 149 |
+
pse3 = orth.legendre(3)(psub)
|
| 150 |
+
pse4 = orth.legendre(4)(psub)
|
| 151 |
+
pse5 = orth.legendre(5)(psub)
|
| 152 |
+
assert_array_almost_equal(Ps0.c,pse0.c,13)
|
| 153 |
+
assert_array_almost_equal(Ps1.c,pse1.c,13)
|
| 154 |
+
assert_array_almost_equal(Ps2.c,pse2.c,13)
|
| 155 |
+
assert_array_almost_equal(Ps3.c,pse3.c,13)
|
| 156 |
+
assert_array_almost_equal(Ps4.c,pse4.c,12)
|
| 157 |
+
assert_array_almost_equal(Ps5.c,pse5.c,12)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
class TestShChebyt:
|
| 161 |
+
def test_sh_chebyt(self):
|
| 162 |
+
# T*_n(x) = T_n(2x-1)
|
| 163 |
+
psub = np.poly1d([2,-1])
|
| 164 |
+
Ts0 = orth.sh_chebyt(0)
|
| 165 |
+
Ts1 = orth.sh_chebyt(1)
|
| 166 |
+
Ts2 = orth.sh_chebyt(2)
|
| 167 |
+
Ts3 = orth.sh_chebyt(3)
|
| 168 |
+
Ts4 = orth.sh_chebyt(4)
|
| 169 |
+
Ts5 = orth.sh_chebyt(5)
|
| 170 |
+
tse0 = orth.chebyt(0)(psub)
|
| 171 |
+
tse1 = orth.chebyt(1)(psub)
|
| 172 |
+
tse2 = orth.chebyt(2)(psub)
|
| 173 |
+
tse3 = orth.chebyt(3)(psub)
|
| 174 |
+
tse4 = orth.chebyt(4)(psub)
|
| 175 |
+
tse5 = orth.chebyt(5)(psub)
|
| 176 |
+
assert_array_almost_equal(Ts0.c,tse0.c,13)
|
| 177 |
+
assert_array_almost_equal(Ts1.c,tse1.c,13)
|
| 178 |
+
assert_array_almost_equal(Ts2.c,tse2.c,13)
|
| 179 |
+
assert_array_almost_equal(Ts3.c,tse3.c,13)
|
| 180 |
+
assert_array_almost_equal(Ts4.c,tse4.c,12)
|
| 181 |
+
assert_array_almost_equal(Ts5.c,tse5.c,12)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
class TestShChebyu:
|
| 185 |
+
def test_sh_chebyu(self):
|
| 186 |
+
# U*_n(x) = U_n(2x-1)
|
| 187 |
+
psub = np.poly1d([2,-1])
|
| 188 |
+
Us0 = orth.sh_chebyu(0)
|
| 189 |
+
Us1 = orth.sh_chebyu(1)
|
| 190 |
+
Us2 = orth.sh_chebyu(2)
|
| 191 |
+
Us3 = orth.sh_chebyu(3)
|
| 192 |
+
Us4 = orth.sh_chebyu(4)
|
| 193 |
+
Us5 = orth.sh_chebyu(5)
|
| 194 |
+
use0 = orth.chebyu(0)(psub)
|
| 195 |
+
use1 = orth.chebyu(1)(psub)
|
| 196 |
+
use2 = orth.chebyu(2)(psub)
|
| 197 |
+
use3 = orth.chebyu(3)(psub)
|
| 198 |
+
use4 = orth.chebyu(4)(psub)
|
| 199 |
+
use5 = orth.chebyu(5)(psub)
|
| 200 |
+
assert_array_almost_equal(Us0.c,use0.c,13)
|
| 201 |
+
assert_array_almost_equal(Us1.c,use1.c,13)
|
| 202 |
+
assert_array_almost_equal(Us2.c,use2.c,13)
|
| 203 |
+
assert_array_almost_equal(Us3.c,use3.c,13)
|
| 204 |
+
assert_array_almost_equal(Us4.c,use4.c,12)
|
| 205 |
+
assert_array_almost_equal(Us5.c,use5.c,11)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
class TestShJacobi:
|
| 209 |
+
def test_sh_jacobi(self):
|
| 210 |
+
# G^(p,q)_n(x) = n! gamma(n+p)/gamma(2*n+p) * P^(p-q,q-1)_n(2*x-1)
|
| 211 |
+
def conv(n, p):
|
| 212 |
+
return gamma(n + 1) * gamma(n + p) / gamma(2 * n + p)
|
| 213 |
+
psub = np.poly1d([2,-1])
|
| 214 |
+
q = 4 * np.random.random()
|
| 215 |
+
p = q-1 + 2*np.random.random()
|
| 216 |
+
# print("shifted jacobi p,q = ", p, q)
|
| 217 |
+
G0 = orth.sh_jacobi(0,p,q)
|
| 218 |
+
G1 = orth.sh_jacobi(1,p,q)
|
| 219 |
+
G2 = orth.sh_jacobi(2,p,q)
|
| 220 |
+
G3 = orth.sh_jacobi(3,p,q)
|
| 221 |
+
G4 = orth.sh_jacobi(4,p,q)
|
| 222 |
+
G5 = orth.sh_jacobi(5,p,q)
|
| 223 |
+
ge0 = orth.jacobi(0,p-q,q-1)(psub) * conv(0,p)
|
| 224 |
+
ge1 = orth.jacobi(1,p-q,q-1)(psub) * conv(1,p)
|
| 225 |
+
ge2 = orth.jacobi(2,p-q,q-1)(psub) * conv(2,p)
|
| 226 |
+
ge3 = orth.jacobi(3,p-q,q-1)(psub) * conv(3,p)
|
| 227 |
+
ge4 = orth.jacobi(4,p-q,q-1)(psub) * conv(4,p)
|
| 228 |
+
ge5 = orth.jacobi(5,p-q,q-1)(psub) * conv(5,p)
|
| 229 |
+
|
| 230 |
+
assert_array_almost_equal(G0.c,ge0.c,13)
|
| 231 |
+
assert_array_almost_equal(G1.c,ge1.c,13)
|
| 232 |
+
assert_array_almost_equal(G2.c,ge2.c,13)
|
| 233 |
+
assert_array_almost_equal(G3.c,ge3.c,13)
|
| 234 |
+
assert_array_almost_equal(G4.c,ge4.c,13)
|
| 235 |
+
assert_array_almost_equal(G5.c,ge5.c,13)
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
class TestCall:
|
| 239 |
+
def test_call(self):
|
| 240 |
+
poly = []
|
| 241 |
+
for n in range(5):
|
| 242 |
+
poly.extend([x.strip() for x in
|
| 243 |
+
("""
|
| 244 |
+
orth.jacobi(%(n)d,0.3,0.9)
|
| 245 |
+
orth.sh_jacobi(%(n)d,0.3,0.9)
|
| 246 |
+
orth.genlaguerre(%(n)d,0.3)
|
| 247 |
+
orth.laguerre(%(n)d)
|
| 248 |
+
orth.hermite(%(n)d)
|
| 249 |
+
orth.hermitenorm(%(n)d)
|
| 250 |
+
orth.gegenbauer(%(n)d,0.3)
|
| 251 |
+
orth.chebyt(%(n)d)
|
| 252 |
+
orth.chebyu(%(n)d)
|
| 253 |
+
orth.chebyc(%(n)d)
|
| 254 |
+
orth.chebys(%(n)d)
|
| 255 |
+
orth.sh_chebyt(%(n)d)
|
| 256 |
+
orth.sh_chebyu(%(n)d)
|
| 257 |
+
orth.legendre(%(n)d)
|
| 258 |
+
orth.sh_legendre(%(n)d)
|
| 259 |
+
""" % dict(n=n)).split()
|
| 260 |
+
])
|
| 261 |
+
with np.errstate(all='ignore'):
|
| 262 |
+
for pstr in poly:
|
| 263 |
+
p = eval(pstr)
|
| 264 |
+
assert_almost_equal(p(0.315), np.poly1d(p.coef)(0.315),
|
| 265 |
+
err_msg=pstr)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
class TestGenlaguerre:
|
| 269 |
+
def test_regression(self):
|
| 270 |
+
assert_equal(orth.genlaguerre(1, 1, monic=False)(0), 2.)
|
| 271 |
+
assert_equal(orth.genlaguerre(1, 1, monic=True)(0), -2.)
|
| 272 |
+
assert_equal(orth.genlaguerre(1, 1, monic=False), np.poly1d([-1, 2]))
|
| 273 |
+
assert_equal(orth.genlaguerre(1, 1, monic=True), np.poly1d([1, -2]))
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def verify_gauss_quad(root_func, eval_func, weight_func, a, b, N,
|
| 277 |
+
rtol=1e-15, atol=5e-14):
|
| 278 |
+
# this test is copied from numpy's TestGauss in test_hermite.py
|
| 279 |
+
x, w, mu = root_func(N, True)
|
| 280 |
+
|
| 281 |
+
n = np.arange(N, dtype=np.dtype("long"))
|
| 282 |
+
v = eval_func(n[:,np.newaxis], x)
|
| 283 |
+
vv = np.dot(v*w, v.T)
|
| 284 |
+
vd = 1 / np.sqrt(vv.diagonal())
|
| 285 |
+
vv = vd[:, np.newaxis] * vv * vd
|
| 286 |
+
assert_allclose(vv, np.eye(N), rtol, atol)
|
| 287 |
+
|
| 288 |
+
# check that the integral of 1 is correct
|
| 289 |
+
assert_allclose(w.sum(), mu, rtol, atol)
|
| 290 |
+
|
| 291 |
+
# compare the results of integrating a function with quad.
|
| 292 |
+
def f(x):
|
| 293 |
+
return x ** 3 - 3 * x ** 2 + x - 2
|
| 294 |
+
resI = integrate.quad(lambda x: f(x)*weight_func(x), a, b)
|
| 295 |
+
resG = np.vdot(f(x), w)
|
| 296 |
+
rtol = 1e-6 if 1e-6 < resI[1] else resI[1] * 10
|
| 297 |
+
assert_allclose(resI[0], resG, rtol=rtol)
|
| 298 |
+
|
| 299 |
+
def test_roots_jacobi():
|
| 300 |
+
def rf(a, b):
|
| 301 |
+
return lambda n, mu: sc.roots_jacobi(n, a, b, mu)
|
| 302 |
+
def ef(a, b):
|
| 303 |
+
return lambda n, x: sc.eval_jacobi(n, a, b, x)
|
| 304 |
+
def wf(a, b):
|
| 305 |
+
return lambda x: (1 - x) ** a * (1 + x) ** b
|
| 306 |
+
|
| 307 |
+
vgq = verify_gauss_quad
|
| 308 |
+
vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1., 5)
|
| 309 |
+
vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1.,
|
| 310 |
+
25, atol=1e-12)
|
| 311 |
+
vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1.,
|
| 312 |
+
100, atol=1e-11)
|
| 313 |
+
|
| 314 |
+
vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 5)
|
| 315 |
+
vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 25, atol=1.5e-13)
|
| 316 |
+
vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 100, atol=2e-12)
|
| 317 |
+
|
| 318 |
+
vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 5, atol=2e-13)
|
| 319 |
+
vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 25, atol=2e-13)
|
| 320 |
+
vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 100, atol=1e-12)
|
| 321 |
+
|
| 322 |
+
vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 5)
|
| 323 |
+
vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 25, atol=1e-13)
|
| 324 |
+
vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 100, atol=3e-13)
|
| 325 |
+
|
| 326 |
+
vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 5)
|
| 327 |
+
vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 25,
|
| 328 |
+
atol=1.1e-14)
|
| 329 |
+
vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1.,
|
| 330 |
+
100, atol=1e-13)
|
| 331 |
+
|
| 332 |
+
vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 5, atol=1e-13)
|
| 333 |
+
vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 25, atol=2e-13)
|
| 334 |
+
vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1.,
|
| 335 |
+
100, atol=1e-11)
|
| 336 |
+
|
| 337 |
+
vgq(rf(1., 658.), ef(1., 658.), wf(1., 658.), -1., 1., 5, atol=2e-13)
|
| 338 |
+
vgq(rf(1., 658.), ef(1., 658.), wf(1., 658.), -1., 1., 25, atol=1e-12)
|
| 339 |
+
vgq(rf(1., 658.), ef(1., 658.), wf(1., 658.), -1., 1., 100, atol=1e-11)
|
| 340 |
+
vgq(rf(1., 658.), ef(1., 658.), wf(1., 658.), -1., 1., 250, atol=1e-11)
|
| 341 |
+
|
| 342 |
+
vgq(rf(511., 511.), ef(511., 511.), wf(511., 511.), -1., 1., 5,
|
| 343 |
+
atol=1e-12)
|
| 344 |
+
vgq(rf(511., 511.), ef(511., 511.), wf(511., 511.), -1., 1., 25,
|
| 345 |
+
atol=1e-11)
|
| 346 |
+
vgq(rf(511., 511.), ef(511., 511.), wf(511., 511.), -1., 1., 100,
|
| 347 |
+
atol=1e-10)
|
| 348 |
+
|
| 349 |
+
vgq(rf(511., 512.), ef(511., 512.), wf(511., 512.), -1., 1., 5,
|
| 350 |
+
atol=1e-12)
|
| 351 |
+
vgq(rf(511., 512.), ef(511., 512.), wf(511., 512.), -1., 1., 25,
|
| 352 |
+
atol=1e-11)
|
| 353 |
+
vgq(rf(511., 512.), ef(511., 512.), wf(511., 512.), -1., 1., 100,
|
| 354 |
+
atol=1e-10)
|
| 355 |
+
|
| 356 |
+
vgq(rf(1000., 500.), ef(1000., 500.), wf(1000., 500.), -1., 1., 5,
|
| 357 |
+
atol=1e-12)
|
| 358 |
+
vgq(rf(1000., 500.), ef(1000., 500.), wf(1000., 500.), -1., 1., 25,
|
| 359 |
+
atol=1e-11)
|
| 360 |
+
vgq(rf(1000., 500.), ef(1000., 500.), wf(1000., 500.), -1., 1., 100,
|
| 361 |
+
atol=1e-10)
|
| 362 |
+
|
| 363 |
+
vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 5)
|
| 364 |
+
vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 25,
|
| 365 |
+
atol=1e-13)
|
| 366 |
+
vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 100,
|
| 367 |
+
atol=1e-13)
|
| 368 |
+
|
| 369 |
+
# when alpha == beta == 0, P_n^{a,b}(x) == P_n(x)
|
| 370 |
+
xj, wj = sc.roots_jacobi(6, 0.0, 0.0)
|
| 371 |
+
xl, wl = sc.roots_legendre(6)
|
| 372 |
+
assert_allclose(xj, xl, 1e-14, 1e-14)
|
| 373 |
+
assert_allclose(wj, wl, 1e-14, 1e-14)
|
| 374 |
+
|
| 375 |
+
# when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x)
|
| 376 |
+
xj, wj = sc.roots_jacobi(6, 4.0, 4.0)
|
| 377 |
+
xc, wc = sc.roots_gegenbauer(6, 4.5)
|
| 378 |
+
assert_allclose(xj, xc, 1e-14, 1e-14)
|
| 379 |
+
assert_allclose(wj, wc, 1e-14, 1e-14)
|
| 380 |
+
|
| 381 |
+
x, w = sc.roots_jacobi(5, 2, 3, False)
|
| 382 |
+
y, v, m = sc.roots_jacobi(5, 2, 3, True)
|
| 383 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 384 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 385 |
+
|
| 386 |
+
muI, muI_err = integrate.quad(wf(2,3), -1, 1)
|
| 387 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 388 |
+
|
| 389 |
+
assert_raises(ValueError, sc.roots_jacobi, 0, 1, 1)
|
| 390 |
+
assert_raises(ValueError, sc.roots_jacobi, 3.3, 1, 1)
|
| 391 |
+
assert_raises(ValueError, sc.roots_jacobi, 3, -2, 1)
|
| 392 |
+
assert_raises(ValueError, sc.roots_jacobi, 3, 1, -2)
|
| 393 |
+
assert_raises(ValueError, sc.roots_jacobi, 3, -2, -2)
|
| 394 |
+
|
| 395 |
+
def test_roots_sh_jacobi():
|
| 396 |
+
def rf(a, b):
|
| 397 |
+
return lambda n, mu: sc.roots_sh_jacobi(n, a, b, mu)
|
| 398 |
+
def ef(a, b):
|
| 399 |
+
return lambda n, x: sc.eval_sh_jacobi(n, a, b, x)
|
| 400 |
+
def wf(a, b):
|
| 401 |
+
return lambda x: (1.0 - x) ** (a - b) * x ** (b - 1.0)
|
| 402 |
+
|
| 403 |
+
vgq = verify_gauss_quad
|
| 404 |
+
vgq(rf(-0.5, 0.25), ef(-0.5, 0.25), wf(-0.5, 0.25), 0., 1., 5)
|
| 405 |
+
vgq(rf(-0.5, 0.25), ef(-0.5, 0.25), wf(-0.5, 0.25), 0., 1.,
|
| 406 |
+
25, atol=1e-12)
|
| 407 |
+
vgq(rf(-0.5, 0.25), ef(-0.5, 0.25), wf(-0.5, 0.25), 0., 1.,
|
| 408 |
+
100, atol=1e-11)
|
| 409 |
+
|
| 410 |
+
vgq(rf(0.5, 0.5), ef(0.5, 0.5), wf(0.5, 0.5), 0., 1., 5)
|
| 411 |
+
vgq(rf(0.5, 0.5), ef(0.5, 0.5), wf(0.5, 0.5), 0., 1., 25, atol=1e-13)
|
| 412 |
+
vgq(rf(0.5, 0.5), ef(0.5, 0.5), wf(0.5, 0.5), 0., 1., 100, atol=1e-12)
|
| 413 |
+
|
| 414 |
+
vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), 0., 1., 5)
|
| 415 |
+
vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), 0., 1., 25, atol=1.5e-13)
|
| 416 |
+
vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), 0., 1., 100, atol=2e-12)
|
| 417 |
+
|
| 418 |
+
vgq(rf(2, 0.9), ef(2, 0.9), wf(2, 0.9), 0., 1., 5)
|
| 419 |
+
vgq(rf(2, 0.9), ef(2, 0.9), wf(2, 0.9), 0., 1., 25, atol=1e-13)
|
| 420 |
+
vgq(rf(2, 0.9), ef(2, 0.9), wf(2, 0.9), 0., 1., 100, atol=1e-12)
|
| 421 |
+
|
| 422 |
+
vgq(rf(27.3, 18.24), ef(27.3, 18.24), wf(27.3, 18.24), 0., 1., 5)
|
| 423 |
+
vgq(rf(27.3, 18.24), ef(27.3, 18.24), wf(27.3, 18.24), 0., 1., 25)
|
| 424 |
+
vgq(rf(27.3, 18.24), ef(27.3, 18.24), wf(27.3, 18.24), 0., 1.,
|
| 425 |
+
100, atol=1e-13)
|
| 426 |
+
|
| 427 |
+
vgq(rf(47.1, 0.2), ef(47.1, 0.2), wf(47.1, 0.2), 0., 1., 5, atol=1e-12)
|
| 428 |
+
vgq(rf(47.1, 0.2), ef(47.1, 0.2), wf(47.1, 0.2), 0., 1., 25, atol=1e-11)
|
| 429 |
+
vgq(rf(47.1, 0.2), ef(47.1, 0.2), wf(47.1, 0.2), 0., 1., 100, atol=1e-10)
|
| 430 |
+
|
| 431 |
+
vgq(rf(68.9, 2.25), ef(68.9, 2.25), wf(68.9, 2.25), 0., 1., 5, atol=3.5e-14)
|
| 432 |
+
vgq(rf(68.9, 2.25), ef(68.9, 2.25), wf(68.9, 2.25), 0., 1., 25, atol=2e-13)
|
| 433 |
+
vgq(rf(68.9, 2.25), ef(68.9, 2.25), wf(68.9, 2.25), 0., 1.,
|
| 434 |
+
100, atol=1e-12)
|
| 435 |
+
|
| 436 |
+
x, w = sc.roots_sh_jacobi(5, 3, 2, False)
|
| 437 |
+
y, v, m = sc.roots_sh_jacobi(5, 3, 2, True)
|
| 438 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 439 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 440 |
+
|
| 441 |
+
muI, muI_err = integrate.quad(wf(3,2), 0, 1)
|
| 442 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 443 |
+
|
| 444 |
+
assert_raises(ValueError, sc.roots_sh_jacobi, 0, 1, 1)
|
| 445 |
+
assert_raises(ValueError, sc.roots_sh_jacobi, 3.3, 1, 1)
|
| 446 |
+
assert_raises(ValueError, sc.roots_sh_jacobi, 3, 1, 2) # p - q <= -1
|
| 447 |
+
assert_raises(ValueError, sc.roots_sh_jacobi, 3, 2, -1) # q <= 0
|
| 448 |
+
assert_raises(ValueError, sc.roots_sh_jacobi, 3, -2, -1) # both
|
| 449 |
+
|
| 450 |
+
def test_roots_hermite():
|
| 451 |
+
rootf = sc.roots_hermite
|
| 452 |
+
evalf = sc.eval_hermite
|
| 453 |
+
weightf = orth.hermite(5).weight_func
|
| 454 |
+
|
| 455 |
+
verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 5)
|
| 456 |
+
verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 25, atol=1e-13)
|
| 457 |
+
verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 100, atol=1e-12)
|
| 458 |
+
|
| 459 |
+
# Golub-Welsch branch
|
| 460 |
+
x, w = sc.roots_hermite(5, False)
|
| 461 |
+
y, v, m = sc.roots_hermite(5, True)
|
| 462 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 463 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 464 |
+
|
| 465 |
+
muI, muI_err = integrate.quad(weightf, -np.inf, np.inf)
|
| 466 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 467 |
+
|
| 468 |
+
# Asymptotic branch (switch over at n >= 150)
|
| 469 |
+
x, w = sc.roots_hermite(200, False)
|
| 470 |
+
y, v, m = sc.roots_hermite(200, True)
|
| 471 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 472 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 473 |
+
assert_allclose(sum(v), m, 1e-14, 1e-14)
|
| 474 |
+
|
| 475 |
+
assert_raises(ValueError, sc.roots_hermite, 0)
|
| 476 |
+
assert_raises(ValueError, sc.roots_hermite, 3.3)
|
| 477 |
+
|
| 478 |
+
def test_roots_hermite_asy():
|
| 479 |
+
# Recursion for Hermite functions
|
| 480 |
+
def hermite_recursion(n, nodes):
|
| 481 |
+
H = np.zeros((n, nodes.size))
|
| 482 |
+
H[0,:] = np.pi**(-0.25) * np.exp(-0.5*nodes**2)
|
| 483 |
+
if n > 1:
|
| 484 |
+
H[1,:] = sqrt(2.0) * nodes * H[0,:]
|
| 485 |
+
for k in range(2, n):
|
| 486 |
+
H[k,:] = sqrt(2.0/k) * nodes * H[k-1,:] - sqrt((k-1.0)/k) * H[k-2,:]
|
| 487 |
+
return H
|
| 488 |
+
|
| 489 |
+
# This tests only the nodes
|
| 490 |
+
def test(N, rtol=1e-15, atol=1e-14):
|
| 491 |
+
x, w = orth._roots_hermite_asy(N)
|
| 492 |
+
H = hermite_recursion(N+1, x)
|
| 493 |
+
assert_allclose(H[-1,:], np.zeros(N), rtol, atol)
|
| 494 |
+
assert_allclose(sum(w), sqrt(np.pi), rtol, atol)
|
| 495 |
+
|
| 496 |
+
test(150, atol=1e-12)
|
| 497 |
+
test(151, atol=1e-12)
|
| 498 |
+
test(300, atol=1e-12)
|
| 499 |
+
test(301, atol=1e-12)
|
| 500 |
+
test(500, atol=1e-12)
|
| 501 |
+
test(501, atol=1e-12)
|
| 502 |
+
test(999, atol=1e-12)
|
| 503 |
+
test(1000, atol=1e-12)
|
| 504 |
+
test(2000, atol=1e-12)
|
| 505 |
+
test(5000, atol=1e-12)
|
| 506 |
+
|
| 507 |
+
def test_roots_hermitenorm():
|
| 508 |
+
rootf = sc.roots_hermitenorm
|
| 509 |
+
evalf = sc.eval_hermitenorm
|
| 510 |
+
weightf = orth.hermitenorm(5).weight_func
|
| 511 |
+
|
| 512 |
+
verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 5)
|
| 513 |
+
verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 25, atol=1e-13)
|
| 514 |
+
verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 100, atol=1e-12)
|
| 515 |
+
|
| 516 |
+
x, w = sc.roots_hermitenorm(5, False)
|
| 517 |
+
y, v, m = sc.roots_hermitenorm(5, True)
|
| 518 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 519 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 520 |
+
|
| 521 |
+
muI, muI_err = integrate.quad(weightf, -np.inf, np.inf)
|
| 522 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 523 |
+
|
| 524 |
+
assert_raises(ValueError, sc.roots_hermitenorm, 0)
|
| 525 |
+
assert_raises(ValueError, sc.roots_hermitenorm, 3.3)
|
| 526 |
+
|
| 527 |
+
def test_roots_gegenbauer():
|
| 528 |
+
def rootf(a):
|
| 529 |
+
return lambda n, mu: sc.roots_gegenbauer(n, a, mu)
|
| 530 |
+
def evalf(a):
|
| 531 |
+
return lambda n, x: sc.eval_gegenbauer(n, a, x)
|
| 532 |
+
def weightf(a):
|
| 533 |
+
return lambda x: (1 - x ** 2) ** (a - 0.5)
|
| 534 |
+
|
| 535 |
+
vgq = verify_gauss_quad
|
| 536 |
+
vgq(rootf(-0.25), evalf(-0.25), weightf(-0.25), -1., 1., 5)
|
| 537 |
+
vgq(rootf(-0.25), evalf(-0.25), weightf(-0.25), -1., 1., 25, atol=1e-12)
|
| 538 |
+
vgq(rootf(-0.25), evalf(-0.25), weightf(-0.25), -1., 1., 100, atol=1e-11)
|
| 539 |
+
|
| 540 |
+
vgq(rootf(0.1), evalf(0.1), weightf(0.1), -1., 1., 5)
|
| 541 |
+
vgq(rootf(0.1), evalf(0.1), weightf(0.1), -1., 1., 25, atol=1e-13)
|
| 542 |
+
vgq(rootf(0.1), evalf(0.1), weightf(0.1), -1., 1., 100, atol=1e-12)
|
| 543 |
+
|
| 544 |
+
vgq(rootf(1), evalf(1), weightf(1), -1., 1., 5)
|
| 545 |
+
vgq(rootf(1), evalf(1), weightf(1), -1., 1., 25, atol=1e-13)
|
| 546 |
+
vgq(rootf(1), evalf(1), weightf(1), -1., 1., 100, atol=1e-12)
|
| 547 |
+
|
| 548 |
+
vgq(rootf(10), evalf(10), weightf(10), -1., 1., 5)
|
| 549 |
+
vgq(rootf(10), evalf(10), weightf(10), -1., 1., 25, atol=1e-13)
|
| 550 |
+
vgq(rootf(10), evalf(10), weightf(10), -1., 1., 100, atol=1e-12)
|
| 551 |
+
|
| 552 |
+
vgq(rootf(50), evalf(50), weightf(50), -1., 1., 5, atol=1e-13)
|
| 553 |
+
vgq(rootf(50), evalf(50), weightf(50), -1., 1., 25, atol=1e-12)
|
| 554 |
+
vgq(rootf(50), evalf(50), weightf(50), -1., 1., 100, atol=1e-11)
|
| 555 |
+
|
| 556 |
+
# Alpha=170 is where the approximation used in roots_gegenbauer changes
|
| 557 |
+
vgq(rootf(170), evalf(170), weightf(170), -1., 1., 5, atol=1e-13)
|
| 558 |
+
vgq(rootf(170), evalf(170), weightf(170), -1., 1., 25, atol=1e-12)
|
| 559 |
+
vgq(rootf(170), evalf(170), weightf(170), -1., 1., 100, atol=1e-11)
|
| 560 |
+
vgq(rootf(170.5), evalf(170.5), weightf(170.5), -1., 1., 5, atol=1.25e-13)
|
| 561 |
+
vgq(rootf(170.5), evalf(170.5), weightf(170.5), -1., 1., 25, atol=1e-12)
|
| 562 |
+
vgq(rootf(170.5), evalf(170.5), weightf(170.5), -1., 1., 100, atol=1e-11)
|
| 563 |
+
|
| 564 |
+
# Test for failures, e.g. overflows, resulting from large alphas
|
| 565 |
+
vgq(rootf(238), evalf(238), weightf(238), -1., 1., 5, atol=1e-13)
|
| 566 |
+
vgq(rootf(238), evalf(238), weightf(238), -1., 1., 25, atol=1e-12)
|
| 567 |
+
vgq(rootf(238), evalf(238), weightf(238), -1., 1., 100, atol=1e-11)
|
| 568 |
+
vgq(rootf(512.5), evalf(512.5), weightf(512.5), -1., 1., 5, atol=1e-12)
|
| 569 |
+
vgq(rootf(512.5), evalf(512.5), weightf(512.5), -1., 1., 25, atol=1e-11)
|
| 570 |
+
vgq(rootf(512.5), evalf(512.5), weightf(512.5), -1., 1., 100, atol=1e-10)
|
| 571 |
+
|
| 572 |
+
# this is a special case that the old code supported.
|
| 573 |
+
# when alpha = 0, the gegenbauer polynomial is uniformly 0. but it goes
|
| 574 |
+
# to a scaled down copy of T_n(x) there.
|
| 575 |
+
vgq(rootf(0), sc.eval_chebyt, weightf(0), -1., 1., 5)
|
| 576 |
+
vgq(rootf(0), sc.eval_chebyt, weightf(0), -1., 1., 25)
|
| 577 |
+
vgq(rootf(0), sc.eval_chebyt, weightf(0), -1., 1., 100, atol=1e-12)
|
| 578 |
+
|
| 579 |
+
x, w = sc.roots_gegenbauer(5, 2, False)
|
| 580 |
+
y, v, m = sc.roots_gegenbauer(5, 2, True)
|
| 581 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 582 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 583 |
+
|
| 584 |
+
muI, muI_err = integrate.quad(weightf(2), -1, 1)
|
| 585 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 586 |
+
|
| 587 |
+
assert_raises(ValueError, sc.roots_gegenbauer, 0, 2)
|
| 588 |
+
assert_raises(ValueError, sc.roots_gegenbauer, 3.3, 2)
|
| 589 |
+
assert_raises(ValueError, sc.roots_gegenbauer, 3, -.75)
|
| 590 |
+
|
| 591 |
+
def test_roots_chebyt():
|
| 592 |
+
weightf = orth.chebyt(5).weight_func
|
| 593 |
+
verify_gauss_quad(sc.roots_chebyt, sc.eval_chebyt, weightf, -1., 1., 5)
|
| 594 |
+
verify_gauss_quad(sc.roots_chebyt, sc.eval_chebyt, weightf, -1., 1., 25)
|
| 595 |
+
verify_gauss_quad(sc.roots_chebyt, sc.eval_chebyt, weightf, -1., 1., 100,
|
| 596 |
+
atol=1e-12)
|
| 597 |
+
|
| 598 |
+
x, w = sc.roots_chebyt(5, False)
|
| 599 |
+
y, v, m = sc.roots_chebyt(5, True)
|
| 600 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 601 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 602 |
+
|
| 603 |
+
muI, muI_err = integrate.quad(weightf, -1, 1)
|
| 604 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 605 |
+
|
| 606 |
+
assert_raises(ValueError, sc.roots_chebyt, 0)
|
| 607 |
+
assert_raises(ValueError, sc.roots_chebyt, 3.3)
|
| 608 |
+
|
| 609 |
+
def test_chebyt_symmetry():
|
| 610 |
+
x, w = sc.roots_chebyt(21)
|
| 611 |
+
pos, neg = x[:10], x[11:]
|
| 612 |
+
assert_equal(neg, -pos[::-1])
|
| 613 |
+
assert_equal(x[10], 0)
|
| 614 |
+
|
| 615 |
+
def test_roots_chebyu():
|
| 616 |
+
weightf = orth.chebyu(5).weight_func
|
| 617 |
+
verify_gauss_quad(sc.roots_chebyu, sc.eval_chebyu, weightf, -1., 1., 5)
|
| 618 |
+
verify_gauss_quad(sc.roots_chebyu, sc.eval_chebyu, weightf, -1., 1., 25)
|
| 619 |
+
verify_gauss_quad(sc.roots_chebyu, sc.eval_chebyu, weightf, -1., 1., 100)
|
| 620 |
+
|
| 621 |
+
x, w = sc.roots_chebyu(5, False)
|
| 622 |
+
y, v, m = sc.roots_chebyu(5, True)
|
| 623 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 624 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 625 |
+
|
| 626 |
+
muI, muI_err = integrate.quad(weightf, -1, 1)
|
| 627 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 628 |
+
|
| 629 |
+
assert_raises(ValueError, sc.roots_chebyu, 0)
|
| 630 |
+
assert_raises(ValueError, sc.roots_chebyu, 3.3)
|
| 631 |
+
|
| 632 |
+
def test_roots_chebyc():
|
| 633 |
+
weightf = orth.chebyc(5).weight_func
|
| 634 |
+
verify_gauss_quad(sc.roots_chebyc, sc.eval_chebyc, weightf, -2., 2., 5)
|
| 635 |
+
verify_gauss_quad(sc.roots_chebyc, sc.eval_chebyc, weightf, -2., 2., 25)
|
| 636 |
+
verify_gauss_quad(sc.roots_chebyc, sc.eval_chebyc, weightf, -2., 2., 100,
|
| 637 |
+
atol=1e-12)
|
| 638 |
+
|
| 639 |
+
x, w = sc.roots_chebyc(5, False)
|
| 640 |
+
y, v, m = sc.roots_chebyc(5, True)
|
| 641 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 642 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 643 |
+
|
| 644 |
+
muI, muI_err = integrate.quad(weightf, -2, 2)
|
| 645 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 646 |
+
|
| 647 |
+
assert_raises(ValueError, sc.roots_chebyc, 0)
|
| 648 |
+
assert_raises(ValueError, sc.roots_chebyc, 3.3)
|
| 649 |
+
|
| 650 |
+
def test_roots_chebys():
|
| 651 |
+
weightf = orth.chebys(5).weight_func
|
| 652 |
+
verify_gauss_quad(sc.roots_chebys, sc.eval_chebys, weightf, -2., 2., 5)
|
| 653 |
+
verify_gauss_quad(sc.roots_chebys, sc.eval_chebys, weightf, -2., 2., 25)
|
| 654 |
+
verify_gauss_quad(sc.roots_chebys, sc.eval_chebys, weightf, -2., 2., 100)
|
| 655 |
+
|
| 656 |
+
x, w = sc.roots_chebys(5, False)
|
| 657 |
+
y, v, m = sc.roots_chebys(5, True)
|
| 658 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 659 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 660 |
+
|
| 661 |
+
muI, muI_err = integrate.quad(weightf, -2, 2)
|
| 662 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 663 |
+
|
| 664 |
+
assert_raises(ValueError, sc.roots_chebys, 0)
|
| 665 |
+
assert_raises(ValueError, sc.roots_chebys, 3.3)
|
| 666 |
+
|
| 667 |
+
def test_roots_sh_chebyt():
|
| 668 |
+
weightf = orth.sh_chebyt(5).weight_func
|
| 669 |
+
verify_gauss_quad(sc.roots_sh_chebyt, sc.eval_sh_chebyt, weightf, 0., 1., 5)
|
| 670 |
+
verify_gauss_quad(sc.roots_sh_chebyt, sc.eval_sh_chebyt, weightf, 0., 1., 25)
|
| 671 |
+
verify_gauss_quad(sc.roots_sh_chebyt, sc.eval_sh_chebyt, weightf, 0., 1.,
|
| 672 |
+
100, atol=1e-13)
|
| 673 |
+
|
| 674 |
+
x, w = sc.roots_sh_chebyt(5, False)
|
| 675 |
+
y, v, m = sc.roots_sh_chebyt(5, True)
|
| 676 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 677 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 678 |
+
|
| 679 |
+
muI, muI_err = integrate.quad(weightf, 0, 1)
|
| 680 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 681 |
+
|
| 682 |
+
assert_raises(ValueError, sc.roots_sh_chebyt, 0)
|
| 683 |
+
assert_raises(ValueError, sc.roots_sh_chebyt, 3.3)
|
| 684 |
+
|
| 685 |
+
def test_roots_sh_chebyu():
|
| 686 |
+
weightf = orth.sh_chebyu(5).weight_func
|
| 687 |
+
verify_gauss_quad(sc.roots_sh_chebyu, sc.eval_sh_chebyu, weightf, 0., 1., 5)
|
| 688 |
+
verify_gauss_quad(sc.roots_sh_chebyu, sc.eval_sh_chebyu, weightf, 0., 1., 25)
|
| 689 |
+
verify_gauss_quad(sc.roots_sh_chebyu, sc.eval_sh_chebyu, weightf, 0., 1.,
|
| 690 |
+
100, atol=1e-13)
|
| 691 |
+
|
| 692 |
+
x, w = sc.roots_sh_chebyu(5, False)
|
| 693 |
+
y, v, m = sc.roots_sh_chebyu(5, True)
|
| 694 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 695 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 696 |
+
|
| 697 |
+
muI, muI_err = integrate.quad(weightf, 0, 1)
|
| 698 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 699 |
+
|
| 700 |
+
assert_raises(ValueError, sc.roots_sh_chebyu, 0)
|
| 701 |
+
assert_raises(ValueError, sc.roots_sh_chebyu, 3.3)
|
| 702 |
+
|
| 703 |
+
def test_roots_legendre():
|
| 704 |
+
weightf = orth.legendre(5).weight_func
|
| 705 |
+
verify_gauss_quad(sc.roots_legendre, sc.eval_legendre, weightf, -1., 1., 5)
|
| 706 |
+
verify_gauss_quad(sc.roots_legendre, sc.eval_legendre, weightf, -1., 1.,
|
| 707 |
+
25, atol=1e-13)
|
| 708 |
+
verify_gauss_quad(sc.roots_legendre, sc.eval_legendre, weightf, -1., 1.,
|
| 709 |
+
100, atol=1e-12)
|
| 710 |
+
|
| 711 |
+
x, w = sc.roots_legendre(5, False)
|
| 712 |
+
y, v, m = sc.roots_legendre(5, True)
|
| 713 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 714 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 715 |
+
|
| 716 |
+
muI, muI_err = integrate.quad(weightf, -1, 1)
|
| 717 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 718 |
+
|
| 719 |
+
assert_raises(ValueError, sc.roots_legendre, 0)
|
| 720 |
+
assert_raises(ValueError, sc.roots_legendre, 3.3)
|
| 721 |
+
|
| 722 |
+
def test_roots_sh_legendre():
|
| 723 |
+
weightf = orth.sh_legendre(5).weight_func
|
| 724 |
+
verify_gauss_quad(sc.roots_sh_legendre, sc.eval_sh_legendre, weightf, 0., 1., 5)
|
| 725 |
+
verify_gauss_quad(sc.roots_sh_legendre, sc.eval_sh_legendre, weightf, 0., 1.,
|
| 726 |
+
25, atol=1e-13)
|
| 727 |
+
verify_gauss_quad(sc.roots_sh_legendre, sc.eval_sh_legendre, weightf, 0., 1.,
|
| 728 |
+
100, atol=1e-12)
|
| 729 |
+
|
| 730 |
+
x, w = sc.roots_sh_legendre(5, False)
|
| 731 |
+
y, v, m = sc.roots_sh_legendre(5, True)
|
| 732 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 733 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 734 |
+
|
| 735 |
+
muI, muI_err = integrate.quad(weightf, 0, 1)
|
| 736 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 737 |
+
|
| 738 |
+
assert_raises(ValueError, sc.roots_sh_legendre, 0)
|
| 739 |
+
assert_raises(ValueError, sc.roots_sh_legendre, 3.3)
|
| 740 |
+
|
| 741 |
+
def test_roots_laguerre():
|
| 742 |
+
weightf = orth.laguerre(5).weight_func
|
| 743 |
+
verify_gauss_quad(sc.roots_laguerre, sc.eval_laguerre, weightf, 0., np.inf, 5)
|
| 744 |
+
verify_gauss_quad(sc.roots_laguerre, sc.eval_laguerre, weightf, 0., np.inf,
|
| 745 |
+
25, atol=1e-13)
|
| 746 |
+
verify_gauss_quad(sc.roots_laguerre, sc.eval_laguerre, weightf, 0., np.inf,
|
| 747 |
+
100, atol=1e-12)
|
| 748 |
+
|
| 749 |
+
x, w = sc.roots_laguerre(5, False)
|
| 750 |
+
y, v, m = sc.roots_laguerre(5, True)
|
| 751 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 752 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 753 |
+
|
| 754 |
+
muI, muI_err = integrate.quad(weightf, 0, np.inf)
|
| 755 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 756 |
+
|
| 757 |
+
assert_raises(ValueError, sc.roots_laguerre, 0)
|
| 758 |
+
assert_raises(ValueError, sc.roots_laguerre, 3.3)
|
| 759 |
+
|
| 760 |
+
def test_roots_genlaguerre():
|
| 761 |
+
def rootf(a):
|
| 762 |
+
return lambda n, mu: sc.roots_genlaguerre(n, a, mu)
|
| 763 |
+
def evalf(a):
|
| 764 |
+
return lambda n, x: sc.eval_genlaguerre(n, a, x)
|
| 765 |
+
def weightf(a):
|
| 766 |
+
return lambda x: x ** a * np.exp(-x)
|
| 767 |
+
|
| 768 |
+
vgq = verify_gauss_quad
|
| 769 |
+
vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 5)
|
| 770 |
+
vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 25, atol=1e-13)
|
| 771 |
+
vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 100, atol=1e-12)
|
| 772 |
+
|
| 773 |
+
vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 5)
|
| 774 |
+
vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 25, atol=1e-13)
|
| 775 |
+
vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 100, atol=1.6e-13)
|
| 776 |
+
|
| 777 |
+
vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 5)
|
| 778 |
+
vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 25, atol=1e-13)
|
| 779 |
+
vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 100, atol=1.03e-13)
|
| 780 |
+
|
| 781 |
+
vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 5)
|
| 782 |
+
vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 25, atol=1e-13)
|
| 783 |
+
vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 100, atol=1e-12)
|
| 784 |
+
|
| 785 |
+
vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 5)
|
| 786 |
+
vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 25, atol=1e-13)
|
| 787 |
+
vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 100, rtol=1e-14, atol=2e-13)
|
| 788 |
+
|
| 789 |
+
x, w = sc.roots_genlaguerre(5, 2, False)
|
| 790 |
+
y, v, m = sc.roots_genlaguerre(5, 2, True)
|
| 791 |
+
assert_allclose(x, y, 1e-14, 1e-14)
|
| 792 |
+
assert_allclose(w, v, 1e-14, 1e-14)
|
| 793 |
+
|
| 794 |
+
muI, muI_err = integrate.quad(weightf(2.), 0., np.inf)
|
| 795 |
+
assert_allclose(m, muI, rtol=muI_err)
|
| 796 |
+
|
| 797 |
+
assert_raises(ValueError, sc.roots_genlaguerre, 0, 2)
|
| 798 |
+
assert_raises(ValueError, sc.roots_genlaguerre, 3.3, 2)
|
| 799 |
+
assert_raises(ValueError, sc.roots_genlaguerre, 3, -1.1)
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
def test_gh_6721():
|
| 803 |
+
# Regression test for gh_6721. This should not raise.
|
| 804 |
+
sc.chebyt(65)(0.2)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_pdtr.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import scipy.special as sc
|
| 3 |
+
from numpy.testing import assert_almost_equal, assert_array_equal
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestPdtr:
|
| 7 |
+
def test(self):
|
| 8 |
+
val = sc.pdtr(0, 1)
|
| 9 |
+
assert_almost_equal(val, np.exp(-1))
|
| 10 |
+
|
| 11 |
+
def test_m_zero(self):
|
| 12 |
+
val = sc.pdtr([0, 1, 2], 0)
|
| 13 |
+
assert_array_equal(val, [1, 1, 1])
|
| 14 |
+
|
| 15 |
+
def test_rounding(self):
|
| 16 |
+
double_val = sc.pdtr([0.1, 1.1, 2.1], 1.0)
|
| 17 |
+
int_val = sc.pdtr([0, 1, 2], 1.0)
|
| 18 |
+
assert_array_equal(double_val, int_val)
|
| 19 |
+
|
| 20 |
+
def test_inf(self):
|
| 21 |
+
val = sc.pdtr(np.inf, 1.0)
|
| 22 |
+
assert_almost_equal(val, 1.0)
|
| 23 |
+
|
| 24 |
+
def test_domain(self):
|
| 25 |
+
val = sc.pdtr(-1.1, 1.0)
|
| 26 |
+
assert np.isnan(val)
|
| 27 |
+
|
| 28 |
+
class TestPdtrc:
|
| 29 |
+
def test_value(self):
|
| 30 |
+
val = sc.pdtrc(0, 1)
|
| 31 |
+
assert_almost_equal(val, 1 - np.exp(-1))
|
| 32 |
+
|
| 33 |
+
def test_m_zero(self):
|
| 34 |
+
val = sc.pdtrc([0, 1, 2], 0.0)
|
| 35 |
+
assert_array_equal(val, [0, 0, 0])
|
| 36 |
+
|
| 37 |
+
def test_rounding(self):
|
| 38 |
+
double_val = sc.pdtrc([0.1, 1.1, 2.1], 1.0)
|
| 39 |
+
int_val = sc.pdtrc([0, 1, 2], 1.0)
|
| 40 |
+
assert_array_equal(double_val, int_val)
|
| 41 |
+
|
| 42 |
+
def test_inf(self):
|
| 43 |
+
val = sc.pdtrc(np.inf, 1.0)
|
| 44 |
+
assert_almost_equal(val, 0.0)
|
| 45 |
+
|
| 46 |
+
def test_domain(self):
|
| 47 |
+
val = sc.pdtrc(-1.1, 1.0)
|
| 48 |
+
assert np.isnan(val)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_precompute_expn_asy.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from numpy.testing import assert_equal
|
| 2 |
+
|
| 3 |
+
from scipy.special._testutils import check_version, MissingModule
|
| 4 |
+
from scipy.special._precompute.expn_asy import generate_A
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
import sympy
|
| 8 |
+
from sympy import Poly
|
| 9 |
+
except ImportError:
|
| 10 |
+
sympy = MissingModule("sympy")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@check_version(sympy, "1.0")
|
| 14 |
+
def test_generate_A():
|
| 15 |
+
# Data from DLMF 8.20.5
|
| 16 |
+
x = sympy.symbols('x')
|
| 17 |
+
Astd = [Poly(1, x),
|
| 18 |
+
Poly(1, x),
|
| 19 |
+
Poly(1 - 2*x),
|
| 20 |
+
Poly(1 - 8*x + 6*x**2)]
|
| 21 |
+
Ares = generate_A(len(Astd))
|
| 22 |
+
|
| 23 |
+
for p, q in zip(Astd, Ares):
|
| 24 |
+
assert_equal(p, q)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_trig.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.testing import assert_equal, assert_allclose, suppress_warnings
|
| 4 |
+
|
| 5 |
+
from scipy.special._ufuncs import _sinpi as sinpi
|
| 6 |
+
from scipy.special._ufuncs import _cospi as cospi
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def test_integer_real_part():
|
| 10 |
+
x = np.arange(-100, 101)
|
| 11 |
+
y = np.hstack((-np.linspace(310, -30, 10), np.linspace(-30, 310, 10)))
|
| 12 |
+
x, y = np.meshgrid(x, y)
|
| 13 |
+
z = x + 1j*y
|
| 14 |
+
# In the following we should be *exactly* right
|
| 15 |
+
res = sinpi(z)
|
| 16 |
+
assert_equal(res.real, 0.0)
|
| 17 |
+
res = cospi(z)
|
| 18 |
+
assert_equal(res.imag, 0.0)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_half_integer_real_part():
|
| 22 |
+
x = np.arange(-100, 101) + 0.5
|
| 23 |
+
y = np.hstack((-np.linspace(310, -30, 10), np.linspace(-30, 310, 10)))
|
| 24 |
+
x, y = np.meshgrid(x, y)
|
| 25 |
+
z = x + 1j*y
|
| 26 |
+
# In the following we should be *exactly* right
|
| 27 |
+
res = sinpi(z)
|
| 28 |
+
assert_equal(res.imag, 0.0)
|
| 29 |
+
res = cospi(z)
|
| 30 |
+
assert_equal(res.real, 0.0)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@pytest.mark.skip("Temporary skip while gh-19526 is being resolved")
|
| 34 |
+
def test_intermediate_overlow():
|
| 35 |
+
# Make sure we avoid overflow in situations where cosh/sinh would
|
| 36 |
+
# overflow but the product with sin/cos would not
|
| 37 |
+
sinpi_pts = [complex(1 + 1e-14, 227),
|
| 38 |
+
complex(1e-35, 250),
|
| 39 |
+
complex(1e-301, 445)]
|
| 40 |
+
# Data generated with mpmath
|
| 41 |
+
sinpi_std = [complex(-8.113438309924894e+295, -np.inf),
|
| 42 |
+
complex(1.9507801934611995e+306, np.inf),
|
| 43 |
+
complex(2.205958493464539e+306, np.inf)]
|
| 44 |
+
with suppress_warnings() as sup:
|
| 45 |
+
sup.filter(RuntimeWarning, "invalid value encountered in multiply")
|
| 46 |
+
for p, std in zip(sinpi_pts, sinpi_std):
|
| 47 |
+
res = sinpi(p)
|
| 48 |
+
assert_allclose(res.real, std.real)
|
| 49 |
+
assert_allclose(res.imag, std.imag)
|
| 50 |
+
|
| 51 |
+
# Test for cosine, less interesting because cos(0) = 1.
|
| 52 |
+
p = complex(0.5 + 1e-14, 227)
|
| 53 |
+
std = complex(-8.113438309924894e+295, -np.inf)
|
| 54 |
+
with suppress_warnings() as sup:
|
| 55 |
+
sup.filter(RuntimeWarning, "invalid value encountered in multiply")
|
| 56 |
+
res = cospi(p)
|
| 57 |
+
assert_allclose(res.real, std.real)
|
| 58 |
+
assert_allclose(res.imag, std.imag)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_zero_sign():
|
| 62 |
+
y = sinpi(-0.0)
|
| 63 |
+
assert y == 0.0
|
| 64 |
+
assert np.signbit(y)
|
| 65 |
+
|
| 66 |
+
y = sinpi(0.0)
|
| 67 |
+
assert y == 0.0
|
| 68 |
+
assert not np.signbit(y)
|
| 69 |
+
|
| 70 |
+
y = cospi(0.5)
|
| 71 |
+
assert y == 0.0
|
| 72 |
+
assert not np.signbit(y)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_wrightomega.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.testing import assert_, assert_equal, assert_allclose
|
| 4 |
+
|
| 5 |
+
import scipy.special as sc
|
| 6 |
+
from scipy.special._testutils import assert_func_equal
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def test_wrightomega_nan():
|
| 10 |
+
pts = [complex(np.nan, 0),
|
| 11 |
+
complex(0, np.nan),
|
| 12 |
+
complex(np.nan, np.nan),
|
| 13 |
+
complex(np.nan, 1),
|
| 14 |
+
complex(1, np.nan)]
|
| 15 |
+
for p in pts:
|
| 16 |
+
res = sc.wrightomega(p)
|
| 17 |
+
assert_(np.isnan(res.real))
|
| 18 |
+
assert_(np.isnan(res.imag))
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_wrightomega_inf_branch():
|
| 22 |
+
pts = [complex(-np.inf, np.pi/4),
|
| 23 |
+
complex(-np.inf, -np.pi/4),
|
| 24 |
+
complex(-np.inf, 3*np.pi/4),
|
| 25 |
+
complex(-np.inf, -3*np.pi/4)]
|
| 26 |
+
expected_results = [complex(0.0, 0.0),
|
| 27 |
+
complex(0.0, -0.0),
|
| 28 |
+
complex(-0.0, 0.0),
|
| 29 |
+
complex(-0.0, -0.0)]
|
| 30 |
+
for p, expected in zip(pts, expected_results):
|
| 31 |
+
res = sc.wrightomega(p)
|
| 32 |
+
# We can't use assert_equal(res, expected) because in older versions of
|
| 33 |
+
# numpy, assert_equal doesn't check the sign of the real and imaginary
|
| 34 |
+
# parts when comparing complex zeros. It does check the sign when the
|
| 35 |
+
# arguments are *real* scalars.
|
| 36 |
+
assert_equal(res.real, expected.real)
|
| 37 |
+
assert_equal(res.imag, expected.imag)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_wrightomega_inf():
|
| 41 |
+
pts = [complex(np.inf, 10),
|
| 42 |
+
complex(-np.inf, 10),
|
| 43 |
+
complex(10, np.inf),
|
| 44 |
+
complex(10, -np.inf)]
|
| 45 |
+
for p in pts:
|
| 46 |
+
assert_equal(sc.wrightomega(p), p)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_wrightomega_singular():
|
| 50 |
+
pts = [complex(-1.0, np.pi),
|
| 51 |
+
complex(-1.0, -np.pi)]
|
| 52 |
+
for p in pts:
|
| 53 |
+
res = sc.wrightomega(p)
|
| 54 |
+
assert_equal(res, -1.0)
|
| 55 |
+
assert_(np.signbit(res.imag) == np.bool_(False))
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@pytest.mark.parametrize('x, desired', [
|
| 59 |
+
(-np.inf, 0),
|
| 60 |
+
(np.inf, np.inf),
|
| 61 |
+
])
|
| 62 |
+
def test_wrightomega_real_infinities(x, desired):
|
| 63 |
+
assert sc.wrightomega(x) == desired
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_wrightomega_real_nan():
|
| 67 |
+
assert np.isnan(sc.wrightomega(np.nan))
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def test_wrightomega_real_series_crossover():
|
| 71 |
+
desired_error = 2 * np.finfo(float).eps
|
| 72 |
+
crossover = 1e20
|
| 73 |
+
x_before_crossover = np.nextafter(crossover, -np.inf)
|
| 74 |
+
x_after_crossover = np.nextafter(crossover, np.inf)
|
| 75 |
+
# Computed using Mpmath
|
| 76 |
+
desired_before_crossover = 99999999999999983569.948
|
| 77 |
+
desired_after_crossover = 100000000000000016337.948
|
| 78 |
+
assert_allclose(
|
| 79 |
+
sc.wrightomega(x_before_crossover),
|
| 80 |
+
desired_before_crossover,
|
| 81 |
+
atol=0,
|
| 82 |
+
rtol=desired_error,
|
| 83 |
+
)
|
| 84 |
+
assert_allclose(
|
| 85 |
+
sc.wrightomega(x_after_crossover),
|
| 86 |
+
desired_after_crossover,
|
| 87 |
+
atol=0,
|
| 88 |
+
rtol=desired_error,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_wrightomega_exp_approximation_crossover():
|
| 93 |
+
desired_error = 2 * np.finfo(float).eps
|
| 94 |
+
crossover = -50
|
| 95 |
+
x_before_crossover = np.nextafter(crossover, np.inf)
|
| 96 |
+
x_after_crossover = np.nextafter(crossover, -np.inf)
|
| 97 |
+
# Computed using Mpmath
|
| 98 |
+
desired_before_crossover = 1.9287498479639314876e-22
|
| 99 |
+
desired_after_crossover = 1.9287498479639040784e-22
|
| 100 |
+
assert_allclose(
|
| 101 |
+
sc.wrightomega(x_before_crossover),
|
| 102 |
+
desired_before_crossover,
|
| 103 |
+
atol=0,
|
| 104 |
+
rtol=desired_error,
|
| 105 |
+
)
|
| 106 |
+
assert_allclose(
|
| 107 |
+
sc.wrightomega(x_after_crossover),
|
| 108 |
+
desired_after_crossover,
|
| 109 |
+
atol=0,
|
| 110 |
+
rtol=desired_error,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_wrightomega_real_versus_complex():
|
| 115 |
+
x = np.linspace(-500, 500, 1001)
|
| 116 |
+
results = sc.wrightomega(x + 0j).real
|
| 117 |
+
assert_func_equal(sc.wrightomega, results, x, atol=0, rtol=1e-14)
|
parrot/lib/python3.10/site-packages/scipy/special/tests/test_zeta.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import scipy.special as sc
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.testing import assert_equal, assert_allclose
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_zeta():
|
| 7 |
+
assert_allclose(sc.zeta(2,2), np.pi**2/6 - 1, rtol=1e-12)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_zetac():
|
| 11 |
+
# Expected values in the following were computed using Wolfram
|
| 12 |
+
# Alpha's `Zeta[x] - 1`
|
| 13 |
+
x = [-2.1, 0.8, 0.9999, 9, 50, 75]
|
| 14 |
+
desired = [
|
| 15 |
+
-0.9972705002153750,
|
| 16 |
+
-5.437538415895550,
|
| 17 |
+
-10000.42279161673,
|
| 18 |
+
0.002008392826082214,
|
| 19 |
+
8.881784210930816e-16,
|
| 20 |
+
2.646977960169853e-23,
|
| 21 |
+
]
|
| 22 |
+
assert_allclose(sc.zetac(x), desired, rtol=1e-12)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_zetac_special_cases():
|
| 26 |
+
assert sc.zetac(np.inf) == 0
|
| 27 |
+
assert np.isnan(sc.zetac(-np.inf))
|
| 28 |
+
assert sc.zetac(0) == -1.5
|
| 29 |
+
assert sc.zetac(1.0) == np.inf
|
| 30 |
+
|
| 31 |
+
assert_equal(sc.zetac([-2, -50, -100]), -1)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_riemann_zeta_special_cases():
|
| 35 |
+
assert np.isnan(sc.zeta(np.nan))
|
| 36 |
+
assert sc.zeta(np.inf) == 1
|
| 37 |
+
assert sc.zeta(0) == -0.5
|
| 38 |
+
|
| 39 |
+
# Riemann zeta is zero add negative even integers.
|
| 40 |
+
assert_equal(sc.zeta([-2, -4, -6, -8, -10]), 0)
|
| 41 |
+
|
| 42 |
+
assert_allclose(sc.zeta(2), np.pi**2/6, rtol=1e-12)
|
| 43 |
+
assert_allclose(sc.zeta(4), np.pi**4/90, rtol=1e-12)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_riemann_zeta_avoid_overflow():
|
| 47 |
+
s = -260.00000000001
|
| 48 |
+
desired = -5.6966307844402683127e+297 # Computed with Mpmath
|
| 49 |
+
assert_allclose(sc.zeta(s), desired, atol=0, rtol=5e-14)
|
vllm/lib/python3.10/site-packages/opencensus/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (227 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
|
vllm/lib/python3.10/site-packages/opencensus/common/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (234 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/backports/__init__.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import six
|
| 16 |
+
|
| 17 |
+
import weakref
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class WeakMethod(weakref.ref): # pragma: NO COVER
|
| 21 |
+
"""
|
| 22 |
+
A custom `weakref.ref` subclass which simulates a weak reference to
|
| 23 |
+
a bound method, working around the lifetime problem of bound methods.
|
| 24 |
+
|
| 25 |
+
This is a copy of the WeakMethod class that ships with weakref in the
|
| 26 |
+
python 3.7 standard library, adapted to work in 2.6. See:
|
| 27 |
+
https://github.com/python/cpython/blob/a31f4cc881992e84d351957bd9ac1a92f882fa39/Lib/weakref.py#L36-L87
|
| 28 |
+
""" # noqa
|
| 29 |
+
|
| 30 |
+
__slots__ = "_func_ref", "_meth_type", "_alive", "__weakref__"
|
| 31 |
+
|
| 32 |
+
def __new__(cls, meth, callback=None):
|
| 33 |
+
try:
|
| 34 |
+
obj = meth.__self__
|
| 35 |
+
func = meth.__func__
|
| 36 |
+
except AttributeError:
|
| 37 |
+
error = TypeError("argument should be a bound method, not {}"
|
| 38 |
+
.format(type(meth)))
|
| 39 |
+
six.raise_from(error, None)
|
| 40 |
+
|
| 41 |
+
def _cb(arg):
|
| 42 |
+
# The self-weakref trick is needed to avoid creating a reference
|
| 43 |
+
# cycle.
|
| 44 |
+
self = self_wr()
|
| 45 |
+
if self._alive:
|
| 46 |
+
self._alive = False
|
| 47 |
+
if callback is not None:
|
| 48 |
+
callback(self)
|
| 49 |
+
self = weakref.ref.__new__(cls, obj, _cb)
|
| 50 |
+
self._func_ref = weakref.ref(func, _cb)
|
| 51 |
+
self._meth_type = type(meth)
|
| 52 |
+
self._alive = True
|
| 53 |
+
self_wr = weakref.ref(self)
|
| 54 |
+
return self
|
| 55 |
+
|
| 56 |
+
def __call__(self):
|
| 57 |
+
obj = super(WeakMethod, self).__call__()
|
| 58 |
+
func = self._func_ref()
|
| 59 |
+
if obj is None or func is None:
|
| 60 |
+
return None
|
| 61 |
+
return self._meth_type(func, obj)
|
| 62 |
+
|
| 63 |
+
def __eq__(self, other):
|
| 64 |
+
if isinstance(other, WeakMethod):
|
| 65 |
+
if not self._alive or not other._alive:
|
| 66 |
+
return self is other
|
| 67 |
+
return (weakref.ref.__eq__(self, other)
|
| 68 |
+
and self._func_ref == other._func_ref)
|
| 69 |
+
return False
|
| 70 |
+
|
| 71 |
+
def __ne__(self, other):
|
| 72 |
+
if isinstance(other, WeakMethod):
|
| 73 |
+
if not self._alive or not other._alive:
|
| 74 |
+
return self is not other
|
| 75 |
+
return (weakref.ref.__ne__(self, other)
|
| 76 |
+
or self._func_ref != other._func_ref)
|
| 77 |
+
return True
|
| 78 |
+
|
| 79 |
+
__hash__ = weakref.ref.__hash__
|
vllm/lib/python3.10/site-packages/opencensus/common/backports/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (2.24 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/configuration/__init__.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import importlib
|
| 16 |
+
|
| 17 |
+
__all__ = ['Namespace', 'load']
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class Namespace(object):
|
| 21 |
+
def __init__(self, name, parent=None):
|
| 22 |
+
self.parent = parent
|
| 23 |
+
self.name = name
|
| 24 |
+
|
| 25 |
+
def __getattr__(self, name):
|
| 26 |
+
return type(self)(name, self)
|
| 27 |
+
|
| 28 |
+
def __str__(self):
|
| 29 |
+
if self.parent is None:
|
| 30 |
+
return self.name
|
| 31 |
+
return '{!s}.{}'.format(self.parent, self.name)
|
| 32 |
+
|
| 33 |
+
def __call__(self, *args, **kwargs):
|
| 34 |
+
ctor = getattr(importlib.import_module(str(self.parent)), self.name)
|
| 35 |
+
return ctor(*args, **kwargs)
|
| 36 |
+
|
| 37 |
+
@classmethod
|
| 38 |
+
def eval(cls, expr):
|
| 39 |
+
return eval(expr, {}, {'opencensus': cls('opencensus')})
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def load(expr):
|
| 43 |
+
"""Dynamically import OpenCensus components and evaluate the provided
|
| 44 |
+
configuration expression.
|
| 45 |
+
"""
|
| 46 |
+
return Namespace.eval(expr)
|
vllm/lib/python3.10/site-packages/opencensus/common/configuration/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.47 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/http_handler/__init__.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# For Python 3.0 and later
|
| 17 |
+
from urllib.request import urlopen, Request
|
| 18 |
+
from urllib.error import HTTPError, URLError
|
| 19 |
+
except ImportError:
|
| 20 |
+
# Fall back to Python 2's urllib2
|
| 21 |
+
from urllib2 import urlopen, Request
|
| 22 |
+
from urllib2 import HTTPError, URLError
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
import socket
|
| 26 |
+
|
| 27 |
+
_REQUEST_TIMEOUT = 2 # in secs
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_request(request_url, request_headers=dict()):
|
| 31 |
+
"""Execute http get request on given request_url with optional headers
|
| 32 |
+
"""
|
| 33 |
+
request = Request(request_url)
|
| 34 |
+
for key, val in request_headers.items():
|
| 35 |
+
request.add_header(key, val)
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
response = urlopen(request, timeout=_REQUEST_TIMEOUT)
|
| 39 |
+
response_content = response.read()
|
| 40 |
+
except (HTTPError, URLError, socket.timeout):
|
| 41 |
+
response_content = None
|
| 42 |
+
|
| 43 |
+
return response_content
|
vllm/lib/python3.10/site-packages/opencensus/common/http_handler/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (892 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__init__.py
ADDED
|
File without changes
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (186 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/aws_identity_doc_utils.cpython-310.pyc
ADDED
|
Binary file (2.19 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/gcp_metadata_config.cpython-310.pyc
ADDED
|
Binary file (2.84 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/k8s_utils.cpython-310.pyc
ADDED
|
Binary file (1.26 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/__pycache__/monitored_resource.cpython-310.pyc
ADDED
|
Binary file (1.73 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/aws_identity_doc_utils.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
|
| 17 |
+
from opencensus.common.http_handler import get_request
|
| 18 |
+
|
| 19 |
+
REGION_KEY = 'region'
|
| 20 |
+
ACCOUNT_ID_KEY = 'aws_account'
|
| 21 |
+
INSTANCE_ID_KEY = 'instance_id'
|
| 22 |
+
|
| 23 |
+
# AWS provides Instance Metadata via below url
|
| 24 |
+
_AWS_INSTANCE_IDENTITY_DOCUMENT_URI = \
|
| 25 |
+
"http://169.254.169.254/latest/dynamic/instance-identity/document"
|
| 26 |
+
|
| 27 |
+
_AWS_ATTRIBUTES = {
|
| 28 |
+
# Region is the AWS region for the VM. The format of this field is
|
| 29 |
+
# "aws:{region}", where supported values for {region} are listed at
|
| 30 |
+
# http://docs.aws.amazon.com/general/latest/gr/rande.html.
|
| 31 |
+
'region': REGION_KEY,
|
| 32 |
+
|
| 33 |
+
# accountId is the AWS account number for the VM.
|
| 34 |
+
'accountId': ACCOUNT_ID_KEY,
|
| 35 |
+
|
| 36 |
+
# instanceId is the instance id of the instance.
|
| 37 |
+
'instanceId': INSTANCE_ID_KEY
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# inited is used to make sure AWS initialize executes only once.
|
| 41 |
+
inited = False
|
| 42 |
+
|
| 43 |
+
# Detects if the application is running on EC2 by making a connection to AWS
|
| 44 |
+
# instance identity document URI.If connection is successful, application
|
| 45 |
+
# should be on an EC2 instance.
|
| 46 |
+
is_running_on_aws = False
|
| 47 |
+
|
| 48 |
+
aws_metadata_map = {}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class AwsIdentityDocumentUtils(object):
|
| 52 |
+
"""Util methods for getting and parsing AWS instance identity document."""
|
| 53 |
+
|
| 54 |
+
inited = False
|
| 55 |
+
is_running = False
|
| 56 |
+
|
| 57 |
+
@classmethod
|
| 58 |
+
def _initialize_aws_identity_document(cls):
|
| 59 |
+
"""This method, tries to establish an HTTP connection to AWS instance
|
| 60 |
+
identity document url. If the application is running on an EC2
|
| 61 |
+
instance, we should be able to get back a valid JSON document. Make a
|
| 62 |
+
http get request call and store data in local map.
|
| 63 |
+
This method should only be called once.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
if cls.inited:
|
| 67 |
+
return
|
| 68 |
+
|
| 69 |
+
content = get_request(_AWS_INSTANCE_IDENTITY_DOCUMENT_URI)
|
| 70 |
+
if content is not None:
|
| 71 |
+
content = json.loads(content)
|
| 72 |
+
for env_var, attribute_key in _AWS_ATTRIBUTES.items():
|
| 73 |
+
attribute_value = content.get(env_var)
|
| 74 |
+
if attribute_value is not None:
|
| 75 |
+
aws_metadata_map[attribute_key] = attribute_value
|
| 76 |
+
|
| 77 |
+
cls.is_running = True
|
| 78 |
+
|
| 79 |
+
cls.inited = True
|
| 80 |
+
|
| 81 |
+
@classmethod
|
| 82 |
+
def is_running_on_aws(cls):
|
| 83 |
+
cls._initialize_aws_identity_document()
|
| 84 |
+
return cls.is_running
|
| 85 |
+
|
| 86 |
+
def get_aws_metadata(self):
|
| 87 |
+
"""AWS Instance Identity Document is a JSON file.
|
| 88 |
+
See docs.aws.amazon.com/AWSEC2/latest/UserGuide/
|
| 89 |
+
instance-identity-documents.html.
|
| 90 |
+
:return:
|
| 91 |
+
"""
|
| 92 |
+
if self.is_running_on_aws():
|
| 93 |
+
return aws_metadata_map
|
| 94 |
+
|
| 95 |
+
return dict()
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/gcp_metadata_config.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from opencensus.common.http_handler import get_request
|
| 16 |
+
|
| 17 |
+
_GCP_METADATA_URI = 'http://metadata.google.internal/computeMetadata/v1/'
|
| 18 |
+
_GCP_METADATA_URI_HEADER = {'Metadata-Flavor': 'Google'}
|
| 19 |
+
|
| 20 |
+
# ID of the GCP project associated with this resource, such as "my-project"
|
| 21 |
+
PROJECT_ID_KEY = 'project_id'
|
| 22 |
+
|
| 23 |
+
# Numeric VM instance identifier assigned by GCE
|
| 24 |
+
INSTANCE_ID_KEY = 'instance_id'
|
| 25 |
+
|
| 26 |
+
# The GCE zone in which the VM is running
|
| 27 |
+
ZONE_KEY = 'zone'
|
| 28 |
+
|
| 29 |
+
# GKE cluster name
|
| 30 |
+
CLUSTER_NAME_KEY = 'instance/attributes/cluster-name'
|
| 31 |
+
|
| 32 |
+
# GCE common attributes
|
| 33 |
+
# See: https://cloud.google.com/appengine/docs/flexible/python/runtime#environment_variables # noqa
|
| 34 |
+
_GCE_ATTRIBUTES = {
|
| 35 |
+
PROJECT_ID_KEY: 'project/project-id',
|
| 36 |
+
INSTANCE_ID_KEY: 'instance/id',
|
| 37 |
+
ZONE_KEY: 'instance/zone'
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
_ATTRIBUTE_URI_TRANSFORMATIONS = {
|
| 41 |
+
_GCE_ATTRIBUTES[ZONE_KEY]:
|
| 42 |
+
lambda v: v[v.rfind('/') + 1:] if '/' in v else v
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
_GCP_METADATA_MAP = {}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class GcpMetadataConfig(object):
|
| 49 |
+
"""GcpMetadata represents metadata retrieved from GCP (GKE and GCE)
|
| 50 |
+
environment. Some attributes are retrieved from the system environment.
|
| 51 |
+
see : <a href="https://cloud.google.com/compute/docs/
|
| 52 |
+
storing-retrieving-metadata"> https://cloud.google.com/compute/docs/storing
|
| 53 |
+
-retrieving-metadata</a>
|
| 54 |
+
"""
|
| 55 |
+
inited = False
|
| 56 |
+
is_running = False
|
| 57 |
+
|
| 58 |
+
@classmethod
|
| 59 |
+
def _initialize_metadata_service(cls):
|
| 60 |
+
"""Initialize metadata service once and load gcp metadata into map
|
| 61 |
+
This method should only be called once.
|
| 62 |
+
"""
|
| 63 |
+
if cls.inited:
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
instance_id = cls.get_attribute('instance/id')
|
| 67 |
+
|
| 68 |
+
if instance_id is not None:
|
| 69 |
+
cls.is_running = True
|
| 70 |
+
|
| 71 |
+
_GCP_METADATA_MAP['instance_id'] = instance_id
|
| 72 |
+
|
| 73 |
+
# fetch attributes from metadata request
|
| 74 |
+
for attribute_key, attribute_uri in _GCE_ATTRIBUTES.items():
|
| 75 |
+
if attribute_key not in _GCP_METADATA_MAP:
|
| 76 |
+
attribute_value = cls.get_attribute(attribute_uri)
|
| 77 |
+
if attribute_value is not None: # pragma: NO COVER
|
| 78 |
+
_GCP_METADATA_MAP[attribute_key] = attribute_value
|
| 79 |
+
|
| 80 |
+
cls.inited = True
|
| 81 |
+
|
| 82 |
+
@classmethod
|
| 83 |
+
def is_running_on_gcp(cls):
|
| 84 |
+
cls._initialize_metadata_service()
|
| 85 |
+
return cls.is_running
|
| 86 |
+
|
| 87 |
+
def get_gce_metadata(self):
|
| 88 |
+
"""for GCP GCE instance"""
|
| 89 |
+
if self.is_running_on_gcp():
|
| 90 |
+
return _GCP_METADATA_MAP
|
| 91 |
+
|
| 92 |
+
return dict()
|
| 93 |
+
|
| 94 |
+
@staticmethod
|
| 95 |
+
def get_attribute(attribute_uri):
|
| 96 |
+
"""
|
| 97 |
+
Fetch the requested instance metadata entry.
|
| 98 |
+
:param attribute_uri: attribute_uri: attribute name relative to the
|
| 99 |
+
computeMetadata/v1 prefix
|
| 100 |
+
:return: The value read from the metadata service or None
|
| 101 |
+
"""
|
| 102 |
+
attribute_value = get_request(_GCP_METADATA_URI + attribute_uri,
|
| 103 |
+
_GCP_METADATA_URI_HEADER)
|
| 104 |
+
|
| 105 |
+
if attribute_value is not None and isinstance(attribute_value, bytes):
|
| 106 |
+
# At least in python3, bytes are are returned from
|
| 107 |
+
# urllib (although the response is text), convert
|
| 108 |
+
# to a normal string:
|
| 109 |
+
attribute_value = attribute_value.decode('utf-8')
|
| 110 |
+
|
| 111 |
+
transformation = _ATTRIBUTE_URI_TRANSFORMATIONS.get(attribute_uri)
|
| 112 |
+
if transformation is not None:
|
| 113 |
+
attribute_value = transformation(attribute_value)
|
| 114 |
+
|
| 115 |
+
return attribute_value
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/k8s_utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
|
| 17 |
+
from opencensus.common.monitored_resource import gcp_metadata_config
|
| 18 |
+
|
| 19 |
+
# Env var that signals that we're in a kubernetes container
|
| 20 |
+
_KUBERNETES_SERVICE_HOST = 'KUBERNETES_SERVICE_HOST'
|
| 21 |
+
|
| 22 |
+
# Name of the cluster the container is running in
|
| 23 |
+
CLUSTER_NAME_KEY = 'k8s.io/cluster/name'
|
| 24 |
+
|
| 25 |
+
# ID of the instance the container is running on
|
| 26 |
+
NAMESPACE_NAME_KEY = 'k8s.io/namespace/name'
|
| 27 |
+
|
| 28 |
+
# Container pod ID
|
| 29 |
+
POD_NAME_KEY = 'k8s.io/pod/name'
|
| 30 |
+
|
| 31 |
+
# Container name
|
| 32 |
+
CONTAINER_NAME_KEY = 'k8s.io/container/name'
|
| 33 |
+
|
| 34 |
+
# Attributes set from environment variables
|
| 35 |
+
_K8S_ENV_ATTRIBUTES = {
|
| 36 |
+
CONTAINER_NAME_KEY: 'CONTAINER_NAME',
|
| 37 |
+
NAMESPACE_NAME_KEY: 'NAMESPACE',
|
| 38 |
+
POD_NAME_KEY: 'HOSTNAME'
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def is_k8s_environment():
|
| 43 |
+
"""Whether the environment is a kubernetes container.
|
| 44 |
+
|
| 45 |
+
The KUBERNETES_SERVICE_HOST environment variable must be set.
|
| 46 |
+
"""
|
| 47 |
+
return _KUBERNETES_SERVICE_HOST in os.environ
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def get_k8s_metadata():
|
| 51 |
+
"""Get kubernetes container metadata, as on GCP GKE."""
|
| 52 |
+
k8s_metadata = {}
|
| 53 |
+
|
| 54 |
+
gcp_cluster = (gcp_metadata_config.GcpMetadataConfig
|
| 55 |
+
.get_attribute(gcp_metadata_config.CLUSTER_NAME_KEY))
|
| 56 |
+
if gcp_cluster is not None:
|
| 57 |
+
k8s_metadata[CLUSTER_NAME_KEY] = gcp_cluster
|
| 58 |
+
|
| 59 |
+
for attribute_key, attribute_env in _K8S_ENV_ATTRIBUTES.items():
|
| 60 |
+
attribute_value = os.environ.get(attribute_env)
|
| 61 |
+
if attribute_value is not None:
|
| 62 |
+
k8s_metadata[attribute_key] = attribute_value
|
| 63 |
+
|
| 64 |
+
return k8s_metadata
|
vllm/lib/python3.10/site-packages/opencensus/common/monitored_resource/monitored_resource.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from opencensus.common import resource
|
| 16 |
+
from opencensus.common.monitored_resource import (
|
| 17 |
+
aws_identity_doc_utils,
|
| 18 |
+
gcp_metadata_config,
|
| 19 |
+
k8s_utils,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Supported environments (resource types)
|
| 23 |
+
_GCE_INSTANCE = "gce_instance"
|
| 24 |
+
_K8S_CONTAINER = "k8s_container"
|
| 25 |
+
_AWS_EC2_INSTANCE = "aws_ec2_instance"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def is_gce_environment():
|
| 29 |
+
"""Whether the environment is a virtual machine on GCE."""
|
| 30 |
+
return gcp_metadata_config.GcpMetadataConfig.is_running_on_gcp()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def is_aws_environment():
|
| 34 |
+
"""Whether the environment is a virtual machine instance on EC2."""
|
| 35 |
+
return aws_identity_doc_utils.AwsIdentityDocumentUtils.is_running_on_aws()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def get_instance():
|
| 39 |
+
"""Get a resource based on the application environment.
|
| 40 |
+
|
| 41 |
+
Returns a `Resource` configured for the current environment, or None if the
|
| 42 |
+
environment is unknown or unsupported.
|
| 43 |
+
|
| 44 |
+
:rtype: :class:`opencensus.common.resource.Resource` or None
|
| 45 |
+
:return: A `Resource` configured for the current environment.
|
| 46 |
+
"""
|
| 47 |
+
resources = []
|
| 48 |
+
env_resource = resource.get_from_env()
|
| 49 |
+
if env_resource is not None:
|
| 50 |
+
resources.append(env_resource)
|
| 51 |
+
|
| 52 |
+
if k8s_utils.is_k8s_environment():
|
| 53 |
+
resources.append(resource.Resource(
|
| 54 |
+
_K8S_CONTAINER, k8s_utils.get_k8s_metadata()))
|
| 55 |
+
|
| 56 |
+
if is_gce_environment():
|
| 57 |
+
resources.append(resource.Resource(
|
| 58 |
+
_GCE_INSTANCE,
|
| 59 |
+
gcp_metadata_config.GcpMetadataConfig().get_gce_metadata()))
|
| 60 |
+
elif is_aws_environment():
|
| 61 |
+
resources.append(resource.Resource(
|
| 62 |
+
_AWS_EC2_INSTANCE,
|
| 63 |
+
(aws_identity_doc_utils.AwsIdentityDocumentUtils()
|
| 64 |
+
.get_aws_metadata())))
|
| 65 |
+
|
| 66 |
+
if not resources:
|
| 67 |
+
return None
|
| 68 |
+
return resource.merge_resources(resources)
|
vllm/lib/python3.10/site-packages/opencensus/common/resource/__init__.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import logging
|
| 17 |
+
import os
|
| 18 |
+
import re
|
| 19 |
+
from copy import copy
|
| 20 |
+
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
OC_RESOURCE_TYPE = 'OC_RESOURCE_TYPE'
|
| 25 |
+
OC_RESOURCE_LABELS = 'OC_RESOURCE_LABELS'
|
| 26 |
+
|
| 27 |
+
# Matches anything outside ASCII 32-126 inclusive
|
| 28 |
+
_NON_PRINTABLE_ASCII = re.compile(
|
| 29 |
+
r'[^ !"#$%&\'()*+,\-./:;<=>?@\[\\\]^_`{|}~0-9a-zA-Z]')
|
| 30 |
+
|
| 31 |
+
# Label key/value tokens, may be quoted
|
| 32 |
+
_WORD_RES = r'(\'[^\']*\'|"[^"]*"|[^\s,=]+)'
|
| 33 |
+
|
| 34 |
+
_KV_RE = re.compile(r"""
|
| 35 |
+
\s* # ignore leading spaces
|
| 36 |
+
(?P<key>{word_re}) # capture the key word
|
| 37 |
+
\s*=\s*
|
| 38 |
+
(?P<val>{word_re}) # capture the value word
|
| 39 |
+
\s* # ignore trailing spaces
|
| 40 |
+
""".format(word_re=_WORD_RES), re.VERBOSE)
|
| 41 |
+
|
| 42 |
+
_LABELS_RE = re.compile(r"""
|
| 43 |
+
^\s*{word_re}\s*=\s*{word_re}\s* # _KV_RE without the named groups
|
| 44 |
+
(,\s*{word_re}\s*=\s*{word_re}\s*)* # more KV pairs, comma delimited
|
| 45 |
+
$
|
| 46 |
+
""".format(word_re=_WORD_RES), re.VERBOSE)
|
| 47 |
+
|
| 48 |
+
_UNQUOTE_RE = re.compile(r'^([\'"]?)([^\1]*)(\1)$')
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def merge_resources(resource_list):
|
| 52 |
+
"""Merge multiple resources to get a new resource.
|
| 53 |
+
|
| 54 |
+
Resources earlier in the list take precedence: if multiple resources share
|
| 55 |
+
a label key, use the value from the first resource in the list with that
|
| 56 |
+
key. The combined resource's type will be the first non-null type in the
|
| 57 |
+
list.
|
| 58 |
+
|
| 59 |
+
:type resource_list: list(:class:`Resource`)
|
| 60 |
+
:param resource_list: The list of resources to combine.
|
| 61 |
+
|
| 62 |
+
:rtype: :class:`Resource`
|
| 63 |
+
:return: The new combined resource.
|
| 64 |
+
"""
|
| 65 |
+
if not resource_list:
|
| 66 |
+
raise ValueError
|
| 67 |
+
rtype = None
|
| 68 |
+
for rr in resource_list:
|
| 69 |
+
if rr.type:
|
| 70 |
+
rtype = rr.type
|
| 71 |
+
break
|
| 72 |
+
labels = {}
|
| 73 |
+
for rr in reversed(resource_list):
|
| 74 |
+
labels.update(rr.labels)
|
| 75 |
+
return Resource(rtype, labels)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def check_ascii_256(string):
|
| 79 |
+
"""Check that `string` is printable ASCII and at most 256 chars.
|
| 80 |
+
|
| 81 |
+
Raise a `ValueError` if this check fails. Note that `string` itself doesn't
|
| 82 |
+
have to be ASCII-encoded.
|
| 83 |
+
|
| 84 |
+
:type string: str
|
| 85 |
+
:param string: The string to check.
|
| 86 |
+
"""
|
| 87 |
+
if string is None:
|
| 88 |
+
return
|
| 89 |
+
if len(string) > 256:
|
| 90 |
+
raise ValueError("Value is longer than 256 characters")
|
| 91 |
+
bad_char = _NON_PRINTABLE_ASCII.search(string)
|
| 92 |
+
if bad_char:
|
| 93 |
+
raise ValueError(u'Character "{}" at position {} is not printable '
|
| 94 |
+
'ASCII'
|
| 95 |
+
.format(
|
| 96 |
+
string[bad_char.start():bad_char.end()],
|
| 97 |
+
bad_char.start()))
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class Resource(object):
|
| 101 |
+
"""A description of the entity for which signals are reported.
|
| 102 |
+
|
| 103 |
+
`type_` and `labels`' keys and values should contain only printable ASCII
|
| 104 |
+
and should be at most 256 characters.
|
| 105 |
+
|
| 106 |
+
See:
|
| 107 |
+
https://github.com/census-instrumentation/opencensus-specs/blob/master/resource/Resource.md
|
| 108 |
+
|
| 109 |
+
:type type_: str
|
| 110 |
+
:param type_: The resource type identifier.
|
| 111 |
+
|
| 112 |
+
:type labels: dict
|
| 113 |
+
:param labels: Key-value pairs that describe the entity.
|
| 114 |
+
""" # noqa
|
| 115 |
+
|
| 116 |
+
def __init__(self, type_=None, labels=None):
|
| 117 |
+
if type_ is not None and not type_:
|
| 118 |
+
raise ValueError("Resource type must not be empty")
|
| 119 |
+
check_ascii_256(type_)
|
| 120 |
+
if labels is None:
|
| 121 |
+
labels = {}
|
| 122 |
+
for key, value in labels.items():
|
| 123 |
+
if not key:
|
| 124 |
+
raise ValueError("Resource key must not be null or empty")
|
| 125 |
+
if value is None:
|
| 126 |
+
raise ValueError("Resource value must not be null")
|
| 127 |
+
check_ascii_256(key)
|
| 128 |
+
check_ascii_256(value)
|
| 129 |
+
|
| 130 |
+
self.type = type_
|
| 131 |
+
self.labels = copy(labels)
|
| 132 |
+
|
| 133 |
+
def get_type(self):
|
| 134 |
+
"""Get this resource's type.
|
| 135 |
+
|
| 136 |
+
:rtype: str
|
| 137 |
+
:return: The resource's type.
|
| 138 |
+
"""
|
| 139 |
+
return self.type
|
| 140 |
+
|
| 141 |
+
def get_labels(self):
|
| 142 |
+
"""Get this resource's labels.
|
| 143 |
+
|
| 144 |
+
:rtype: dict
|
| 145 |
+
:return: The resource's label dict.
|
| 146 |
+
"""
|
| 147 |
+
return copy(self.labels)
|
| 148 |
+
|
| 149 |
+
def merge(self, other):
|
| 150 |
+
"""Get a copy of this resource combined with another resource.
|
| 151 |
+
|
| 152 |
+
The combined resource will have the union of both resources' labels,
|
| 153 |
+
keeping this resource's label values if they conflict.
|
| 154 |
+
|
| 155 |
+
:type other: :class:`Resource`
|
| 156 |
+
:param other: The other resource to merge.
|
| 157 |
+
|
| 158 |
+
:rtype: :class:`Resource`
|
| 159 |
+
:return: The new combined resource.
|
| 160 |
+
"""
|
| 161 |
+
return merge_resources([self, other])
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def unquote(string):
|
| 165 |
+
"""Strip quotes surrounding `string` if they exist.
|
| 166 |
+
|
| 167 |
+
>>> unquote('abc')
|
| 168 |
+
'abc'
|
| 169 |
+
>>> unquote('"abc"')
|
| 170 |
+
'abc'
|
| 171 |
+
>>> unquote("'abc'")
|
| 172 |
+
'abc'
|
| 173 |
+
>>> unquote('"a\\'b\\'c"')
|
| 174 |
+
"a'b'c"
|
| 175 |
+
"""
|
| 176 |
+
return _UNQUOTE_RE.sub(r'\2', string)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def parse_labels(labels_str):
|
| 180 |
+
"""Parse label keys and values following the Resource spec.
|
| 181 |
+
|
| 182 |
+
>>> parse_labels("k=v")
|
| 183 |
+
{'k': 'v'}
|
| 184 |
+
>>> parse_labels("k1=v1, k2=v2")
|
| 185 |
+
{'k1': 'v1', 'k2': 'v2'}
|
| 186 |
+
>>> parse_labels("k1='v1,=z1'")
|
| 187 |
+
{'k1': 'v1,=z1'}
|
| 188 |
+
"""
|
| 189 |
+
if not _LABELS_RE.match(labels_str):
|
| 190 |
+
return None
|
| 191 |
+
labels = {}
|
| 192 |
+
for kv in _KV_RE.finditer(labels_str):
|
| 193 |
+
gd = kv.groupdict()
|
| 194 |
+
key = unquote(gd['key'])
|
| 195 |
+
if key in labels:
|
| 196 |
+
logger.warning('Duplicate label key "%s"', key)
|
| 197 |
+
labels[key] = unquote(gd['val'])
|
| 198 |
+
return labels
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def get_from_env():
|
| 202 |
+
"""Get a Resource from environment variables.
|
| 203 |
+
|
| 204 |
+
:rtype: :class:`Resource`
|
| 205 |
+
:return: A resource with type and labels from the environment.
|
| 206 |
+
"""
|
| 207 |
+
type_env = os.getenv(OC_RESOURCE_TYPE)
|
| 208 |
+
if type_env is None:
|
| 209 |
+
return None
|
| 210 |
+
type_env = type_env.strip()
|
| 211 |
+
|
| 212 |
+
labels_env = os.getenv(OC_RESOURCE_LABELS)
|
| 213 |
+
if labels_env is None:
|
| 214 |
+
return Resource(type_env)
|
| 215 |
+
|
| 216 |
+
labels = parse_labels(labels_env)
|
| 217 |
+
|
| 218 |
+
return Resource(type_env, labels)
|
vllm/lib/python3.10/site-packages/opencensus/common/resource/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (5.71 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/__init__.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
import contextvars
|
| 18 |
+
except ImportError:
|
| 19 |
+
contextvars = None
|
| 20 |
+
|
| 21 |
+
import threading
|
| 22 |
+
|
| 23 |
+
__all__ = ['RuntimeContext']
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class _RuntimeContext(object):
|
| 27 |
+
@classmethod
|
| 28 |
+
def clear(cls):
|
| 29 |
+
"""Clear all slots to their default value."""
|
| 30 |
+
|
| 31 |
+
raise NotImplementedError # pragma: NO COVER
|
| 32 |
+
|
| 33 |
+
@classmethod
|
| 34 |
+
def register_slot(cls, name, default=None):
|
| 35 |
+
"""Register a context slot with an optional default value.
|
| 36 |
+
|
| 37 |
+
:type name: str
|
| 38 |
+
:param name: The name of the context slot.
|
| 39 |
+
|
| 40 |
+
:type default: object
|
| 41 |
+
:param name: The default value of the slot, can be a value or lambda.
|
| 42 |
+
|
| 43 |
+
:returns: The registered slot.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
raise NotImplementedError # pragma: NO COVER
|
| 47 |
+
|
| 48 |
+
def apply(self, snapshot):
|
| 49 |
+
"""Set the current context from a given snapshot dictionary"""
|
| 50 |
+
|
| 51 |
+
for name in snapshot:
|
| 52 |
+
setattr(self, name, snapshot[name])
|
| 53 |
+
|
| 54 |
+
def snapshot(self):
|
| 55 |
+
"""Return a dictionary of current slots by reference."""
|
| 56 |
+
|
| 57 |
+
return dict((n, self._slots[n].get()) for n in self._slots.keys())
|
| 58 |
+
|
| 59 |
+
def __repr__(self):
|
| 60 |
+
return ('{}({})'.format(type(self).__name__, self.snapshot()))
|
| 61 |
+
|
| 62 |
+
def __getattr__(self, name):
|
| 63 |
+
if name not in self._slots:
|
| 64 |
+
raise AttributeError('{} is not a registered context slot'
|
| 65 |
+
.format(name))
|
| 66 |
+
slot = self._slots[name]
|
| 67 |
+
return slot.get()
|
| 68 |
+
|
| 69 |
+
def __setattr__(self, name, value):
|
| 70 |
+
if name not in self._slots:
|
| 71 |
+
raise AttributeError('{} is not a registered context slot'
|
| 72 |
+
.format(name))
|
| 73 |
+
slot = self._slots[name]
|
| 74 |
+
slot.set(value)
|
| 75 |
+
|
| 76 |
+
def with_current_context(self, func):
|
| 77 |
+
"""Capture the current context and apply it to the provided func"""
|
| 78 |
+
|
| 79 |
+
caller_context = self.snapshot()
|
| 80 |
+
|
| 81 |
+
def call_with_current_context(*args, **kwargs):
|
| 82 |
+
try:
|
| 83 |
+
backup_context = self.snapshot()
|
| 84 |
+
self.apply(caller_context)
|
| 85 |
+
return func(*args, **kwargs)
|
| 86 |
+
finally:
|
| 87 |
+
self.apply(backup_context)
|
| 88 |
+
|
| 89 |
+
return call_with_current_context
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class _ThreadLocalRuntimeContext(_RuntimeContext):
|
| 93 |
+
_lock = threading.Lock()
|
| 94 |
+
_slots = {}
|
| 95 |
+
|
| 96 |
+
class Slot(object):
|
| 97 |
+
_thread_local = threading.local()
|
| 98 |
+
|
| 99 |
+
def __init__(self, name, default):
|
| 100 |
+
self.name = name
|
| 101 |
+
self.default = default if callable(default) else (lambda: default)
|
| 102 |
+
|
| 103 |
+
def clear(self):
|
| 104 |
+
setattr(self._thread_local, self.name, self.default())
|
| 105 |
+
|
| 106 |
+
def get(self):
|
| 107 |
+
try:
|
| 108 |
+
return getattr(self._thread_local, self.name)
|
| 109 |
+
except AttributeError:
|
| 110 |
+
value = self.default()
|
| 111 |
+
self.set(value)
|
| 112 |
+
return value
|
| 113 |
+
|
| 114 |
+
def set(self, value):
|
| 115 |
+
setattr(self._thread_local, self.name, value)
|
| 116 |
+
|
| 117 |
+
@classmethod
|
| 118 |
+
def clear(cls):
|
| 119 |
+
with cls._lock:
|
| 120 |
+
for name in cls._slots:
|
| 121 |
+
slot = cls._slots[name]
|
| 122 |
+
slot.clear()
|
| 123 |
+
|
| 124 |
+
@classmethod
|
| 125 |
+
def register_slot(cls, name, default=None):
|
| 126 |
+
with cls._lock:
|
| 127 |
+
if name in cls._slots:
|
| 128 |
+
raise ValueError('slot {} already registered'.format(name))
|
| 129 |
+
slot = cls.Slot(name, default)
|
| 130 |
+
cls._slots[name] = slot
|
| 131 |
+
return slot
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
class _AsyncRuntimeContext(_RuntimeContext):
|
| 135 |
+
_lock = threading.Lock()
|
| 136 |
+
_slots = {}
|
| 137 |
+
|
| 138 |
+
class Slot(object):
|
| 139 |
+
def __init__(self, name, default):
|
| 140 |
+
self.name = name
|
| 141 |
+
self.contextvar = contextvars.ContextVar(name)
|
| 142 |
+
self.default = default if callable(default) else (lambda: default)
|
| 143 |
+
|
| 144 |
+
def clear(self):
|
| 145 |
+
self.contextvar.set(self.default())
|
| 146 |
+
|
| 147 |
+
def get(self):
|
| 148 |
+
try:
|
| 149 |
+
return self.contextvar.get()
|
| 150 |
+
except LookupError:
|
| 151 |
+
value = self.default()
|
| 152 |
+
self.set(value)
|
| 153 |
+
return value
|
| 154 |
+
|
| 155 |
+
def set(self, value):
|
| 156 |
+
self.contextvar.set(value)
|
| 157 |
+
|
| 158 |
+
@classmethod
|
| 159 |
+
def clear(cls):
|
| 160 |
+
with cls._lock:
|
| 161 |
+
for name in cls._slots:
|
| 162 |
+
slot = cls._slots[name]
|
| 163 |
+
slot.clear()
|
| 164 |
+
|
| 165 |
+
@classmethod
|
| 166 |
+
def register_slot(cls, name, default=None):
|
| 167 |
+
with cls._lock:
|
| 168 |
+
if name in cls._slots:
|
| 169 |
+
raise ValueError('slot {} already registered'.format(name))
|
| 170 |
+
slot = cls.Slot(name, default)
|
| 171 |
+
cls._slots[name] = slot
|
| 172 |
+
return slot
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
RuntimeContext = _ThreadLocalRuntimeContext()
|
| 176 |
+
if contextvars:
|
| 177 |
+
RuntimeContext = _AsyncRuntimeContext()
|
vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (6.21 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/__pycache__/version.cpython-310.pyc
ADDED
|
Binary file (203 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/runtime_context/version.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
__version__ = '0.1.3'
|
vllm/lib/python3.10/site-packages/opencensus/common/schedule/__init__.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from six.moves import queue
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
import threading
|
| 19 |
+
import time
|
| 20 |
+
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class PeriodicTask(threading.Thread):
|
| 25 |
+
"""Thread that periodically calls a given function.
|
| 26 |
+
|
| 27 |
+
:type interval: int or float
|
| 28 |
+
:param interval: Seconds between calls to the function.
|
| 29 |
+
|
| 30 |
+
:type function: function
|
| 31 |
+
:param function: The function to call.
|
| 32 |
+
|
| 33 |
+
:type args: list
|
| 34 |
+
:param args: The args passed in while calling `function`.
|
| 35 |
+
|
| 36 |
+
:type kwargs: dict
|
| 37 |
+
:param kwargs: The kwargs passed in while calling `function`.
|
| 38 |
+
|
| 39 |
+
:type name: str
|
| 40 |
+
:param name: The source of the worker. Used for naming.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def __init__(self, interval, function, args=None, kwargs=None, name=None):
|
| 44 |
+
super(PeriodicTask, self).__init__(name=name)
|
| 45 |
+
self.interval = interval
|
| 46 |
+
self.function = function
|
| 47 |
+
self.args = args or []
|
| 48 |
+
self.kwargs = kwargs or {}
|
| 49 |
+
self.finished = threading.Event()
|
| 50 |
+
|
| 51 |
+
def run(self):
|
| 52 |
+
wait_time = self.interval
|
| 53 |
+
while not self.finished.wait(wait_time):
|
| 54 |
+
start_time = time.time()
|
| 55 |
+
self.function(*self.args, **self.kwargs)
|
| 56 |
+
elapsed_time = time.time() - start_time
|
| 57 |
+
wait_time = max(self.interval - elapsed_time, 0)
|
| 58 |
+
|
| 59 |
+
def cancel(self):
|
| 60 |
+
self.finished.set()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class QueueEvent(object):
|
| 64 |
+
def __init__(self, name):
|
| 65 |
+
self.name = name
|
| 66 |
+
self.event = threading.Event()
|
| 67 |
+
|
| 68 |
+
def __repr__(self):
|
| 69 |
+
return ('{}({})'.format(type(self).__name__, self.name))
|
| 70 |
+
|
| 71 |
+
def set(self):
|
| 72 |
+
return self.event.set()
|
| 73 |
+
|
| 74 |
+
def wait(self, timeout=None):
|
| 75 |
+
return self.event.wait(timeout)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class QueueExitEvent(QueueEvent):
|
| 79 |
+
pass
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class Queue(object):
|
| 83 |
+
def __init__(self, capacity):
|
| 84 |
+
self.EXIT_EVENT = QueueExitEvent('EXIT')
|
| 85 |
+
self._queue = queue.Queue(maxsize=capacity)
|
| 86 |
+
|
| 87 |
+
def _gets(self, count, timeout):
|
| 88 |
+
start_time = time.time()
|
| 89 |
+
elapsed_time = 0
|
| 90 |
+
cnt = 0
|
| 91 |
+
while cnt < count:
|
| 92 |
+
try:
|
| 93 |
+
item = self._queue.get(block=False)
|
| 94 |
+
yield item
|
| 95 |
+
if isinstance(item, QueueEvent):
|
| 96 |
+
return
|
| 97 |
+
except queue.Empty:
|
| 98 |
+
break
|
| 99 |
+
cnt += 1
|
| 100 |
+
while cnt < count:
|
| 101 |
+
wait_time = max(timeout - elapsed_time, 0)
|
| 102 |
+
try:
|
| 103 |
+
item = self._queue.get(block=True, timeout=wait_time)
|
| 104 |
+
yield item
|
| 105 |
+
if isinstance(item, QueueEvent):
|
| 106 |
+
return
|
| 107 |
+
except queue.Empty:
|
| 108 |
+
break
|
| 109 |
+
cnt += 1
|
| 110 |
+
elapsed_time = time.time() - start_time
|
| 111 |
+
|
| 112 |
+
def gets(self, count, timeout):
|
| 113 |
+
return tuple(self._gets(count, timeout))
|
| 114 |
+
|
| 115 |
+
def is_empty(self):
|
| 116 |
+
return not self._queue.qsize()
|
| 117 |
+
|
| 118 |
+
def flush(self, timeout=None):
|
| 119 |
+
if self._queue.qsize() == 0:
|
| 120 |
+
return 0
|
| 121 |
+
start_time = time.time()
|
| 122 |
+
wait_time = timeout
|
| 123 |
+
event = QueueEvent('SYNC(timeout={})'.format(wait_time))
|
| 124 |
+
try:
|
| 125 |
+
self._queue.put(event, block=True, timeout=wait_time)
|
| 126 |
+
except queue.Full:
|
| 127 |
+
return
|
| 128 |
+
elapsed_time = time.time() - start_time
|
| 129 |
+
wait_time = timeout and max(timeout - elapsed_time, 0)
|
| 130 |
+
if event.wait(wait_time):
|
| 131 |
+
return time.time() - start_time # time taken to flush
|
| 132 |
+
|
| 133 |
+
def put(self, item, block=True, timeout=None):
|
| 134 |
+
try:
|
| 135 |
+
self._queue.put(item, block, timeout)
|
| 136 |
+
except queue.Full:
|
| 137 |
+
logger.warning('Queue is full. Dropping telemetry.')
|
| 138 |
+
|
| 139 |
+
def puts(self, items, block=True, timeout=None):
|
| 140 |
+
if block and timeout is not None:
|
| 141 |
+
start_time = time.time()
|
| 142 |
+
elapsed_time = 0
|
| 143 |
+
for item in items:
|
| 144 |
+
wait_time = max(timeout - elapsed_time, 0)
|
| 145 |
+
self.put(item, block=True, timeout=wait_time)
|
| 146 |
+
elapsed_time = time.time() - start_time
|
| 147 |
+
else:
|
| 148 |
+
for item in items:
|
| 149 |
+
self.put(item, block, timeout)
|
vllm/lib/python3.10/site-packages/opencensus/common/schedule/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (4.67 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (178 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/async_.cpython-310.pyc
ADDED
|
Binary file (6.83 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/base.cpython-310.pyc
ADDED
|
Binary file (870 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/__pycache__/sync.cpython-310.pyc
ADDED
|
Binary file (802 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/async_.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from six.moves import queue, range
|
| 16 |
+
|
| 17 |
+
import atexit
|
| 18 |
+
import logging
|
| 19 |
+
import threading
|
| 20 |
+
|
| 21 |
+
from opencensus.common.transports import base
|
| 22 |
+
from opencensus.trace import execution_context
|
| 23 |
+
|
| 24 |
+
_DEFAULT_GRACE_PERIOD = 5.0 # Seconds
|
| 25 |
+
_DEFAULT_MAX_BATCH_SIZE = 600
|
| 26 |
+
_DEFAULT_WAIT_PERIOD = 60.0 # Seconds
|
| 27 |
+
_WORKER_THREAD_NAME = 'opencensus.common.Worker'
|
| 28 |
+
_WORKER_TERMINATOR = object()
|
| 29 |
+
|
| 30 |
+
logger = logging.getLogger(__name__)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class _Worker(object):
|
| 34 |
+
"""A background thread that exports batches of data.
|
| 35 |
+
|
| 36 |
+
:type exporter: :class:`~opencensus.trace.base_exporter.Exporter` or
|
| 37 |
+
:class:`~opencensus.stats.base_exporter.StatsExporter`
|
| 38 |
+
:param exporter: Instance of Exporter object.
|
| 39 |
+
|
| 40 |
+
:type grace_period: float
|
| 41 |
+
:param grace_period: The amount of time to wait for pending data to
|
| 42 |
+
be submitted when the process is shutting down.
|
| 43 |
+
|
| 44 |
+
:type max_batch_size: int
|
| 45 |
+
:param max_batch_size: The maximum number of items to send at a time
|
| 46 |
+
in the background thread.
|
| 47 |
+
|
| 48 |
+
:type wait_period: int
|
| 49 |
+
:param wait_period: The amount of time to wait before sending the next
|
| 50 |
+
batch of data.
|
| 51 |
+
"""
|
| 52 |
+
def __init__(self, exporter,
|
| 53 |
+
grace_period=_DEFAULT_GRACE_PERIOD,
|
| 54 |
+
max_batch_size=_DEFAULT_MAX_BATCH_SIZE,
|
| 55 |
+
wait_period=_DEFAULT_WAIT_PERIOD):
|
| 56 |
+
self.exporter = exporter
|
| 57 |
+
self._grace_period = grace_period
|
| 58 |
+
self._max_batch_size = max_batch_size
|
| 59 |
+
self._wait_period = wait_period
|
| 60 |
+
self._queue = queue.Queue(0)
|
| 61 |
+
self._lock = threading.Lock()
|
| 62 |
+
self._event = threading.Event()
|
| 63 |
+
self._thread = None
|
| 64 |
+
|
| 65 |
+
@property
|
| 66 |
+
def is_alive(self):
|
| 67 |
+
"""Returns True is the background thread is running."""
|
| 68 |
+
return self._thread is not None and self._thread.is_alive()
|
| 69 |
+
|
| 70 |
+
def _get_items(self):
|
| 71 |
+
"""Get multiple items from a Queue.
|
| 72 |
+
|
| 73 |
+
Gets at least one (blocking) and at most ``max_batch_size`` items
|
| 74 |
+
(non-blocking) from a given Queue. Does not mark the items as done.
|
| 75 |
+
|
| 76 |
+
:rtype: Sequence
|
| 77 |
+
:returns: A sequence of items retrieved from the queue.
|
| 78 |
+
"""
|
| 79 |
+
items = [self._queue.get()]
|
| 80 |
+
|
| 81 |
+
while len(items) < self._max_batch_size:
|
| 82 |
+
try:
|
| 83 |
+
items.append(self._queue.get_nowait())
|
| 84 |
+
except queue.Empty:
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
return items
|
| 88 |
+
|
| 89 |
+
def _thread_main(self):
|
| 90 |
+
"""The entry point for the worker thread.
|
| 91 |
+
|
| 92 |
+
Pulls pending data off the queue and writes them in
|
| 93 |
+
batches to the specified tracing backend using the exporter.
|
| 94 |
+
"""
|
| 95 |
+
# Indicate that this thread is an exporter thread.
|
| 96 |
+
# Used to suppress tracking of requests in this thread
|
| 97 |
+
execution_context.set_is_exporter(True)
|
| 98 |
+
quit_ = False
|
| 99 |
+
|
| 100 |
+
while True:
|
| 101 |
+
items = self._get_items()
|
| 102 |
+
data = []
|
| 103 |
+
|
| 104 |
+
for item in items:
|
| 105 |
+
if item is _WORKER_TERMINATOR:
|
| 106 |
+
quit_ = True
|
| 107 |
+
# Continue processing items, don't break, try to process
|
| 108 |
+
# all items we got back before quitting.
|
| 109 |
+
else:
|
| 110 |
+
data.extend(item)
|
| 111 |
+
|
| 112 |
+
if data:
|
| 113 |
+
try:
|
| 114 |
+
self.exporter.emit(data)
|
| 115 |
+
except Exception:
|
| 116 |
+
logger.exception(
|
| 117 |
+
'%s failed to emit data.'
|
| 118 |
+
'Dropping %s objects from queue.',
|
| 119 |
+
self.exporter.__class__.__name__,
|
| 120 |
+
len(data))
|
| 121 |
+
pass
|
| 122 |
+
|
| 123 |
+
for _ in range(len(items)):
|
| 124 |
+
self._queue.task_done()
|
| 125 |
+
|
| 126 |
+
# self._event is set at exit, at which point we start draining the
|
| 127 |
+
# queue immediately. If self._event is unset, block for
|
| 128 |
+
# self.wait_period between each batch of exports.
|
| 129 |
+
self._event.wait(self._wait_period)
|
| 130 |
+
|
| 131 |
+
if quit_:
|
| 132 |
+
break
|
| 133 |
+
|
| 134 |
+
def start(self):
|
| 135 |
+
"""Starts the background thread.
|
| 136 |
+
|
| 137 |
+
Additionally, this registers a handler for process exit to attempt
|
| 138 |
+
to send any pending data before shutdown.
|
| 139 |
+
"""
|
| 140 |
+
with self._lock:
|
| 141 |
+
if self.is_alive:
|
| 142 |
+
return
|
| 143 |
+
|
| 144 |
+
self._thread = threading.Thread(
|
| 145 |
+
target=self._thread_main, name=_WORKER_THREAD_NAME)
|
| 146 |
+
self._thread.daemon = True
|
| 147 |
+
self._thread.start()
|
| 148 |
+
atexit.register(self._export_pending_data)
|
| 149 |
+
|
| 150 |
+
def stop(self):
|
| 151 |
+
"""Signals the background thread to stop.
|
| 152 |
+
|
| 153 |
+
This does not terminate the background thread. It simply queues the
|
| 154 |
+
stop signal. If the main process exits before the background thread
|
| 155 |
+
processes the stop signal, it will be terminated without finishing
|
| 156 |
+
work. The ``grace_period`` parameter will give the background
|
| 157 |
+
thread some time to finish processing before this function returns.
|
| 158 |
+
|
| 159 |
+
:rtype: bool
|
| 160 |
+
:returns: True if the thread terminated. False if the thread is still
|
| 161 |
+
running.
|
| 162 |
+
"""
|
| 163 |
+
if not self.is_alive:
|
| 164 |
+
return True
|
| 165 |
+
|
| 166 |
+
with self._lock:
|
| 167 |
+
self._queue.put_nowait(_WORKER_TERMINATOR)
|
| 168 |
+
self._thread.join(timeout=self._grace_period)
|
| 169 |
+
|
| 170 |
+
success = not self.is_alive
|
| 171 |
+
self._thread = None
|
| 172 |
+
|
| 173 |
+
return success
|
| 174 |
+
|
| 175 |
+
def _export_pending_data(self):
|
| 176 |
+
"""Callback that attempts to send pending data before termination."""
|
| 177 |
+
if not self.is_alive:
|
| 178 |
+
return
|
| 179 |
+
# Stop blocking between export batches
|
| 180 |
+
self._event.set()
|
| 181 |
+
self.stop()
|
| 182 |
+
|
| 183 |
+
def enqueue(self, data):
|
| 184 |
+
"""Queues data to be written by the background thread."""
|
| 185 |
+
self._queue.put_nowait(data)
|
| 186 |
+
|
| 187 |
+
def flush(self):
|
| 188 |
+
"""Submit any pending data."""
|
| 189 |
+
self._queue.join()
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
class AsyncTransport(base.Transport):
|
| 193 |
+
"""Asynchronous transport that uses a background thread.
|
| 194 |
+
|
| 195 |
+
:type exporter: :class:`~opencensus.trace.base_exporter.Exporter` or
|
| 196 |
+
:class:`~opencensus.stats.base_exporter.StatsExporter`
|
| 197 |
+
:param exporter: Instance of Exporter object.
|
| 198 |
+
|
| 199 |
+
:type grace_period: float
|
| 200 |
+
:param grace_period: The amount of time to wait for pending data to
|
| 201 |
+
be submitted when the process is shutting down.
|
| 202 |
+
|
| 203 |
+
:type max_batch_size: int
|
| 204 |
+
:param max_batch_size: The maximum number of items to send at a time
|
| 205 |
+
in the background thread.
|
| 206 |
+
|
| 207 |
+
:type wait_period: int
|
| 208 |
+
:param wait_period: The amount of time to wait before sending the next
|
| 209 |
+
batch of data.
|
| 210 |
+
"""
|
| 211 |
+
|
| 212 |
+
def __init__(self, exporter,
|
| 213 |
+
grace_period=_DEFAULT_GRACE_PERIOD,
|
| 214 |
+
max_batch_size=_DEFAULT_MAX_BATCH_SIZE,
|
| 215 |
+
wait_period=_DEFAULT_WAIT_PERIOD):
|
| 216 |
+
self.exporter = exporter
|
| 217 |
+
self.worker = _Worker(
|
| 218 |
+
exporter,
|
| 219 |
+
grace_period,
|
| 220 |
+
max_batch_size,
|
| 221 |
+
wait_period,
|
| 222 |
+
)
|
| 223 |
+
self.worker.start()
|
| 224 |
+
|
| 225 |
+
def export(self, data):
|
| 226 |
+
"""Put the trace/stats to be exported into queue."""
|
| 227 |
+
self.worker.enqueue(data)
|
| 228 |
+
|
| 229 |
+
def flush(self):
|
| 230 |
+
"""Submit any pending traces/stats."""
|
| 231 |
+
self.worker.flush()
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/base.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""Module containing base class for transport."""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Transport(object):
|
| 19 |
+
"""Base class for transport.
|
| 20 |
+
|
| 21 |
+
Subclasses of :class:`Transport` must override :meth:`export`.
|
| 22 |
+
"""
|
| 23 |
+
def export(self, datas):
|
| 24 |
+
"""Export the data."""
|
| 25 |
+
raise NotImplementedError
|
| 26 |
+
|
| 27 |
+
def flush(self):
|
| 28 |
+
"""Submit any pending data.
|
| 29 |
+
|
| 30 |
+
For blocking/sync transports, this is a no-op.
|
| 31 |
+
"""
|
vllm/lib/python3.10/site-packages/opencensus/common/transports/sync.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2018, OpenCensus Authors
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from opencensus.common.transports import base
|
| 16 |
+
from opencensus.trace import execution_context
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class SyncTransport(base.Transport):
|
| 20 |
+
def __init__(self, exporter):
|
| 21 |
+
self.exporter = exporter
|
| 22 |
+
|
| 23 |
+
def export(self, datas):
|
| 24 |
+
# Used to suppress tracking of requests in export
|
| 25 |
+
execution_context.set_is_exporter(True)
|
| 26 |
+
self.exporter.emit(datas)
|
| 27 |
+
# Reset the context
|
| 28 |
+
execution_context.set_is_exporter(False)
|