Exemplo n.º 1
0
def nmFactorial(n, m):
    """ Calculates the expression below avoiding overflows.

    .. math::
        \\frac{(n + m)!}{(n - m)!}
    """
    ntemp = loggamma(n + m + 1)
    nSign = gammasgn(n + m + 1)

    mtemp = loggamma(n - m + 1)
    mSign = gammasgn(n - m + 1)

    return nSign * mSign * np.exp(mtemp - ntemp)
Exemplo n.º 2
0
def _gammaincc(a, x):
    '''gammaincc for positive or negative indices and scalar inputs'''
    if x < 0:
        raise ValueError('negative x in gammaincc')
    if x == 0:
        if a > 0:
            return 1
        s = sc.gammasgn(a)
        if s == 0:
            return 0
        return s*np.inf
    if np.isinf(x):
        return 0
    if a < 0:
        n = np.floor(a)
    else:
        n = 0
    g = sc.gammaincc(a-n, x)
    if n < 0:
        f = np.exp(sc.xlogy(a-n, x)-x-sc.gammaln(a-n+1))
        while n < 0:
            f *= (a-n)/x
            g -= f
            n += 1
    return g
Exemplo n.º 3
0
def loggamma_quotients(num_args=[], den_args=[]):
    log = 0
    sgn = 1
    for arg in (num_args + den_args):
        sgn *= gammasgn(arg)
    for num in num_args:
        log += loggamma(num)
    for den in den_args:
        log -= loggamma(den)
    return sgn, log
Exemplo n.º 4
0
def gamma_quotients(num_args=[], den_args=[]):
    import warnings
    warnings.filterwarnings("error")
    log = 0
    sgn = 1
    for arg in (num_args + den_args):
        sgn *= spc.gammasgn(arg)
    for num in num_args:
        log += spc.loggamma(num)
    for den in den_args:
        log -= spc.loggamma(den)
    try:
        res = sgn * np.exp(log)
        warnings.filterwarnings("default")
        return res
    except RuntimeWarning:
        with open('./runtimewarnings.txt', 'a') as f:
            f.writelines(str([num_args, den_args]) + '\n')
        warnings.filterwarnings("default")
        return sgn * np.exp(log)
Exemplo n.º 5
0
def gammaincc(a, x):
    r'''Regularized upper incomplete gamma function.

    This implementation of `gammaincc` allows :math:`a` real and :math:`x`
    nonnegative.

    Parameters
    ----------
    a : array_like
        Real parameter.

    x : array_like
        Nonnegative argument.

    Returns
    -------
    scalar or ndarray
        Values of the upper incomplete gamma function.

    Notes
    -----
    The function value is computed via a recurrence from the value of
    `scipy.special.gammaincc` for arguments :math:`a-n, x` where :math:`n` is
    the smallest integer such that :math:`a-n \ge 0`.

    See also
    --------
    scipy.special.gammaincc : Computes the start of the recurrence.

    Examples
    --------
    This implementation of `gammaincc` supports positive and negative values
    of `a`.

    >>> from skypy.utils.special import gammaincc
    >>> gammaincc([-1.5, -0.5, 0.5, 1.5], 1.2)
    array([ 0.03084582, -0.03378949,  0.12133525,  0.49363462])

    '''
    if np.broadcast(a, x).ndim == 0:
        return _gammaincc(a, x)
    a, x = np.broadcast_arrays(a, x)
    if np.any(x < 0):
        raise ValueError('negative x in gammaincc')

    # nonpositive a need special treatment
    i = a <= 0

    # find integer n such that a + n >= 0
    n = np.where(i, np.floor(a), 0)

    # compute gammaincc for a-n and x as usual
    g = sc.gammaincc(a-n, x)

    # deal with nonpositive a
    # the number n keeps track of iterations still to do
    if np.any(i) > 0:
        # all x = inf are done
        n[i & np.isinf(x)] = 0

        # do x = 0 for nonpositive a; depends on Gamma(a)
        i = i & (x == 0)
        s = sc.gammasgn(a[i])
        g[i] = np.where(s == 0, 0, s*np.inf)
        n[i] = 0

        # these are still to do
        i = n < 0

        # recurrence
        f = np.empty_like(g)
        f[i] = np.exp(sc.xlogy(a[i]-n[i], x[i])-x[i]-sc.gammaln(a[i]-n[i]+1))
        while np.any(i):
            f[i] *= (a[i]-n[i])/x[i]
            g[i] -= f[i]
            n[i] += 1
            i[i] = n[i] < 0
    return g
Exemplo n.º 6
0
def factorial_quotient(num, den):
    return gammasgn(num + 1) * gammasgn(den + 1) * np.exp(
            loggamma(num + 1) - loggamma(den + 1))
Exemplo n.º 7
0
def gammaln(x):
    """computes natural log of the gamma function which is more numerically stable than the gamma"""
    return special.gammasgn(x)*special.gammaln(x)