Beispiel #1
0
 def __add__(self, other):
     res = _new_mpc()  # TODO use context precision
     if isinstance(other, mpc):
         gmp.mpc_add(res, self._mpc, other._mpc, gmp.MPC_RNDNN)
     elif isinstance(other, mpfr):
         gmp.mpc_add_fr(res, self._mpc, other._mpfr, gmp.MPC_RNDNN)
     elif isinstance(other, mpq):
         gmp.mpfr_add_q(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         gmp.mpfr_add_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, complex):
         gmp.mpfr_add_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other.real, gmp.MPFR_RNDN)
         gmp.mpfr_add_d(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), other.imag, gmp.MPFR_RNDN)
     elif isinstance(other, float):
         gmp.mpfr_add_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other, gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             gmp.mpc_add_ui(res, self._mpc, other, gmp.MPC_RNDNN)
         elif -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_add_si(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other, gmp.MPFR_RNDN)
             gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pyint_to_mpz(other, tmp_mpz)
             gmp.mpfr_add_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), tmp_mpz, gmp.MPFR_RNDN)
             gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpc._from_c_mpc(res)
Beispiel #2
0
 def __add__(self, other):
     res = _new_mpc()  # TODO use context precision
     if isinstance(other, mpc):
         gmp.mpc_add(res, self._mpc, other._mpc, gmp.MPC_RNDNN)
     elif isinstance(other, mpfr):
         gmp.mpc_add_fr(res, self._mpc, other._mpfr, gmp.MPC_RNDNN)
     elif isinstance(other, mpq):
         gmp.mpfr_add_q(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                      gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         gmp.mpfr_add_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                      gmp.MPFR_RNDN)
     elif isinstance(other, complex):
         gmp.mpfr_add_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other.real, gmp.MPFR_RNDN)
         gmp.mpfr_add_d(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                        other.imag, gmp.MPFR_RNDN)
     elif isinstance(other, float):
         gmp.mpfr_add_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other, gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                      gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             gmp.mpc_add_ui(res, self._mpc, other, gmp.MPC_RNDNN)
         elif -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_add_si(gmp.mpc_realref(res),
                             gmp.mpc_realref(self._mpc), other,
                             gmp.MPFR_RNDN)
             gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                          gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pyint_to_mpz(other, tmp_mpz)
             gmp.mpfr_add_z(gmp.mpc_realref(res),
                            gmp.mpc_realref(self._mpc), tmp_mpz,
                            gmp.MPFR_RNDN)
             gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                          gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpc._from_c_mpc(res)
Beispiel #3
0
 def __add__(self, other):
     res = _new_mpfr()
     if isinstance(other, mpfr):
         gmp.mpfr_add(res, self._mpfr, other._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, mpq):
         gmp.mpfr_add_q(res, self._mpfr, other._mpq, gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         gmp.mpfr_add_z(res, self._mpfr, other._mpz, gmp.MPFR_RNDN)
     elif isinstance(other, float):
         gmp.mpfr_add_d(res, self._mpfr, other, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_add_si(res, self._mpfr, other, gmp.MPFR_RNDN)
         elif 0 <= other <= MAX_UI:
             gmp.mpfr_add_ui(res, self._mpfr, other, gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pylong_to_mpz(other, tmp_mpz)
             gmp.mpfr_add_z(res, self._mpfr, tmp_mpz, gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpfr._from_c_mpfr(res)