Exemple #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))
Exemple #2
0
def _init_check_mpfr(x):
    """
    Returns a new mpfr and a pointer to a c mpfr storing the value of x
    """
    if isinstance(x, mpfr):
        res = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN)
    elif isinstance(x, (int, long)):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError
    return res, mpfr_x
Exemple #3
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))
Exemple #4
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)
Exemple #5
0
def atan2(y, x):
    """
    atan2(y, x) -> number

    Return the arc-tangent of (y/x).
    """
    # X
    if isinstance(x, mpfr):
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        mpfr_x = _new_mpfr()
        gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN)
    elif isinstance(x, (int, long)):
        mpfr_x = _new_mpfr()
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        mpfr_x = _new_mpfr()
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        mpfr_x = _new_mpfr()
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError

    # Y
    if isinstance(y, mpfr):
        mpfr_y = y._mpfr
    elif isinstance(y, float):
        mpfr_y = _new_mpfr()
        gmp.mpfr_set_d(mpfr_y, y, gmp.MPFR_RNDN)
    elif isinstance(y, (int, long)):
        mpfr_y = _new_mpfr()
        _pyint_to_mpfr(y, mpfr_y)
    elif isinstance(y, mpz):
        mpfr_y = _new_mpfr()
        gmp.mpfr_set_z(mpfr_y, y._mpz, gmp.MPFR_RNDN)
    elif isinstance(y, mpq):
        mpfr_y = _new_mpfr()
        gmp.mpfr_set_q(mpfr_y, y._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError

    res = _new_mpfr(min(gmp.mpfr_get_prec(mpfr_x), gmp.mpfr_get_prec(mpfr_x)))
    gmp.mpfr_atan2(res, mpfr_y, mpfr_x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Exemple #6
0
def factorial(n):
    """
    factorial(n) -> number

    Return the floating-point approximation to the factorial of n.

    See fac(n) to get the exact integer result.
    """
    if isinstance(n, (int, long)):
        if 0 <= n <= MAX_UI:
            res = _new_mpfr()
            gmp.mpfr_fac_ui(res, n, gmp.MPFR_RNDN)
            return mpfr._from_c_mpfr(res)
        elif n < 0:
            raise ValueError("factorial() of negative number")
    raise TypeError("factorial() requires 'int' argument")