

@defun
def nprod(ctx, f, interval, nsum=False, **kwargs):
    "\n    Computes the product\n\n    .. math ::\n\n        P = \\prod_{k=a}^b f(k)\n\n    where `(a, b)` = *interval*, and where `a = -\\infty` and/or\n    `b = \\infty` are allowed.\n\n    By default, :func:`~mpmath.nprod` uses the same extrapolation methods as\n    :func:`~mpmath.nsum`, except applied to the partial products rather than\n    partial sums, and the same keyword options as for :func:`~mpmath.nsum` are\n    supported. If ``nsum=True``, the product is instead computed via\n    :func:`~mpmath.nsum` as\n\n    .. math ::\n\n        P = \\exp\\left( \\sum_{k=a}^b \\log(f(k)) \\right).\n\n    This is slower, but can sometimes yield better results. It is\n    also required (and used automatically) when Euler-Maclaurin\n    summation is requested.\n\n    **Examples**\n\n    A simple finite product::\n\n        >>> from mpmath import *\n        >>> mp.dps = 25; mp.pretty = True\n        >>> nprod(lambda k: k, [1, 4])\n        24.0\n\n    A large number of infinite products have known exact values,\n    and can therefore be used as a reference. Most of the following\n    examples are taken from MathWorld [1].\n\n    A few infinite products with simple values are::\n\n        >>> 2*nprod(lambda k: (4*k**2)/(4*k**2-1), [1, inf])\n        3.141592653589793238462643\n        >>> nprod(lambda k: (1+1/k)**2/(1+2/k), [1, inf])\n        2.0\n        >>> nprod(lambda k: (k**3-1)/(k**3+1), [2, inf])\n        0.6666666666666666666666667\n        >>> nprod(lambda k: (1-1/k**2), [2, inf])\n        0.5\n\n    Next, several more infinite products with more complicated\n    values::\n\n        >>> nprod(lambda k: exp(1/k**2), [1, inf]); exp(pi**2/6)\n        5.180668317897115748416626\n        5.180668317897115748416626\n\n        >>> nprod(lambda k: (k**2-1)/(k**2+1), [2, inf]); pi*csch(pi)\n        0.2720290549821331629502366\n        0.2720290549821331629502366\n\n        >>> nprod(lambda k: (k**4-1)/(k**4+1), [2, inf])\n        0.8480540493529003921296502\n        >>> pi*sinh(pi)/(cosh(sqrt(2)*pi)-cos(sqrt(2)*pi))\n        0.8480540493529003921296502\n\n        >>> nprod(lambda k: (1+1/k+1/k**2)**2/(1+2/k+3/k**2), [1, inf])\n        1.848936182858244485224927\n        >>> 3*sqrt(2)*cosh(pi*sqrt(3)/2)**2*csch(pi*sqrt(2))/pi\n        1.848936182858244485224927\n\n        >>> nprod(lambda k: (1-1/k**4), [2, inf]); sinh(pi)/(4*pi)\n        0.9190194775937444301739244\n        0.9190194775937444301739244\n\n        >>> nprod(lambda k: (1-1/k**6), [2, inf])\n        0.9826842777421925183244759\n        >>> (1+cosh(pi*sqrt(3)))/(12*pi**2)\n        0.9826842777421925183244759\n\n        >>> nprod(lambda k: (1+1/k**2), [2, inf]); sinh(pi)/(2*pi)\n        1.838038955187488860347849\n        1.838038955187488860347849\n\n        >>> nprod(lambda n: (1+1/n)**n * exp(1/(2*n)-1), [1, inf])\n        1.447255926890365298959138\n        >>> exp(1+euler/2)/sqrt(2*pi)\n        1.447255926890365298959138\n\n    The following two products are equivalent and can be evaluated in\n    terms of a Jacobi theta function. Pi can be replaced by any value\n    (as long as convergence is preserved)::\n\n        >>> nprod(lambda k: (1-pi**-k)/(1+pi**-k), [1, inf])\n        0.3838451207481672404778686\n        >>> nprod(lambda k: tanh(k*log(pi)/2), [1, inf])\n        0.3838451207481672404778686\n        >>> jtheta(4,0,1/pi)\n        0.3838451207481672404778686\n\n    This product does not have a known closed form value::\n\n        >>> nprod(lambda k: (1-1/2**k), [1, inf])\n        0.2887880950866024212788997\n\n    A product taken from `-\\infty`::\n\n        >>> nprod(lambda k: 1-k**(-3), [-inf,-2])\n        0.8093965973662901095786805\n        >>> cosh(pi*sqrt(3)/2)/(3*pi)\n        0.8093965973662901095786805\n\n    A doubly infinite product::\n\n        >>> nprod(lambda k: exp(1/(1+k**2)), [-inf, inf])\n        23.41432688231864337420035\n        >>> exp(pi/tanh(pi))\n        23.41432688231864337420035\n\n    A product requiring the use of Euler-Maclaurin summation to compute\n    an accurate value::\n\n        >>> nprod(lambda k: (1-1/k**2.5), [2, inf], method='e')\n        0.696155111336231052898125\n\n    **References**\n\n    1. [Weisstein]_ http://mathworld.wolfram.com/InfiniteProduct.html\n\n    "
    if (nsum or ('e' in kwargs.get('method', ''))):
        orig = ctx.prec
        try:
            ctx.prec += 10
            v = ctx.nsum((lambda n: ctx.ln(f(n))), interval, **kwargs)
        finally:
            ctx.prec = orig
        return (+ ctx.exp(v))
    (a, b) = ctx._as_points(interval)
    if (a == ctx.ninf):
        if (b == ctx.inf):
            return (f(0) * ctx.nprod((lambda k: (f((- k)) * f(k))), [1, ctx.inf], **kwargs))
        return ctx.nprod(f, [(- b), ctx.inf], **kwargs)
    elif (b != ctx.inf):
        return ctx.fprod((f(ctx.mpf(k)) for k in xrange(int(a), (int(b) + 1))))
    a = int(a)

    def update(partial_products, indices):
        if partial_products:
            pprod = partial_products[(- 1)]
        else:
            pprod = ctx.one
        for k in indices:
            pprod = (pprod * f((a + ctx.mpf(k))))
            partial_products.append(pprod)
    return (+ ctx.adaptive_extrapolation(update, None, kwargs))

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