

@defun
def limit(ctx, f, x, direction=1, exp=False, **kwargs):
    "\n    Computes an estimate of the limit\n\n    .. math ::\n\n        \\lim_{t \\to x} f(t)\n\n    where `x` may be finite or infinite.\n\n    For finite `x`, :func:`~mpmath.limit` evaluates `f(x + d/n)` for\n    consecutive integer values of `n`, where the approach direction\n    `d` may be specified using the *direction* keyword argument.\n    For infinite `x`, :func:`~mpmath.limit` evaluates values of\n    `f(\\mathrm{sign}(x) \\cdot n)`.\n\n    If the approach to the limit is not sufficiently fast to give\n    an accurate estimate directly, :func:`~mpmath.limit` attempts to find\n    the limit using Richardson extrapolation or the Shanks\n    transformation. You can select between these methods using\n    the *method* keyword (see documentation of :func:`~mpmath.nsum` for\n    more information).\n\n    **Options**\n\n    The following options are available with essentially the\n    same meaning as for :func:`~mpmath.nsum`: *tol*, *method*, *maxterms*,\n    *steps*, *verbose*.\n\n    If the option *exp=True* is set, `f` will be\n    sampled at exponentially spaced points `n = 2^1, 2^2, 2^3, \\ldots`\n    instead of the linearly spaced points `n = 1, 2, 3, \\ldots`.\n    This can sometimes improve the rate of convergence so that\n    :func:`~mpmath.limit` may return a more accurate answer (and faster).\n    However, do note that this can only be used if `f`\n    supports fast and accurate evaluation for arguments that\n    are extremely close to the limit point (or if infinite,\n    very large arguments).\n\n    **Examples**\n\n    A basic evaluation of a removable singularity::\n\n        >>> from mpmath import *\n        >>> mp.dps = 30; mp.pretty = True\n        >>> limit(lambda x: (x-sin(x))/x**3, 0)\n        0.166666666666666666666666666667\n\n    Computing the exponential function using its limit definition::\n\n        >>> limit(lambda n: (1+3/n)**n, inf)\n        20.0855369231876677409285296546\n        >>> exp(3)\n        20.0855369231876677409285296546\n\n    A limit for `\\pi`::\n\n        >>> f = lambda n: 2**(4*n+1)*fac(n)**4/(2*n+1)/fac(2*n)**2\n        >>> limit(f, inf)\n        3.14159265358979323846264338328\n\n    Calculating the coefficient in Stirling's formula::\n\n        >>> limit(lambda n: fac(n) / (sqrt(n)*(n/e)**n), inf)\n        2.50662827463100050241576528481\n        >>> sqrt(2*pi)\n        2.50662827463100050241576528481\n\n    Evaluating Euler's constant `\\gamma` using the limit representation\n\n    .. math ::\n\n        \\gamma = \\lim_{n \\rightarrow \\infty } \\left[ \\left(\n        \\sum_{k=1}^n \\frac{1}{k} \\right) - \\log(n) \\right]\n\n    (which converges notoriously slowly)::\n\n        >>> f = lambda n: sum([mpf(1)/k for k in range(1,int(n)+1)]) - log(n)\n        >>> limit(f, inf)\n        0.577215664901532860606512090082\n        >>> +euler\n        0.577215664901532860606512090082\n\n    With default settings, the following limit converges too slowly\n    to be evaluated accurately. Changing to exponential sampling\n    however gives a perfect result::\n\n        >>> f = lambda x: sqrt(x**3+x**2)/(sqrt(x**3)+x)\n        >>> limit(f, inf)\n        0.992831158558330281129249686491\n        >>> limit(f, inf, exp=True)\n        1.0\n\n    "
    if ctx.isinf(x):
        direction = ctx.sign(x)
        g = (lambda k: f((ctx.mpf((k + 1)) * direction)))
    else:
        direction *= ctx.one
        g = (lambda k: f((x + (direction / (k + 1)))))
    if exp:
        h = g
        g = (lambda k: h((2 ** k)))

    def update(values, indices):
        for k in indices:
            values.append(g((k + 1)))
    if (not ('steps' in kwargs)):
        kwargs['steps'] = [10]
    return (+ ctx.adaptive_extrapolation(update, None, kwargs))

dataset/ETHPy150Open fredrik-johansson/mpmath/mpmath/calculus/extrapolation.py limit/original