def _init_check_mpc(x): """ Returns a new mpc and a pointer to a c mpc storing the value of x """ if isinstance(x, mpc): res = _new_mpc() mpc_x = x._mpc elif isinstance(x, complex): res = _new_mpc() mpc_x = res # avoid initialising another c mpc gmp.mpc_set_d_d(mpc_x, x.real, x.imag, gmp.MPC_RNDNN) elif isinstance(x, mpfr): res = _new_mpc() mpc_x = res # avoid initialising another c mpc gmp.mpc_set_fr(mpc_x, x, gmp.MPC_RNDNN) elif isinstance(x, float): res = _new_mpc() gmp.mpc_set_d(mpc_x, x, gmp.MPC_RNDNN) elif isinstance(x, (int, long)): res = _new_mpc() mpc_x = res # avoid initialising another c mpc _pyint_to_mpfr(x, gmp.mpc_realref(mpc_x)) gmp.mpfr_set_ui(gmp.mpc_imagref(mpc_x), 0, gmp.MPC_RNDNN) elif isinstance(x, mpz): res = _new_mpc() mpc_x = res # avoid initialising another c mpc gmp.mpc_set_z(mpc_x, x._mpz, gmp.MPC_RNDNN) elif isinstance(x, mpq): res = _new_mpc() mpc_x = res # avoid initialising another c mpc gmp.mpc_set_q(mpc_x, x._mpq, gmp.MPC_RNDNN) else: raise TypeError return res, mpc_x
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))