Пример #1
0
def sinh_cosh(x):
    """
    sinh_cosh(x) -> (number, number)

    Return a tuple containing the hyperbolic sine and cosine of x.
    """
    if isinstance(x, mpfr):
        res1 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        res2 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN)
    elif isinstance(x, (int, long)):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError
    gmp.mpfr_sinh_cosh(res1, res2, mpfr_x, gmp.MPFR_RNDN)
    return (mpfr._from_c_mpfr(res1), mpfr._from_c_mpfr(res2))
Пример #2
0
def sin_cos(x):
    """
    sin_cos(x) -> (number, number)

    Return a tuple containing the sine and cosine of x; x in radians.
    """
    if isinstance(x, mpfr):
        res1 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        res2 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
    elif isinstance(x, (int, long)):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        if isinstance(x, mpc):
            res1 = _new_mpc()
            res2 = _new_mpc()
            mpc_x = x._mpc
        elif isinstance(x, complex):
            res1 = _new_mpc()
            res2 = _new_mpc()
            mpc_x = res1
            gmp.mpc_set_d_d(mpc_x, x.real, x.imag, gmp.MPC_RNDNN)
        else:
            raise TypeError
        gmp.mpc_sin_cos(res1, res2, mpc_x, gmp.MPC_RNDNN, gmp.MPC_RNDNN)
        return (mpc._from_c_mpc(res1), mpc._from_c_mpc(res2))
    gmp.mpfr_sin_cos(res1, res2, mpfr_x, gmp.MPFR_RNDN)
    return (mpfr._from_c_mpfr(res1), mpfr._from_c_mpfr(res2))
