Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- vllm/lib/python3.10/site-packages/mpmath/calculus/__init__.py +6 -0
- vllm/lib/python3.10/site-packages/mpmath/calculus/differentiation.py +647 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/elliptic.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/factorials.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/functions.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/orthogonal.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/qfunctions.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/rszeta.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/theta.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/zeta.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/zetazeros.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/functions/functions.py +645 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__init__.py +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/extratest_zeta.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/runtests.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_compatibility.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_convert.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_division.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_eigen_symmetric.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_elliptic.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_fp.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_functions.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_gammazeta.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_hp.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_identify.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_interval.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_levin.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_linalg.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_matrices.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_mpmath.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_pickle.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_power.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_quad.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_rootfinding.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_str.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_summation.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_visualization.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/torture.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/extratest_gamma.py +215 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/extratest_zeta.py +30 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/runtests.py +161 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_basic_ops.py +451 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_bitwise.py +188 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_calculus.py +216 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_compatibility.py +77 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_convert.py +233 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_diff.py +61 -0
- vllm/lib/python3.10/site-packages/mpmath/tests/test_division.py +143 -0
vllm/lib/python3.10/site-packages/mpmath/calculus/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from . import calculus
|
| 2 |
+
# XXX: hack to set methods
|
| 3 |
+
from . import approximation
|
| 4 |
+
from . import differentiation
|
| 5 |
+
from . import extrapolation
|
| 6 |
+
from . import polynomials
|
vllm/lib/python3.10/site-packages/mpmath/calculus/differentiation.py
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ..libmp.backend import xrange
|
| 2 |
+
from .calculus import defun
|
| 3 |
+
|
| 4 |
+
try:
|
| 5 |
+
iteritems = dict.iteritems
|
| 6 |
+
except AttributeError:
|
| 7 |
+
iteritems = dict.items
|
| 8 |
+
|
| 9 |
+
#----------------------------------------------------------------------------#
|
| 10 |
+
# Differentiation #
|
| 11 |
+
#----------------------------------------------------------------------------#
|
| 12 |
+
|
| 13 |
+
@defun
|
| 14 |
+
def difference(ctx, s, n):
|
| 15 |
+
r"""
|
| 16 |
+
Given a sequence `(s_k)` containing at least `n+1` items, returns the
|
| 17 |
+
`n`-th forward difference,
|
| 18 |
+
|
| 19 |
+
.. math ::
|
| 20 |
+
|
| 21 |
+
\Delta^n = \sum_{k=0}^{\infty} (-1)^{k+n} {n \choose k} s_k.
|
| 22 |
+
"""
|
| 23 |
+
n = int(n)
|
| 24 |
+
d = ctx.zero
|
| 25 |
+
b = (-1) ** (n & 1)
|
| 26 |
+
for k in xrange(n+1):
|
| 27 |
+
d += b * s[k]
|
| 28 |
+
b = (b * (k-n)) // (k+1)
|
| 29 |
+
return d
|
| 30 |
+
|
| 31 |
+
def hsteps(ctx, f, x, n, prec, **options):
|
| 32 |
+
singular = options.get('singular')
|
| 33 |
+
addprec = options.get('addprec', 10)
|
| 34 |
+
direction = options.get('direction', 0)
|
| 35 |
+
workprec = (prec+2*addprec) * (n+1)
|
| 36 |
+
orig = ctx.prec
|
| 37 |
+
try:
|
| 38 |
+
ctx.prec = workprec
|
| 39 |
+
h = options.get('h')
|
| 40 |
+
if h is None:
|
| 41 |
+
if options.get('relative'):
|
| 42 |
+
hextramag = int(ctx.mag(x))
|
| 43 |
+
else:
|
| 44 |
+
hextramag = 0
|
| 45 |
+
h = ctx.ldexp(1, -prec-addprec-hextramag)
|
| 46 |
+
else:
|
| 47 |
+
h = ctx.convert(h)
|
| 48 |
+
# Directed: steps x, x+h, ... x+n*h
|
| 49 |
+
direction = options.get('direction', 0)
|
| 50 |
+
if direction:
|
| 51 |
+
h *= ctx.sign(direction)
|
| 52 |
+
steps = xrange(n+1)
|
| 53 |
+
norm = h
|
| 54 |
+
# Central: steps x-n*h, x-(n-2)*h ..., x, ..., x+(n-2)*h, x+n*h
|
| 55 |
+
else:
|
| 56 |
+
steps = xrange(-n, n+1, 2)
|
| 57 |
+
norm = (2*h)
|
| 58 |
+
# Perturb
|
| 59 |
+
if singular:
|
| 60 |
+
x += 0.5*h
|
| 61 |
+
values = [f(x+k*h) for k in steps]
|
| 62 |
+
return values, norm, workprec
|
| 63 |
+
finally:
|
| 64 |
+
ctx.prec = orig
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@defun
|
| 68 |
+
def diff(ctx, f, x, n=1, **options):
|
| 69 |
+
r"""
|
| 70 |
+
Numerically computes the derivative of `f`, `f'(x)`, or generally for
|
| 71 |
+
an integer `n \ge 0`, the `n`-th derivative `f^{(n)}(x)`.
|
| 72 |
+
A few basic examples are::
|
| 73 |
+
|
| 74 |
+
>>> from mpmath import *
|
| 75 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 76 |
+
>>> diff(lambda x: x**2 + x, 1.0)
|
| 77 |
+
3.0
|
| 78 |
+
>>> diff(lambda x: x**2 + x, 1.0, 2)
|
| 79 |
+
2.0
|
| 80 |
+
>>> diff(lambda x: x**2 + x, 1.0, 3)
|
| 81 |
+
0.0
|
| 82 |
+
>>> nprint([diff(exp, 3, n) for n in range(5)]) # exp'(x) = exp(x)
|
| 83 |
+
[20.0855, 20.0855, 20.0855, 20.0855, 20.0855]
|
| 84 |
+
|
| 85 |
+
Even more generally, given a tuple of arguments `(x_1, \ldots, x_k)`
|
| 86 |
+
and order `(n_1, \ldots, n_k)`, the partial derivative
|
| 87 |
+
`f^{(n_1,\ldots,n_k)}(x_1,\ldots,x_k)` is evaluated. For example::
|
| 88 |
+
|
| 89 |
+
>>> diff(lambda x,y: 3*x*y + 2*y - x, (0.25, 0.5), (0,1))
|
| 90 |
+
2.75
|
| 91 |
+
>>> diff(lambda x,y: 3*x*y + 2*y - x, (0.25, 0.5), (1,1))
|
| 92 |
+
3.0
|
| 93 |
+
|
| 94 |
+
**Options**
|
| 95 |
+
|
| 96 |
+
The following optional keyword arguments are recognized:
|
| 97 |
+
|
| 98 |
+
``method``
|
| 99 |
+
Supported methods are ``'step'`` or ``'quad'``: derivatives may be
|
| 100 |
+
computed using either a finite difference with a small step
|
| 101 |
+
size `h` (default), or numerical quadrature.
|
| 102 |
+
``direction``
|
| 103 |
+
Direction of finite difference: can be -1 for a left
|
| 104 |
+
difference, 0 for a central difference (default), or +1
|
| 105 |
+
for a right difference; more generally can be any complex number.
|
| 106 |
+
``addprec``
|
| 107 |
+
Extra precision for `h` used to account for the function's
|
| 108 |
+
sensitivity to perturbations (default = 10).
|
| 109 |
+
``relative``
|
| 110 |
+
Choose `h` relative to the magnitude of `x`, rather than an
|
| 111 |
+
absolute value; useful for large or tiny `x` (default = False).
|
| 112 |
+
``h``
|
| 113 |
+
As an alternative to ``addprec`` and ``relative``, manually
|
| 114 |
+
select the step size `h`.
|
| 115 |
+
``singular``
|
| 116 |
+
If True, evaluation exactly at the point `x` is avoided; this is
|
| 117 |
+
useful for differentiating functions with removable singularities.
|
| 118 |
+
Default = False.
|
| 119 |
+
``radius``
|
| 120 |
+
Radius of integration contour (with ``method = 'quad'``).
|
| 121 |
+
Default = 0.25. A larger radius typically is faster and more
|
| 122 |
+
accurate, but it must be chosen so that `f` has no
|
| 123 |
+
singularities within the radius from the evaluation point.
|
| 124 |
+
|
| 125 |
+
A finite difference requires `n+1` function evaluations and must be
|
| 126 |
+
performed at `(n+1)` times the target precision. Accordingly, `f` must
|
| 127 |
+
support fast evaluation at high precision.
|
| 128 |
+
|
| 129 |
+
With integration, a larger number of function evaluations is
|
| 130 |
+
required, but not much extra precision is required. For high order
|
| 131 |
+
derivatives, this method may thus be faster if f is very expensive to
|
| 132 |
+
evaluate at high precision.
|
| 133 |
+
|
| 134 |
+
**Further examples**
|
| 135 |
+
|
| 136 |
+
The direction option is useful for computing left- or right-sided
|
| 137 |
+
derivatives of nonsmooth functions::
|
| 138 |
+
|
| 139 |
+
>>> diff(abs, 0, direction=0)
|
| 140 |
+
0.0
|
| 141 |
+
>>> diff(abs, 0, direction=1)
|
| 142 |
+
1.0
|
| 143 |
+
>>> diff(abs, 0, direction=-1)
|
| 144 |
+
-1.0
|
| 145 |
+
|
| 146 |
+
More generally, if the direction is nonzero, a right difference
|
| 147 |
+
is computed where the step size is multiplied by sign(direction).
|
| 148 |
+
For example, with direction=+j, the derivative from the positive
|
| 149 |
+
imaginary direction will be computed::
|
| 150 |
+
|
| 151 |
+
>>> diff(abs, 0, direction=j)
|
| 152 |
+
(0.0 - 1.0j)
|
| 153 |
+
|
| 154 |
+
With integration, the result may have a small imaginary part
|
| 155 |
+
even even if the result is purely real::
|
| 156 |
+
|
| 157 |
+
>>> diff(sqrt, 1, method='quad') # doctest:+ELLIPSIS
|
| 158 |
+
(0.5 - 4.59...e-26j)
|
| 159 |
+
>>> chop(_)
|
| 160 |
+
0.5
|
| 161 |
+
|
| 162 |
+
Adding precision to obtain an accurate value::
|
| 163 |
+
|
| 164 |
+
>>> diff(cos, 1e-30)
|
| 165 |
+
0.0
|
| 166 |
+
>>> diff(cos, 1e-30, h=0.0001)
|
| 167 |
+
-9.99999998328279e-31
|
| 168 |
+
>>> diff(cos, 1e-30, addprec=100)
|
| 169 |
+
-1.0e-30
|
| 170 |
+
|
| 171 |
+
"""
|
| 172 |
+
partial = False
|
| 173 |
+
try:
|
| 174 |
+
orders = list(n)
|
| 175 |
+
x = list(x)
|
| 176 |
+
partial = True
|
| 177 |
+
except TypeError:
|
| 178 |
+
pass
|
| 179 |
+
if partial:
|
| 180 |
+
x = [ctx.convert(_) for _ in x]
|
| 181 |
+
return _partial_diff(ctx, f, x, orders, options)
|
| 182 |
+
method = options.get('method', 'step')
|
| 183 |
+
if n == 0 and method != 'quad' and not options.get('singular'):
|
| 184 |
+
return f(ctx.convert(x))
|
| 185 |
+
prec = ctx.prec
|
| 186 |
+
try:
|
| 187 |
+
if method == 'step':
|
| 188 |
+
values, norm, workprec = hsteps(ctx, f, x, n, prec, **options)
|
| 189 |
+
ctx.prec = workprec
|
| 190 |
+
v = ctx.difference(values, n) / norm**n
|
| 191 |
+
elif method == 'quad':
|
| 192 |
+
ctx.prec += 10
|
| 193 |
+
radius = ctx.convert(options.get('radius', 0.25))
|
| 194 |
+
def g(t):
|
| 195 |
+
rei = radius*ctx.expj(t)
|
| 196 |
+
z = x + rei
|
| 197 |
+
return f(z) / rei**n
|
| 198 |
+
d = ctx.quadts(g, [0, 2*ctx.pi])
|
| 199 |
+
v = d * ctx.factorial(n) / (2*ctx.pi)
|
| 200 |
+
else:
|
| 201 |
+
raise ValueError("unknown method: %r" % method)
|
| 202 |
+
finally:
|
| 203 |
+
ctx.prec = prec
|
| 204 |
+
return +v
|
| 205 |
+
|
| 206 |
+
def _partial_diff(ctx, f, xs, orders, options):
|
| 207 |
+
if not orders:
|
| 208 |
+
return f()
|
| 209 |
+
if not sum(orders):
|
| 210 |
+
return f(*xs)
|
| 211 |
+
i = 0
|
| 212 |
+
for i in range(len(orders)):
|
| 213 |
+
if orders[i]:
|
| 214 |
+
break
|
| 215 |
+
order = orders[i]
|
| 216 |
+
def fdiff_inner(*f_args):
|
| 217 |
+
def inner(t):
|
| 218 |
+
return f(*(f_args[:i] + (t,) + f_args[i+1:]))
|
| 219 |
+
return ctx.diff(inner, f_args[i], order, **options)
|
| 220 |
+
orders[i] = 0
|
| 221 |
+
return _partial_diff(ctx, fdiff_inner, xs, orders, options)
|
| 222 |
+
|
| 223 |
+
@defun
|
| 224 |
+
def diffs(ctx, f, x, n=None, **options):
|
| 225 |
+
r"""
|
| 226 |
+
Returns a generator that yields the sequence of derivatives
|
| 227 |
+
|
| 228 |
+
.. math ::
|
| 229 |
+
|
| 230 |
+
f(x), f'(x), f''(x), \ldots, f^{(k)}(x), \ldots
|
| 231 |
+
|
| 232 |
+
With ``method='step'``, :func:`~mpmath.diffs` uses only `O(k)`
|
| 233 |
+
function evaluations to generate the first `k` derivatives,
|
| 234 |
+
rather than the roughly `O(k^2)` evaluations
|
| 235 |
+
required if one calls :func:`~mpmath.diff` `k` separate times.
|
| 236 |
+
|
| 237 |
+
With `n < \infty`, the generator stops as soon as the
|
| 238 |
+
`n`-th derivative has been generated. If the exact number of
|
| 239 |
+
needed derivatives is known in advance, this is further
|
| 240 |
+
slightly more efficient.
|
| 241 |
+
|
| 242 |
+
Options are the same as for :func:`~mpmath.diff`.
|
| 243 |
+
|
| 244 |
+
**Examples**
|
| 245 |
+
|
| 246 |
+
>>> from mpmath import *
|
| 247 |
+
>>> mp.dps = 15
|
| 248 |
+
>>> nprint(list(diffs(cos, 1, 5)))
|
| 249 |
+
[0.540302, -0.841471, -0.540302, 0.841471, 0.540302, -0.841471]
|
| 250 |
+
>>> for i, d in zip(range(6), diffs(cos, 1)):
|
| 251 |
+
... print("%s %s" % (i, d))
|
| 252 |
+
...
|
| 253 |
+
0 0.54030230586814
|
| 254 |
+
1 -0.841470984807897
|
| 255 |
+
2 -0.54030230586814
|
| 256 |
+
3 0.841470984807897
|
| 257 |
+
4 0.54030230586814
|
| 258 |
+
5 -0.841470984807897
|
| 259 |
+
|
| 260 |
+
"""
|
| 261 |
+
if n is None:
|
| 262 |
+
n = ctx.inf
|
| 263 |
+
else:
|
| 264 |
+
n = int(n)
|
| 265 |
+
if options.get('method', 'step') != 'step':
|
| 266 |
+
k = 0
|
| 267 |
+
while k < n + 1:
|
| 268 |
+
yield ctx.diff(f, x, k, **options)
|
| 269 |
+
k += 1
|
| 270 |
+
return
|
| 271 |
+
singular = options.get('singular')
|
| 272 |
+
if singular:
|
| 273 |
+
yield ctx.diff(f, x, 0, singular=True)
|
| 274 |
+
else:
|
| 275 |
+
yield f(ctx.convert(x))
|
| 276 |
+
if n < 1:
|
| 277 |
+
return
|
| 278 |
+
if n == ctx.inf:
|
| 279 |
+
A, B = 1, 2
|
| 280 |
+
else:
|
| 281 |
+
A, B = 1, n+1
|
| 282 |
+
while 1:
|
| 283 |
+
callprec = ctx.prec
|
| 284 |
+
y, norm, workprec = hsteps(ctx, f, x, B, callprec, **options)
|
| 285 |
+
for k in xrange(A, B):
|
| 286 |
+
try:
|
| 287 |
+
ctx.prec = workprec
|
| 288 |
+
d = ctx.difference(y, k) / norm**k
|
| 289 |
+
finally:
|
| 290 |
+
ctx.prec = callprec
|
| 291 |
+
yield +d
|
| 292 |
+
if k >= n:
|
| 293 |
+
return
|
| 294 |
+
A, B = B, int(A*1.4+1)
|
| 295 |
+
B = min(B, n)
|
| 296 |
+
|
| 297 |
+
def iterable_to_function(gen):
|
| 298 |
+
gen = iter(gen)
|
| 299 |
+
data = []
|
| 300 |
+
def f(k):
|
| 301 |
+
for i in xrange(len(data), k+1):
|
| 302 |
+
data.append(next(gen))
|
| 303 |
+
return data[k]
|
| 304 |
+
return f
|
| 305 |
+
|
| 306 |
+
@defun
|
| 307 |
+
def diffs_prod(ctx, factors):
|
| 308 |
+
r"""
|
| 309 |
+
Given a list of `N` iterables or generators yielding
|
| 310 |
+
`f_k(x), f'_k(x), f''_k(x), \ldots` for `k = 1, \ldots, N`,
|
| 311 |
+
generate `g(x), g'(x), g''(x), \ldots` where
|
| 312 |
+
`g(x) = f_1(x) f_2(x) \cdots f_N(x)`.
|
| 313 |
+
|
| 314 |
+
At high precision and for large orders, this is typically more efficient
|
| 315 |
+
than numerical differentiation if the derivatives of each `f_k(x)`
|
| 316 |
+
admit direct computation.
|
| 317 |
+
|
| 318 |
+
Note: This function does not increase the working precision internally,
|
| 319 |
+
so guard digits may have to be added externally for full accuracy.
|
| 320 |
+
|
| 321 |
+
**Examples**
|
| 322 |
+
|
| 323 |
+
>>> from mpmath import *
|
| 324 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 325 |
+
>>> f = lambda x: exp(x)*cos(x)*sin(x)
|
| 326 |
+
>>> u = diffs(f, 1)
|
| 327 |
+
>>> v = mp.diffs_prod([diffs(exp,1), diffs(cos,1), diffs(sin,1)])
|
| 328 |
+
>>> next(u); next(v)
|
| 329 |
+
1.23586333600241
|
| 330 |
+
1.23586333600241
|
| 331 |
+
>>> next(u); next(v)
|
| 332 |
+
0.104658952245596
|
| 333 |
+
0.104658952245596
|
| 334 |
+
>>> next(u); next(v)
|
| 335 |
+
-5.96999877552086
|
| 336 |
+
-5.96999877552086
|
| 337 |
+
>>> next(u); next(v)
|
| 338 |
+
-12.4632923122697
|
| 339 |
+
-12.4632923122697
|
| 340 |
+
|
| 341 |
+
"""
|
| 342 |
+
N = len(factors)
|
| 343 |
+
if N == 1:
|
| 344 |
+
for c in factors[0]:
|
| 345 |
+
yield c
|
| 346 |
+
else:
|
| 347 |
+
u = iterable_to_function(ctx.diffs_prod(factors[:N//2]))
|
| 348 |
+
v = iterable_to_function(ctx.diffs_prod(factors[N//2:]))
|
| 349 |
+
n = 0
|
| 350 |
+
while 1:
|
| 351 |
+
#yield sum(binomial(n,k)*u(n-k)*v(k) for k in xrange(n+1))
|
| 352 |
+
s = u(n) * v(0)
|
| 353 |
+
a = 1
|
| 354 |
+
for k in xrange(1,n+1):
|
| 355 |
+
a = a * (n-k+1) // k
|
| 356 |
+
s += a * u(n-k) * v(k)
|
| 357 |
+
yield s
|
| 358 |
+
n += 1
|
| 359 |
+
|
| 360 |
+
def dpoly(n, _cache={}):
|
| 361 |
+
"""
|
| 362 |
+
nth differentiation polynomial for exp (Faa di Bruno's formula).
|
| 363 |
+
|
| 364 |
+
TODO: most exponents are zero, so maybe a sparse representation
|
| 365 |
+
would be better.
|
| 366 |
+
"""
|
| 367 |
+
if n in _cache:
|
| 368 |
+
return _cache[n]
|
| 369 |
+
if not _cache:
|
| 370 |
+
_cache[0] = {(0,):1}
|
| 371 |
+
R = dpoly(n-1)
|
| 372 |
+
R = dict((c+(0,),v) for (c,v) in iteritems(R))
|
| 373 |
+
Ra = {}
|
| 374 |
+
for powers, count in iteritems(R):
|
| 375 |
+
powers1 = (powers[0]+1,) + powers[1:]
|
| 376 |
+
if powers1 in Ra:
|
| 377 |
+
Ra[powers1] += count
|
| 378 |
+
else:
|
| 379 |
+
Ra[powers1] = count
|
| 380 |
+
for powers, count in iteritems(R):
|
| 381 |
+
if not sum(powers):
|
| 382 |
+
continue
|
| 383 |
+
for k,p in enumerate(powers):
|
| 384 |
+
if p:
|
| 385 |
+
powers2 = powers[:k] + (p-1,powers[k+1]+1) + powers[k+2:]
|
| 386 |
+
if powers2 in Ra:
|
| 387 |
+
Ra[powers2] += p*count
|
| 388 |
+
else:
|
| 389 |
+
Ra[powers2] = p*count
|
| 390 |
+
_cache[n] = Ra
|
| 391 |
+
return _cache[n]
|
| 392 |
+
|
| 393 |
+
@defun
|
| 394 |
+
def diffs_exp(ctx, fdiffs):
|
| 395 |
+
r"""
|
| 396 |
+
Given an iterable or generator yielding `f(x), f'(x), f''(x), \ldots`
|
| 397 |
+
generate `g(x), g'(x), g''(x), \ldots` where `g(x) = \exp(f(x))`.
|
| 398 |
+
|
| 399 |
+
At high precision and for large orders, this is typically more efficient
|
| 400 |
+
than numerical differentiation if the derivatives of `f(x)`
|
| 401 |
+
admit direct computation.
|
| 402 |
+
|
| 403 |
+
Note: This function does not increase the working precision internally,
|
| 404 |
+
so guard digits may have to be added externally for full accuracy.
|
| 405 |
+
|
| 406 |
+
**Examples**
|
| 407 |
+
|
| 408 |
+
The derivatives of the gamma function can be computed using
|
| 409 |
+
logarithmic differentiation::
|
| 410 |
+
|
| 411 |
+
>>> from mpmath import *
|
| 412 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 413 |
+
>>>
|
| 414 |
+
>>> def diffs_loggamma(x):
|
| 415 |
+
... yield loggamma(x)
|
| 416 |
+
... i = 0
|
| 417 |
+
... while 1:
|
| 418 |
+
... yield psi(i,x)
|
| 419 |
+
... i += 1
|
| 420 |
+
...
|
| 421 |
+
>>> u = diffs_exp(diffs_loggamma(3))
|
| 422 |
+
>>> v = diffs(gamma, 3)
|
| 423 |
+
>>> next(u); next(v)
|
| 424 |
+
2.0
|
| 425 |
+
2.0
|
| 426 |
+
>>> next(u); next(v)
|
| 427 |
+
1.84556867019693
|
| 428 |
+
1.84556867019693
|
| 429 |
+
>>> next(u); next(v)
|
| 430 |
+
2.49292999190269
|
| 431 |
+
2.49292999190269
|
| 432 |
+
>>> next(u); next(v)
|
| 433 |
+
3.44996501352367
|
| 434 |
+
3.44996501352367
|
| 435 |
+
|
| 436 |
+
"""
|
| 437 |
+
fn = iterable_to_function(fdiffs)
|
| 438 |
+
f0 = ctx.exp(fn(0))
|
| 439 |
+
yield f0
|
| 440 |
+
i = 1
|
| 441 |
+
while 1:
|
| 442 |
+
s = ctx.mpf(0)
|
| 443 |
+
for powers, c in iteritems(dpoly(i)):
|
| 444 |
+
s += c*ctx.fprod(fn(k+1)**p for (k,p) in enumerate(powers) if p)
|
| 445 |
+
yield s * f0
|
| 446 |
+
i += 1
|
| 447 |
+
|
| 448 |
+
@defun
|
| 449 |
+
def differint(ctx, f, x, n=1, x0=0):
|
| 450 |
+
r"""
|
| 451 |
+
Calculates the Riemann-Liouville differintegral, or fractional
|
| 452 |
+
derivative, defined by
|
| 453 |
+
|
| 454 |
+
.. math ::
|
| 455 |
+
|
| 456 |
+
\,_{x_0}{\mathbb{D}}^n_xf(x) = \frac{1}{\Gamma(m-n)} \frac{d^m}{dx^m}
|
| 457 |
+
\int_{x_0}^{x}(x-t)^{m-n-1}f(t)dt
|
| 458 |
+
|
| 459 |
+
where `f` is a given (presumably well-behaved) function,
|
| 460 |
+
`x` is the evaluation point, `n` is the order, and `x_0` is
|
| 461 |
+
the reference point of integration (`m` is an arbitrary
|
| 462 |
+
parameter selected automatically).
|
| 463 |
+
|
| 464 |
+
With `n = 1`, this is just the standard derivative `f'(x)`; with `n = 2`,
|
| 465 |
+
the second derivative `f''(x)`, etc. With `n = -1`, it gives
|
| 466 |
+
`\int_{x_0}^x f(t) dt`, with `n = -2`
|
| 467 |
+
it gives `\int_{x_0}^x \left( \int_{x_0}^t f(u) du \right) dt`, etc.
|
| 468 |
+
|
| 469 |
+
As `n` is permitted to be any number, this operator generalizes
|
| 470 |
+
iterated differentiation and iterated integration to a single
|
| 471 |
+
operator with a continuous order parameter.
|
| 472 |
+
|
| 473 |
+
**Examples**
|
| 474 |
+
|
| 475 |
+
There is an exact formula for the fractional derivative of a
|
| 476 |
+
monomial `x^p`, which may be used as a reference. For example,
|
| 477 |
+
the following gives a half-derivative (order 0.5)::
|
| 478 |
+
|
| 479 |
+
>>> from mpmath import *
|
| 480 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 481 |
+
>>> x = mpf(3); p = 2; n = 0.5
|
| 482 |
+
>>> differint(lambda t: t**p, x, n)
|
| 483 |
+
7.81764019044672
|
| 484 |
+
>>> gamma(p+1)/gamma(p-n+1) * x**(p-n)
|
| 485 |
+
7.81764019044672
|
| 486 |
+
|
| 487 |
+
Another useful test function is the exponential function, whose
|
| 488 |
+
integration / differentiation formula easy generalizes
|
| 489 |
+
to arbitrary order. Here we first compute a third derivative,
|
| 490 |
+
and then a triply nested integral. (The reference point `x_0`
|
| 491 |
+
is set to `-\infty` to avoid nonzero endpoint terms.)::
|
| 492 |
+
|
| 493 |
+
>>> differint(lambda x: exp(pi*x), -1.5, 3)
|
| 494 |
+
0.278538406900792
|
| 495 |
+
>>> exp(pi*-1.5) * pi**3
|
| 496 |
+
0.278538406900792
|
| 497 |
+
>>> differint(lambda x: exp(pi*x), 3.5, -3, -inf)
|
| 498 |
+
1922.50563031149
|
| 499 |
+
>>> exp(pi*3.5) / pi**3
|
| 500 |
+
1922.50563031149
|
| 501 |
+
|
| 502 |
+
However, for noninteger `n`, the differentiation formula for the
|
| 503 |
+
exponential function must be modified to give the same result as the
|
| 504 |
+
Riemann-Liouville differintegral::
|
| 505 |
+
|
| 506 |
+
>>> x = mpf(3.5)
|
| 507 |
+
>>> c = pi
|
| 508 |
+
>>> n = 1+2*j
|
| 509 |
+
>>> differint(lambda x: exp(c*x), x, n)
|
| 510 |
+
(-123295.005390743 + 140955.117867654j)
|
| 511 |
+
>>> x**(-n) * exp(c)**x * (x*c)**n * gammainc(-n, 0, x*c) / gamma(-n)
|
| 512 |
+
(-123295.005390743 + 140955.117867654j)
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
"""
|
| 516 |
+
m = max(int(ctx.ceil(ctx.re(n)))+1, 1)
|
| 517 |
+
r = m-n-1
|
| 518 |
+
g = lambda x: ctx.quad(lambda t: (x-t)**r * f(t), [x0, x])
|
| 519 |
+
return ctx.diff(g, x, m) / ctx.gamma(m-n)
|
| 520 |
+
|
| 521 |
+
@defun
|
| 522 |
+
def diffun(ctx, f, n=1, **options):
|
| 523 |
+
r"""
|
| 524 |
+
Given a function `f`, returns a function `g(x)` that evaluates the nth
|
| 525 |
+
derivative `f^{(n)}(x)`::
|
| 526 |
+
|
| 527 |
+
>>> from mpmath import *
|
| 528 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 529 |
+
>>> cos2 = diffun(sin)
|
| 530 |
+
>>> sin2 = diffun(sin, 4)
|
| 531 |
+
>>> cos(1.3), cos2(1.3)
|
| 532 |
+
(0.267498828624587, 0.267498828624587)
|
| 533 |
+
>>> sin(1.3), sin2(1.3)
|
| 534 |
+
(0.963558185417193, 0.963558185417193)
|
| 535 |
+
|
| 536 |
+
The function `f` must support arbitrary precision evaluation.
|
| 537 |
+
See :func:`~mpmath.diff` for additional details and supported
|
| 538 |
+
keyword options.
|
| 539 |
+
"""
|
| 540 |
+
if n == 0:
|
| 541 |
+
return f
|
| 542 |
+
def g(x):
|
| 543 |
+
return ctx.diff(f, x, n, **options)
|
| 544 |
+
return g
|
| 545 |
+
|
| 546 |
+
@defun
|
| 547 |
+
def taylor(ctx, f, x, n, **options):
|
| 548 |
+
r"""
|
| 549 |
+
Produces a degree-`n` Taylor polynomial around the point `x` of the
|
| 550 |
+
given function `f`. The coefficients are returned as a list.
|
| 551 |
+
|
| 552 |
+
>>> from mpmath import *
|
| 553 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 554 |
+
>>> nprint(chop(taylor(sin, 0, 5)))
|
| 555 |
+
[0.0, 1.0, 0.0, -0.166667, 0.0, 0.00833333]
|
| 556 |
+
|
| 557 |
+
The coefficients are computed using high-order numerical
|
| 558 |
+
differentiation. The function must be possible to evaluate
|
| 559 |
+
to arbitrary precision. See :func:`~mpmath.diff` for additional details
|
| 560 |
+
and supported keyword options.
|
| 561 |
+
|
| 562 |
+
Note that to evaluate the Taylor polynomial as an approximation
|
| 563 |
+
of `f`, e.g. with :func:`~mpmath.polyval`, the coefficients must be reversed,
|
| 564 |
+
and the point of the Taylor expansion must be subtracted from
|
| 565 |
+
the argument:
|
| 566 |
+
|
| 567 |
+
>>> p = taylor(exp, 2.0, 10)
|
| 568 |
+
>>> polyval(p[::-1], 2.5 - 2.0)
|
| 569 |
+
12.1824939606092
|
| 570 |
+
>>> exp(2.5)
|
| 571 |
+
12.1824939607035
|
| 572 |
+
|
| 573 |
+
"""
|
| 574 |
+
gen = enumerate(ctx.diffs(f, x, n, **options))
|
| 575 |
+
if options.get("chop", True):
|
| 576 |
+
return [ctx.chop(d)/ctx.factorial(i) for i, d in gen]
|
| 577 |
+
else:
|
| 578 |
+
return [d/ctx.factorial(i) for i, d in gen]
|
| 579 |
+
|
| 580 |
+
@defun
|
| 581 |
+
def pade(ctx, a, L, M):
|
| 582 |
+
r"""
|
| 583 |
+
Computes a Pade approximation of degree `(L, M)` to a function.
|
| 584 |
+
Given at least `L+M+1` Taylor coefficients `a` approximating
|
| 585 |
+
a function `A(x)`, :func:`~mpmath.pade` returns coefficients of
|
| 586 |
+
polynomials `P, Q` satisfying
|
| 587 |
+
|
| 588 |
+
.. math ::
|
| 589 |
+
|
| 590 |
+
P = \sum_{k=0}^L p_k x^k
|
| 591 |
+
|
| 592 |
+
Q = \sum_{k=0}^M q_k x^k
|
| 593 |
+
|
| 594 |
+
Q_0 = 1
|
| 595 |
+
|
| 596 |
+
A(x) Q(x) = P(x) + O(x^{L+M+1})
|
| 597 |
+
|
| 598 |
+
`P(x)/Q(x)` can provide a good approximation to an analytic function
|
| 599 |
+
beyond the radius of convergence of its Taylor series (example
|
| 600 |
+
from G.A. Baker 'Essentials of Pade Approximants' Academic Press,
|
| 601 |
+
Ch.1A)::
|
| 602 |
+
|
| 603 |
+
>>> from mpmath import *
|
| 604 |
+
>>> mp.dps = 15; mp.pretty = True
|
| 605 |
+
>>> one = mpf(1)
|
| 606 |
+
>>> def f(x):
|
| 607 |
+
... return sqrt((one + 2*x)/(one + x))
|
| 608 |
+
...
|
| 609 |
+
>>> a = taylor(f, 0, 6)
|
| 610 |
+
>>> p, q = pade(a, 3, 3)
|
| 611 |
+
>>> x = 10
|
| 612 |
+
>>> polyval(p[::-1], x)/polyval(q[::-1], x)
|
| 613 |
+
1.38169105566806
|
| 614 |
+
>>> f(x)
|
| 615 |
+
1.38169855941551
|
| 616 |
+
|
| 617 |
+
"""
|
| 618 |
+
# To determine L+1 coefficients of P and M coefficients of Q
|
| 619 |
+
# L+M+1 coefficients of A must be provided
|
| 620 |
+
if len(a) < L+M+1:
|
| 621 |
+
raise ValueError("L+M+1 Coefficients should be provided")
|
| 622 |
+
|
| 623 |
+
if M == 0:
|
| 624 |
+
if L == 0:
|
| 625 |
+
return [ctx.one], [ctx.one]
|
| 626 |
+
else:
|
| 627 |
+
return a[:L+1], [ctx.one]
|
| 628 |
+
|
| 629 |
+
# Solve first
|
| 630 |
+
# a[L]*q[1] + ... + a[L-M+1]*q[M] = -a[L+1]
|
| 631 |
+
# ...
|
| 632 |
+
# a[L+M-1]*q[1] + ... + a[L]*q[M] = -a[L+M]
|
| 633 |
+
A = ctx.matrix(M)
|
| 634 |
+
for j in range(M):
|
| 635 |
+
for i in range(min(M, L+j+1)):
|
| 636 |
+
A[j, i] = a[L+j-i]
|
| 637 |
+
v = -ctx.matrix(a[(L+1):(L+M+1)])
|
| 638 |
+
x = ctx.lu_solve(A, v)
|
| 639 |
+
q = [ctx.one] + list(x)
|
| 640 |
+
# compute p
|
| 641 |
+
p = [0]*(L+1)
|
| 642 |
+
for i in range(L+1):
|
| 643 |
+
s = a[i]
|
| 644 |
+
for j in range(1, min(M,i) + 1):
|
| 645 |
+
s += q[j]*a[i-j]
|
| 646 |
+
p[i] = s
|
| 647 |
+
return p, q
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (577 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/elliptic.cpython-310.pyc
ADDED
|
Binary file (40.3 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/factorials.cpython-310.pyc
ADDED
|
Binary file (4.48 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/functions.cpython-310.pyc
ADDED
|
Binary file (17.7 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/orthogonal.cpython-310.pyc
ADDED
|
Binary file (14.3 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/qfunctions.cpython-310.pyc
ADDED
|
Binary file (7.56 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/rszeta.cpython-310.pyc
ADDED
|
Binary file (29.4 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/theta.cpython-310.pyc
ADDED
|
Binary file (21 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/zeta.cpython-310.pyc
ADDED
|
Binary file (32.2 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/__pycache__/zetazeros.cpython-310.pyc
ADDED
|
Binary file (29.9 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/functions/functions.py
ADDED
|
@@ -0,0 +1,645 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ..libmp.backend import xrange
|
| 2 |
+
|
| 3 |
+
class SpecialFunctions(object):
|
| 4 |
+
"""
|
| 5 |
+
This class implements special functions using high-level code.
|
| 6 |
+
|
| 7 |
+
Elementary and some other functions (e.g. gamma function, basecase
|
| 8 |
+
hypergeometric series) are assumed to be predefined by the context as
|
| 9 |
+
"builtins" or "low-level" functions.
|
| 10 |
+
"""
|
| 11 |
+
defined_functions = {}
|
| 12 |
+
|
| 13 |
+
# The series for the Jacobi theta functions converge for |q| < 1;
|
| 14 |
+
# in the current implementation they throw a ValueError for
|
| 15 |
+
# abs(q) > THETA_Q_LIM
|
| 16 |
+
THETA_Q_LIM = 1 - 10**-7
|
| 17 |
+
|
| 18 |
+
def __init__(self):
|
| 19 |
+
cls = self.__class__
|
| 20 |
+
for name in cls.defined_functions:
|
| 21 |
+
f, wrap = cls.defined_functions[name]
|
| 22 |
+
cls._wrap_specfun(name, f, wrap)
|
| 23 |
+
|
| 24 |
+
self.mpq_1 = self._mpq((1,1))
|
| 25 |
+
self.mpq_0 = self._mpq((0,1))
|
| 26 |
+
self.mpq_1_2 = self._mpq((1,2))
|
| 27 |
+
self.mpq_3_2 = self._mpq((3,2))
|
| 28 |
+
self.mpq_1_4 = self._mpq((1,4))
|
| 29 |
+
self.mpq_1_16 = self._mpq((1,16))
|
| 30 |
+
self.mpq_3_16 = self._mpq((3,16))
|
| 31 |
+
self.mpq_5_2 = self._mpq((5,2))
|
| 32 |
+
self.mpq_3_4 = self._mpq((3,4))
|
| 33 |
+
self.mpq_7_4 = self._mpq((7,4))
|
| 34 |
+
self.mpq_5_4 = self._mpq((5,4))
|
| 35 |
+
self.mpq_1_3 = self._mpq((1,3))
|
| 36 |
+
self.mpq_2_3 = self._mpq((2,3))
|
| 37 |
+
self.mpq_4_3 = self._mpq((4,3))
|
| 38 |
+
self.mpq_1_6 = self._mpq((1,6))
|
| 39 |
+
self.mpq_5_6 = self._mpq((5,6))
|
| 40 |
+
self.mpq_5_3 = self._mpq((5,3))
|
| 41 |
+
|
| 42 |
+
self._misc_const_cache = {}
|
| 43 |
+
|
| 44 |
+
self._aliases.update({
|
| 45 |
+
'phase' : 'arg',
|
| 46 |
+
'conjugate' : 'conj',
|
| 47 |
+
'nthroot' : 'root',
|
| 48 |
+
'polygamma' : 'psi',
|
| 49 |
+
'hurwitz' : 'zeta',
|
| 50 |
+
#'digamma' : 'psi0',
|
| 51 |
+
#'trigamma' : 'psi1',
|
| 52 |
+
#'tetragamma' : 'psi2',
|
| 53 |
+
#'pentagamma' : 'psi3',
|
| 54 |
+
'fibonacci' : 'fib',
|
| 55 |
+
'factorial' : 'fac',
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
self.zetazero_memoized = self.memoize(self.zetazero)
|
| 59 |
+
|
| 60 |
+
# Default -- do nothing
|
| 61 |
+
@classmethod
|
| 62 |
+
def _wrap_specfun(cls, name, f, wrap):
|
| 63 |
+
setattr(cls, name, f)
|
| 64 |
+
|
| 65 |
+
# Optional fast versions of common functions in common cases.
|
| 66 |
+
# If not overridden, default (generic hypergeometric series)
|
| 67 |
+
# implementations will be used
|
| 68 |
+
def _besselj(ctx, n, z): raise NotImplementedError
|
| 69 |
+
def _erf(ctx, z): raise NotImplementedError
|
| 70 |
+
def _erfc(ctx, z): raise NotImplementedError
|
| 71 |
+
def _gamma_upper_int(ctx, z, a): raise NotImplementedError
|
| 72 |
+
def _expint_int(ctx, n, z): raise NotImplementedError
|
| 73 |
+
def _zeta(ctx, s): raise NotImplementedError
|
| 74 |
+
def _zetasum_fast(ctx, s, a, n, derivatives, reflect): raise NotImplementedError
|
| 75 |
+
def _ei(ctx, z): raise NotImplementedError
|
| 76 |
+
def _e1(ctx, z): raise NotImplementedError
|
| 77 |
+
def _ci(ctx, z): raise NotImplementedError
|
| 78 |
+
def _si(ctx, z): raise NotImplementedError
|
| 79 |
+
def _altzeta(ctx, s): raise NotImplementedError
|
| 80 |
+
|
| 81 |
+
def defun_wrapped(f):
|
| 82 |
+
SpecialFunctions.defined_functions[f.__name__] = f, True
|
| 83 |
+
return f
|
| 84 |
+
|
| 85 |
+
def defun(f):
|
| 86 |
+
SpecialFunctions.defined_functions[f.__name__] = f, False
|
| 87 |
+
return f
|
| 88 |
+
|
| 89 |
+
def defun_static(f):
|
| 90 |
+
setattr(SpecialFunctions, f.__name__, f)
|
| 91 |
+
return f
|
| 92 |
+
|
| 93 |
+
@defun_wrapped
|
| 94 |
+
def cot(ctx, z): return ctx.one / ctx.tan(z)
|
| 95 |
+
|
| 96 |
+
@defun_wrapped
|
| 97 |
+
def sec(ctx, z): return ctx.one / ctx.cos(z)
|
| 98 |
+
|
| 99 |
+
@defun_wrapped
|
| 100 |
+
def csc(ctx, z): return ctx.one / ctx.sin(z)
|
| 101 |
+
|
| 102 |
+
@defun_wrapped
|
| 103 |
+
def coth(ctx, z): return ctx.one / ctx.tanh(z)
|
| 104 |
+
|
| 105 |
+
@defun_wrapped
|
| 106 |
+
def sech(ctx, z): return ctx.one / ctx.cosh(z)
|
| 107 |
+
|
| 108 |
+
@defun_wrapped
|
| 109 |
+
def csch(ctx, z): return ctx.one / ctx.sinh(z)
|
| 110 |
+
|
| 111 |
+
@defun_wrapped
|
| 112 |
+
def acot(ctx, z):
|
| 113 |
+
if not z:
|
| 114 |
+
return ctx.pi * 0.5
|
| 115 |
+
else:
|
| 116 |
+
return ctx.atan(ctx.one / z)
|
| 117 |
+
|
| 118 |
+
@defun_wrapped
|
| 119 |
+
def asec(ctx, z): return ctx.acos(ctx.one / z)
|
| 120 |
+
|
| 121 |
+
@defun_wrapped
|
| 122 |
+
def acsc(ctx, z): return ctx.asin(ctx.one / z)
|
| 123 |
+
|
| 124 |
+
@defun_wrapped
|
| 125 |
+
def acoth(ctx, z):
|
| 126 |
+
if not z:
|
| 127 |
+
return ctx.pi * 0.5j
|
| 128 |
+
else:
|
| 129 |
+
return ctx.atanh(ctx.one / z)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@defun_wrapped
|
| 133 |
+
def asech(ctx, z): return ctx.acosh(ctx.one / z)
|
| 134 |
+
|
| 135 |
+
@defun_wrapped
|
| 136 |
+
def acsch(ctx, z): return ctx.asinh(ctx.one / z)
|
| 137 |
+
|
| 138 |
+
@defun
|
| 139 |
+
def sign(ctx, x):
|
| 140 |
+
x = ctx.convert(x)
|
| 141 |
+
if not x or ctx.isnan(x):
|
| 142 |
+
return x
|
| 143 |
+
if ctx._is_real_type(x):
|
| 144 |
+
if x > 0:
|
| 145 |
+
return ctx.one
|
| 146 |
+
else:
|
| 147 |
+
return -ctx.one
|
| 148 |
+
return x / abs(x)
|
| 149 |
+
|
| 150 |
+
@defun
|
| 151 |
+
def agm(ctx, a, b=1):
|
| 152 |
+
if b == 1:
|
| 153 |
+
return ctx.agm1(a)
|
| 154 |
+
a = ctx.convert(a)
|
| 155 |
+
b = ctx.convert(b)
|
| 156 |
+
return ctx._agm(a, b)
|
| 157 |
+
|
| 158 |
+
@defun_wrapped
|
| 159 |
+
def sinc(ctx, x):
|
| 160 |
+
if ctx.isinf(x):
|
| 161 |
+
return 1/x
|
| 162 |
+
if not x:
|
| 163 |
+
return x+1
|
| 164 |
+
return ctx.sin(x)/x
|
| 165 |
+
|
| 166 |
+
@defun_wrapped
|
| 167 |
+
def sincpi(ctx, x):
|
| 168 |
+
if ctx.isinf(x):
|
| 169 |
+
return 1/x
|
| 170 |
+
if not x:
|
| 171 |
+
return x+1
|
| 172 |
+
return ctx.sinpi(x)/(ctx.pi*x)
|
| 173 |
+
|
| 174 |
+
# TODO: tests; improve implementation
|
| 175 |
+
@defun_wrapped
|
| 176 |
+
def expm1(ctx, x):
|
| 177 |
+
if not x:
|
| 178 |
+
return ctx.zero
|
| 179 |
+
# exp(x) - 1 ~ x
|
| 180 |
+
if ctx.mag(x) < -ctx.prec:
|
| 181 |
+
return x + 0.5*x**2
|
| 182 |
+
# TODO: accurately eval the smaller of the real/imag parts
|
| 183 |
+
return ctx.sum_accurately(lambda: iter([ctx.exp(x),-1]),1)
|
| 184 |
+
|
| 185 |
+
@defun_wrapped
|
| 186 |
+
def log1p(ctx, x):
|
| 187 |
+
if not x:
|
| 188 |
+
return ctx.zero
|
| 189 |
+
if ctx.mag(x) < -ctx.prec:
|
| 190 |
+
return x - 0.5*x**2
|
| 191 |
+
return ctx.log(ctx.fadd(1, x, prec=2*ctx.prec))
|
| 192 |
+
|
| 193 |
+
@defun_wrapped
|
| 194 |
+
def powm1(ctx, x, y):
|
| 195 |
+
mag = ctx.mag
|
| 196 |
+
one = ctx.one
|
| 197 |
+
w = x**y - one
|
| 198 |
+
M = mag(w)
|
| 199 |
+
# Only moderate cancellation
|
| 200 |
+
if M > -8:
|
| 201 |
+
return w
|
| 202 |
+
# Check for the only possible exact cases
|
| 203 |
+
if not w:
|
| 204 |
+
if (not y) or (x in (1, -1, 1j, -1j) and ctx.isint(y)):
|
| 205 |
+
return w
|
| 206 |
+
x1 = x - one
|
| 207 |
+
magy = mag(y)
|
| 208 |
+
lnx = ctx.ln(x)
|
| 209 |
+
# Small y: x^y - 1 ~ log(x)*y + O(log(x)^2 * y^2)
|
| 210 |
+
if magy + mag(lnx) < -ctx.prec:
|
| 211 |
+
return lnx*y + (lnx*y)**2/2
|
| 212 |
+
# TODO: accurately eval the smaller of the real/imag part
|
| 213 |
+
return ctx.sum_accurately(lambda: iter([x**y, -1]), 1)
|
| 214 |
+
|
| 215 |
+
@defun
|
| 216 |
+
def _rootof1(ctx, k, n):
|
| 217 |
+
k = int(k)
|
| 218 |
+
n = int(n)
|
| 219 |
+
k %= n
|
| 220 |
+
if not k:
|
| 221 |
+
return ctx.one
|
| 222 |
+
elif 2*k == n:
|
| 223 |
+
return -ctx.one
|
| 224 |
+
elif 4*k == n:
|
| 225 |
+
return ctx.j
|
| 226 |
+
elif 4*k == 3*n:
|
| 227 |
+
return -ctx.j
|
| 228 |
+
return ctx.expjpi(2*ctx.mpf(k)/n)
|
| 229 |
+
|
| 230 |
+
@defun
|
| 231 |
+
def root(ctx, x, n, k=0):
|
| 232 |
+
n = int(n)
|
| 233 |
+
x = ctx.convert(x)
|
| 234 |
+
if k:
|
| 235 |
+
# Special case: there is an exact real root
|
| 236 |
+
if (n & 1 and 2*k == n-1) and (not ctx.im(x)) and (ctx.re(x) < 0):
|
| 237 |
+
return -ctx.root(-x, n)
|
| 238 |
+
# Multiply by root of unity
|
| 239 |
+
prec = ctx.prec
|
| 240 |
+
try:
|
| 241 |
+
ctx.prec += 10
|
| 242 |
+
v = ctx.root(x, n, 0) * ctx._rootof1(k, n)
|
| 243 |
+
finally:
|
| 244 |
+
ctx.prec = prec
|
| 245 |
+
return +v
|
| 246 |
+
return ctx._nthroot(x, n)
|
| 247 |
+
|
| 248 |
+
@defun
|
| 249 |
+
def unitroots(ctx, n, primitive=False):
|
| 250 |
+
gcd = ctx._gcd
|
| 251 |
+
prec = ctx.prec
|
| 252 |
+
try:
|
| 253 |
+
ctx.prec += 10
|
| 254 |
+
if primitive:
|
| 255 |
+
v = [ctx._rootof1(k,n) for k in range(n) if gcd(k,n) == 1]
|
| 256 |
+
else:
|
| 257 |
+
# TODO: this can be done *much* faster
|
| 258 |
+
v = [ctx._rootof1(k,n) for k in range(n)]
|
| 259 |
+
finally:
|
| 260 |
+
ctx.prec = prec
|
| 261 |
+
return [+x for x in v]
|
| 262 |
+
|
| 263 |
+
@defun
|
| 264 |
+
def arg(ctx, x):
|
| 265 |
+
x = ctx.convert(x)
|
| 266 |
+
re = ctx._re(x)
|
| 267 |
+
im = ctx._im(x)
|
| 268 |
+
return ctx.atan2(im, re)
|
| 269 |
+
|
| 270 |
+
@defun
|
| 271 |
+
def fabs(ctx, x):
|
| 272 |
+
return abs(ctx.convert(x))
|
| 273 |
+
|
| 274 |
+
@defun
|
| 275 |
+
def re(ctx, x):
|
| 276 |
+
x = ctx.convert(x)
|
| 277 |
+
if hasattr(x, "real"): # py2.5 doesn't have .real/.imag for all numbers
|
| 278 |
+
return x.real
|
| 279 |
+
return x
|
| 280 |
+
|
| 281 |
+
@defun
|
| 282 |
+
def im(ctx, x):
|
| 283 |
+
x = ctx.convert(x)
|
| 284 |
+
if hasattr(x, "imag"): # py2.5 doesn't have .real/.imag for all numbers
|
| 285 |
+
return x.imag
|
| 286 |
+
return ctx.zero
|
| 287 |
+
|
| 288 |
+
@defun
|
| 289 |
+
def conj(ctx, x):
|
| 290 |
+
x = ctx.convert(x)
|
| 291 |
+
try:
|
| 292 |
+
return x.conjugate()
|
| 293 |
+
except AttributeError:
|
| 294 |
+
return x
|
| 295 |
+
|
| 296 |
+
@defun
|
| 297 |
+
def polar(ctx, z):
|
| 298 |
+
return (ctx.fabs(z), ctx.arg(z))
|
| 299 |
+
|
| 300 |
+
@defun_wrapped
|
| 301 |
+
def rect(ctx, r, phi):
|
| 302 |
+
return r * ctx.mpc(*ctx.cos_sin(phi))
|
| 303 |
+
|
| 304 |
+
@defun
|
| 305 |
+
def log(ctx, x, b=None):
|
| 306 |
+
if b is None:
|
| 307 |
+
return ctx.ln(x)
|
| 308 |
+
wp = ctx.prec + 20
|
| 309 |
+
return ctx.ln(x, prec=wp) / ctx.ln(b, prec=wp)
|
| 310 |
+
|
| 311 |
+
@defun
|
| 312 |
+
def log10(ctx, x):
|
| 313 |
+
return ctx.log(x, 10)
|
| 314 |
+
|
| 315 |
+
@defun
|
| 316 |
+
def fmod(ctx, x, y):
|
| 317 |
+
return ctx.convert(x) % ctx.convert(y)
|
| 318 |
+
|
| 319 |
+
@defun
|
| 320 |
+
def degrees(ctx, x):
|
| 321 |
+
return x / ctx.degree
|
| 322 |
+
|
| 323 |
+
@defun
|
| 324 |
+
def radians(ctx, x):
|
| 325 |
+
return x * ctx.degree
|
| 326 |
+
|
| 327 |
+
def _lambertw_special(ctx, z, k):
|
| 328 |
+
# W(0,0) = 0; all other branches are singular
|
| 329 |
+
if not z:
|
| 330 |
+
if not k:
|
| 331 |
+
return z
|
| 332 |
+
return ctx.ninf + z
|
| 333 |
+
if z == ctx.inf:
|
| 334 |
+
if k == 0:
|
| 335 |
+
return z
|
| 336 |
+
else:
|
| 337 |
+
return z + 2*k*ctx.pi*ctx.j
|
| 338 |
+
if z == ctx.ninf:
|
| 339 |
+
return (-z) + (2*k+1)*ctx.pi*ctx.j
|
| 340 |
+
# Some kind of nan or complex inf/nan?
|
| 341 |
+
return ctx.ln(z)
|
| 342 |
+
|
| 343 |
+
import math
|
| 344 |
+
import cmath
|
| 345 |
+
|
| 346 |
+
def _lambertw_approx_hybrid(z, k):
|
| 347 |
+
imag_sign = 0
|
| 348 |
+
if hasattr(z, "imag"):
|
| 349 |
+
x = float(z.real)
|
| 350 |
+
y = z.imag
|
| 351 |
+
if y:
|
| 352 |
+
imag_sign = (-1) ** (y < 0)
|
| 353 |
+
y = float(y)
|
| 354 |
+
else:
|
| 355 |
+
x = float(z)
|
| 356 |
+
y = 0.0
|
| 357 |
+
imag_sign = 0
|
| 358 |
+
# hack to work regardless of whether Python supports -0.0
|
| 359 |
+
if not y:
|
| 360 |
+
y = 0.0
|
| 361 |
+
z = complex(x,y)
|
| 362 |
+
if k == 0:
|
| 363 |
+
if -4.0 < y < 4.0 and -1.0 < x < 2.5:
|
| 364 |
+
if imag_sign:
|
| 365 |
+
# Taylor series in upper/lower half-plane
|
| 366 |
+
if y > 1.00: return (0.876+0.645j) + (0.118-0.174j)*(z-(0.75+2.5j))
|
| 367 |
+
if y > 0.25: return (0.505+0.204j) + (0.375-0.132j)*(z-(0.75+0.5j))
|
| 368 |
+
if y < -1.00: return (0.876-0.645j) + (0.118+0.174j)*(z-(0.75-2.5j))
|
| 369 |
+
if y < -0.25: return (0.505-0.204j) + (0.375+0.132j)*(z-(0.75-0.5j))
|
| 370 |
+
# Taylor series near -1
|
| 371 |
+
if x < -0.5:
|
| 372 |
+
if imag_sign >= 0:
|
| 373 |
+
return (-0.318+1.34j) + (-0.697-0.593j)*(z+1)
|
| 374 |
+
else:
|
| 375 |
+
return (-0.318-1.34j) + (-0.697+0.593j)*(z+1)
|
| 376 |
+
# return real type
|
| 377 |
+
r = -0.367879441171442
|
| 378 |
+
if (not imag_sign) and x > r:
|
| 379 |
+
z = x
|
| 380 |
+
# Singularity near -1/e
|
| 381 |
+
if x < -0.2:
|
| 382 |
+
return -1 + 2.33164398159712*(z-r)**0.5 - 1.81218788563936*(z-r)
|
| 383 |
+
# Taylor series near 0
|
| 384 |
+
if x < 0.5: return z
|
| 385 |
+
# Simple linear approximation
|
| 386 |
+
return 0.2 + 0.3*z
|
| 387 |
+
if (not imag_sign) and x > 0.0:
|
| 388 |
+
L1 = math.log(x); L2 = math.log(L1)
|
| 389 |
+
else:
|
| 390 |
+
L1 = cmath.log(z); L2 = cmath.log(L1)
|
| 391 |
+
elif k == -1:
|
| 392 |
+
# return real type
|
| 393 |
+
r = -0.367879441171442
|
| 394 |
+
if (not imag_sign) and r < x < 0.0:
|
| 395 |
+
z = x
|
| 396 |
+
if (imag_sign >= 0) and y < 0.1 and -0.6 < x < -0.2:
|
| 397 |
+
return -1 - 2.33164398159712*(z-r)**0.5 - 1.81218788563936*(z-r)
|
| 398 |
+
if (not imag_sign) and -0.2 <= x < 0.0:
|
| 399 |
+
L1 = math.log(-x)
|
| 400 |
+
return L1 - math.log(-L1)
|
| 401 |
+
else:
|
| 402 |
+
if imag_sign == -1 and (not y) and x < 0.0:
|
| 403 |
+
L1 = cmath.log(z) - 3.1415926535897932j
|
| 404 |
+
else:
|
| 405 |
+
L1 = cmath.log(z) - 6.2831853071795865j
|
| 406 |
+
L2 = cmath.log(L1)
|
| 407 |
+
return L1 - L2 + L2/L1 + L2*(L2-2)/(2*L1**2)
|
| 408 |
+
|
| 409 |
+
def _lambertw_series(ctx, z, k, tol):
|
| 410 |
+
"""
|
| 411 |
+
Return rough approximation for W_k(z) from an asymptotic series,
|
| 412 |
+
sufficiently accurate for the Halley iteration to converge to
|
| 413 |
+
the correct value.
|
| 414 |
+
"""
|
| 415 |
+
magz = ctx.mag(z)
|
| 416 |
+
if (-10 < magz < 900) and (-1000 < k < 1000):
|
| 417 |
+
# Near the branch point at -1/e
|
| 418 |
+
if magz < 1 and abs(z+0.36787944117144) < 0.05:
|
| 419 |
+
if k == 0 or (k == -1 and ctx._im(z) >= 0) or \
|
| 420 |
+
(k == 1 and ctx._im(z) < 0):
|
| 421 |
+
delta = ctx.sum_accurately(lambda: [z, ctx.exp(-1)])
|
| 422 |
+
cancellation = -ctx.mag(delta)
|
| 423 |
+
ctx.prec += cancellation
|
| 424 |
+
# Use series given in Corless et al.
|
| 425 |
+
p = ctx.sqrt(2*(ctx.e*z+1))
|
| 426 |
+
ctx.prec -= cancellation
|
| 427 |
+
u = {0:ctx.mpf(-1), 1:ctx.mpf(1)}
|
| 428 |
+
a = {0:ctx.mpf(2), 1:ctx.mpf(-1)}
|
| 429 |
+
if k != 0:
|
| 430 |
+
p = -p
|
| 431 |
+
s = ctx.zero
|
| 432 |
+
# The series converges, so we could use it directly, but unless
|
| 433 |
+
# *extremely* close, it is better to just use the first few
|
| 434 |
+
# terms to get a good approximation for the iteration
|
| 435 |
+
for l in xrange(max(2,cancellation)):
|
| 436 |
+
if l not in u:
|
| 437 |
+
a[l] = ctx.fsum(u[j]*u[l+1-j] for j in xrange(2,l))
|
| 438 |
+
u[l] = (l-1)*(u[l-2]/2+a[l-2]/4)/(l+1)-a[l]/2-u[l-1]/(l+1)
|
| 439 |
+
term = u[l] * p**l
|
| 440 |
+
s += term
|
| 441 |
+
if ctx.mag(term) < -tol:
|
| 442 |
+
return s, True
|
| 443 |
+
l += 1
|
| 444 |
+
ctx.prec += cancellation//2
|
| 445 |
+
return s, False
|
| 446 |
+
if k == 0 or k == -1:
|
| 447 |
+
return _lambertw_approx_hybrid(z, k), False
|
| 448 |
+
if k == 0:
|
| 449 |
+
if magz < -1:
|
| 450 |
+
return z*(1-z), False
|
| 451 |
+
L1 = ctx.ln(z)
|
| 452 |
+
L2 = ctx.ln(L1)
|
| 453 |
+
elif k == -1 and (not ctx._im(z)) and (-0.36787944117144 < ctx._re(z) < 0):
|
| 454 |
+
L1 = ctx.ln(-z)
|
| 455 |
+
return L1 - ctx.ln(-L1), False
|
| 456 |
+
else:
|
| 457 |
+
# This holds both as z -> 0 and z -> inf.
|
| 458 |
+
# Relative error is O(1/log(z)).
|
| 459 |
+
L1 = ctx.ln(z) + 2j*ctx.pi*k
|
| 460 |
+
L2 = ctx.ln(L1)
|
| 461 |
+
return L1 - L2 + L2/L1 + L2*(L2-2)/(2*L1**2), False
|
| 462 |
+
|
| 463 |
+
@defun
|
| 464 |
+
def lambertw(ctx, z, k=0):
|
| 465 |
+
z = ctx.convert(z)
|
| 466 |
+
k = int(k)
|
| 467 |
+
if not ctx.isnormal(z):
|
| 468 |
+
return _lambertw_special(ctx, z, k)
|
| 469 |
+
prec = ctx.prec
|
| 470 |
+
ctx.prec += 20 + ctx.mag(k or 1)
|
| 471 |
+
wp = ctx.prec
|
| 472 |
+
tol = wp - 5
|
| 473 |
+
w, done = _lambertw_series(ctx, z, k, tol)
|
| 474 |
+
if not done:
|
| 475 |
+
# Use Halley iteration to solve w*exp(w) = z
|
| 476 |
+
two = ctx.mpf(2)
|
| 477 |
+
for i in xrange(100):
|
| 478 |
+
ew = ctx.exp(w)
|
| 479 |
+
wew = w*ew
|
| 480 |
+
wewz = wew-z
|
| 481 |
+
wn = w - wewz/(wew+ew-(w+two)*wewz/(two*w+two))
|
| 482 |
+
if ctx.mag(wn-w) <= ctx.mag(wn) - tol:
|
| 483 |
+
w = wn
|
| 484 |
+
break
|
| 485 |
+
else:
|
| 486 |
+
w = wn
|
| 487 |
+
if i == 100:
|
| 488 |
+
ctx.warn("Lambert W iteration failed to converge for z = %s" % z)
|
| 489 |
+
ctx.prec = prec
|
| 490 |
+
return +w
|
| 491 |
+
|
| 492 |
+
@defun_wrapped
|
| 493 |
+
def bell(ctx, n, x=1):
|
| 494 |
+
x = ctx.convert(x)
|
| 495 |
+
if not n:
|
| 496 |
+
if ctx.isnan(x):
|
| 497 |
+
return x
|
| 498 |
+
return type(x)(1)
|
| 499 |
+
if ctx.isinf(x) or ctx.isinf(n) or ctx.isnan(x) or ctx.isnan(n):
|
| 500 |
+
return x**n
|
| 501 |
+
if n == 1: return x
|
| 502 |
+
if n == 2: return x*(x+1)
|
| 503 |
+
if x == 0: return ctx.sincpi(n)
|
| 504 |
+
return _polyexp(ctx, n, x, True) / ctx.exp(x)
|
| 505 |
+
|
| 506 |
+
def _polyexp(ctx, n, x, extra=False):
|
| 507 |
+
def _terms():
|
| 508 |
+
if extra:
|
| 509 |
+
yield ctx.sincpi(n)
|
| 510 |
+
t = x
|
| 511 |
+
k = 1
|
| 512 |
+
while 1:
|
| 513 |
+
yield k**n * t
|
| 514 |
+
k += 1
|
| 515 |
+
t = t*x/k
|
| 516 |
+
return ctx.sum_accurately(_terms, check_step=4)
|
| 517 |
+
|
| 518 |
+
@defun_wrapped
|
| 519 |
+
def polyexp(ctx, s, z):
|
| 520 |
+
if ctx.isinf(z) or ctx.isinf(s) or ctx.isnan(z) or ctx.isnan(s):
|
| 521 |
+
return z**s
|
| 522 |
+
if z == 0: return z*s
|
| 523 |
+
if s == 0: return ctx.expm1(z)
|
| 524 |
+
if s == 1: return ctx.exp(z)*z
|
| 525 |
+
if s == 2: return ctx.exp(z)*z*(z+1)
|
| 526 |
+
return _polyexp(ctx, s, z)
|
| 527 |
+
|
| 528 |
+
@defun_wrapped
|
| 529 |
+
def cyclotomic(ctx, n, z):
|
| 530 |
+
n = int(n)
|
| 531 |
+
if n < 0:
|
| 532 |
+
raise ValueError("n cannot be negative")
|
| 533 |
+
p = ctx.one
|
| 534 |
+
if n == 0:
|
| 535 |
+
return p
|
| 536 |
+
if n == 1:
|
| 537 |
+
return z - p
|
| 538 |
+
if n == 2:
|
| 539 |
+
return z + p
|
| 540 |
+
# Use divisor product representation. Unfortunately, this sometimes
|
| 541 |
+
# includes singularities for roots of unity, which we have to cancel out.
|
| 542 |
+
# Matching zeros/poles pairwise, we have (1-z^a)/(1-z^b) ~ a/b + O(z-1).
|
| 543 |
+
a_prod = 1
|
| 544 |
+
b_prod = 1
|
| 545 |
+
num_zeros = 0
|
| 546 |
+
num_poles = 0
|
| 547 |
+
for d in range(1,n+1):
|
| 548 |
+
if not n % d:
|
| 549 |
+
w = ctx.moebius(n//d)
|
| 550 |
+
# Use powm1 because it is important that we get 0 only
|
| 551 |
+
# if it really is exactly 0
|
| 552 |
+
b = -ctx.powm1(z, d)
|
| 553 |
+
if b:
|
| 554 |
+
p *= b**w
|
| 555 |
+
else:
|
| 556 |
+
if w == 1:
|
| 557 |
+
a_prod *= d
|
| 558 |
+
num_zeros += 1
|
| 559 |
+
elif w == -1:
|
| 560 |
+
b_prod *= d
|
| 561 |
+
num_poles += 1
|
| 562 |
+
#print n, num_zeros, num_poles
|
| 563 |
+
if num_zeros:
|
| 564 |
+
if num_zeros > num_poles:
|
| 565 |
+
p *= 0
|
| 566 |
+
else:
|
| 567 |
+
p *= a_prod
|
| 568 |
+
p /= b_prod
|
| 569 |
+
return p
|
| 570 |
+
|
| 571 |
+
@defun
|
| 572 |
+
def mangoldt(ctx, n):
|
| 573 |
+
r"""
|
| 574 |
+
Evaluates the von Mangoldt function `\Lambda(n) = \log p`
|
| 575 |
+
if `n = p^k` a power of a prime, and `\Lambda(n) = 0` otherwise.
|
| 576 |
+
|
| 577 |
+
**Examples**
|
| 578 |
+
|
| 579 |
+
>>> from mpmath import *
|
| 580 |
+
>>> mp.dps = 25; mp.pretty = True
|
| 581 |
+
>>> [mangoldt(n) for n in range(-2,3)]
|
| 582 |
+
[0.0, 0.0, 0.0, 0.0, 0.6931471805599453094172321]
|
| 583 |
+
>>> mangoldt(6)
|
| 584 |
+
0.0
|
| 585 |
+
>>> mangoldt(7)
|
| 586 |
+
1.945910149055313305105353
|
| 587 |
+
>>> mangoldt(8)
|
| 588 |
+
0.6931471805599453094172321
|
| 589 |
+
>>> fsum(mangoldt(n) for n in range(101))
|
| 590 |
+
94.04531122935739224600493
|
| 591 |
+
>>> fsum(mangoldt(n) for n in range(10001))
|
| 592 |
+
10013.39669326311478372032
|
| 593 |
+
|
| 594 |
+
"""
|
| 595 |
+
n = int(n)
|
| 596 |
+
if n < 2:
|
| 597 |
+
return ctx.zero
|
| 598 |
+
if n % 2 == 0:
|
| 599 |
+
# Must be a power of two
|
| 600 |
+
if n & (n-1) == 0:
|
| 601 |
+
return +ctx.ln2
|
| 602 |
+
else:
|
| 603 |
+
return ctx.zero
|
| 604 |
+
# TODO: the following could be generalized into a perfect
|
| 605 |
+
# power testing function
|
| 606 |
+
# ---
|
| 607 |
+
# Look for a small factor
|
| 608 |
+
for p in (3,5,7,11,13,17,19,23,29,31):
|
| 609 |
+
if not n % p:
|
| 610 |
+
q, r = n // p, 0
|
| 611 |
+
while q > 1:
|
| 612 |
+
q, r = divmod(q, p)
|
| 613 |
+
if r:
|
| 614 |
+
return ctx.zero
|
| 615 |
+
return ctx.ln(p)
|
| 616 |
+
if ctx.isprime(n):
|
| 617 |
+
return ctx.ln(n)
|
| 618 |
+
# Obviously, we could use arbitrary-precision arithmetic for this...
|
| 619 |
+
if n > 10**30:
|
| 620 |
+
raise NotImplementedError
|
| 621 |
+
k = 2
|
| 622 |
+
while 1:
|
| 623 |
+
p = int(n**(1./k) + 0.5)
|
| 624 |
+
if p < 2:
|
| 625 |
+
return ctx.zero
|
| 626 |
+
if p ** k == n:
|
| 627 |
+
if ctx.isprime(p):
|
| 628 |
+
return ctx.ln(p)
|
| 629 |
+
k += 1
|
| 630 |
+
|
| 631 |
+
@defun
|
| 632 |
+
def stirling1(ctx, n, k, exact=False):
|
| 633 |
+
v = ctx._stirling1(int(n), int(k))
|
| 634 |
+
if exact:
|
| 635 |
+
return int(v)
|
| 636 |
+
else:
|
| 637 |
+
return ctx.mpf(v)
|
| 638 |
+
|
| 639 |
+
@defun
|
| 640 |
+
def stirling2(ctx, n, k, exact=False):
|
| 641 |
+
v = ctx._stirling2(int(n), int(k))
|
| 642 |
+
if exact:
|
| 643 |
+
return int(v)
|
| 644 |
+
else:
|
| 645 |
+
return ctx.mpf(v)
|
vllm/lib/python3.10/site-packages/mpmath/tests/__init__.py
ADDED
|
File without changes
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (162 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/extratest_zeta.cpython-310.pyc
ADDED
|
Binary file (953 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/runtests.cpython-310.pyc
ADDED
|
Binary file (3.7 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_compatibility.cpython-310.pyc
ADDED
|
Binary file (1.96 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_convert.cpython-310.pyc
ADDED
|
Binary file (9.5 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_division.cpython-310.pyc
ADDED
|
Binary file (4.44 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_eigen_symmetric.cpython-310.pyc
ADDED
|
Binary file (8.95 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_elliptic.cpython-310.pyc
ADDED
|
Binary file (19.8 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_fp.cpython-310.pyc
ADDED
|
Binary file (53.4 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_functions.cpython-310.pyc
ADDED
|
Binary file (31.7 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_gammazeta.cpython-310.pyc
ADDED
|
Binary file (26.1 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_hp.cpython-310.pyc
ADDED
|
Binary file (8.7 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_identify.cpython-310.pyc
ADDED
|
Binary file (1.04 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_interval.cpython-310.pyc
ADDED
|
Binary file (16.3 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_levin.cpython-310.pyc
ADDED
|
Binary file (5.95 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_linalg.cpython-310.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_matrices.cpython-310.pyc
ADDED
|
Binary file (8.87 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_mpmath.cpython-310.pyc
ADDED
|
Binary file (446 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_pickle.cpython-310.pyc
ADDED
|
Binary file (696 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_power.cpython-310.pyc
ADDED
|
Binary file (4.33 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_quad.cpython-310.pyc
ADDED
|
Binary file (8.61 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_rootfinding.cpython-310.pyc
ADDED
|
Binary file (5.68 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_str.cpython-310.pyc
ADDED
|
Binary file (756 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_summation.cpython-310.pyc
ADDED
|
Binary file (3.71 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/test_visualization.cpython-310.pyc
ADDED
|
Binary file (1.41 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/__pycache__/torture.cpython-310.pyc
ADDED
|
Binary file (8.17 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/mpmath/tests/extratest_gamma.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mpmath import *
|
| 2 |
+
from mpmath.libmp import ifac
|
| 3 |
+
|
| 4 |
+
import sys
|
| 5 |
+
if "-dps" in sys.argv:
|
| 6 |
+
maxdps = int(sys.argv[sys.argv.index("-dps")+1])
|
| 7 |
+
else:
|
| 8 |
+
maxdps = 1000
|
| 9 |
+
|
| 10 |
+
raise_ = "-raise" in sys.argv
|
| 11 |
+
|
| 12 |
+
errcount = 0
|
| 13 |
+
|
| 14 |
+
def check(name, func, z, y):
|
| 15 |
+
global errcount
|
| 16 |
+
try:
|
| 17 |
+
x = func(z)
|
| 18 |
+
except:
|
| 19 |
+
errcount += 1
|
| 20 |
+
if raise_:
|
| 21 |
+
raise
|
| 22 |
+
print()
|
| 23 |
+
print(name)
|
| 24 |
+
print("EXCEPTION")
|
| 25 |
+
import traceback
|
| 26 |
+
traceback.print_tb(sys.exc_info()[2])
|
| 27 |
+
print()
|
| 28 |
+
return
|
| 29 |
+
xre = x.real
|
| 30 |
+
xim = x.imag
|
| 31 |
+
yre = y.real
|
| 32 |
+
yim = y.imag
|
| 33 |
+
tol = eps*8
|
| 34 |
+
err = 0
|
| 35 |
+
if abs(xre-yre) > abs(yre)*tol:
|
| 36 |
+
err = 1
|
| 37 |
+
print()
|
| 38 |
+
print("Error! %s (re = %s, wanted %s, err=%s)" % (name, nstr(xre,10), nstr(yre,10), nstr(abs(xre-yre))))
|
| 39 |
+
errcount += 1
|
| 40 |
+
if raise_:
|
| 41 |
+
raise SystemExit
|
| 42 |
+
if abs(xim-yim) > abs(yim)*tol:
|
| 43 |
+
err = 1
|
| 44 |
+
print()
|
| 45 |
+
print("Error! %s (im = %s, wanted %s, err=%s)" % (name, nstr(xim,10), nstr(yim,10), nstr(abs(xim-yim))))
|
| 46 |
+
errcount += 1
|
| 47 |
+
if raise_:
|
| 48 |
+
raise SystemExit
|
| 49 |
+
if not err:
|
| 50 |
+
sys.stdout.write("%s ok; " % name)
|
| 51 |
+
|
| 52 |
+
def testcase(case):
|
| 53 |
+
z, result = case
|
| 54 |
+
print("Testing z =", z)
|
| 55 |
+
mp.dps = 1010
|
| 56 |
+
z = eval(z)
|
| 57 |
+
mp.dps = maxdps + 50
|
| 58 |
+
if result is None:
|
| 59 |
+
gamma_val = gamma(z)
|
| 60 |
+
loggamma_val = loggamma(z)
|
| 61 |
+
factorial_val = factorial(z)
|
| 62 |
+
rgamma_val = rgamma(z)
|
| 63 |
+
else:
|
| 64 |
+
loggamma_val = eval(result)
|
| 65 |
+
gamma_val = exp(loggamma_val)
|
| 66 |
+
factorial_val = z * gamma_val
|
| 67 |
+
rgamma_val = 1/gamma_val
|
| 68 |
+
for dps in [5, 10, 15, 25, 40, 60, 90, 120, 250, 600, 1000, 1800, 3600]:
|
| 69 |
+
if dps > maxdps:
|
| 70 |
+
break
|
| 71 |
+
mp.dps = dps
|
| 72 |
+
print("dps = %s" % dps)
|
| 73 |
+
check("gamma", gamma, z, gamma_val)
|
| 74 |
+
check("rgamma", rgamma, z, rgamma_val)
|
| 75 |
+
check("loggamma", loggamma, z, loggamma_val)
|
| 76 |
+
check("factorial", factorial, z, factorial_val)
|
| 77 |
+
print()
|
| 78 |
+
mp.dps = 15
|
| 79 |
+
|
| 80 |
+
testcases = []
|
| 81 |
+
|
| 82 |
+
# Basic values
|
| 83 |
+
for n in list(range(1,200)) + list(range(201,2000,17)):
|
| 84 |
+
testcases.append(["%s" % n, None])
|
| 85 |
+
for n in range(-200,200):
|
| 86 |
+
testcases.append(["%s+0.5" % n, None])
|
| 87 |
+
testcases.append(["%s+0.37" % n, None])
|
| 88 |
+
|
| 89 |
+
testcases += [\
|
| 90 |
+
["(0.1+1j)", None],
|
| 91 |
+
["(-0.1+1j)", None],
|
| 92 |
+
["(0.1-1j)", None],
|
| 93 |
+
["(-0.1-1j)", None],
|
| 94 |
+
["10j", None],
|
| 95 |
+
["-10j", None],
|
| 96 |
+
["100j", None],
|
| 97 |
+
["10000j", None],
|
| 98 |
+
["-10000000j", None],
|
| 99 |
+
["(10**100)*j", None],
|
| 100 |
+
["125+(10**100)*j", None],
|
| 101 |
+
["-125+(10**100)*j", None],
|
| 102 |
+
["(10**10)*(1+j)", None],
|
| 103 |
+
["(10**10)*(-1+j)", None],
|
| 104 |
+
["(10**100)*(1+j)", None],
|
| 105 |
+
["(10**100)*(-1+j)", None],
|
| 106 |
+
["(1.5-1j)", None],
|
| 107 |
+
["(6+4j)", None],
|
| 108 |
+
["(4+1j)", None],
|
| 109 |
+
["(3.5+2j)", None],
|
| 110 |
+
["(1.5-1j)", None],
|
| 111 |
+
["(-6-4j)", None],
|
| 112 |
+
["(-2-3j)", None],
|
| 113 |
+
["(-2.5-2j)", None],
|
| 114 |
+
["(4+1j)", None],
|
| 115 |
+
["(3+3j)", None],
|
| 116 |
+
["(2-2j)", None],
|
| 117 |
+
["1", "0"],
|
| 118 |
+
["2", "0"],
|
| 119 |
+
["3", "log(2)"],
|
| 120 |
+
["4", "log(6)"],
|
| 121 |
+
["5", "log(24)"],
|
| 122 |
+
["0.5", "log(pi)/2"],
|
| 123 |
+
["1.5", "log(sqrt(pi)/2)"],
|
| 124 |
+
["2.5", "log(3*sqrt(pi)/4)"],
|
| 125 |
+
["mpf('0.37')", None],
|
| 126 |
+
["0.25", "log(sqrt(2*sqrt(2*pi**3)/agm(1,sqrt(2))))"],
|
| 127 |
+
["-0.4", None],
|
| 128 |
+
["mpf('-1.9')", None],
|
| 129 |
+
["mpf('12.8')", None],
|
| 130 |
+
["mpf('33.7')", None],
|
| 131 |
+
["mpf('95.2')", None],
|
| 132 |
+
["mpf('160.3')", None],
|
| 133 |
+
["mpf('2057.8')", None],
|
| 134 |
+
["25", "log(ifac(24))"],
|
| 135 |
+
["80", "log(ifac(79))"],
|
| 136 |
+
["500", "log(ifac(500-1))"],
|
| 137 |
+
["8000", "log(ifac(8000-1))"],
|
| 138 |
+
["8000.5", None],
|
| 139 |
+
["mpf('8000.1')", None],
|
| 140 |
+
["mpf('1.37e10')", None],
|
| 141 |
+
["mpf('1.37e10')*(1+j)", None],
|
| 142 |
+
["mpf('1.37e10')*(-1+j)", None],
|
| 143 |
+
["mpf('1.37e10')*(-1-j)", None],
|
| 144 |
+
["mpf('1.37e10')*(-1+j)", None],
|
| 145 |
+
["mpf('1.37e100')", None],
|
| 146 |
+
["mpf('1.37e100')*(1+j)", None],
|
| 147 |
+
["mpf('1.37e100')*(-1+j)", None],
|
| 148 |
+
["mpf('1.37e100')*(-1-j)", None],
|
| 149 |
+
["mpf('1.37e100')*(-1+j)", None],
|
| 150 |
+
["3+4j",
|
| 151 |
+
"mpc('"
|
| 152 |
+
"-1.7566267846037841105306041816232757851567066070613445016197619371316057169"
|
| 153 |
+
"4723618263960834804618463052988607348289672535780644470689771115236512106002"
|
| 154 |
+
"5970873471563240537307638968509556191696167970488390423963867031934333890838"
|
| 155 |
+
"8009531786948197210025029725361069435208930363494971027388382086721660805397"
|
| 156 |
+
"9163230643216054580167976201709951509519218635460317367338612500626714783631"
|
| 157 |
+
"7498317478048447525674016344322545858832610325861086336204591943822302971823"
|
| 158 |
+
"5161814175530618223688296232894588415495615809337292518431903058265147109853"
|
| 159 |
+
"1710568942184987827643886816200452860853873815413367529829631430146227470517"
|
| 160 |
+
"6579967222200868632179482214312673161276976117132204633283806161971389519137"
|
| 161 |
+
"1243359764435612951384238091232760634271570950240717650166551484551654327989"
|
| 162 |
+
"9360285030081716934130446150245110557038117075172576825490035434069388648124"
|
| 163 |
+
"6678152254554001586736120762641422590778766100376515737713938521275749049949"
|
| 164 |
+
"1284143906816424244705094759339932733567910991920631339597278805393743140853"
|
| 165 |
+
"391550313363278558195609260225928','"
|
| 166 |
+
"4.74266443803465792819488940755002274088830335171164611359052405215840070271"
|
| 167 |
+
"5906813009373171139767051863542508136875688550817670379002790304870822775498"
|
| 168 |
+
"2809996675877564504192565392367259119610438951593128982646945990372179860613"
|
| 169 |
+
"4294436498090428077839141927485901735557543641049637962003652638924845391650"
|
| 170 |
+
"9546290137755550107224907606529385248390667634297183361902055842228798984200"
|
| 171 |
+
"9591180450211798341715874477629099687609819466457990642030707080894518168924"
|
| 172 |
+
"6805549314043258530272479246115112769957368212585759640878745385160943755234"
|
| 173 |
+
"9398036774908108204370323896757543121853650025529763655312360354244898913463"
|
| 174 |
+
"7115955702828838923393113618205074162812089732064414530813087483533203244056"
|
| 175 |
+
"0546577484241423134079056537777170351934430586103623577814746004431994179990"
|
| 176 |
+
"5318522939077992613855205801498201930221975721246498720895122345420698451980"
|
| 177 |
+
"0051215797310305885845964334761831751370672996984756815410977750799748813563"
|
| 178 |
+
"8784405288158432214886648743541773208808731479748217023665577802702269468013"
|
| 179 |
+
"673719173759245720489020315779001')"],
|
| 180 |
+
]
|
| 181 |
+
|
| 182 |
+
for z in [4, 14, 34, 64]:
|
| 183 |
+
testcases.append(["(2+j)*%s/3" % z, None])
|
| 184 |
+
testcases.append(["(-2+j)*%s/3" % z, None])
|
| 185 |
+
testcases.append(["(1+2*j)*%s/3" % z, None])
|
| 186 |
+
testcases.append(["(2-j)*%s/3" % z, None])
|
| 187 |
+
testcases.append(["(20+j)*%s/3" % z, None])
|
| 188 |
+
testcases.append(["(-20+j)*%s/3" % z, None])
|
| 189 |
+
testcases.append(["(1+20*j)*%s/3" % z, None])
|
| 190 |
+
testcases.append(["(20-j)*%s/3" % z, None])
|
| 191 |
+
testcases.append(["(200+j)*%s/3" % z, None])
|
| 192 |
+
testcases.append(["(-200+j)*%s/3" % z, None])
|
| 193 |
+
testcases.append(["(1+200*j)*%s/3" % z, None])
|
| 194 |
+
testcases.append(["(200-j)*%s/3" % z, None])
|
| 195 |
+
|
| 196 |
+
# Poles
|
| 197 |
+
for n in [0,1,2,3,4,25,-1,-2,-3,-4,-20,-21,-50,-51,-200,-201,-20000,-20001]:
|
| 198 |
+
for t in ['1e-5', '1e-20', '1e-100', '1e-10000']:
|
| 199 |
+
testcases.append(["fadd(%s,'%s',exact=True)" % (n, t), None])
|
| 200 |
+
testcases.append(["fsub(%s,'%s',exact=True)" % (n, t), None])
|
| 201 |
+
testcases.append(["fadd(%s,'%sj',exact=True)" % (n, t), None])
|
| 202 |
+
testcases.append(["fsub(%s,'%sj',exact=True)" % (n, t), None])
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
from timeit import default_timer as clock
|
| 206 |
+
tot_time = 0.0
|
| 207 |
+
for case in testcases:
|
| 208 |
+
t1 = clock()
|
| 209 |
+
testcase(case)
|
| 210 |
+
t2 = clock()
|
| 211 |
+
print("Test time:", t2-t1)
|
| 212 |
+
print()
|
| 213 |
+
tot_time += (t2-t1)
|
| 214 |
+
print("Total time:", tot_time)
|
| 215 |
+
print("Errors:", errcount)
|
vllm/lib/python3.10/site-packages/mpmath/tests/extratest_zeta.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mpmath import zetazero
|
| 2 |
+
from timeit import default_timer as clock
|
| 3 |
+
|
| 4 |
+
def test_zetazero():
|
| 5 |
+
cases = [\
|
| 6 |
+
(399999999, 156762524.6750591511),
|
| 7 |
+
(241389216, 97490234.2276711795),
|
| 8 |
+
(526196239, 202950727.691229534),
|
| 9 |
+
(542964976, 209039046.578535272),
|
| 10 |
+
(1048449112, 388858885.231056486),
|
| 11 |
+
(1048449113, 388858885.384337406),
|
| 12 |
+
(1048449114, 388858886.002285122),
|
| 13 |
+
(1048449115, 388858886.00239369),
|
| 14 |
+
(1048449116, 388858886.690745053)
|
| 15 |
+
]
|
| 16 |
+
for n, v in cases:
|
| 17 |
+
print(n, v)
|
| 18 |
+
t1 = clock()
|
| 19 |
+
ok = zetazero(n).ae(complex(0.5,v))
|
| 20 |
+
t2 = clock()
|
| 21 |
+
print("ok =", ok, ("(time = %s)" % round(t2-t1,3)))
|
| 22 |
+
print("Now computing two huge zeros (this may take hours)")
|
| 23 |
+
print("Computing zetazero(8637740722917)")
|
| 24 |
+
ok = zetazero(8637740722917).ae(complex(0.5,2124447368584.39296466152))
|
| 25 |
+
print("ok =", ok)
|
| 26 |
+
ok = zetazero(8637740722918).ae(complex(0.5,2124447368584.39298170604))
|
| 27 |
+
print("ok =", ok)
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
test_zetazero()
|
vllm/lib/python3.10/site-packages/mpmath/tests/runtests.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
python runtests.py -py
|
| 5 |
+
Use py.test to run tests (more useful for debugging)
|
| 6 |
+
|
| 7 |
+
python runtests.py -coverage
|
| 8 |
+
Generate test coverage report. Statistics are written to /tmp
|
| 9 |
+
|
| 10 |
+
python runtests.py -profile
|
| 11 |
+
Generate profile stats (this is much slower)
|
| 12 |
+
|
| 13 |
+
python runtests.py -nogmpy
|
| 14 |
+
Run tests without using GMPY even if it exists
|
| 15 |
+
|
| 16 |
+
python runtests.py -strict
|
| 17 |
+
Enforce extra tests in normalize()
|
| 18 |
+
|
| 19 |
+
python runtests.py -local
|
| 20 |
+
Insert '../..' at the beginning of sys.path to use local mpmath
|
| 21 |
+
|
| 22 |
+
python runtests.py -skip ...
|
| 23 |
+
Skip tests from the listed modules
|
| 24 |
+
|
| 25 |
+
Additional arguments are used to filter the tests to run. Only files that have
|
| 26 |
+
one of the arguments in their name are executed.
|
| 27 |
+
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
import sys, os, traceback
|
| 31 |
+
|
| 32 |
+
profile = False
|
| 33 |
+
if "-profile" in sys.argv:
|
| 34 |
+
sys.argv.remove('-profile')
|
| 35 |
+
profile = True
|
| 36 |
+
|
| 37 |
+
coverage = False
|
| 38 |
+
if "-coverage" in sys.argv:
|
| 39 |
+
sys.argv.remove('-coverage')
|
| 40 |
+
coverage = True
|
| 41 |
+
|
| 42 |
+
if "-nogmpy" in sys.argv:
|
| 43 |
+
sys.argv.remove('-nogmpy')
|
| 44 |
+
os.environ['MPMATH_NOGMPY'] = 'Y'
|
| 45 |
+
|
| 46 |
+
if "-strict" in sys.argv:
|
| 47 |
+
sys.argv.remove('-strict')
|
| 48 |
+
os.environ['MPMATH_STRICT'] = 'Y'
|
| 49 |
+
|
| 50 |
+
if "-local" in sys.argv:
|
| 51 |
+
sys.argv.remove('-local')
|
| 52 |
+
importdir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),
|
| 53 |
+
'../..'))
|
| 54 |
+
else:
|
| 55 |
+
importdir = ''
|
| 56 |
+
|
| 57 |
+
# TODO: add a flag for this
|
| 58 |
+
testdir = ''
|
| 59 |
+
|
| 60 |
+
def testit(importdir='', testdir='', exit_on_fail=False):
|
| 61 |
+
"""Run all tests in testdir while importing from importdir."""
|
| 62 |
+
if importdir:
|
| 63 |
+
sys.path.insert(1, importdir)
|
| 64 |
+
if testdir:
|
| 65 |
+
sys.path.insert(1, testdir)
|
| 66 |
+
import os.path
|
| 67 |
+
import mpmath
|
| 68 |
+
print("mpmath imported from %s" % os.path.dirname(mpmath.__file__))
|
| 69 |
+
print("mpmath backend: %s" % mpmath.libmp.backend.BACKEND)
|
| 70 |
+
print("mpmath mp class: %s" % repr(mpmath.mp))
|
| 71 |
+
print("mpmath version: %s" % mpmath.__version__)
|
| 72 |
+
print("Python version: %s" % sys.version)
|
| 73 |
+
print("")
|
| 74 |
+
if "-py" in sys.argv:
|
| 75 |
+
sys.argv.remove('-py')
|
| 76 |
+
import py
|
| 77 |
+
py.test.cmdline.main()
|
| 78 |
+
else:
|
| 79 |
+
import glob
|
| 80 |
+
from timeit import default_timer as clock
|
| 81 |
+
modules = []
|
| 82 |
+
args = sys.argv[1:]
|
| 83 |
+
excluded = []
|
| 84 |
+
if '-skip' in args:
|
| 85 |
+
excluded = args[args.index('-skip')+1:]
|
| 86 |
+
args = args[:args.index('-skip')]
|
| 87 |
+
# search for tests in directory of this file if not otherwise specified
|
| 88 |
+
if not testdir:
|
| 89 |
+
pattern = os.path.dirname(sys.argv[0])
|
| 90 |
+
else:
|
| 91 |
+
pattern = testdir
|
| 92 |
+
if pattern:
|
| 93 |
+
pattern += '/'
|
| 94 |
+
pattern += 'test*.py'
|
| 95 |
+
# look for tests (respecting specified filter)
|
| 96 |
+
for f in glob.glob(pattern):
|
| 97 |
+
name = os.path.splitext(os.path.basename(f))[0]
|
| 98 |
+
# If run as a script, only run tests given as args, if any are given
|
| 99 |
+
if args and __name__ == "__main__":
|
| 100 |
+
ok = False
|
| 101 |
+
for arg in args:
|
| 102 |
+
if arg in name:
|
| 103 |
+
ok = True
|
| 104 |
+
break
|
| 105 |
+
if not ok:
|
| 106 |
+
continue
|
| 107 |
+
elif name in excluded:
|
| 108 |
+
continue
|
| 109 |
+
module = __import__(name)
|
| 110 |
+
priority = module.__dict__.get('priority', 100)
|
| 111 |
+
if priority == 666:
|
| 112 |
+
modules = [[priority, name, module]]
|
| 113 |
+
break
|
| 114 |
+
modules.append([priority, name, module])
|
| 115 |
+
# execute tests
|
| 116 |
+
modules.sort()
|
| 117 |
+
tstart = clock()
|
| 118 |
+
for priority, name, module in modules:
|
| 119 |
+
print(name)
|
| 120 |
+
for f in sorted(module.__dict__.keys()):
|
| 121 |
+
if f.startswith('test_'):
|
| 122 |
+
if coverage and ('numpy' in f):
|
| 123 |
+
continue
|
| 124 |
+
sys.stdout.write(" " + f[5:].ljust(25) + " ")
|
| 125 |
+
t1 = clock()
|
| 126 |
+
try:
|
| 127 |
+
module.__dict__[f]()
|
| 128 |
+
except:
|
| 129 |
+
etype, evalue, trb = sys.exc_info()
|
| 130 |
+
if etype in (KeyboardInterrupt, SystemExit):
|
| 131 |
+
raise
|
| 132 |
+
print("")
|
| 133 |
+
print("TEST FAILED!")
|
| 134 |
+
print("")
|
| 135 |
+
traceback.print_exc()
|
| 136 |
+
if exit_on_fail:
|
| 137 |
+
return
|
| 138 |
+
t2 = clock()
|
| 139 |
+
print("ok " + " " + ("%.7f" % (t2-t1)) + " s")
|
| 140 |
+
tend = clock()
|
| 141 |
+
print("")
|
| 142 |
+
print("finished tests in " + ("%.2f" % (tend-tstart)) + " seconds")
|
| 143 |
+
# clean sys.path
|
| 144 |
+
if importdir:
|
| 145 |
+
sys.path.remove(importdir)
|
| 146 |
+
if testdir:
|
| 147 |
+
sys.path.remove(testdir)
|
| 148 |
+
|
| 149 |
+
if __name__ == '__main__':
|
| 150 |
+
if profile:
|
| 151 |
+
import cProfile
|
| 152 |
+
cProfile.run("testit('%s', '%s')" % (importdir, testdir), sort=1)
|
| 153 |
+
elif coverage:
|
| 154 |
+
import trace
|
| 155 |
+
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
|
| 156 |
+
trace=0, count=1)
|
| 157 |
+
tracer.run('testit(importdir, testdir)')
|
| 158 |
+
r = tracer.results()
|
| 159 |
+
r.write_results(show_missing=True, summary=True, coverdir="/tmp")
|
| 160 |
+
else:
|
| 161 |
+
testit(importdir, testdir)
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_basic_ops.py
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import mpmath
|
| 2 |
+
from mpmath import *
|
| 3 |
+
from mpmath.libmp import *
|
| 4 |
+
import random
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
long = long
|
| 9 |
+
except NameError:
|
| 10 |
+
long = int
|
| 11 |
+
|
| 12 |
+
def test_type_compare():
|
| 13 |
+
assert mpf(2) == mpc(2,0)
|
| 14 |
+
assert mpf(0) == mpc(0)
|
| 15 |
+
assert mpf(2) != mpc(2, 0.00001)
|
| 16 |
+
assert mpf(2) == 2.0
|
| 17 |
+
assert mpf(2) != 3.0
|
| 18 |
+
assert mpf(2) == 2
|
| 19 |
+
assert mpf(2) != '2.0'
|
| 20 |
+
assert mpc(2) != '2.0'
|
| 21 |
+
|
| 22 |
+
def test_add():
|
| 23 |
+
assert mpf(2.5) + mpf(3) == 5.5
|
| 24 |
+
assert mpf(2.5) + 3 == 5.5
|
| 25 |
+
assert mpf(2.5) + 3.0 == 5.5
|
| 26 |
+
assert 3 + mpf(2.5) == 5.5
|
| 27 |
+
assert 3.0 + mpf(2.5) == 5.5
|
| 28 |
+
assert (3+0j) + mpf(2.5) == 5.5
|
| 29 |
+
assert mpc(2.5) + mpf(3) == 5.5
|
| 30 |
+
assert mpc(2.5) + 3 == 5.5
|
| 31 |
+
assert mpc(2.5) + 3.0 == 5.5
|
| 32 |
+
assert mpc(2.5) + (3+0j) == 5.5
|
| 33 |
+
assert 3 + mpc(2.5) == 5.5
|
| 34 |
+
assert 3.0 + mpc(2.5) == 5.5
|
| 35 |
+
assert (3+0j) + mpc(2.5) == 5.5
|
| 36 |
+
|
| 37 |
+
def test_sub():
|
| 38 |
+
assert mpf(2.5) - mpf(3) == -0.5
|
| 39 |
+
assert mpf(2.5) - 3 == -0.5
|
| 40 |
+
assert mpf(2.5) - 3.0 == -0.5
|
| 41 |
+
assert 3 - mpf(2.5) == 0.5
|
| 42 |
+
assert 3.0 - mpf(2.5) == 0.5
|
| 43 |
+
assert (3+0j) - mpf(2.5) == 0.5
|
| 44 |
+
assert mpc(2.5) - mpf(3) == -0.5
|
| 45 |
+
assert mpc(2.5) - 3 == -0.5
|
| 46 |
+
assert mpc(2.5) - 3.0 == -0.5
|
| 47 |
+
assert mpc(2.5) - (3+0j) == -0.5
|
| 48 |
+
assert 3 - mpc(2.5) == 0.5
|
| 49 |
+
assert 3.0 - mpc(2.5) == 0.5
|
| 50 |
+
assert (3+0j) - mpc(2.5) == 0.5
|
| 51 |
+
|
| 52 |
+
def test_mul():
|
| 53 |
+
assert mpf(2.5) * mpf(3) == 7.5
|
| 54 |
+
assert mpf(2.5) * 3 == 7.5
|
| 55 |
+
assert mpf(2.5) * 3.0 == 7.5
|
| 56 |
+
assert 3 * mpf(2.5) == 7.5
|
| 57 |
+
assert 3.0 * mpf(2.5) == 7.5
|
| 58 |
+
assert (3+0j) * mpf(2.5) == 7.5
|
| 59 |
+
assert mpc(2.5) * mpf(3) == 7.5
|
| 60 |
+
assert mpc(2.5) * 3 == 7.5
|
| 61 |
+
assert mpc(2.5) * 3.0 == 7.5
|
| 62 |
+
assert mpc(2.5) * (3+0j) == 7.5
|
| 63 |
+
assert 3 * mpc(2.5) == 7.5
|
| 64 |
+
assert 3.0 * mpc(2.5) == 7.5
|
| 65 |
+
assert (3+0j) * mpc(2.5) == 7.5
|
| 66 |
+
|
| 67 |
+
def test_div():
|
| 68 |
+
assert mpf(6) / mpf(3) == 2.0
|
| 69 |
+
assert mpf(6) / 3 == 2.0
|
| 70 |
+
assert mpf(6) / 3.0 == 2.0
|
| 71 |
+
assert 6 / mpf(3) == 2.0
|
| 72 |
+
assert 6.0 / mpf(3) == 2.0
|
| 73 |
+
assert (6+0j) / mpf(3.0) == 2.0
|
| 74 |
+
assert mpc(6) / mpf(3) == 2.0
|
| 75 |
+
assert mpc(6) / 3 == 2.0
|
| 76 |
+
assert mpc(6) / 3.0 == 2.0
|
| 77 |
+
assert mpc(6) / (3+0j) == 2.0
|
| 78 |
+
assert 6 / mpc(3) == 2.0
|
| 79 |
+
assert 6.0 / mpc(3) == 2.0
|
| 80 |
+
assert (6+0j) / mpc(3) == 2.0
|
| 81 |
+
|
| 82 |
+
def test_pow():
|
| 83 |
+
assert mpf(6) ** mpf(3) == 216.0
|
| 84 |
+
assert mpf(6) ** 3 == 216.0
|
| 85 |
+
assert mpf(6) ** 3.0 == 216.0
|
| 86 |
+
assert 6 ** mpf(3) == 216.0
|
| 87 |
+
assert 6.0 ** mpf(3) == 216.0
|
| 88 |
+
assert (6+0j) ** mpf(3.0) == 216.0
|
| 89 |
+
assert mpc(6) ** mpf(3) == 216.0
|
| 90 |
+
assert mpc(6) ** 3 == 216.0
|
| 91 |
+
assert mpc(6) ** 3.0 == 216.0
|
| 92 |
+
assert mpc(6) ** (3+0j) == 216.0
|
| 93 |
+
assert 6 ** mpc(3) == 216.0
|
| 94 |
+
assert 6.0 ** mpc(3) == 216.0
|
| 95 |
+
assert (6+0j) ** mpc(3) == 216.0
|
| 96 |
+
|
| 97 |
+
def test_mixed_misc():
|
| 98 |
+
assert 1 + mpf(3) == mpf(3) + 1 == 4
|
| 99 |
+
assert 1 - mpf(3) == -(mpf(3) - 1) == -2
|
| 100 |
+
assert 3 * mpf(2) == mpf(2) * 3 == 6
|
| 101 |
+
assert 6 / mpf(2) == mpf(6) / 2 == 3
|
| 102 |
+
assert 1.0 + mpf(3) == mpf(3) + 1.0 == 4
|
| 103 |
+
assert 1.0 - mpf(3) == -(mpf(3) - 1.0) == -2
|
| 104 |
+
assert 3.0 * mpf(2) == mpf(2) * 3.0 == 6
|
| 105 |
+
assert 6.0 / mpf(2) == mpf(6) / 2.0 == 3
|
| 106 |
+
|
| 107 |
+
def test_add_misc():
|
| 108 |
+
mp.dps = 15
|
| 109 |
+
assert mpf(4) + mpf(-70) == -66
|
| 110 |
+
assert mpf(1) + mpf(1.1)/80 == 1 + 1.1/80
|
| 111 |
+
assert mpf((1, 10000000000)) + mpf(3) == mpf((1, 10000000000))
|
| 112 |
+
assert mpf(3) + mpf((1, 10000000000)) == mpf((1, 10000000000))
|
| 113 |
+
assert mpf((1, -10000000000)) + mpf(3) == mpf(3)
|
| 114 |
+
assert mpf(3) + mpf((1, -10000000000)) == mpf(3)
|
| 115 |
+
assert mpf(1) + 1e-15 != 1
|
| 116 |
+
assert mpf(1) + 1e-20 == 1
|
| 117 |
+
assert mpf(1.07e-22) + 0 == mpf(1.07e-22)
|
| 118 |
+
assert mpf(0) + mpf(1.07e-22) == mpf(1.07e-22)
|
| 119 |
+
|
| 120 |
+
def test_complex_misc():
|
| 121 |
+
# many more tests needed
|
| 122 |
+
assert 1 + mpc(2) == 3
|
| 123 |
+
assert not mpc(2).ae(2 + 1e-13)
|
| 124 |
+
assert mpc(2+1e-15j).ae(2)
|
| 125 |
+
|
| 126 |
+
def test_complex_zeros():
|
| 127 |
+
for a in [0,2]:
|
| 128 |
+
for b in [0,3]:
|
| 129 |
+
for c in [0,4]:
|
| 130 |
+
for d in [0,5]:
|
| 131 |
+
assert mpc(a,b)*mpc(c,d) == complex(a,b)*complex(c,d)
|
| 132 |
+
|
| 133 |
+
def test_hash():
|
| 134 |
+
for i in range(-256, 256):
|
| 135 |
+
assert hash(mpf(i)) == hash(i)
|
| 136 |
+
assert hash(mpf(0.5)) == hash(0.5)
|
| 137 |
+
assert hash(mpc(2,3)) == hash(2+3j)
|
| 138 |
+
# Check that this doesn't fail
|
| 139 |
+
assert hash(inf)
|
| 140 |
+
# Check that overflow doesn't assign equal hashes to large numbers
|
| 141 |
+
assert hash(mpf('1e1000')) != hash('1e10000')
|
| 142 |
+
assert hash(mpc(100,'1e1000')) != hash(mpc(200,'1e1000'))
|
| 143 |
+
from mpmath.rational import mpq
|
| 144 |
+
assert hash(mp.mpq(1,3))
|
| 145 |
+
assert hash(mp.mpq(0,1)) == 0
|
| 146 |
+
assert hash(mp.mpq(-1,1)) == hash(-1)
|
| 147 |
+
assert hash(mp.mpq(1,1)) == hash(1)
|
| 148 |
+
assert hash(mp.mpq(5,1)) == hash(5)
|
| 149 |
+
assert hash(mp.mpq(1,2)) == hash(0.5)
|
| 150 |
+
if sys.version_info >= (3, 2):
|
| 151 |
+
assert hash(mpf(1)*2**2000) == hash(2**2000)
|
| 152 |
+
assert hash(mpf(1)/2**2000) == hash(mpq(1,2**2000))
|
| 153 |
+
|
| 154 |
+
# Advanced rounding test
|
| 155 |
+
def test_add_rounding():
|
| 156 |
+
mp.dps = 15
|
| 157 |
+
a = from_float(1e-50)
|
| 158 |
+
assert mpf_sub(mpf_add(fone, a, 53, round_up), fone, 53, round_up) == from_float(2.2204460492503131e-16)
|
| 159 |
+
assert mpf_sub(fone, a, 53, round_up) == fone
|
| 160 |
+
assert mpf_sub(fone, mpf_sub(fone, a, 53, round_down), 53, round_down) == from_float(1.1102230246251565e-16)
|
| 161 |
+
assert mpf_add(fone, a, 53, round_down) == fone
|
| 162 |
+
|
| 163 |
+
def test_almost_equal():
|
| 164 |
+
assert mpf(1.2).ae(mpf(1.20000001), 1e-7)
|
| 165 |
+
assert not mpf(1.2).ae(mpf(1.20000001), 1e-9)
|
| 166 |
+
assert not mpf(-0.7818314824680298).ae(mpf(-0.774695868667929))
|
| 167 |
+
|
| 168 |
+
def test_arithmetic_functions():
|
| 169 |
+
import operator
|
| 170 |
+
ops = [(operator.add, fadd), (operator.sub, fsub), (operator.mul, fmul),
|
| 171 |
+
(operator.truediv, fdiv)]
|
| 172 |
+
a = mpf(0.27)
|
| 173 |
+
b = mpf(1.13)
|
| 174 |
+
c = mpc(0.51+2.16j)
|
| 175 |
+
d = mpc(1.08-0.99j)
|
| 176 |
+
for x in [a,b,c,d]:
|
| 177 |
+
for y in [a,b,c,d]:
|
| 178 |
+
for op, fop in ops:
|
| 179 |
+
if fop is not fdiv:
|
| 180 |
+
mp.prec = 200
|
| 181 |
+
z0 = op(x,y)
|
| 182 |
+
mp.prec = 60
|
| 183 |
+
z1 = op(x,y)
|
| 184 |
+
mp.prec = 53
|
| 185 |
+
z2 = op(x,y)
|
| 186 |
+
assert fop(x, y, prec=60) == z1
|
| 187 |
+
assert fop(x, y) == z2
|
| 188 |
+
if fop is not fdiv:
|
| 189 |
+
assert fop(x, y, prec=inf) == z0
|
| 190 |
+
assert fop(x, y, dps=inf) == z0
|
| 191 |
+
assert fop(x, y, exact=True) == z0
|
| 192 |
+
assert fneg(fneg(z1, exact=True), prec=inf) == z1
|
| 193 |
+
assert fneg(z1) == -(+z1)
|
| 194 |
+
mp.dps = 15
|
| 195 |
+
|
| 196 |
+
def test_exact_integer_arithmetic():
|
| 197 |
+
# XXX: re-fix this so that all operations are tested with all rounding modes
|
| 198 |
+
random.seed(0)
|
| 199 |
+
for prec in [6, 10, 25, 40, 100, 250, 725]:
|
| 200 |
+
for rounding in ['d', 'u', 'f', 'c', 'n']:
|
| 201 |
+
mp.dps = prec
|
| 202 |
+
M = 10**(prec-2)
|
| 203 |
+
M2 = 10**(prec//2-2)
|
| 204 |
+
for i in range(10):
|
| 205 |
+
a = random.randint(-M, M)
|
| 206 |
+
b = random.randint(-M, M)
|
| 207 |
+
assert mpf(a, rounding=rounding) == a
|
| 208 |
+
assert int(mpf(a, rounding=rounding)) == a
|
| 209 |
+
assert int(mpf(str(a), rounding=rounding)) == a
|
| 210 |
+
assert mpf(a) + mpf(b) == a + b
|
| 211 |
+
assert mpf(a) - mpf(b) == a - b
|
| 212 |
+
assert -mpf(a) == -a
|
| 213 |
+
a = random.randint(-M2, M2)
|
| 214 |
+
b = random.randint(-M2, M2)
|
| 215 |
+
assert mpf(a) * mpf(b) == a*b
|
| 216 |
+
assert mpf_mul(from_int(a), from_int(b), mp.prec, rounding) == from_int(a*b)
|
| 217 |
+
mp.dps = 15
|
| 218 |
+
|
| 219 |
+
def test_odd_int_bug():
|
| 220 |
+
assert to_int(from_int(3), round_nearest) == 3
|
| 221 |
+
|
| 222 |
+
def test_str_1000_digits():
|
| 223 |
+
mp.dps = 1001
|
| 224 |
+
# last digit may be wrong
|
| 225 |
+
assert str(mpf(2)**0.5)[-10:-1] == '9518488472'[:9]
|
| 226 |
+
assert str(pi)[-10:-1] == '2164201989'[:9]
|
| 227 |
+
mp.dps = 15
|
| 228 |
+
|
| 229 |
+
def test_str_10000_digits():
|
| 230 |
+
mp.dps = 10001
|
| 231 |
+
# last digit may be wrong
|
| 232 |
+
assert str(mpf(2)**0.5)[-10:-1] == '5873258351'[:9]
|
| 233 |
+
assert str(pi)[-10:-1] == '5256375678'[:9]
|
| 234 |
+
mp.dps = 15
|
| 235 |
+
|
| 236 |
+
def test_monitor():
|
| 237 |
+
f = lambda x: x**2
|
| 238 |
+
a = []
|
| 239 |
+
b = []
|
| 240 |
+
g = monitor(f, a.append, b.append)
|
| 241 |
+
assert g(3) == 9
|
| 242 |
+
assert g(4) == 16
|
| 243 |
+
assert a[0] == ((3,), {})
|
| 244 |
+
assert b[0] == 9
|
| 245 |
+
|
| 246 |
+
def test_nint_distance():
|
| 247 |
+
assert nint_distance(mpf(-3)) == (-3, -inf)
|
| 248 |
+
assert nint_distance(mpc(-3)) == (-3, -inf)
|
| 249 |
+
assert nint_distance(mpf(-3.1)) == (-3, -3)
|
| 250 |
+
assert nint_distance(mpf(-3.01)) == (-3, -6)
|
| 251 |
+
assert nint_distance(mpf(-3.001)) == (-3, -9)
|
| 252 |
+
assert nint_distance(mpf(-3.0001)) == (-3, -13)
|
| 253 |
+
assert nint_distance(mpf(-2.9)) == (-3, -3)
|
| 254 |
+
assert nint_distance(mpf(-2.99)) == (-3, -6)
|
| 255 |
+
assert nint_distance(mpf(-2.999)) == (-3, -9)
|
| 256 |
+
assert nint_distance(mpf(-2.9999)) == (-3, -13)
|
| 257 |
+
assert nint_distance(mpc(-3+0.1j)) == (-3, -3)
|
| 258 |
+
assert nint_distance(mpc(-3+0.01j)) == (-3, -6)
|
| 259 |
+
assert nint_distance(mpc(-3.1+0.1j)) == (-3, -3)
|
| 260 |
+
assert nint_distance(mpc(-3.01+0.01j)) == (-3, -6)
|
| 261 |
+
assert nint_distance(mpc(-3.001+0.001j)) == (-3, -9)
|
| 262 |
+
assert nint_distance(mpf(0)) == (0, -inf)
|
| 263 |
+
assert nint_distance(mpf(0.01)) == (0, -6)
|
| 264 |
+
assert nint_distance(mpf('1e-100')) == (0, -332)
|
| 265 |
+
|
| 266 |
+
def test_floor_ceil_nint_frac():
|
| 267 |
+
mp.dps = 15
|
| 268 |
+
for n in range(-10,10):
|
| 269 |
+
assert floor(n) == n
|
| 270 |
+
assert floor(n+0.5) == n
|
| 271 |
+
assert ceil(n) == n
|
| 272 |
+
assert ceil(n+0.5) == n+1
|
| 273 |
+
assert nint(n) == n
|
| 274 |
+
# nint rounds to even
|
| 275 |
+
if n % 2 == 1:
|
| 276 |
+
assert nint(n+0.5) == n+1
|
| 277 |
+
else:
|
| 278 |
+
assert nint(n+0.5) == n
|
| 279 |
+
assert floor(inf) == inf
|
| 280 |
+
assert floor(ninf) == ninf
|
| 281 |
+
assert isnan(floor(nan))
|
| 282 |
+
assert ceil(inf) == inf
|
| 283 |
+
assert ceil(ninf) == ninf
|
| 284 |
+
assert isnan(ceil(nan))
|
| 285 |
+
assert nint(inf) == inf
|
| 286 |
+
assert nint(ninf) == ninf
|
| 287 |
+
assert isnan(nint(nan))
|
| 288 |
+
assert floor(0.1) == 0
|
| 289 |
+
assert floor(0.9) == 0
|
| 290 |
+
assert floor(-0.1) == -1
|
| 291 |
+
assert floor(-0.9) == -1
|
| 292 |
+
assert floor(10000000000.1) == 10000000000
|
| 293 |
+
assert floor(10000000000.9) == 10000000000
|
| 294 |
+
assert floor(-10000000000.1) == -10000000000-1
|
| 295 |
+
assert floor(-10000000000.9) == -10000000000-1
|
| 296 |
+
assert floor(1e-100) == 0
|
| 297 |
+
assert floor(-1e-100) == -1
|
| 298 |
+
assert floor(1e100) == 1e100
|
| 299 |
+
assert floor(-1e100) == -1e100
|
| 300 |
+
assert ceil(0.1) == 1
|
| 301 |
+
assert ceil(0.9) == 1
|
| 302 |
+
assert ceil(-0.1) == 0
|
| 303 |
+
assert ceil(-0.9) == 0
|
| 304 |
+
assert ceil(10000000000.1) == 10000000000+1
|
| 305 |
+
assert ceil(10000000000.9) == 10000000000+1
|
| 306 |
+
assert ceil(-10000000000.1) == -10000000000
|
| 307 |
+
assert ceil(-10000000000.9) == -10000000000
|
| 308 |
+
assert ceil(1e-100) == 1
|
| 309 |
+
assert ceil(-1e-100) == 0
|
| 310 |
+
assert ceil(1e100) == 1e100
|
| 311 |
+
assert ceil(-1e100) == -1e100
|
| 312 |
+
assert nint(0.1) == 0
|
| 313 |
+
assert nint(0.9) == 1
|
| 314 |
+
assert nint(-0.1) == 0
|
| 315 |
+
assert nint(-0.9) == -1
|
| 316 |
+
assert nint(10000000000.1) == 10000000000
|
| 317 |
+
assert nint(10000000000.9) == 10000000000+1
|
| 318 |
+
assert nint(-10000000000.1) == -10000000000
|
| 319 |
+
assert nint(-10000000000.9) == -10000000000-1
|
| 320 |
+
assert nint(1e-100) == 0
|
| 321 |
+
assert nint(-1e-100) == 0
|
| 322 |
+
assert nint(1e100) == 1e100
|
| 323 |
+
assert nint(-1e100) == -1e100
|
| 324 |
+
assert floor(3.2+4.6j) == 3+4j
|
| 325 |
+
assert ceil(3.2+4.6j) == 4+5j
|
| 326 |
+
assert nint(3.2+4.6j) == 3+5j
|
| 327 |
+
for n in range(-10,10):
|
| 328 |
+
assert frac(n) == 0
|
| 329 |
+
assert frac(0.25) == 0.25
|
| 330 |
+
assert frac(1.25) == 0.25
|
| 331 |
+
assert frac(2.25) == 0.25
|
| 332 |
+
assert frac(-0.25) == 0.75
|
| 333 |
+
assert frac(-1.25) == 0.75
|
| 334 |
+
assert frac(-2.25) == 0.75
|
| 335 |
+
assert frac('1e100000000000000') == 0
|
| 336 |
+
u = mpf('1e-100000000000000')
|
| 337 |
+
assert frac(u) == u
|
| 338 |
+
assert frac(-u) == 1 # rounding!
|
| 339 |
+
u = mpf('1e-400')
|
| 340 |
+
assert frac(-u, prec=0) == fsub(1, u, exact=True)
|
| 341 |
+
assert frac(3.25+4.75j) == 0.25+0.75j
|
| 342 |
+
|
| 343 |
+
def test_isnan_etc():
|
| 344 |
+
from mpmath.rational import mpq
|
| 345 |
+
assert isnan(nan) == True
|
| 346 |
+
assert isnan(3) == False
|
| 347 |
+
assert isnan(mpf(3)) == False
|
| 348 |
+
assert isnan(inf) == False
|
| 349 |
+
assert isnan(mpc(2,nan)) == True
|
| 350 |
+
assert isnan(mpc(2,nan)) == True
|
| 351 |
+
assert isnan(mpc(nan,nan)) == True
|
| 352 |
+
assert isnan(mpc(2,2)) == False
|
| 353 |
+
assert isnan(mpc(nan,inf)) == True
|
| 354 |
+
assert isnan(mpc(inf,inf)) == False
|
| 355 |
+
assert isnan(mpq((3,2))) == False
|
| 356 |
+
assert isnan(mpq((0,1))) == False
|
| 357 |
+
assert isinf(inf) == True
|
| 358 |
+
assert isinf(-inf) == True
|
| 359 |
+
assert isinf(3) == False
|
| 360 |
+
assert isinf(nan) == False
|
| 361 |
+
assert isinf(3+4j) == False
|
| 362 |
+
assert isinf(mpc(inf)) == True
|
| 363 |
+
assert isinf(mpc(3,inf)) == True
|
| 364 |
+
assert isinf(mpc(inf,3)) == True
|
| 365 |
+
assert isinf(mpc(inf,inf)) == True
|
| 366 |
+
assert isinf(mpc(nan,inf)) == True
|
| 367 |
+
assert isinf(mpc(inf,nan)) == True
|
| 368 |
+
assert isinf(mpc(nan,nan)) == False
|
| 369 |
+
assert isinf(mpq((3,2))) == False
|
| 370 |
+
assert isinf(mpq((0,1))) == False
|
| 371 |
+
assert isnormal(3) == True
|
| 372 |
+
assert isnormal(3.5) == True
|
| 373 |
+
assert isnormal(mpf(3.5)) == True
|
| 374 |
+
assert isnormal(0) == False
|
| 375 |
+
assert isnormal(mpf(0)) == False
|
| 376 |
+
assert isnormal(0.0) == False
|
| 377 |
+
assert isnormal(inf) == False
|
| 378 |
+
assert isnormal(-inf) == False
|
| 379 |
+
assert isnormal(nan) == False
|
| 380 |
+
assert isnormal(float(inf)) == False
|
| 381 |
+
assert isnormal(mpc(0,0)) == False
|
| 382 |
+
assert isnormal(mpc(3,0)) == True
|
| 383 |
+
assert isnormal(mpc(0,3)) == True
|
| 384 |
+
assert isnormal(mpc(3,3)) == True
|
| 385 |
+
assert isnormal(mpc(0,nan)) == False
|
| 386 |
+
assert isnormal(mpc(0,inf)) == False
|
| 387 |
+
assert isnormal(mpc(3,nan)) == False
|
| 388 |
+
assert isnormal(mpc(3,inf)) == False
|
| 389 |
+
assert isnormal(mpc(3,-inf)) == False
|
| 390 |
+
assert isnormal(mpc(nan,0)) == False
|
| 391 |
+
assert isnormal(mpc(inf,0)) == False
|
| 392 |
+
assert isnormal(mpc(nan,3)) == False
|
| 393 |
+
assert isnormal(mpc(inf,3)) == False
|
| 394 |
+
assert isnormal(mpc(inf,nan)) == False
|
| 395 |
+
assert isnormal(mpc(nan,inf)) == False
|
| 396 |
+
assert isnormal(mpc(nan,nan)) == False
|
| 397 |
+
assert isnormal(mpc(inf,inf)) == False
|
| 398 |
+
assert isnormal(mpq((3,2))) == True
|
| 399 |
+
assert isnormal(mpq((0,1))) == False
|
| 400 |
+
assert isint(3) == True
|
| 401 |
+
assert isint(0) == True
|
| 402 |
+
assert isint(long(3)) == True
|
| 403 |
+
assert isint(long(0)) == True
|
| 404 |
+
assert isint(mpf(3)) == True
|
| 405 |
+
assert isint(mpf(0)) == True
|
| 406 |
+
assert isint(mpf(-3)) == True
|
| 407 |
+
assert isint(mpf(3.2)) == False
|
| 408 |
+
assert isint(3.2) == False
|
| 409 |
+
assert isint(nan) == False
|
| 410 |
+
assert isint(inf) == False
|
| 411 |
+
assert isint(-inf) == False
|
| 412 |
+
assert isint(mpc(0)) == True
|
| 413 |
+
assert isint(mpc(3)) == True
|
| 414 |
+
assert isint(mpc(3.2)) == False
|
| 415 |
+
assert isint(mpc(3,inf)) == False
|
| 416 |
+
assert isint(mpc(inf)) == False
|
| 417 |
+
assert isint(mpc(3,2)) == False
|
| 418 |
+
assert isint(mpc(0,2)) == False
|
| 419 |
+
assert isint(mpc(3,2),gaussian=True) == True
|
| 420 |
+
assert isint(mpc(3,0),gaussian=True) == True
|
| 421 |
+
assert isint(mpc(0,3),gaussian=True) == True
|
| 422 |
+
assert isint(3+4j) == False
|
| 423 |
+
assert isint(3+4j, gaussian=True) == True
|
| 424 |
+
assert isint(3+0j) == True
|
| 425 |
+
assert isint(mpq((3,2))) == False
|
| 426 |
+
assert isint(mpq((3,9))) == False
|
| 427 |
+
assert isint(mpq((9,3))) == True
|
| 428 |
+
assert isint(mpq((0,4))) == True
|
| 429 |
+
assert isint(mpq((1,1))) == True
|
| 430 |
+
assert isint(mpq((-1,1))) == True
|
| 431 |
+
assert mp.isnpint(0) == True
|
| 432 |
+
assert mp.isnpint(1) == False
|
| 433 |
+
assert mp.isnpint(-1) == True
|
| 434 |
+
assert mp.isnpint(-1.1) == False
|
| 435 |
+
assert mp.isnpint(-1.0) == True
|
| 436 |
+
assert mp.isnpint(mp.mpq(1,2)) == False
|
| 437 |
+
assert mp.isnpint(mp.mpq(-1,2)) == False
|
| 438 |
+
assert mp.isnpint(mp.mpq(-3,1)) == True
|
| 439 |
+
assert mp.isnpint(mp.mpq(0,1)) == True
|
| 440 |
+
assert mp.isnpint(mp.mpq(1,1)) == False
|
| 441 |
+
assert mp.isnpint(0+0j) == True
|
| 442 |
+
assert mp.isnpint(-1+0j) == True
|
| 443 |
+
assert mp.isnpint(-1.1+0j) == False
|
| 444 |
+
assert mp.isnpint(-1+0.1j) == False
|
| 445 |
+
assert mp.isnpint(0+0.1j) == False
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
def test_issue_438():
|
| 449 |
+
assert mpf(finf) == mpf('inf')
|
| 450 |
+
assert mpf(fninf) == mpf('-inf')
|
| 451 |
+
assert mpf(fnan)._mpf_ == mpf('nan')._mpf_
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_bitwise.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test bit-level integer and mpf operations
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from mpmath import *
|
| 6 |
+
from mpmath.libmp import *
|
| 7 |
+
|
| 8 |
+
def test_bitcount():
|
| 9 |
+
assert bitcount(0) == 0
|
| 10 |
+
assert bitcount(1) == 1
|
| 11 |
+
assert bitcount(7) == 3
|
| 12 |
+
assert bitcount(8) == 4
|
| 13 |
+
assert bitcount(2**100) == 101
|
| 14 |
+
assert bitcount(2**100-1) == 100
|
| 15 |
+
|
| 16 |
+
def test_trailing():
|
| 17 |
+
assert trailing(0) == 0
|
| 18 |
+
assert trailing(1) == 0
|
| 19 |
+
assert trailing(2) == 1
|
| 20 |
+
assert trailing(7) == 0
|
| 21 |
+
assert trailing(8) == 3
|
| 22 |
+
assert trailing(2**100) == 100
|
| 23 |
+
assert trailing(2**100-1) == 0
|
| 24 |
+
|
| 25 |
+
def test_round_down():
|
| 26 |
+
assert from_man_exp(0, -4, 4, round_down)[:3] == (0, 0, 0)
|
| 27 |
+
assert from_man_exp(0xf0, -4, 4, round_down)[:3] == (0, 15, 0)
|
| 28 |
+
assert from_man_exp(0xf1, -4, 4, round_down)[:3] == (0, 15, 0)
|
| 29 |
+
assert from_man_exp(0xff, -4, 4, round_down)[:3] == (0, 15, 0)
|
| 30 |
+
assert from_man_exp(-0xf0, -4, 4, round_down)[:3] == (1, 15, 0)
|
| 31 |
+
assert from_man_exp(-0xf1, -4, 4, round_down)[:3] == (1, 15, 0)
|
| 32 |
+
assert from_man_exp(-0xff, -4, 4, round_down)[:3] == (1, 15, 0)
|
| 33 |
+
|
| 34 |
+
def test_round_up():
|
| 35 |
+
assert from_man_exp(0, -4, 4, round_up)[:3] == (0, 0, 0)
|
| 36 |
+
assert from_man_exp(0xf0, -4, 4, round_up)[:3] == (0, 15, 0)
|
| 37 |
+
assert from_man_exp(0xf1, -4, 4, round_up)[:3] == (0, 1, 4)
|
| 38 |
+
assert from_man_exp(0xff, -4, 4, round_up)[:3] == (0, 1, 4)
|
| 39 |
+
assert from_man_exp(-0xf0, -4, 4, round_up)[:3] == (1, 15, 0)
|
| 40 |
+
assert from_man_exp(-0xf1, -4, 4, round_up)[:3] == (1, 1, 4)
|
| 41 |
+
assert from_man_exp(-0xff, -4, 4, round_up)[:3] == (1, 1, 4)
|
| 42 |
+
|
| 43 |
+
def test_round_floor():
|
| 44 |
+
assert from_man_exp(0, -4, 4, round_floor)[:3] == (0, 0, 0)
|
| 45 |
+
assert from_man_exp(0xf0, -4, 4, round_floor)[:3] == (0, 15, 0)
|
| 46 |
+
assert from_man_exp(0xf1, -4, 4, round_floor)[:3] == (0, 15, 0)
|
| 47 |
+
assert from_man_exp(0xff, -4, 4, round_floor)[:3] == (0, 15, 0)
|
| 48 |
+
assert from_man_exp(-0xf0, -4, 4, round_floor)[:3] == (1, 15, 0)
|
| 49 |
+
assert from_man_exp(-0xf1, -4, 4, round_floor)[:3] == (1, 1, 4)
|
| 50 |
+
assert from_man_exp(-0xff, -4, 4, round_floor)[:3] == (1, 1, 4)
|
| 51 |
+
|
| 52 |
+
def test_round_ceiling():
|
| 53 |
+
assert from_man_exp(0, -4, 4, round_ceiling)[:3] == (0, 0, 0)
|
| 54 |
+
assert from_man_exp(0xf0, -4, 4, round_ceiling)[:3] == (0, 15, 0)
|
| 55 |
+
assert from_man_exp(0xf1, -4, 4, round_ceiling)[:3] == (0, 1, 4)
|
| 56 |
+
assert from_man_exp(0xff, -4, 4, round_ceiling)[:3] == (0, 1, 4)
|
| 57 |
+
assert from_man_exp(-0xf0, -4, 4, round_ceiling)[:3] == (1, 15, 0)
|
| 58 |
+
assert from_man_exp(-0xf1, -4, 4, round_ceiling)[:3] == (1, 15, 0)
|
| 59 |
+
assert from_man_exp(-0xff, -4, 4, round_ceiling)[:3] == (1, 15, 0)
|
| 60 |
+
|
| 61 |
+
def test_round_nearest():
|
| 62 |
+
assert from_man_exp(0, -4, 4, round_nearest)[:3] == (0, 0, 0)
|
| 63 |
+
assert from_man_exp(0xf0, -4, 4, round_nearest)[:3] == (0, 15, 0)
|
| 64 |
+
assert from_man_exp(0xf7, -4, 4, round_nearest)[:3] == (0, 15, 0)
|
| 65 |
+
assert from_man_exp(0xf8, -4, 4, round_nearest)[:3] == (0, 1, 4) # 1111.1000 -> 10000.0
|
| 66 |
+
assert from_man_exp(0xf9, -4, 4, round_nearest)[:3] == (0, 1, 4) # 1111.1001 -> 10000.0
|
| 67 |
+
assert from_man_exp(0xe8, -4, 4, round_nearest)[:3] == (0, 7, 1) # 1110.1000 -> 1110.0
|
| 68 |
+
assert from_man_exp(0xe9, -4, 4, round_nearest)[:3] == (0, 15, 0) # 1110.1001 -> 1111.0
|
| 69 |
+
assert from_man_exp(-0xf0, -4, 4, round_nearest)[:3] == (1, 15, 0)
|
| 70 |
+
assert from_man_exp(-0xf7, -4, 4, round_nearest)[:3] == (1, 15, 0)
|
| 71 |
+
assert from_man_exp(-0xf8, -4, 4, round_nearest)[:3] == (1, 1, 4)
|
| 72 |
+
assert from_man_exp(-0xf9, -4, 4, round_nearest)[:3] == (1, 1, 4)
|
| 73 |
+
assert from_man_exp(-0xe8, -4, 4, round_nearest)[:3] == (1, 7, 1)
|
| 74 |
+
assert from_man_exp(-0xe9, -4, 4, round_nearest)[:3] == (1, 15, 0)
|
| 75 |
+
|
| 76 |
+
def test_rounding_bugs():
|
| 77 |
+
# 1 less than power-of-two cases
|
| 78 |
+
assert from_man_exp(72057594037927935, -56, 53, round_up) == (0, 1, 0, 1)
|
| 79 |
+
assert from_man_exp(73786976294838205979, -65, 53, round_nearest) == (0, 1, 1, 1)
|
| 80 |
+
assert from_man_exp(31, 0, 4, round_up) == (0, 1, 5, 1)
|
| 81 |
+
assert from_man_exp(-31, 0, 4, round_floor) == (1, 1, 5, 1)
|
| 82 |
+
assert from_man_exp(255, 0, 7, round_up) == (0, 1, 8, 1)
|
| 83 |
+
assert from_man_exp(-255, 0, 7, round_floor) == (1, 1, 8, 1)
|
| 84 |
+
|
| 85 |
+
def test_rounding_issue_200():
|
| 86 |
+
a = from_man_exp(9867,-100)
|
| 87 |
+
b = from_man_exp(9867,-200)
|
| 88 |
+
c = from_man_exp(-1,0)
|
| 89 |
+
z = (1, 1023, -10, 10)
|
| 90 |
+
assert mpf_add(a, c, 10, 'd') == z
|
| 91 |
+
assert mpf_add(b, c, 10, 'd') == z
|
| 92 |
+
assert mpf_add(c, a, 10, 'd') == z
|
| 93 |
+
assert mpf_add(c, b, 10, 'd') == z
|
| 94 |
+
|
| 95 |
+
def test_perturb():
|
| 96 |
+
a = fone
|
| 97 |
+
b = from_float(0.99999999999999989)
|
| 98 |
+
c = from_float(1.0000000000000002)
|
| 99 |
+
assert mpf_perturb(a, 0, 53, round_nearest) == a
|
| 100 |
+
assert mpf_perturb(a, 1, 53, round_nearest) == a
|
| 101 |
+
assert mpf_perturb(a, 0, 53, round_up) == c
|
| 102 |
+
assert mpf_perturb(a, 0, 53, round_ceiling) == c
|
| 103 |
+
assert mpf_perturb(a, 0, 53, round_down) == a
|
| 104 |
+
assert mpf_perturb(a, 0, 53, round_floor) == a
|
| 105 |
+
assert mpf_perturb(a, 1, 53, round_up) == a
|
| 106 |
+
assert mpf_perturb(a, 1, 53, round_ceiling) == a
|
| 107 |
+
assert mpf_perturb(a, 1, 53, round_down) == b
|
| 108 |
+
assert mpf_perturb(a, 1, 53, round_floor) == b
|
| 109 |
+
a = mpf_neg(a)
|
| 110 |
+
b = mpf_neg(b)
|
| 111 |
+
c = mpf_neg(c)
|
| 112 |
+
assert mpf_perturb(a, 0, 53, round_nearest) == a
|
| 113 |
+
assert mpf_perturb(a, 1, 53, round_nearest) == a
|
| 114 |
+
assert mpf_perturb(a, 0, 53, round_up) == a
|
| 115 |
+
assert mpf_perturb(a, 0, 53, round_floor) == a
|
| 116 |
+
assert mpf_perturb(a, 0, 53, round_down) == b
|
| 117 |
+
assert mpf_perturb(a, 0, 53, round_ceiling) == b
|
| 118 |
+
assert mpf_perturb(a, 1, 53, round_up) == c
|
| 119 |
+
assert mpf_perturb(a, 1, 53, round_floor) == c
|
| 120 |
+
assert mpf_perturb(a, 1, 53, round_down) == a
|
| 121 |
+
assert mpf_perturb(a, 1, 53, round_ceiling) == a
|
| 122 |
+
|
| 123 |
+
def test_add_exact():
|
| 124 |
+
ff = from_float
|
| 125 |
+
assert mpf_add(ff(3.0), ff(2.5)) == ff(5.5)
|
| 126 |
+
assert mpf_add(ff(3.0), ff(-2.5)) == ff(0.5)
|
| 127 |
+
assert mpf_add(ff(-3.0), ff(2.5)) == ff(-0.5)
|
| 128 |
+
assert mpf_add(ff(-3.0), ff(-2.5)) == ff(-5.5)
|
| 129 |
+
assert mpf_sub(mpf_add(fone, ff(1e-100)), fone) == ff(1e-100)
|
| 130 |
+
assert mpf_sub(mpf_add(ff(1e-100), fone), fone) == ff(1e-100)
|
| 131 |
+
assert mpf_sub(mpf_add(fone, ff(-1e-100)), fone) == ff(-1e-100)
|
| 132 |
+
assert mpf_sub(mpf_add(ff(-1e-100), fone), fone) == ff(-1e-100)
|
| 133 |
+
assert mpf_add(fone, fzero) == fone
|
| 134 |
+
assert mpf_add(fzero, fone) == fone
|
| 135 |
+
assert mpf_add(fzero, fzero) == fzero
|
| 136 |
+
|
| 137 |
+
def test_long_exponent_shifts():
|
| 138 |
+
mp.dps = 15
|
| 139 |
+
# Check for possible bugs due to exponent arithmetic overflow
|
| 140 |
+
# in a C implementation
|
| 141 |
+
x = mpf(1)
|
| 142 |
+
for p in [32, 64]:
|
| 143 |
+
a = ldexp(1,2**(p-1))
|
| 144 |
+
b = ldexp(1,2**p)
|
| 145 |
+
c = ldexp(1,2**(p+1))
|
| 146 |
+
d = ldexp(1,-2**(p-1))
|
| 147 |
+
e = ldexp(1,-2**p)
|
| 148 |
+
f = ldexp(1,-2**(p+1))
|
| 149 |
+
assert (x+a) == a
|
| 150 |
+
assert (x+b) == b
|
| 151 |
+
assert (x+c) == c
|
| 152 |
+
assert (x+d) == x
|
| 153 |
+
assert (x+e) == x
|
| 154 |
+
assert (x+f) == x
|
| 155 |
+
assert (a+x) == a
|
| 156 |
+
assert (b+x) == b
|
| 157 |
+
assert (c+x) == c
|
| 158 |
+
assert (d+x) == x
|
| 159 |
+
assert (e+x) == x
|
| 160 |
+
assert (f+x) == x
|
| 161 |
+
assert (x-a) == -a
|
| 162 |
+
assert (x-b) == -b
|
| 163 |
+
assert (x-c) == -c
|
| 164 |
+
assert (x-d) == x
|
| 165 |
+
assert (x-e) == x
|
| 166 |
+
assert (x-f) == x
|
| 167 |
+
assert (a-x) == a
|
| 168 |
+
assert (b-x) == b
|
| 169 |
+
assert (c-x) == c
|
| 170 |
+
assert (d-x) == -x
|
| 171 |
+
assert (e-x) == -x
|
| 172 |
+
assert (f-x) == -x
|
| 173 |
+
|
| 174 |
+
def test_float_rounding():
|
| 175 |
+
mp.prec = 64
|
| 176 |
+
for x in [mpf(1), mpf(1)+eps, mpf(1)-eps, -mpf(1)+eps, -mpf(1)-eps]:
|
| 177 |
+
fa = float(x)
|
| 178 |
+
fb = float(fadd(x,0,prec=53,rounding='n'))
|
| 179 |
+
assert fa == fb
|
| 180 |
+
z = mpc(x,x)
|
| 181 |
+
ca = complex(z)
|
| 182 |
+
cb = complex(fadd(z,0,prec=53,rounding='n'))
|
| 183 |
+
assert ca == cb
|
| 184 |
+
for rnd in ['n', 'd', 'u', 'f', 'c']:
|
| 185 |
+
fa = to_float(x._mpf_, rnd=rnd)
|
| 186 |
+
fb = to_float(fadd(x,0,prec=53,rounding=rnd)._mpf_, rnd=rnd)
|
| 187 |
+
assert fa == fb
|
| 188 |
+
mp.prec = 53
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_calculus.py
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from mpmath import *
|
| 3 |
+
|
| 4 |
+
def test_approximation():
|
| 5 |
+
mp.dps = 15
|
| 6 |
+
f = lambda x: cos(2-2*x)/x
|
| 7 |
+
p, err = chebyfit(f, [2, 4], 8, error=True)
|
| 8 |
+
assert err < 1e-5
|
| 9 |
+
for i in range(10):
|
| 10 |
+
x = 2 + i/5.
|
| 11 |
+
assert abs(polyval(p, x) - f(x)) < err
|
| 12 |
+
|
| 13 |
+
def test_limits():
|
| 14 |
+
mp.dps = 15
|
| 15 |
+
assert limit(lambda x: (x-sin(x))/x**3, 0).ae(mpf(1)/6)
|
| 16 |
+
assert limit(lambda n: (1+1/n)**n, inf).ae(e)
|
| 17 |
+
|
| 18 |
+
def test_polyval():
|
| 19 |
+
assert polyval([], 3) == 0
|
| 20 |
+
assert polyval([0], 3) == 0
|
| 21 |
+
assert polyval([5], 3) == 5
|
| 22 |
+
# 4x^3 - 2x + 5
|
| 23 |
+
p = [4, 0, -2, 5]
|
| 24 |
+
assert polyval(p,4) == 253
|
| 25 |
+
assert polyval(p,4,derivative=True) == (253, 190)
|
| 26 |
+
|
| 27 |
+
def test_polyroots():
|
| 28 |
+
p = polyroots([1,-4])
|
| 29 |
+
assert p[0].ae(4)
|
| 30 |
+
p, q = polyroots([1,2,3])
|
| 31 |
+
assert p.ae(-1 - sqrt(2)*j)
|
| 32 |
+
assert q.ae(-1 + sqrt(2)*j)
|
| 33 |
+
#this is not a real test, it only tests a specific case
|
| 34 |
+
assert polyroots([1]) == []
|
| 35 |
+
pytest.raises(ValueError, lambda: polyroots([0]))
|
| 36 |
+
|
| 37 |
+
def test_polyroots_legendre():
|
| 38 |
+
n = 64
|
| 39 |
+
coeffs = [11975573020964041433067793888190275875, 0,
|
| 40 |
+
-190100434726484311252477736051902332000, 0,
|
| 41 |
+
1437919688271127330313741595496589239248, 0,
|
| 42 |
+
-6897338342113537600691931230430793911840, 0,
|
| 43 |
+
23556405536185284408974715545252277554280, 0,
|
| 44 |
+
-60969520211303089058522793175947071316960, 0,
|
| 45 |
+
124284021969194758465450309166353645376880, 0,
|
| 46 |
+
-204721258548015217049921875719981284186016, 0,
|
| 47 |
+
277415422258095841688223780704620656114900, 0,
|
| 48 |
+
-313237834141273382807123548182995095192800, 0,
|
| 49 |
+
297432255354328395601259515935229287637200, 0,
|
| 50 |
+
-239057700565161140389797367947941296605600, 0,
|
| 51 |
+
163356095386193445933028201431093219347160, 0,
|
| 52 |
+
-95158890516229191805647495979277603503200, 0,
|
| 53 |
+
47310254620162038075933656063247634556400, 0,
|
| 54 |
+
-20071017111583894941305187420771723751200, 0,
|
| 55 |
+
7255051932731034189479516844750603752850, 0,
|
| 56 |
+
-2228176940331017311443863996901733412640, 0,
|
| 57 |
+
579006552594977616773047095969088431600, 0,
|
| 58 |
+
-126584428502545713788439446082310831200, 0,
|
| 59 |
+
23112325428835593809686977515028663000, 0,
|
| 60 |
+
-3491517141958743235617737161547844000, 0,
|
| 61 |
+
431305058712550634988073414073557200, 0,
|
| 62 |
+
-42927166660756742088912492757452000, 0,
|
| 63 |
+
3378527005707706553294038781836500, 0,
|
| 64 |
+
-205277590220215081719131470288800, 0,
|
| 65 |
+
9330799555464321896324157740400, 0,
|
| 66 |
+
-304114948474392713657972548576, 0,
|
| 67 |
+
6695289961520387531608984680, 0,
|
| 68 |
+
-91048139350447232095702560, 0,
|
| 69 |
+
659769125727878493447120, 0,
|
| 70 |
+
-1905929106580294155360, 0,
|
| 71 |
+
916312070471295267]
|
| 72 |
+
|
| 73 |
+
with mp.workdps(3):
|
| 74 |
+
with pytest.raises(mp.NoConvergence):
|
| 75 |
+
polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
|
| 76 |
+
extraprec=n*10)
|
| 77 |
+
|
| 78 |
+
roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
|
| 79 |
+
extraprec=n*10)
|
| 80 |
+
roots = [str(r) for r in roots]
|
| 81 |
+
assert roots == \
|
| 82 |
+
['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
|
| 83 |
+
'-0.946', '-0.93', '-0.911', '-0.889', '-0.866', '-0.841',
|
| 84 |
+
'-0.813', '-0.784', '-0.753', '-0.72', '-0.685', '-0.649',
|
| 85 |
+
'-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
|
| 86 |
+
'-0.357', '-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
|
| 87 |
+
'-0.073', '-0.0243', '0.0243', '0.073', '0.121', '0.17', '0.217',
|
| 88 |
+
'0.265', '0.311', '0.357', '0.402', '0.446', '0.489', '0.531',
|
| 89 |
+
'0.572', '0.611', '0.649', '0.685', '0.72', '0.753', '0.784',
|
| 90 |
+
'0.813', '0.841', '0.866', '0.889', '0.911', '0.93', '0.946',
|
| 91 |
+
'0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
|
| 92 |
+
|
| 93 |
+
def test_polyroots_legendre_init():
|
| 94 |
+
extra_prec = 100
|
| 95 |
+
coeffs = [11975573020964041433067793888190275875, 0,
|
| 96 |
+
-190100434726484311252477736051902332000, 0,
|
| 97 |
+
1437919688271127330313741595496589239248, 0,
|
| 98 |
+
-6897338342113537600691931230430793911840, 0,
|
| 99 |
+
23556405536185284408974715545252277554280, 0,
|
| 100 |
+
-60969520211303089058522793175947071316960, 0,
|
| 101 |
+
124284021969194758465450309166353645376880, 0,
|
| 102 |
+
-204721258548015217049921875719981284186016, 0,
|
| 103 |
+
277415422258095841688223780704620656114900, 0,
|
| 104 |
+
-313237834141273382807123548182995095192800, 0,
|
| 105 |
+
297432255354328395601259515935229287637200, 0,
|
| 106 |
+
-239057700565161140389797367947941296605600, 0,
|
| 107 |
+
163356095386193445933028201431093219347160, 0,
|
| 108 |
+
-95158890516229191805647495979277603503200, 0,
|
| 109 |
+
47310254620162038075933656063247634556400, 0,
|
| 110 |
+
-20071017111583894941305187420771723751200, 0,
|
| 111 |
+
7255051932731034189479516844750603752850, 0,
|
| 112 |
+
-2228176940331017311443863996901733412640, 0,
|
| 113 |
+
579006552594977616773047095969088431600, 0,
|
| 114 |
+
-126584428502545713788439446082310831200, 0,
|
| 115 |
+
23112325428835593809686977515028663000, 0,
|
| 116 |
+
-3491517141958743235617737161547844000, 0,
|
| 117 |
+
431305058712550634988073414073557200, 0,
|
| 118 |
+
-42927166660756742088912492757452000, 0,
|
| 119 |
+
3378527005707706553294038781836500, 0,
|
| 120 |
+
-205277590220215081719131470288800, 0,
|
| 121 |
+
9330799555464321896324157740400, 0,
|
| 122 |
+
-304114948474392713657972548576, 0,
|
| 123 |
+
6695289961520387531608984680, 0,
|
| 124 |
+
-91048139350447232095702560, 0,
|
| 125 |
+
659769125727878493447120, 0,
|
| 126 |
+
-1905929106580294155360, 0,
|
| 127 |
+
916312070471295267]
|
| 128 |
+
|
| 129 |
+
roots_init = matrix(['-0.999', '-0.996', '-0.991', '-0.983', '-0.973',
|
| 130 |
+
'-0.961', '-0.946', '-0.93', '-0.911', '-0.889',
|
| 131 |
+
'-0.866', '-0.841', '-0.813', '-0.784', '-0.753',
|
| 132 |
+
'-0.72', '-0.685', '-0.649', '-0.611', '-0.572',
|
| 133 |
+
'-0.531', '-0.489', '-0.446', '-0.402', '-0.357',
|
| 134 |
+
'-0.311', '-0.265', '-0.217', '-0.17', '-0.121',
|
| 135 |
+
'-0.073', '-0.0243', '0.0243', '0.073', '0.121',
|
| 136 |
+
'0.17', '0.217', '0.265', ' 0.311', '0.357',
|
| 137 |
+
'0.402', '0.446', '0.489', '0.531', '0.572',
|
| 138 |
+
'0.611', '0.649', '0.685', '0.72', '0.753',
|
| 139 |
+
'0.784', '0.813', '0.841', '0.866', '0.889',
|
| 140 |
+
'0.911', '0.93', '0.946', '0.961', '0.973',
|
| 141 |
+
'0.983', '0.991', '0.996', '0.999', '1.0'])
|
| 142 |
+
with mp.workdps(2*mp.dps):
|
| 143 |
+
roots_exact = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
|
| 144 |
+
extraprec=2*extra_prec)
|
| 145 |
+
with pytest.raises(mp.NoConvergence):
|
| 146 |
+
polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
|
| 147 |
+
extraprec=extra_prec)
|
| 148 |
+
roots,err = polyroots(coeffs, maxsteps=5, cleanup=True, error=True,
|
| 149 |
+
extraprec=extra_prec,roots_init=roots_init)
|
| 150 |
+
assert max(matrix(roots_exact)-matrix(roots).apply(abs)) < err
|
| 151 |
+
roots1,err1 = polyroots(coeffs, maxsteps=25, cleanup=True, error=True,
|
| 152 |
+
extraprec=extra_prec,roots_init=roots_init[:60])
|
| 153 |
+
assert max(matrix(roots_exact)-matrix(roots1).apply(abs)) < err1
|
| 154 |
+
|
| 155 |
+
def test_pade():
|
| 156 |
+
one = mpf(1)
|
| 157 |
+
mp.dps = 20
|
| 158 |
+
N = 10
|
| 159 |
+
a = [one]
|
| 160 |
+
k = 1
|
| 161 |
+
for i in range(1, N+1):
|
| 162 |
+
k *= i
|
| 163 |
+
a.append(one/k)
|
| 164 |
+
p, q = pade(a, N//2, N//2)
|
| 165 |
+
for x in arange(0, 1, 0.1):
|
| 166 |
+
r = polyval(p[::-1], x)/polyval(q[::-1], x)
|
| 167 |
+
assert(r.ae(exp(x), 1.0e-10))
|
| 168 |
+
mp.dps = 15
|
| 169 |
+
|
| 170 |
+
def test_fourier():
|
| 171 |
+
mp.dps = 15
|
| 172 |
+
c, s = fourier(lambda x: x+1, [-1, 2], 2)
|
| 173 |
+
#plot([lambda x: x+1, lambda x: fourierval((c, s), [-1, 2], x)], [-1, 2])
|
| 174 |
+
assert c[0].ae(1.5)
|
| 175 |
+
assert c[1].ae(-3*sqrt(3)/(2*pi))
|
| 176 |
+
assert c[2].ae(3*sqrt(3)/(4*pi))
|
| 177 |
+
assert s[0] == 0
|
| 178 |
+
assert s[1].ae(3/(2*pi))
|
| 179 |
+
assert s[2].ae(3/(4*pi))
|
| 180 |
+
assert fourierval((c, s), [-1, 2], 1).ae(1.9134966715663442)
|
| 181 |
+
|
| 182 |
+
def test_differint():
|
| 183 |
+
mp.dps = 15
|
| 184 |
+
assert differint(lambda t: t, 2, -0.5).ae(8*sqrt(2/pi)/3)
|
| 185 |
+
|
| 186 |
+
def test_invlap():
|
| 187 |
+
mp.dps = 15
|
| 188 |
+
t = 0.01
|
| 189 |
+
fp = lambda p: 1/(p+1)**2
|
| 190 |
+
ft = lambda t: t*exp(-t)
|
| 191 |
+
ftt = ft(t)
|
| 192 |
+
assert invertlaplace(fp,t,method='talbot').ae(ftt)
|
| 193 |
+
assert invertlaplace(fp,t,method='stehfest').ae(ftt)
|
| 194 |
+
assert invertlaplace(fp,t,method='dehoog').ae(ftt)
|
| 195 |
+
assert invertlaplace(fp,t,method='cohen').ae(ftt)
|
| 196 |
+
t = 1.0
|
| 197 |
+
ftt = ft(t)
|
| 198 |
+
assert invertlaplace(fp,t,method='talbot').ae(ftt)
|
| 199 |
+
assert invertlaplace(fp,t,method='stehfest').ae(ftt)
|
| 200 |
+
assert invertlaplace(fp,t,method='dehoog').ae(ftt)
|
| 201 |
+
assert invertlaplace(fp,t,method='cohen').ae(ftt)
|
| 202 |
+
|
| 203 |
+
t = 0.01
|
| 204 |
+
fp = lambda p: log(p)/p
|
| 205 |
+
ft = lambda t: -euler-log(t)
|
| 206 |
+
ftt = ft(t)
|
| 207 |
+
assert invertlaplace(fp,t,method='talbot').ae(ftt)
|
| 208 |
+
assert invertlaplace(fp,t,method='stehfest').ae(ftt)
|
| 209 |
+
assert invertlaplace(fp,t,method='dehoog').ae(ftt)
|
| 210 |
+
assert invertlaplace(fp,t,method='cohen').ae(ftt)
|
| 211 |
+
t = 1.0
|
| 212 |
+
ftt = ft(t)
|
| 213 |
+
assert invertlaplace(fp,t,method='talbot').ae(ftt)
|
| 214 |
+
assert invertlaplace(fp,t,method='stehfest').ae(ftt)
|
| 215 |
+
assert invertlaplace(fp,t,method='dehoog').ae(ftt)
|
| 216 |
+
assert invertlaplace(fp,t,method='cohen').ae(ftt)
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_compatibility.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mpmath import *
|
| 2 |
+
from random import seed, randint, random
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
# Test compatibility with Python floats, which are
|
| 6 |
+
# IEEE doubles (53-bit)
|
| 7 |
+
|
| 8 |
+
N = 5000
|
| 9 |
+
seed(1)
|
| 10 |
+
|
| 11 |
+
# Choosing exponents between roughly -140, 140 ensures that
|
| 12 |
+
# the Python floats don't overflow or underflow
|
| 13 |
+
xs = [(random()-1) * 10**randint(-140, 140) for x in range(N)]
|
| 14 |
+
ys = [(random()-1) * 10**randint(-140, 140) for x in range(N)]
|
| 15 |
+
|
| 16 |
+
# include some equal values
|
| 17 |
+
ys[int(N*0.8):] = xs[int(N*0.8):]
|
| 18 |
+
|
| 19 |
+
# Detect whether Python is compiled to use 80-bit floating-point
|
| 20 |
+
# instructions, in which case the double compatibility test breaks
|
| 21 |
+
uses_x87 = -4.1974624032366689e+117 / -8.4657370748010221e-47 \
|
| 22 |
+
== 4.9581771393902231e+163
|
| 23 |
+
|
| 24 |
+
def test_double_compatibility():
|
| 25 |
+
mp.prec = 53
|
| 26 |
+
for x, y in zip(xs, ys):
|
| 27 |
+
mpx = mpf(x)
|
| 28 |
+
mpy = mpf(y)
|
| 29 |
+
assert mpf(x) == x
|
| 30 |
+
assert (mpx < mpy) == (x < y)
|
| 31 |
+
assert (mpx > mpy) == (x > y)
|
| 32 |
+
assert (mpx == mpy) == (x == y)
|
| 33 |
+
assert (mpx != mpy) == (x != y)
|
| 34 |
+
assert (mpx <= mpy) == (x <= y)
|
| 35 |
+
assert (mpx >= mpy) == (x >= y)
|
| 36 |
+
assert mpx == mpx
|
| 37 |
+
if uses_x87:
|
| 38 |
+
mp.prec = 64
|
| 39 |
+
a = mpx + mpy
|
| 40 |
+
b = mpx * mpy
|
| 41 |
+
c = mpx / mpy
|
| 42 |
+
d = mpx % mpy
|
| 43 |
+
mp.prec = 53
|
| 44 |
+
assert +a == x + y
|
| 45 |
+
assert +b == x * y
|
| 46 |
+
assert +c == x / y
|
| 47 |
+
assert +d == x % y
|
| 48 |
+
else:
|
| 49 |
+
assert mpx + mpy == x + y
|
| 50 |
+
assert mpx * mpy == x * y
|
| 51 |
+
assert mpx / mpy == x / y
|
| 52 |
+
assert mpx % mpy == x % y
|
| 53 |
+
assert abs(mpx) == abs(x)
|
| 54 |
+
assert mpf(repr(x)) == x
|
| 55 |
+
assert ceil(mpx) == math.ceil(x)
|
| 56 |
+
assert floor(mpx) == math.floor(x)
|
| 57 |
+
|
| 58 |
+
def test_sqrt():
|
| 59 |
+
# this fails quite often. it appers to be float
|
| 60 |
+
# that rounds the wrong way, not mpf
|
| 61 |
+
fail = 0
|
| 62 |
+
mp.prec = 53
|
| 63 |
+
for x in xs:
|
| 64 |
+
x = abs(x)
|
| 65 |
+
mp.prec = 100
|
| 66 |
+
mp_high = mpf(x)**0.5
|
| 67 |
+
mp.prec = 53
|
| 68 |
+
mp_low = mpf(x)**0.5
|
| 69 |
+
fp = x**0.5
|
| 70 |
+
assert abs(mp_low-mp_high) <= abs(fp-mp_high)
|
| 71 |
+
fail += mp_low != fp
|
| 72 |
+
assert fail < N/10
|
| 73 |
+
|
| 74 |
+
def test_bugs():
|
| 75 |
+
# particular bugs
|
| 76 |
+
assert mpf(4.4408920985006262E-16) < mpf(1.7763568394002505E-15)
|
| 77 |
+
assert mpf(-4.4408920985006262E-16) > mpf(-1.7763568394002505E-15)
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_convert.py
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from mpmath import *
|
| 3 |
+
from mpmath.libmp import *
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_basic_string():
|
| 7 |
+
"""
|
| 8 |
+
Test basic string conversion
|
| 9 |
+
"""
|
| 10 |
+
mp.dps = 15
|
| 11 |
+
assert mpf('3') == mpf('3.0') == mpf('0003.') == mpf('0.03e2') == mpf(3.0)
|
| 12 |
+
assert mpf('30') == mpf('30.0') == mpf('00030.') == mpf(30.0)
|
| 13 |
+
for i in range(10):
|
| 14 |
+
for j in range(10):
|
| 15 |
+
assert mpf('%ie%i' % (i,j)) == i * 10**j
|
| 16 |
+
assert str(mpf('25000.0')) == '25000.0'
|
| 17 |
+
assert str(mpf('2500.0')) == '2500.0'
|
| 18 |
+
assert str(mpf('250.0')) == '250.0'
|
| 19 |
+
assert str(mpf('25.0')) == '25.0'
|
| 20 |
+
assert str(mpf('2.5')) == '2.5'
|
| 21 |
+
assert str(mpf('0.25')) == '0.25'
|
| 22 |
+
assert str(mpf('0.025')) == '0.025'
|
| 23 |
+
assert str(mpf('0.0025')) == '0.0025'
|
| 24 |
+
assert str(mpf('0.00025')) == '0.00025'
|
| 25 |
+
assert str(mpf('0.000025')) == '2.5e-5'
|
| 26 |
+
assert str(mpf(0)) == '0.0'
|
| 27 |
+
assert str(mpf('2.5e1000000000000000000000')) == '2.5e+1000000000000000000000'
|
| 28 |
+
assert str(mpf('2.6e-1000000000000000000000')) == '2.6e-1000000000000000000000'
|
| 29 |
+
assert str(mpf(1.23402834e-15)) == '1.23402834e-15'
|
| 30 |
+
assert str(mpf(-1.23402834e-15)) == '-1.23402834e-15'
|
| 31 |
+
assert str(mpf(-1.2344e-15)) == '-1.2344e-15'
|
| 32 |
+
assert repr(mpf(-1.2344e-15)) == "mpf('-1.2343999999999999e-15')"
|
| 33 |
+
assert str(mpf("2163048125L")) == '2163048125.0'
|
| 34 |
+
assert str(mpf("-2163048125l")) == '-2163048125.0'
|
| 35 |
+
assert str(mpf("-2163048125L/1088391168")) == '-1.98738118113799'
|
| 36 |
+
assert str(mpf("2163048125/1088391168l")) == '1.98738118113799'
|
| 37 |
+
|
| 38 |
+
def test_pretty():
|
| 39 |
+
mp.pretty = True
|
| 40 |
+
assert repr(mpf(2.5)) == '2.5'
|
| 41 |
+
assert repr(mpc(2.5,3.5)) == '(2.5 + 3.5j)'
|
| 42 |
+
mp.pretty = False
|
| 43 |
+
iv.pretty = True
|
| 44 |
+
assert repr(mpi(2.5,3.5)) == '[2.5, 3.5]'
|
| 45 |
+
iv.pretty = False
|
| 46 |
+
|
| 47 |
+
def test_str_whitespace():
|
| 48 |
+
assert mpf('1.26 ') == 1.26
|
| 49 |
+
|
| 50 |
+
def test_unicode():
|
| 51 |
+
mp.dps = 15
|
| 52 |
+
try:
|
| 53 |
+
unicode = unicode
|
| 54 |
+
except NameError:
|
| 55 |
+
unicode = str
|
| 56 |
+
assert mpf(unicode('2.76')) == 2.76
|
| 57 |
+
assert mpf(unicode('inf')) == inf
|
| 58 |
+
|
| 59 |
+
def test_str_format():
|
| 60 |
+
assert to_str(from_float(0.1),15,strip_zeros=False) == '0.100000000000000'
|
| 61 |
+
assert to_str(from_float(0.0),15,show_zero_exponent=True) == '0.0e+0'
|
| 62 |
+
assert to_str(from_float(0.0),0,show_zero_exponent=True) == '.0e+0'
|
| 63 |
+
assert to_str(from_float(0.0),0,show_zero_exponent=False) == '.0'
|
| 64 |
+
assert to_str(from_float(0.0),1,show_zero_exponent=True) == '0.0e+0'
|
| 65 |
+
assert to_str(from_float(0.0),1,show_zero_exponent=False) == '0.0'
|
| 66 |
+
assert to_str(from_float(1.23),3,show_zero_exponent=True) == '1.23e+0'
|
| 67 |
+
assert to_str(from_float(1.23456789000000e-2),15,strip_zeros=False,min_fixed=0,max_fixed=0) == '1.23456789000000e-2'
|
| 68 |
+
assert to_str(from_float(1.23456789000000e+2),15,strip_zeros=False,min_fixed=0,max_fixed=0) == '1.23456789000000e+2'
|
| 69 |
+
assert to_str(from_float(2.1287e14), 15, max_fixed=1000) == '212870000000000.0'
|
| 70 |
+
assert to_str(from_float(2.1287e15), 15, max_fixed=1000) == '2128700000000000.0'
|
| 71 |
+
assert to_str(from_float(2.1287e16), 15, max_fixed=1000) == '21287000000000000.0'
|
| 72 |
+
assert to_str(from_float(2.1287e30), 15, max_fixed=1000) == '2128700000000000000000000000000.0'
|
| 73 |
+
|
| 74 |
+
def test_tight_string_conversion():
|
| 75 |
+
mp.dps = 15
|
| 76 |
+
# In an old version, '0.5' wasn't recognized as representing
|
| 77 |
+
# an exact binary number and was erroneously rounded up or down
|
| 78 |
+
assert from_str('0.5', 10, round_floor) == fhalf
|
| 79 |
+
assert from_str('0.5', 10, round_ceiling) == fhalf
|
| 80 |
+
|
| 81 |
+
def test_eval_repr_invariant():
|
| 82 |
+
"""Test that eval(repr(x)) == x"""
|
| 83 |
+
random.seed(123)
|
| 84 |
+
for dps in [10, 15, 20, 50, 100]:
|
| 85 |
+
mp.dps = dps
|
| 86 |
+
for i in range(1000):
|
| 87 |
+
a = mpf(random.random())**0.5 * 10**random.randint(-100, 100)
|
| 88 |
+
assert eval(repr(a)) == a
|
| 89 |
+
mp.dps = 15
|
| 90 |
+
|
| 91 |
+
def test_str_bugs():
|
| 92 |
+
mp.dps = 15
|
| 93 |
+
# Decimal rounding used to give the wrong exponent in some cases
|
| 94 |
+
assert str(mpf('1e600')) == '1.0e+600'
|
| 95 |
+
assert str(mpf('1e10000')) == '1.0e+10000'
|
| 96 |
+
|
| 97 |
+
def test_str_prec0():
|
| 98 |
+
assert to_str(from_float(1.234), 0) == '.0e+0'
|
| 99 |
+
assert to_str(from_float(1e-15), 0) == '.0e-15'
|
| 100 |
+
assert to_str(from_float(1e+15), 0) == '.0e+15'
|
| 101 |
+
assert to_str(from_float(-1e-15), 0) == '-.0e-15'
|
| 102 |
+
assert to_str(from_float(-1e+15), 0) == '-.0e+15'
|
| 103 |
+
|
| 104 |
+
def test_convert_rational():
|
| 105 |
+
mp.dps = 15
|
| 106 |
+
assert from_rational(30, 5, 53, round_nearest) == (0, 3, 1, 2)
|
| 107 |
+
assert from_rational(-7, 4, 53, round_nearest) == (1, 7, -2, 3)
|
| 108 |
+
assert to_rational((0, 1, -1, 1)) == (1, 2)
|
| 109 |
+
|
| 110 |
+
def test_custom_class():
|
| 111 |
+
class mympf:
|
| 112 |
+
@property
|
| 113 |
+
def _mpf_(self):
|
| 114 |
+
return mpf(3.5)._mpf_
|
| 115 |
+
class mympc:
|
| 116 |
+
@property
|
| 117 |
+
def _mpc_(self):
|
| 118 |
+
return mpf(3.5)._mpf_, mpf(2.5)._mpf_
|
| 119 |
+
assert mpf(2) + mympf() == 5.5
|
| 120 |
+
assert mympf() + mpf(2) == 5.5
|
| 121 |
+
assert mpf(mympf()) == 3.5
|
| 122 |
+
assert mympc() + mpc(2) == mpc(5.5, 2.5)
|
| 123 |
+
assert mpc(2) + mympc() == mpc(5.5, 2.5)
|
| 124 |
+
assert mpc(mympc()) == (3.5+2.5j)
|
| 125 |
+
|
| 126 |
+
def test_conversion_methods():
|
| 127 |
+
class SomethingRandom:
|
| 128 |
+
pass
|
| 129 |
+
class SomethingReal:
|
| 130 |
+
def _mpmath_(self, prec, rounding):
|
| 131 |
+
return mp.make_mpf(from_str('1.3', prec, rounding))
|
| 132 |
+
class SomethingComplex:
|
| 133 |
+
def _mpmath_(self, prec, rounding):
|
| 134 |
+
return mp.make_mpc((from_str('1.3', prec, rounding), \
|
| 135 |
+
from_str('1.7', prec, rounding)))
|
| 136 |
+
x = mpf(3)
|
| 137 |
+
z = mpc(3)
|
| 138 |
+
a = SomethingRandom()
|
| 139 |
+
y = SomethingReal()
|
| 140 |
+
w = SomethingComplex()
|
| 141 |
+
for d in [15, 45]:
|
| 142 |
+
mp.dps = d
|
| 143 |
+
assert (x+y).ae(mpf('4.3'))
|
| 144 |
+
assert (y+x).ae(mpf('4.3'))
|
| 145 |
+
assert (x+w).ae(mpc('4.3', '1.7'))
|
| 146 |
+
assert (w+x).ae(mpc('4.3', '1.7'))
|
| 147 |
+
assert (z+y).ae(mpc('4.3'))
|
| 148 |
+
assert (y+z).ae(mpc('4.3'))
|
| 149 |
+
assert (z+w).ae(mpc('4.3', '1.7'))
|
| 150 |
+
assert (w+z).ae(mpc('4.3', '1.7'))
|
| 151 |
+
x-y; y-x; x-w; w-x; z-y; y-z; z-w; w-z
|
| 152 |
+
x*y; y*x; x*w; w*x; z*y; y*z; z*w; w*z
|
| 153 |
+
x/y; y/x; x/w; w/x; z/y; y/z; z/w; w/z
|
| 154 |
+
x**y; y**x; x**w; w**x; z**y; y**z; z**w; w**z
|
| 155 |
+
x==y; y==x; x==w; w==x; z==y; y==z; z==w; w==z
|
| 156 |
+
mp.dps = 15
|
| 157 |
+
assert x.__add__(a) is NotImplemented
|
| 158 |
+
assert x.__radd__(a) is NotImplemented
|
| 159 |
+
assert x.__lt__(a) is NotImplemented
|
| 160 |
+
assert x.__gt__(a) is NotImplemented
|
| 161 |
+
assert x.__le__(a) is NotImplemented
|
| 162 |
+
assert x.__ge__(a) is NotImplemented
|
| 163 |
+
assert x.__eq__(a) is NotImplemented
|
| 164 |
+
assert x.__ne__(a) is NotImplemented
|
| 165 |
+
# implementation detail
|
| 166 |
+
if hasattr(x, "__cmp__"):
|
| 167 |
+
assert x.__cmp__(a) is NotImplemented
|
| 168 |
+
assert x.__sub__(a) is NotImplemented
|
| 169 |
+
assert x.__rsub__(a) is NotImplemented
|
| 170 |
+
assert x.__mul__(a) is NotImplemented
|
| 171 |
+
assert x.__rmul__(a) is NotImplemented
|
| 172 |
+
assert x.__div__(a) is NotImplemented
|
| 173 |
+
assert x.__rdiv__(a) is NotImplemented
|
| 174 |
+
assert x.__mod__(a) is NotImplemented
|
| 175 |
+
assert x.__rmod__(a) is NotImplemented
|
| 176 |
+
assert x.__pow__(a) is NotImplemented
|
| 177 |
+
assert x.__rpow__(a) is NotImplemented
|
| 178 |
+
assert z.__add__(a) is NotImplemented
|
| 179 |
+
assert z.__radd__(a) is NotImplemented
|
| 180 |
+
assert z.__eq__(a) is NotImplemented
|
| 181 |
+
assert z.__ne__(a) is NotImplemented
|
| 182 |
+
assert z.__sub__(a) is NotImplemented
|
| 183 |
+
assert z.__rsub__(a) is NotImplemented
|
| 184 |
+
assert z.__mul__(a) is NotImplemented
|
| 185 |
+
assert z.__rmul__(a) is NotImplemented
|
| 186 |
+
assert z.__div__(a) is NotImplemented
|
| 187 |
+
assert z.__rdiv__(a) is NotImplemented
|
| 188 |
+
assert z.__pow__(a) is NotImplemented
|
| 189 |
+
assert z.__rpow__(a) is NotImplemented
|
| 190 |
+
|
| 191 |
+
def test_mpmathify():
|
| 192 |
+
assert mpmathify('1/2') == 0.5
|
| 193 |
+
assert mpmathify('(1.0+1.0j)') == mpc(1, 1)
|
| 194 |
+
assert mpmathify('(1.2e-10 - 3.4e5j)') == mpc('1.2e-10', '-3.4e5')
|
| 195 |
+
assert mpmathify('1j') == mpc(1j)
|
| 196 |
+
|
| 197 |
+
def test_issue548():
|
| 198 |
+
try:
|
| 199 |
+
# This expression is invalid, but may trigger the ReDOS vulnerability
|
| 200 |
+
# in the regular expression for parsing complex numbers.
|
| 201 |
+
mpmathify('(' + '1' * 5000 + '!j')
|
| 202 |
+
except:
|
| 203 |
+
return
|
| 204 |
+
# The expression is invalid and should raise an exception.
|
| 205 |
+
assert False
|
| 206 |
+
|
| 207 |
+
def test_compatibility():
|
| 208 |
+
try:
|
| 209 |
+
import numpy as np
|
| 210 |
+
from fractions import Fraction
|
| 211 |
+
from decimal import Decimal
|
| 212 |
+
import decimal
|
| 213 |
+
except ImportError:
|
| 214 |
+
return
|
| 215 |
+
# numpy types
|
| 216 |
+
for nptype in np.core.numerictypes.typeDict.values():
|
| 217 |
+
if issubclass(nptype, np.complexfloating):
|
| 218 |
+
x = nptype(complex(0.5, -0.5))
|
| 219 |
+
elif issubclass(nptype, np.floating):
|
| 220 |
+
x = nptype(0.5)
|
| 221 |
+
elif issubclass(nptype, np.integer):
|
| 222 |
+
x = nptype(2)
|
| 223 |
+
# Handle the weird types
|
| 224 |
+
try: diff = np.abs(type(np.sqrt(x))(sqrt(x)) - np.sqrt(x))
|
| 225 |
+
except: continue
|
| 226 |
+
assert diff < 2.0**-53
|
| 227 |
+
#Fraction and Decimal
|
| 228 |
+
oldprec = mp.prec
|
| 229 |
+
mp.prec = 1000
|
| 230 |
+
decimal.getcontext().prec = mp.dps
|
| 231 |
+
assert sqrt(Fraction(2, 3)).ae(sqrt(mpf('2/3')))
|
| 232 |
+
assert sqrt(Decimal(2)/Decimal(3)).ae(sqrt(mpf('2/3')))
|
| 233 |
+
mp.prec = oldprec
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_diff.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mpmath import *
|
| 2 |
+
|
| 3 |
+
def test_diff():
|
| 4 |
+
mp.dps = 15
|
| 5 |
+
assert diff(log, 2.0, n=0).ae(log(2))
|
| 6 |
+
assert diff(cos, 1.0).ae(-sin(1))
|
| 7 |
+
assert diff(abs, 0.0) == 0
|
| 8 |
+
assert diff(abs, 0.0, direction=1) == 1
|
| 9 |
+
assert diff(abs, 0.0, direction=-1) == -1
|
| 10 |
+
assert diff(exp, 1.0).ae(e)
|
| 11 |
+
assert diff(exp, 1.0, n=5).ae(e)
|
| 12 |
+
assert diff(exp, 2.0, n=5, direction=3*j).ae(e**2)
|
| 13 |
+
assert diff(lambda x: x**2, 3.0, method='quad').ae(6)
|
| 14 |
+
assert diff(lambda x: 3+x**5, 3.0, n=2, method='quad').ae(540)
|
| 15 |
+
assert diff(lambda x: 3+x**5, 3.0, n=2, method='step').ae(540)
|
| 16 |
+
assert diffun(sin)(2).ae(cos(2))
|
| 17 |
+
assert diffun(sin, n=2)(2).ae(-sin(2))
|
| 18 |
+
|
| 19 |
+
def test_diffs():
|
| 20 |
+
mp.dps = 15
|
| 21 |
+
assert [chop(d) for d in diffs(sin, 0, 1)] == [0, 1]
|
| 22 |
+
assert [chop(d) for d in diffs(sin, 0, 1, method='quad')] == [0, 1]
|
| 23 |
+
assert [chop(d) for d in diffs(sin, 0, 2)] == [0, 1, 0]
|
| 24 |
+
assert [chop(d) for d in diffs(sin, 0, 2, method='quad')] == [0, 1, 0]
|
| 25 |
+
|
| 26 |
+
def test_taylor():
|
| 27 |
+
mp.dps = 15
|
| 28 |
+
# Easy to test since the coefficients are exact in floating-point
|
| 29 |
+
assert taylor(sqrt, 1, 4) == [1, 0.5, -0.125, 0.0625, -0.0390625]
|
| 30 |
+
|
| 31 |
+
def test_diff_partial():
|
| 32 |
+
mp.dps = 15
|
| 33 |
+
x,y,z = xyz = 2,3,7
|
| 34 |
+
f = lambda x,y,z: 3*x**2 * (y+2)**3 * z**5
|
| 35 |
+
assert diff(f, xyz, (0,0,0)).ae(25210500)
|
| 36 |
+
assert diff(f, xyz, (0,0,1)).ae(18007500)
|
| 37 |
+
assert diff(f, xyz, (0,0,2)).ae(10290000)
|
| 38 |
+
assert diff(f, xyz, (0,1,0)).ae(15126300)
|
| 39 |
+
assert diff(f, xyz, (0,1,1)).ae(10804500)
|
| 40 |
+
assert diff(f, xyz, (0,1,2)).ae(6174000)
|
| 41 |
+
assert diff(f, xyz, (0,2,0)).ae(6050520)
|
| 42 |
+
assert diff(f, xyz, (0,2,1)).ae(4321800)
|
| 43 |
+
assert diff(f, xyz, (0,2,2)).ae(2469600)
|
| 44 |
+
assert diff(f, xyz, (1,0,0)).ae(25210500)
|
| 45 |
+
assert diff(f, xyz, (1,0,1)).ae(18007500)
|
| 46 |
+
assert diff(f, xyz, (1,0,2)).ae(10290000)
|
| 47 |
+
assert diff(f, xyz, (1,1,0)).ae(15126300)
|
| 48 |
+
assert diff(f, xyz, (1,1,1)).ae(10804500)
|
| 49 |
+
assert diff(f, xyz, (1,1,2)).ae(6174000)
|
| 50 |
+
assert diff(f, xyz, (1,2,0)).ae(6050520)
|
| 51 |
+
assert diff(f, xyz, (1,2,1)).ae(4321800)
|
| 52 |
+
assert diff(f, xyz, (1,2,2)).ae(2469600)
|
| 53 |
+
assert diff(f, xyz, (2,0,0)).ae(12605250)
|
| 54 |
+
assert diff(f, xyz, (2,0,1)).ae(9003750)
|
| 55 |
+
assert diff(f, xyz, (2,0,2)).ae(5145000)
|
| 56 |
+
assert diff(f, xyz, (2,1,0)).ae(7563150)
|
| 57 |
+
assert diff(f, xyz, (2,1,1)).ae(5402250)
|
| 58 |
+
assert diff(f, xyz, (2,1,2)).ae(3087000)
|
| 59 |
+
assert diff(f, xyz, (2,2,0)).ae(3025260)
|
| 60 |
+
assert diff(f, xyz, (2,2,1)).ae(2160900)
|
| 61 |
+
assert diff(f, xyz, (2,2,2)).ae(1234800)
|
vllm/lib/python3.10/site-packages/mpmath/tests/test_division.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mpmath.libmp import *
|
| 2 |
+
from mpmath import mpf, mp
|
| 3 |
+
|
| 4 |
+
from random import randint, choice, seed
|
| 5 |
+
|
| 6 |
+
all_modes = [round_floor, round_ceiling, round_down, round_up, round_nearest]
|
| 7 |
+
|
| 8 |
+
fb = from_bstr
|
| 9 |
+
fi = from_int
|
| 10 |
+
ff = from_float
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_div_1_3():
|
| 14 |
+
a = fi(1)
|
| 15 |
+
b = fi(3)
|
| 16 |
+
c = fi(-1)
|
| 17 |
+
|
| 18 |
+
# floor rounds down, ceiling rounds up
|
| 19 |
+
assert mpf_div(a, b, 7, round_floor) == fb('0.01010101')
|
| 20 |
+
assert mpf_div(a, b, 7, round_ceiling) == fb('0.01010110')
|
| 21 |
+
assert mpf_div(a, b, 7, round_down) == fb('0.01010101')
|
| 22 |
+
assert mpf_div(a, b, 7, round_up) == fb('0.01010110')
|
| 23 |
+
assert mpf_div(a, b, 7, round_nearest) == fb('0.01010101')
|
| 24 |
+
|
| 25 |
+
# floor rounds up, ceiling rounds down
|
| 26 |
+
assert mpf_div(c, b, 7, round_floor) == fb('-0.01010110')
|
| 27 |
+
assert mpf_div(c, b, 7, round_ceiling) == fb('-0.01010101')
|
| 28 |
+
assert mpf_div(c, b, 7, round_down) == fb('-0.01010101')
|
| 29 |
+
assert mpf_div(c, b, 7, round_up) == fb('-0.01010110')
|
| 30 |
+
assert mpf_div(c, b, 7, round_nearest) == fb('-0.01010101')
|
| 31 |
+
|
| 32 |
+
def test_mpf_divi_1_3():
|
| 33 |
+
a = 1
|
| 34 |
+
b = fi(3)
|
| 35 |
+
c = -1
|
| 36 |
+
assert mpf_rdiv_int(a, b, 7, round_floor) == fb('0.01010101')
|
| 37 |
+
assert mpf_rdiv_int(a, b, 7, round_ceiling) == fb('0.01010110')
|
| 38 |
+
assert mpf_rdiv_int(a, b, 7, round_down) == fb('0.01010101')
|
| 39 |
+
assert mpf_rdiv_int(a, b, 7, round_up) == fb('0.01010110')
|
| 40 |
+
assert mpf_rdiv_int(a, b, 7, round_nearest) == fb('0.01010101')
|
| 41 |
+
assert mpf_rdiv_int(c, b, 7, round_floor) == fb('-0.01010110')
|
| 42 |
+
assert mpf_rdiv_int(c, b, 7, round_ceiling) == fb('-0.01010101')
|
| 43 |
+
assert mpf_rdiv_int(c, b, 7, round_down) == fb('-0.01010101')
|
| 44 |
+
assert mpf_rdiv_int(c, b, 7, round_up) == fb('-0.01010110')
|
| 45 |
+
assert mpf_rdiv_int(c, b, 7, round_nearest) == fb('-0.01010101')
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_div_300():
|
| 49 |
+
|
| 50 |
+
q = fi(1000000)
|
| 51 |
+
a = fi(300499999) # a/q is a little less than a half-integer
|
| 52 |
+
b = fi(300500000) # b/q exactly a half-integer
|
| 53 |
+
c = fi(300500001) # c/q is a little more than a half-integer
|
| 54 |
+
|
| 55 |
+
# Check nearest integer rounding (prec=9 as 2**8 < 300 < 2**9)
|
| 56 |
+
|
| 57 |
+
assert mpf_div(a, q, 9, round_down) == fi(300)
|
| 58 |
+
assert mpf_div(b, q, 9, round_down) == fi(300)
|
| 59 |
+
assert mpf_div(c, q, 9, round_down) == fi(300)
|
| 60 |
+
assert mpf_div(a, q, 9, round_up) == fi(301)
|
| 61 |
+
assert mpf_div(b, q, 9, round_up) == fi(301)
|
| 62 |
+
assert mpf_div(c, q, 9, round_up) == fi(301)
|
| 63 |
+
|
| 64 |
+
# Nearest even integer is down
|
| 65 |
+
assert mpf_div(a, q, 9, round_nearest) == fi(300)
|
| 66 |
+
assert mpf_div(b, q, 9, round_nearest) == fi(300)
|
| 67 |
+
assert mpf_div(c, q, 9, round_nearest) == fi(301)
|
| 68 |
+
|
| 69 |
+
# Nearest even integer is up
|
| 70 |
+
a = fi(301499999)
|
| 71 |
+
b = fi(301500000)
|
| 72 |
+
c = fi(301500001)
|
| 73 |
+
assert mpf_div(a, q, 9, round_nearest) == fi(301)
|
| 74 |
+
assert mpf_div(b, q, 9, round_nearest) == fi(302)
|
| 75 |
+
assert mpf_div(c, q, 9, round_nearest) == fi(302)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_tight_integer_division():
|
| 79 |
+
# Test that integer division at tightest possible precision is exact
|
| 80 |
+
N = 100
|
| 81 |
+
seed(1)
|
| 82 |
+
for i in range(N):
|
| 83 |
+
a = choice([1, -1]) * randint(1, 1<<randint(10, 100))
|
| 84 |
+
b = choice([1, -1]) * randint(1, 1<<randint(10, 100))
|
| 85 |
+
p = a * b
|
| 86 |
+
width = bitcount(abs(b)) - trailing(b)
|
| 87 |
+
a = fi(a); b = fi(b); p = fi(p)
|
| 88 |
+
for mode in all_modes:
|
| 89 |
+
assert mpf_div(p, a, width, mode) == b
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_epsilon_rounding():
|
| 93 |
+
# Verify that mpf_div uses infinite precision; this result will
|
| 94 |
+
# appear to be exactly 0.101 to a near-sighted algorithm
|
| 95 |
+
|
| 96 |
+
a = fb('0.101' + ('0'*200) + '1')
|
| 97 |
+
b = fb('1.10101')
|
| 98 |
+
c = mpf_mul(a, b, 250, round_floor) # exact
|
| 99 |
+
assert mpf_div(c, b, bitcount(a[1]), round_floor) == a # exact
|
| 100 |
+
|
| 101 |
+
assert mpf_div(c, b, 2, round_down) == fb('0.10')
|
| 102 |
+
assert mpf_div(c, b, 3, round_down) == fb('0.101')
|
| 103 |
+
assert mpf_div(c, b, 2, round_up) == fb('0.11')
|
| 104 |
+
assert mpf_div(c, b, 3, round_up) == fb('0.110')
|
| 105 |
+
assert mpf_div(c, b, 2, round_floor) == fb('0.10')
|
| 106 |
+
assert mpf_div(c, b, 3, round_floor) == fb('0.101')
|
| 107 |
+
assert mpf_div(c, b, 2, round_ceiling) == fb('0.11')
|
| 108 |
+
assert mpf_div(c, b, 3, round_ceiling) == fb('0.110')
|
| 109 |
+
|
| 110 |
+
# The same for negative numbers
|
| 111 |
+
a = fb('-0.101' + ('0'*200) + '1')
|
| 112 |
+
b = fb('1.10101')
|
| 113 |
+
c = mpf_mul(a, b, 250, round_floor)
|
| 114 |
+
assert mpf_div(c, b, bitcount(a[1]), round_floor) == a
|
| 115 |
+
|
| 116 |
+
assert mpf_div(c, b, 2, round_down) == fb('-0.10')
|
| 117 |
+
assert mpf_div(c, b, 3, round_up) == fb('-0.110')
|
| 118 |
+
|
| 119 |
+
# Floor goes up, ceiling goes down
|
| 120 |
+
assert mpf_div(c, b, 2, round_floor) == fb('-0.11')
|
| 121 |
+
assert mpf_div(c, b, 3, round_floor) == fb('-0.110')
|
| 122 |
+
assert mpf_div(c, b, 2, round_ceiling) == fb('-0.10')
|
| 123 |
+
assert mpf_div(c, b, 3, round_ceiling) == fb('-0.101')
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def test_mod():
|
| 127 |
+
mp.dps = 15
|
| 128 |
+
assert mpf(234) % 1 == 0
|
| 129 |
+
assert mpf(-3) % 256 == 253
|
| 130 |
+
assert mpf(0.25) % 23490.5 == 0.25
|
| 131 |
+
assert mpf(0.25) % -23490.5 == -23490.25
|
| 132 |
+
assert mpf(-0.25) % 23490.5 == 23490.25
|
| 133 |
+
assert mpf(-0.25) % -23490.5 == -0.25
|
| 134 |
+
# Check that these cases are handled efficiently
|
| 135 |
+
assert mpf('1e10000000000') % 1 == 0
|
| 136 |
+
assert mpf('1.23e-1000000000') % 1 == mpf('1.23e-1000000000')
|
| 137 |
+
# test __rmod__
|
| 138 |
+
assert 3 % mpf('1.75') == 1.25
|
| 139 |
+
|
| 140 |
+
def test_div_negative_rnd_bug():
|
| 141 |
+
mp.dps = 15
|
| 142 |
+
assert (-3) / mpf('0.1531879017645047') == mpf('-19.583791966887116')
|
| 143 |
+
assert mpf('-2.6342475750861301') / mpf('0.35126216427941814') == mpf('-7.4993775104985909')
|