예제 #1
0
    def __xor__(self, other):
        res = _new_mpz()
        if isinstance(other, (int, long)):
            oth = _new_mpz()
            _pyint_to_mpz(other, oth)
            gmp.mpz_xor(res, self._mpz, oth)
            _del_mpz(oth)
        else:
            gmp.mpz_xor(res, self._mpz, other._mpz)

        return mpz._from_c_mpz(res)
예제 #2
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
    def __xor__(self, other):
        res = _new_mpz()
        if isinstance(other, (int, long)):
            oth = _new_mpz()
            _pyint_to_mpz(other, oth)
            gmp.mpz_xor(res, self._mpz, oth)
            _del_mpz(oth)
        else:
            gmp.mpz_xor(res, self._mpz, other._mpz)

        return mpz._from_c_mpz(res)
예제 #3
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __rmod__(self, other):
     if not isinstance(other, (int, long)):
         return NotImplemented
     if self == 0:
         raise ZeroDivisionError("mpz modulo by zero")
     r = _new_mpz()
     oth = _new_mpz()
     _pylong_to_mpz(other, oth)
     gmp.mpz_fdiv_r(r, oth, self._mpz)
     _del_mpz(oth)
     return mpz._from_c_mpz(r)
예제 #4
0
 def __rmod__(self, other):
     if not isinstance(other, (int, long)):
         return NotImplemented
     if self == 0:
         raise ZeroDivisionError('mpz modulo by zero')
     r = _new_mpz()
     oth = _new_mpz()
     _pylong_to_mpz(other, oth)
     gmp.mpz_fdiv_r(r, oth, self._mpz)
     _del_mpz(oth)
     return mpz._from_c_mpz(r)