Пример #3
0
def cot(x):
    """
    cot(x) -> number

    Return the cotangent of x; x in radians.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_cot(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #4
0
def sec(x):
    """
    sec(x) -> number

    Return the secant of x; x in radians.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_sec(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #5
0
def lngamma(x):
    """
    lngamma(x) -> number

    Return logarithm of gamma(x).
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_lngamma(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #6
0
def log2(x):
    """
    log2(x) -> number

    Return the base-2 logarithm of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_log2(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #7
0
def coth(x):
    """
    coth(x) -> number

    Return the hyperbolic cotangent of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_coth(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #8
0
def sech(x):
    """
    sech(x) -> number

    Return the hyperbolic secant of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_sech(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #9
0
def ai(x):
    """
    ai(x) -> number

    Return the Airy function of x.
    """
    res, mpfr_x = _init_check_mpfr(x)
    gmp.mpfr_ai(res, mpfr_x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #10
0
def exp10(x):
    """
    exp10(x) -> number

    Return 10**x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_exp10(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #11
0
def y1(x):
    """
    y1(x) -> number

    Return the second kind Bessel function of order 1 of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_y1(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #12
0
def erfc(x):
    """
    zeta(x) -> number

    Return complementary error function of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_erfc(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #13
0
def j0(x):
    """
    j0(x) -> number

    Return the first kind Bessel function of order 0 of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_j0(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #14
0
def digamma(x):
    """
    digamma(x) -> number

    Return digamma of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_digamma(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #15
0
def li2(x):
    """
    li2(x) -> number

    Return the real part of dilogarithm of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_li2(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #16
0
def eint(x):
    """
    eint(x) -> number

    Return the exponential integral of x.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_eint(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #17
0
def expm1(x):
    """
    expm1(x) -> number

    Return exponential(x) - 1.
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_expm1(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #18
0
def log1p(x):
    """
    log1p(x) -> number

    Return the logarithm of (1+x).
    """
    res, x = _init_check_mpfr(x)
    gmp.mpfr_log1p(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #19
0
def const_catalan(precision=0):
    """
    const_catalan([precision=0]) -> mpfr

    Return the catalan constant  using the specified precision. If no
    precision is specified, the default precision is used.
    """
    res = _new_mpfr(precision)
    gmp.mpfr_const_catalan(res, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #20
0
def yn(x, n):
    """
    yn(x, n) -> number

    Return the second kind Bessel function of order n of x.
    """
    if not (isinstance(n, (int, long)) and -sys.maxsize-1 <= n <= sys.maxsize):
        raise TypeError("yn() requires 'mpfr', 'int' arguments")
    res, x = _init_check_mpfr(x)
    gmp.mpfr_yn(res, n, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #21
0
def hypot(x, y):
    """
    hypot(y, x) -> number

    Return the square root of (x**2 + y**2).
    """
    # XXX Optimise
    res, mpfr_x = _init_check_mpfr(x)
    res, mpfr_y = _init_check_mpfr(y)
    gmp.mpfr_hypot(res, mpfr_x, mpfr_y, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #22
0
def lgamma(x):
    """
    lgamma(x) -> (number, int)

    Return a tuple containing the logarithm of the absolute value of
    gamma(x) and the sign of gamma(x)
    """
    res, x = _init_check_mpfr(x)
    sgn = ffi.new('int *')
    gmp.mpfr_lgamma(res, sgn, x, gmp.MPFR_RNDN)
    return (mpfr._from_c_mpfr(res), int(sgn[0]))
Пример #23
0
def agm(x, y):
    """
    agm(x, y) -> number

    Return the arithmetic-geometric mean of x and y.
    """
    # XXX Optimise
    res, mpfr_x = _init_check_mpfr(x)
    res, mpfr_y = _init_check_mpfr(y)
    gmp.mpfr_agm(res, mpfr_x, mpfr_y, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #24
0
def fms(x, y, z):
    """
    fms(x, y, z) -> number

    Return the correctly rounded result of (x * y) - z.
    """
    # XXX Optimise
    res, mpfr_x = _init_check_mpfr(x)
    res, mpfr_y = _init_check_mpfr(y)
    res, mpfr_z = _init_check_mpfr(z)
    gmp.mpfr_fms(res, mpfr_x, mpfr_y, mpfr_z, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #25
0
def zeta(x):
    """
    zeta(x) -> number

    Return Riemann zeta of x.
    """
    # if isinstance(x, (int, long)) and 0 <= x <= MAX_UI:
    #     res = _new_mpfr()
    #     gmp.mpfr_zeta_ui(res, x, gmp.MPFR_RNDN)
    res, x = _init_check_mpfr(x)
    gmp.mpfr_zeta(res, x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Пример #26
0
def sinh(x):
    """
    sinh(x) -> number

    Return the hyperbolic sine of x.
    """
    try:
        res, x = _init_check_mpfr(x)
        gmp.mpfr_sinh(res, x, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        res, x = _init_check_mpc(x)
        gmp.mpc_sinh(res, x, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)
Пример #27
0
def atanh(x):
    """
    atanh(x) -> number

    Return the inverse hyperbolic tangent of x.
    """
    try:
        res, x = _init_check_mpfr(x)
        gmp.mpfr_atanh(res, x, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        res, x = _init_check_mpc(x)
        gmp.mpc_atanh(res, x, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)
Пример #28
0
def atan(x):
    """
    atan(x) -> number

    Return the arc-tangent of x; x in radians.
    """
    try:
        res, x = _init_check_mpfr(x)
        gmp.mpfr_atan(res, x, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        res, x = _init_check_mpc(x)
        gmp.mpc_atan(res, x, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)
Пример #29
0
def sin(x):
    """
    sin(x) -> number

    Return the sine of x; x in radians.
    """
    try:
        res, x = _init_check_mpfr(x)
        gmp.mpfr_sin(res, x, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        res, x = _init_check_mpc(x)
        gmp.mpc_sin(res, x, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)
Пример #30
0
def log(x):
    """
    log(x) -> number

    Return the natural logarithm of x.
    """
    try:
        res, x = _init_check_mpfr(x)
        gmp.mpfr_log(res, x, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        res, x = _init_check_mpc(x)
        gmp.mpc_log(res, x, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)