Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def __rsub__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_ui_sub(res, other, self._mpz)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_sub(res, res, self._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
Ejemplo n.º 7
0
 def __rsub__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_ui_sub(res, other, self._mpz)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_sub(res, res, self._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
 def __mul__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_mul_ui(res, self._mpz, other)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_mul(res, res, self._mpz)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         res = _new_mpz()
         gmp.mpz_mul(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
Ejemplo n.º 10
0
 def __mul__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_mul_ui(res, self._mpz, other)
         else:
             _pylong_to_mpz(other, res)
             gmp.mpz_mul(res, res, self._mpz)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         res = _new_mpz()
         gmp.mpz_mul(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
Ejemplo n.º 11
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
Ejemplo n.º 12
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
Ejemplo n.º 13
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
Ejemplo n.º 14
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
Ejemplo n.º 15
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)
Ejemplo n.º 16
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
Ejemplo n.º 17
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
Ejemplo n.º 18
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)
Ejemplo n.º 19
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
Ejemplo n.º 20
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)
Ejemplo n.º 21
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)