コード例 #1
0
def ibincoeff(n, k, integer=True):
    """
    Computation of a single binomial coefficient n over k, returning an 
    integer (possibly long). For integer=True integer arithmetics is used 
    throughout and there is no risk of overflow. For integer=False a floating- 
    point gamma function approximation is used and the result converted to 
    integer at the end (if an overflow occurs ERRCODE is returned). 
    """

    assert is_posinteger(n), \
               "n in n_over_k in ibincoeff must be a positive integer!"
    assert is_nonneginteger(k), \
           "k in n_over_k in ibincoeff must be a non-negative integer!"
    assert n >= k, "n must be >= k in n_over_k in ibincoeff!"

    if integer:
        ibico = _bicolongint(n, k)

    else:
        try:
            lnbico = lngamma(n + 1) - lngamma(k + 1) - lngamma(n - k + 1)
            ibico = safeint(round(exp(lnbico)), 'ibincoeff')
        except OverflowError:
            ibico = ERRCODE

    return ibico
コード例 #2
0
ファイル: binco.py プロジェクト: zlongshen/simelements
def ibincoeff(n, k, integer=True):
    """
    Computation of a single binomial coefficient n over k, returning an 
    integer (possibly long). For integer=True integer arithmetics is used 
    throughout and there is no risk of overflow. For integer=False a floating- 
    point gamma function approximation is used and the result converted to 
    integer at the end (if an overflow occurs ERRCODE is returned). 
    """

    assert is_posinteger(n), \
               "n in n_over_k in ibincoeff must be a positive integer!"
    assert is_nonneginteger(k), \
           "k in n_over_k in ibincoeff must be a non-negative integer!"
    assert n >= k, "n must be >= k in n_over_k in ibincoeff!"

    if integer:
        ibico = _bicolongint(n, k)

    else:
        try:
            lnbico  =  lngamma(n+1) - lngamma(k+1) - lngamma(n-k+1)
            ibico   =  safeint(round(exp(lnbico)), 'ibincoeff')
        except OverflowError:
            ibico   =  ERRCODE

    return ibico
コード例 #3
0
def ifactorial(n, integer=True):
    """
    Computation of a factorial, returning an integer. For integer=True a long, 
    exact integer is returned. For integer=False an integer obtained from 
    floating-point arithmetics based on the function lngamma is returned - 
    it is approximate and may not be exact but faster for large n. ERRCODE 
    (cf. misclib.numbers) is returned if a floating-point OverflowError occurs 
    and a warning is sent to stdout (no error can occur when integer=True).
    """

    assert is_nonneginteger(n), \
                 "the argument to ifactorial must be a non-negative integer!"

    if integer:
        fact = factorial(n)

    else:
        try:
            fact = safeint(round(exp(lngamma(n + 1.0))), 'ifactorial')
        except OverflowError:
            fact = ERRCODE
            warn("OverflowError in ifactorial - ERRCODE is returned")

    return fact
コード例 #4
0
ファイル: specfunc.py プロジェクト: zlongshen/simelements
def ifactorial(n, integer=True):
    """
    Computation of a factorial, returning an integer. For integer=True a long, 
    exact integer is returned. For integer=False an integer obtained from 
    floating-point arithmetics based on the function lngamma is returned - 
    it is approximate and may not be exact but faster for large n. ERRCODE 
    (cf. misclib.numbers) is returned if a floating-point OverflowError occurs 
    and a warning is sent to stdout (no error can occur when integer=True).
    """

    assert is_nonneginteger(n), \
                 "the argument to ifactorial must be a non-negative integer!"

    if integer:
        fact = factorial(n)

    else:
        try:
            fact = safeint(round(exp(lngamma(n+1.0))), 'ifactorial')
        except OverflowError:
            fact = ERRCODE
            warn("OverflowError in ifactorial - ERRCODE is returned")

    return fact