Exemplo n.º 1
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))
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
0
def exp(x):
    """
    exp(x) -> number

    Return the exponential of x.
    """
    try:
        res, x = _init_check_mpfr(x)
        gmp.mpfr_exp(res, x, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        res, x = _init_check_mpc(x)
        gmp.mpc_exp(res, x, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)
Exemplo n.º 8
0
def fma(x, y, z):
    """
    fma(x, y, z) -> number

    Return the correctly rounded result of (x * y) + z.
    """
    try:
        # 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_fma(res, mpfr_x, mpfr_y, mpfr_z, gmp.MPFR_RNDN)
        return mpfr._from_c_mpfr(res)
    except TypeError:
        # XXX Optimise
        res, mpc_x = _init_check_mpc(x)
        res, mpc_y = _init_check_mpc(y)
        res, mpc_z = _init_check_mpc(z)
        gmp.mpc_fma(res, mpc_x, mpc_y, mpc_z, gmp.MPC_RNDNN)
        return mpc._from_c_mpc(res)