Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
0
 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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
0
 def __add__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_add_ui(res, self._mpz, other)
         else:
             _pyint_to_mpz(other, res)
             gmp.mpz_add(res, self._mpz, res)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         res = _new_mpz()
         gmp.mpz_add(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
Beispiel #7
0
 def __add__(self, other):
     if isinstance(other, (int, long)):
         res = _new_mpz()
         if 0 <= other <= MAX_UI:
             gmp.mpz_add_ui(res, self._mpz, other)
         else:
             _pyint_to_mpz(other, res)
             gmp.mpz_add(res, self._mpz, res)
         return mpz._from_c_mpz(res)
     elif isinstance(other, mpz):
         res = _new_mpz()
         gmp.mpz_add(res, self._mpz, other._mpz)
         return mpz._from_c_mpz(res)
     else:
         return NotImplemented
Beispiel #8
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)
Beispiel #9
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
Beispiel #10
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
Beispiel #11
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
Beispiel #12
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
Beispiel #13
0
    def __init__(self, *args):
        """
        mpq() -> mpq(0,1)

             If no argument is given, return mpq(0,1).

        mpq(n) -> mpq

             Return an 'mpq' object with a numeric value n. Decimal and
             Fraction values are converted exactly.

        mpq(n,m) -> mpq

             Return an 'mpq' object with a numeric value n/m.

        mpq(s[, base=10]) -> mpq

             Return an 'mpq' object from a string s made up of digits in
             the given base. s may be made up of two numbers in the same
             base separated by a '/' character.
        """

        #TODO kwargs (base)

        nargs = len(args)
        if nargs == 1 and isinstance(args[0], self.__class__):
            self._mpq = args[0]._mpq
            return

        a = self._mpq = ffi.gc(_new_mpq(), _del_mpq)

        if nargs == 0:
            gmp.mpq_set_ui(a, 0, 1)
        elif nargs == 1:
            if isinstance(args[0], float):
                gmp.mpq_set_d(a, args[0])
            elif isinstance(args[0], (int, long)):
                _pyint_to_mpq(args[0], a)
            elif isinstance(args[0], mpz):
                gmp.mpq_set_z(a, args[0]._mpz)
            elif isinstance(args[0], str):
                _str_to_mpq(args[0], 10, a)
            else:
                raise TypeError('mpq() requires numeric or string argument')
        elif nargs == 2:
            if isinstance(args[0], str):
                _str_to_mpq(args[0], args[1], a)
            elif all(isinstance(arg, (int, long, mpz)) for arg in args):
                # Set Numerator
                if isinstance(args[0], mpz):
                    gmp.mpq_set_num(a, args[0]._mpz)
                else:
                    num = gmp.mpq_numref(a)
                    _pyint_to_mpz(args[0], num)

                # Set Denominator
                if args[1] == 0:
                    raise ZeroDivisionError("zero denominator in 'mpq'")

                if isinstance(args[1], mpz):
                    gmp.mpq_set_den(a, args[1]._mpz)
                else:
                    den = gmp.mpq_denref(a)
                    _pyint_to_mpz(args[1], den)
            else:
                # Numerator
                if isinstance(args[0], mpq):
                    gmp.mpq_set(a, args[0]._mpq)
                elif isinstance(args[0], float):
                    gmp.mpq_set_d(a, args[0])
                elif isinstance(args[0], (int, long)):
                    _pyint_to_mpq(args[0], a)
                elif isinstance(args[0], mpz):
                    gmp.mpq_set_z(a, args[0]._mpz)
                else:
                    raise TypeError('mpq() requires numeric or string argument')

                # Denominator
                b = _new_mpq()
                if isinstance(args[1], mpq):
                    gmp.mpq_set(b, args[1]._mpq)
                elif isinstance(args[1], float):
                    gmp.mpq_set_d(b, args[1])
                elif isinstance(args[1], (int, long)):
                    _pyint_to_mpq(args[1], b)
                elif isinstance(args[1], mpz):
                    gmp.mpq_set_z(b, args[1]._mpz)
                else:
                    raise TypeError('mpq() requires numeric or string argument')

                # Divide them
                if gmp.mpq_sgn(b) == 0:
                    _del_mpq(b)
                    raise ZeroDivisionError

                gmp.mpq_div(a, a, b)
                _del_mpq(b)
        else:
            raise TypeError("mpq() requires 0, 1 or 2 arguments")

        # TODO only canonicalize when required (e.g. optimize mpq(42))
        gmp.mpq_canonicalize(a)