예제 #5
0
 def __sub__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_sub_ui(res, self._mpz, other)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_sub(res, self._mpz, res)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         res = _new_mpz()
         gmp.mpz_sub(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
예제 #6
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __sub__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_sub_ui(res, self._mpz, other)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_sub(res, self._mpz, res)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         res = _new_mpz()
         gmp.mpz_sub(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
예제 #7
0
 def __pow__(self, other):
     res = _new_mpfr()
     if isinstance(other, mpfr):
         gmp.mpfr_pow(res, self._mpfr, other._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, mpq):
         # There is no mpfr_pow_q
         gmp.mpfr_set_q(res, other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_pow(res, self._mpfr, res, gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         gmp.mpfr_pow_z(res, self._mpfr, other._mpz, gmp.MPFR_RNDN)
     elif isinstance(other, float):
         # There is no mpfr_pow_d
         gmp.mpfr_set_d(res, other, gmp.MPFR_RNDN)
         gmp.mpfr_pow(res, self._mpfr, res, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_pow_si(res, self._mpfr, other, gmp.MPFR_RNDN)
         elif 0 <= other <= MAX_UI:
             gmp.mpfr_pow_ui(res, self._mpfr, other, gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pylong_to_mpz(other, tmp_mpz)
             gmp.mpfr_pow_z(res, self._mpfr, tmp_mpz, gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpfr._from_c_mpfr(res)
예제 #8
0
 def __rtruediv__(self, other):
     res = _new_mpfr()
     if isinstance(other, mpq):
         # There is no mpfr_q_div
         gmp.mpfr_set_q(res, other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_div(res, res, self._mpfr, gmp.MPFR_RNDN)
         pass
     elif isinstance(other, mpz):
         # There is no mpfr_z_div
         gmp.mpfr_set_z(res, other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_div(res, res, self._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, float):
         gmp.mpfr_d_div(res, other, self._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_si_div(res, other, self._mpfr, gmp.MPFR_RNDN)
         elif 0 <= other <= MAX_UI:
             gmp.mpfr_ui_div(res, other, self._mpfr, gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pylong_to_mpz(other, tmp_mpz)
             gmp.mpfr_set_z(res, tmp_mpz, gmp.MPFR_RNDN)
             gmp.mpfr_div(res, res, self._mpfr, gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpfr._from_c_mpfr(res)
예제 #9
0
 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)
예제 #10
0
파일: mpc.py 프로젝트: sn6uv/gmpy_cffi
 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)
예제 #11
0
 def __rshift__(self, other):
     if not isinstance(other, (int, long, mpz)):
         return NotImplemented
     oth = gmp.mpz_get_ui(other._mpz) if isinstance(other, mpz) else other
     res = _new_mpz()
     gmp.mpz_fdiv_q_2exp(res, self._mpz, oth)
     return mpz._from_c_mpz(res)
예제 #12
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __rshift__(self, other):
     if not isinstance(other, (int, long, mpz)):
         return NotImplemented
     oth = gmp.mpz_get_ui(other._mpz) if isinstance(other, mpz) else other
     res = _new_mpz()
     gmp.mpz_fdiv_q_2exp(res, self._mpz, oth)
     return mpz._from_c_mpz(res)
예제 #13
0
파일: mpc.py 프로젝트: sn6uv/gmpy_cffi
 def __rsub__(self, other):
     res = _new_mpc()  # TODO use context precision
     if isinstance(other, mpfr):
         gmp.mpc_fr_sub(res, other._mpfr, self._mpc, gmp.MPC_RNDNN)
     elif isinstance(other, mpq):
         gmp.mpfr_sub_q(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_neg(gmp.mpc_realref(res), gmp.mpc_realref(res), gmp.MPFR_RNDN)
         gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         gmp.mpfr_z_sub(gmp.mpc_realref(res), other._mpz, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN)
         gmp.mpfr_neg(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, complex):
         gmp.mpfr_d_sub(gmp.mpc_realref(res), other.real, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN)
         gmp.mpfr_d_sub(gmp.mpc_imagref(res), other.imag, gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, float):
         gmp.mpfr_d_sub(gmp.mpc_realref(res), other, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN)
         gmp.mpfr_neg(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             gmp.mpc_ui_sub(res, other, self._mpc, gmp.MPC_RNDNN)
         elif -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_si_sub(gmp.mpc_realref(res), other, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN)
             gmp.mpfr_neg(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_z_sub(gmp.mpc_realref(res), tmp_mpz, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN)
             gmp.mpfr_neg(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)
예제 #14
0
 def __rfloordiv__(self, other):
     if isinstance(other, (int, long)):
         if self == 0:
             raise ZeroDivisionError('mpz division by zero')
         res = _new_mpz()
         _pylong_to_mpz(other, res)
         gmp.mpz_fdiv_q(res, res, self._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
예제 #15
0
 def __floordiv__(self, other):
     if isinstance(other, (int, long)):
         if other == 0:
             raise ZeroDivisionError('mpz division by zero')
         res = _new_mpz()
         if 0 < other <= MAX_UI:
             gmp.mpz_fdiv_q_ui(res, self._mpz, other)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_fdiv_q(res, self._mpz, res)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         if other == 0:
             raise ZeroDivisionError('mpz division by zero')
         res = _new_mpz()
         gmp.mpz_fdiv_q(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
예제 #16
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __rfloordiv__(self, other):
     if isinstance(other, (int, long)):
         if self == 0:
             raise ZeroDivisionError("mpz division by zero")
         res = _new_mpz()
         _pylong_to_mpz(other, res)
         gmp.mpz_fdiv_q(res, res, self._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
예제 #17
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __floordiv__(self, other):
     if isinstance(other, (int, long)):
         if other == 0:
             raise ZeroDivisionError("mpz division by zero")
         res = _new_mpz()
         if 0 < other <= MAX_UI:
             gmp.mpz_fdiv_q_ui(res, self._mpz, other)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_fdiv_q(res, self._mpz, res)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         if other == 0:
             raise ZeroDivisionError("mpz division by zero")
         res = _new_mpz()
         gmp.mpz_fdiv_q(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
예제 #18
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __mod__(self, other):
     if isinstance(other, (int, long)):
         if other == 0:
             raise ZeroDivisionError("mpz modulo by zero")
         r = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_fdiv_r_ui(r, self._mpz, other)
         else:
             oth = _new_mpz()
             _pylong_to_mpz(other, oth)
             gmp.mpz_fdiv_r(r, self._mpz, oth)
             _del_mpz(oth)
         return mpz._from_c_mpz(r)
     elif isinstance(other, mpz):
         if other == 0:
             raise ZeroDivisionError("mpz modulo by zero")
         r = _new_mpz()
         gmp.mpz_fdiv_r(r, self._mpz, other._mpz)
         return mpz._from_c_mpz(r)
     else:
         return NotImplemented
예제 #19
0
    def __rpow__(self, other):
        if not isinstance(other, (int, long)):
            return NotImplemented

        if self < 0:
            raise ValueError('mpz.pow with negative exponent')

        res = _new_mpz()

        exp = int(self)
        if exp > MAX_UI:
            raise ValueError('mpz.pow with outragous exponent')
        if 0 <= other <= MAX_UI:
            gmp.mpz_ui_pow_ui(res, other, exp)
        else:
            base = _new_mpz()
            _pylong_to_mpz(other, base)
            gmp.mpz_pow_ui(res, base, exp)
            _del_mpz(base)

        return mpz._from_c_mpz(res)
예제 #20
0
 def __mod__(self, other):
     if isinstance(other, (int, long)):
         if other == 0:
             raise ZeroDivisionError('mpz modulo by zero')
         r = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_fdiv_r_ui(r, self._mpz, other)
         else:
             oth = _new_mpz()
             _pylong_to_mpz(other, oth)
             gmp.mpz_fdiv_r(r, self._mpz, oth)
             _del_mpz(oth)
         return mpz._from_c_mpz(r)
     elif isinstance(other, mpz):
         if other == 0:
             raise ZeroDivisionError('mpz modulo by zero')
         r = _new_mpz()
         gmp.mpz_fdiv_r(r, self._mpz, other._mpz)
         return mpz._from_c_mpz(r)
     else:
         return NotImplemented
예제 #21
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
    def __rpow__(self, other):
        if not isinstance(other, (int, long)):
            return NotImplemented

        if self < 0:
            raise ValueError("mpz.pow with negative exponent")

        res = _new_mpz()

        exp = int(self)
        if exp > MAX_UI:
            raise ValueError("mpz.pow with outragous exponent")
        if 0 <= other <= MAX_UI:
            gmp.mpz_ui_pow_ui(res, other, exp)
        else:
            base = _new_mpz()
            _pylong_to_mpz(other, base)
            gmp.mpz_pow_ui(res, base, exp)
            _del_mpz(base)

        return mpz._from_c_mpz(res)
예제 #22
0
    def __pow__(self, power, modulo=None):
        if not isinstance(power, (int, long, mpz)):
            return NotImplemented
        if modulo is not None and not isinstance(modulo, (int, long, mpz)):
            return NotImplemented

        if power < 0:
            raise ValueError('mpz.pow with negative exponent')

        res = _new_mpz()
        if modulo is None:
            exp = int(power)
            if exp > MAX_UI:
                raise ValueError('mpz.pow with outragous exponent')
            gmp.mpz_pow_ui(res, self._mpz, exp)
        else:
            del_mod = del_exp = False
            if isinstance(modulo, (int, long)):
                mod = _new_mpz()
                _pylong_to_mpz(abs(modulo), mod)
                del_mod = True
            else:
                mod = modulo._mpz
            if isinstance(power, (int, long)) and power <= MAX_UI:
                gmp.mpz_powm_ui(res, self._mpz, power, mod)
            else:
                if isinstance(power, (int, long)):
                    exp = _new_mpz()
                    _pylong_to_mpz(power, exp)
                    del_exp = True
                else:
                    exp = power._mpz
                gmp.mpz_powm(res, self._mpz, exp, mod)
                if del_exp:
                    _del_mpz(exp)
                if del_mod:
                    _del_mpz(mod)

        return mpz._from_c_mpz(res)
예제 #23
0
 def __int__(self):
     if not gmp.mpfr_number_p(self._mpfr):
         raise ValueError("Cannot convert '%s' to int" % self)
     elif gmp.mpfr_fits_slong_p(self._mpfr, gmp.MPFR_RNDN):
         return gmp.mpfr_get_si(self._mpfr, gmp.MPFR_RNDN)
     elif gmp.mpfr_fits_ulong_p(self._mpfr, gmp.MPFR_RNDN):
         return gmp.mpfr_get_ui(self._mpfr, gmp.MPFR_RNDN)
     else:
         tmp_mpz = _new_mpz()
         gmp.mpfr_get_z(tmp_mpz, self._mpfr, gmp.MPFR_RNDN)
         res = _mpz_to_pylong(tmp_mpz)
         _del_mpz(tmp_mpz)
         return res
예제 #24
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
    def __pow__(self, power, modulo=None):
        if not isinstance(power, (int, long, mpz)):
            return NotImplemented
        if modulo is not None and not isinstance(modulo, (int, long, mpz)):
            return NotImplemented

        if power < 0:
            raise ValueError("mpz.pow with negative exponent")

        res = _new_mpz()
        if modulo is None:
            exp = int(power)
            if exp > MAX_UI:
                raise ValueError("mpz.pow with outragous exponent")
            gmp.mpz_pow_ui(res, self._mpz, exp)
        else:
            del_mod = del_exp = False
            if isinstance(modulo, (int, long)):
                mod = _new_mpz()
                _pylong_to_mpz(abs(modulo), mod)
                del_mod = True
            else:
                mod = modulo._mpz
            if isinstance(power, (int, long)) and power <= MAX_UI:
                gmp.mpz_powm_ui(res, self._mpz, power, mod)
            else:
                if isinstance(power, (int, long)):
                    exp = _new_mpz()
                    _pylong_to_mpz(power, exp)
                    del_exp = True
                else:
                    exp = power._mpz
                gmp.mpz_powm(res, self._mpz, exp, mod)
                if del_exp:
                    _del_mpz(exp)
                if del_mod:
                    _del_mpz(mod)

        return mpz._from_c_mpz(res)
예제 #25
0
 def __cmp(self, other):
     if isinstance(other, mpz):
         res = gmp.mpz_cmp(self._mpz, other._mpz)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             res = gmp.mpz_cmp_ui(self._mpz, other)
         else:
             oth = _new_mpz()
             _pylong_to_mpz(other, oth)
             res = gmp.mpz_cmp(self._mpz, oth)
             _del_mpz(oth)
     elif isinstance(other, float):
         res = gmp.mpz_cmp_d(self._mpz, other)
     else:
         return None
     return res
예제 #26
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __cmp(self, other):
     if isinstance(other, mpz):
         res = gmp.mpz_cmp(self._mpz, other._mpz)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             res = gmp.mpz_cmp_ui(self._mpz, other)
         else:
             oth = _new_mpz()
             _pylong_to_mpz(other, oth)
             res = gmp.mpz_cmp(self._mpz, oth)
             _del_mpz(oth)
     elif isinstance(other, float):
         res = gmp.mpz_cmp_d(self._mpz, other)
     else:
         return None
     return res
예제 #27
0
 def __sub__(self, other):
     res = _new_mpc()  # TODO use context precision
     if isinstance(other, mpc):
         gmp.mpc_sub(res, self._mpc, other._mpc, gmp.MPC_RNDNN)
     elif isinstance(other, mpfr):
         gmp.mpc_sub_fr(res, self._mpc, other._mpfr, gmp.MPC_RNDNN)
     elif isinstance(other, mpq):
         gmp.mpfr_sub_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_sub_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_sub_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other.real, gmp.MPFR_RNDN)
         gmp.mpfr_sub_d(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                        other.imag, gmp.MPFR_RNDN)
     elif isinstance(other, float):
         gmp.mpfr_sub_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_sub_ui(res, self._mpc, other, gmp.MPC_RNDNN)
         elif -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_sub_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_sub_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)
예제 #28
0
    def __init__(self, n=0, base=None):
        """
        mpz() -> mpz(0)

            If no argument is given, return mpz(0).

        mpz(n) -> mpz

            Return an 'mpz' object with a numeric value 'n' (truncating n
            to its integer part if it's a Fraction, 'mpq', Decimal, float
            or 'mpfr').

        mpz(s[, base=0]):

            Return an 'mpz' object from a string 's' made of digits in the
            given base.  If base=0, binary, octal, or hex Python strings
            are recognized by leading 0b, 0o, or 0x characters, otherwise
            the string is assumed to be decimal. Values for base can range
            between 2 and 62.
        """

        if isinstance(n, self.__class__):
            self._mpz = n._mpz
            return
        a = self._mpz = ffi.gc(_new_mpz(), _del_mpz)
        if isinstance(n, str):
            if base is None:
                base = 10
            if base == 0 or 2 <= base <= 62:
                if gmp.mpz_set_str(a, n.encode('UTF-8'), base) != 0:
                    raise ValueError("Can't create mpz from %s with base %s" %
                                     (n, base))
            else:
                raise ValueError('base must be 0 or 2..62, not %s' % base)
        elif base is not None:
            raise ValueError('Base only allowed for str, not for %s.' %
                             type(n))
        elif isinstance(n, float):
            gmp.mpz_set_d(a, n)
        elif isinstance(n, (int, long)):
            _pyint_to_mpz(n, a)
        else:
            raise TypeError
예제 #29
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
    def __init__(self, n=0, base=None):
        """
        mpz() -> mpz(0)

            If no argument is given, return mpz(0).

        mpz(n) -> mpz

            Return an 'mpz' object with a numeric value 'n' (truncating n
            to its integer part if it's a Fraction, 'mpq', Decimal, float
            or 'mpfr').

        mpz(s[, base=0]):

            Return an 'mpz' object from a string 's' made of digits in the
            given base.  If base=0, binary, octal, or hex Python strings
            are recognized by leading 0b, 0o, or 0x characters, otherwise
            the string is assumed to be decimal. Values for base can range
            between 2 and 62.
        """

        if isinstance(n, self.__class__):
            self._mpz = n._mpz
            return
        a = self._mpz = ffi.gc(_new_mpz(), _del_mpz)
        if isinstance(n, str):
            if base is None:
                base = 10
            if base == 0 or 2 <= base <= 62:
                if gmp.mpz_set_str(a, n.encode("UTF-8"), base) != 0:
                    raise ValueError("Can't create mpz from %s with base %s" % (n, base))
            else:
                raise ValueError("base must be 0 or 2..62, not %s" % base)
        elif base is not None:
            raise ValueError("Base only allowed for str, not for %s." % type(n))
        elif isinstance(n, float):
            gmp.mpz_set_d(a, n)
        elif isinstance(n, (int, long)):
            _pyint_to_mpz(n, a)
        else:
            raise TypeError
예제 #30
0
 def __cmp(self, other):
     if isinstance(other, mpfr):
         return gmp.mpfr_cmp(self._mpfr, other._mpfr)
     elif isinstance(other, float):
         return gmp.mpfr_cmp_d(self._mpfr, other)
     if isinstance(other, (int, long)):
         if -sys.maxsize - 1 <= other < sys.maxsize:
             return gmp.mpfr_cmp_ui(self._mpfr, other)
         elif 0 <= other <= MAX_UI:
             return gmp.mpfr_cmp_ui(self._mpfr, other)
         else:
             tmp_mpz = _new_mpz()
             _pylong_to_mpz(other, tmp_mpz)
             result = gmp.mpfr_cmp_z(self._mpfr, tmp_mpz)
             _del_mpz(tmp_mpz)
             return result
     elif isinstance(other, mpz):
         return gmp.mpfr_cmp_z(self._mpfr, other._mpz)
     elif isinstance(other, mpq):
         return gmp.mpfr_cmp_q(self._mpfr, other._mpq)
     return None
예제 #31
0
파일: mpc.py 프로젝트: sn6uv/gmpy_cffi
    def __eq__(self, other):
        # Complex Comparison
        if isinstance(other, mpc):
            return gmp.mpc_cmp(self._mpc, other._mpc) == 0
        elif isinstance(other, complex):
            return (
                gmp.mpfr_cmp_d(gmp.mpc_realref(self._mpc), other.real) == 0
                and gmp.mpfr_cmp_d(gmp.mpc_imagref(self._mpc), other.imag) == 0
            )

        # Real Comparison
        realref = gmp.mpc_realref(self._mpc)
        if isinstance(other, mpfr):
            result = gmp.mpfr_cmp(realref, other._mpfr)
        elif isinstance(other, float):
            result = gmp.mpfr_cmp_d(realref, other)
        elif isinstance(other, (int, long)):
            if -sys.maxsize - 1 <= other < sys.maxsize:
                result = gmp.mpfr_cmp_ui(realref, other)
            elif 0 <= other <= MAX_UI:
                result = gmp.mpfr_cmp_ui(realref, other)
            else:
                tmp_mpz = _new_mpz()
                _pyint_to_mpz(other, tmp_mpz)
                result = gmp.mpfr_cmp_z(realref, tmp_mpz)
                _del_mpz(tmp_mpz)
                result = result
        elif isinstance(other, mpz):
            result = gmp.mpfr_cmp_z(realref, other._mpz)
        elif isinstance(other, mpq):
            result = gmp.mpfr_cmp_q(realref, other._mpq)
        else:
            return NotImplemented

        if not gmp.mpfr_zero_p(gmp.mpc_imagref(self._mpc)):
            return False
        return result == 0
예제 #32
0
    def __eq__(self, other):
        # Complex Comparison
        if isinstance(other, mpc):
            return gmp.mpc_cmp(self._mpc, other._mpc) == 0
        elif isinstance(other, complex):
            return (gmp.mpfr_cmp_d(gmp.mpc_realref(self._mpc), other.real) == 0
                    and gmp.mpfr_cmp_d(gmp.mpc_imagref(self._mpc),
                                       other.imag) == 0)

        # Real Comparison
        realref = gmp.mpc_realref(self._mpc)
        if isinstance(other, mpfr):
            result = gmp.mpfr_cmp(realref, other._mpfr)
        elif isinstance(other, float):
            result = gmp.mpfr_cmp_d(realref, other)
        elif isinstance(other, (int, long)):
            if -sys.maxsize - 1 <= other < sys.maxsize:
                result = gmp.mpfr_cmp_ui(realref, other)
            elif 0 <= other <= MAX_UI:
                result = gmp.mpfr_cmp_ui(realref, other)
            else:
                tmp_mpz = _new_mpz()
                _pyint_to_mpz(other, tmp_mpz)
                result = gmp.mpfr_cmp_z(realref, tmp_mpz)
                _del_mpz(tmp_mpz)
                result = result
        elif isinstance(other, mpz):
            result = gmp.mpfr_cmp_z(realref, other._mpz)
        elif isinstance(other, mpq):
            result = gmp.mpfr_cmp_q(realref, other._mpq)
        else:
            return NotImplemented

        if not gmp.mpfr_zero_p(gmp.mpc_imagref(self._mpc)):
            return False
        return result == 0
예제 #33
0
 def __floor__(self):
     res = _new_mpz()
     gmp.mpz_fdiv_q(res, gmp.mpq_numref(self._mpq), gmp.mpq_denref(self._mpq))
     return mpz._from_c_mpz(res)
예제 #34
0
 def __long__(self):
     res = _new_mpz()
     gmp.mpz_tdiv_q(res, gmp.mpq_numref(self._mpq), gmp.mpq_denref(self._mpq))
     return long(mpz._from_c_mpz(res))
예제 #35
0
 def denominator(self):
     if self._denominator is None:
         den = _new_mpz()
         gmp.mpq_get_den(den, self._mpq)
         self._denominator = mpz._from_c_mpz(den)
     return self._denominator
예제 #36
0
 def numerator(self):
     if self._numerator is None:
         num = _new_mpz()
         gmp.mpq_get_num(num, self._mpq)
         self._numerator = mpz._from_c_mpz(num)
     return self._numerator
예제 #37
0
 def __abs__(self):
     res = _new_mpz()
     gmp.mpz_abs(res, self._mpz)
     return mpz._from_c_mpz(res)
예제 #38
0
 def __neg__(self):
     res = _new_mpz()
     gmp.mpz_neg(res, self._mpz)
     return mpz._from_c_mpz(res)
예제 #39
0
 def __invert__(self):
     res = _new_mpz()
     gmp.mpz_com(res, self._mpz)
     return mpz._from_c_mpz(res)
예제 #40
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __invert__(self):
     res = _new_mpz()
     gmp.mpz_com(res, self._mpz)
     return mpz._from_c_mpz(res)
예제 #41
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __neg__(self):
     res = _new_mpz()
     gmp.mpz_neg(res, self._mpz)
     return mpz._from_c_mpz(res)
예제 #42
0
파일: mpz.py 프로젝트: sn6uv/gmpy_cffi
 def __abs__(self):
     res = _new_mpz()
     gmp.mpz_abs(res, self._mpz)
     return mpz._from_c_mpz(res)