def __rpow__(self, other): res = _new_mpc() # TODO use context precision if isinstance(other, mpfr): gmp.mpc_set_fr(res, other._mpfr, gmp.MPFR_RNDN) gmp.mpc_pow(res, res, self._mpc, gmp.MPFR_RNDN) elif isinstance(other, mpq): gmp.mpc_set_q(res, other._mpq, gmp.MPFR_RNDN) gmp.mpc_pow(res, res, self._mpc, gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpc_set_z(res, other._mpz, gmp.MPC_RNDNN) gmp.mpc_pow(res, res, self._mpc, gmp.MPFR_RNDN) elif isinstance(other, complex): gmp.mpc_set_d_d(res, other.real, other.imag, gmp.MPC_RNDNN) gmp.mpc_pow(res, res, self._mpc, gmp.MPC_RNDNN) elif isinstance(other, float): gmp.mpc_set_d(res, other, gmp.MPFR_RNDN) gmp.mpc_pow(res, res, self._mpc, gmp.MPC_RNDNN) elif isinstance(other, (int, long)): if 0 <= other <= MAX_UI: gmp.mpc_set_ui(res, other, gmp.MPC_RNDNN) elif -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpc_set_si(res, other, gmp.MPC_RNDNN) else: tmp_mpz = _new_mpz() _pyint_to_mpz(other, tmp_mpz) gmp.mpc_set_z(res, tmp_mpz, gmp.MPC_RNDNN) _del_mpz(tmp_mpz) gmp.mpc_pow(res, res, self._mpc, gmp.MPC_RNDNN) else: return NotImplemented return mpc._from_c_mpc(res)
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