Пример #1
0
 def __truediv__(self, other):
     res = _new_mpc()  # TODO use context precision
     if isinstance(other, mpc):
         gmp.mpc_div(res, self._mpc, other._mpc, gmp.MPC_RNDNN)
     elif isinstance(other, mpfr):
         gmp.mpc_div_fr(res, self._mpc, other._mpfr, gmp.MPC_RNDNN)
     elif isinstance(other, mpq):
         gmp.mpc_set_q(res, other._mpq, gmp.MPC_RNDNN)
         gmp.mpc_div(res, self._mpc, res, gmp.MPC_RNDNN)
     elif isinstance(other, mpz):
         gmp.mpfr_div_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_div_z(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), other._mpz, gmp.MPFR_RNDN)
     elif isinstance(other, complex):
         gmp.mpc_set_d_d(res, other.real, other.imag, gmp.MPC_RNDNN)
         gmp.mpc_div(res, self._mpc, res, gmp.MPC_RNDNN)
     elif isinstance(other, float):
         gmp.mpfr_div_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other, gmp.MPFR_RNDN)
         gmp.mpfr_div_d(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), other, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             gmp.mpc_div_ui(res, self._mpc, other, gmp.MPC_RNDNN)
         elif -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_div_si(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other, gmp.MPFR_RNDN)
             gmp.mpfr_div_si(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), other, gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pyint_to_mpz(other, tmp_mpz)
             gmp.mpfr_div_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), tmp_mpz, gmp.MPFR_RNDN)
             gmp.mpfr_div_z(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), tmp_mpz, gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpc._from_c_mpc(res)
Пример #2
0
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
Пример #3
0
def _mpc_to_str(a, base):
    real_str = _mpfr_to_str(gmp.mpc_realref(a))
    imag_str = _mpfr_to_str(gmp.mpc_imagref(a))
    if imag_str.startswith('-'):
        return real_str + imag_str + 'j'
    else:
        return real_str + '+' + imag_str + 'j'
Пример #4
0
def _mpc_to_str(a, base):
    real_str = _mpfr_to_str(gmp.mpc_realref(a))
    imag_str = _mpfr_to_str(gmp.mpc_imagref(a))
    if imag_str.startswith('-'):
        return real_str + imag_str + 'j'
    else:
        return real_str + '+' + imag_str + 'j'
Пример #5
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
Пример #6
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
Пример #7
0
 def __truediv__(self, other):
     res = _new_mpc()  # TODO use context precision
     if isinstance(other, mpc):
         gmp.mpc_div(res, self._mpc, other._mpc, gmp.MPC_RNDNN)
     elif isinstance(other, mpfr):
         gmp.mpc_div_fr(res, self._mpc, other._mpfr, gmp.MPC_RNDNN)
     elif isinstance(other, mpq):
         gmp.mpc_set_q(res, other._mpq, gmp.MPC_RNDNN)
         gmp.mpc_div(res, self._mpc, res, gmp.MPC_RNDNN)
     elif isinstance(other, mpz):
         gmp.mpfr_div_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_div_z(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                        other._mpz, gmp.MPFR_RNDN)
     elif isinstance(other, complex):
         gmp.mpc_set_d_d(res, other.real, other.imag, gmp.MPC_RNDNN)
         gmp.mpc_div(res, self._mpc, res, gmp.MPC_RNDNN)
     elif isinstance(other, float):
         gmp.mpfr_div_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc),
                        other, gmp.MPFR_RNDN)
         gmp.mpfr_div_d(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc),
                        other, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         if 0 <= other <= MAX_UI:
             gmp.mpc_div_ui(res, self._mpc, other, gmp.MPC_RNDNN)
         elif -sys.maxsize - 1 <= other <= sys.maxsize:
             gmp.mpfr_div_si(gmp.mpc_realref(res),
                             gmp.mpc_realref(self._mpc), other,
                             gmp.MPFR_RNDN)
             gmp.mpfr_div_si(gmp.mpc_imagref(res),
                             gmp.mpc_imagref(self._mpc), other,
                             gmp.MPFR_RNDN)
         else:
             tmp_mpz = _new_mpz()
             _pyint_to_mpz(other, tmp_mpz)
             gmp.mpfr_div_z(gmp.mpc_realref(res),
                            gmp.mpc_realref(self._mpc), tmp_mpz,
                            gmp.MPFR_RNDN)
             gmp.mpfr_div_z(gmp.mpc_imagref(res),
                            gmp.mpc_imagref(self._mpc), tmp_mpz,
                            gmp.MPFR_RNDN)
             _del_mpz(tmp_mpz)
     else:
         return NotImplemented
     return mpc._from_c_mpc(res)
Пример #8
0
    def __init__(self, *args):
        nargs = len(args)
        # if nargs == 1 and isinstance(args[0], self.__class__):
        #     self._mpc = args[0]._mpc
        #     return

        if nargs == 0:
            self._mpc = ffi.gc(_new_mpc(), _del_mpc)
            gmp.mpc_set_ui(self._mpc, 0, gmp.MPC_RNDNN)
        elif isinstance(args[0], str):  # unicode?
            # First argument is a string
            if nargs == 1:
                prec, base = 0, 10
            elif nargs == 2:
                prec, base = args[1], 10
            elif nargs == 3:
                prec, base = args[1], args[2]
            else:
                raise TypeError("function takes at most 3 arguments (4 given)")

            prec = _check_prec(prec)

            self._mpc = ffi.gc(_new_mpc(prec), _del_mpc)
            _str_to_mpc(args[0], base, self._mpc)
        elif isinstance(args[0], (mpc, complex)):
            # First argument is complex
            if nargs == 1:
                prec = (0, 0)
            elif nargs == 2:
                prec = _check_prec(args[1])
            else:
                raise TypeError("function takes at most 2 arguments (3 given)")

            self._mpc = ffi.gc(_new_mpc(prec), _del_mpc)

            if isinstance(args[0], mpc):
                gmp.mpc_set(self._mpc, args[0]._mpc, gmp.MPC_RNDNN)
            elif isinstance(args[0], complex):
                gmp.mpc_set_d_d(self._mpc, args[0].real, args[0].imag,
                                gmp.MPC_RNDNN)
        elif isinstance(args[0], (mpfr, mpq, mpz, float, int, long)):
            # First argument is real

            if nargs <= 2:
                prec = (0, 0)
            elif nargs == 3:
                prec = _check_prec(args[2])
            else:
                raise TypeError("function takes at most 3 arguments (4 given)")

            self._mpc = ffi.gc(_new_mpc(prec), _del_mpc)
            realref = gmp.mpc_realref(self._mpc)
            imagref = gmp.mpc_imagref(self._mpc)

            if isinstance(args[0], mpfr):
                gmp.mpfr_set(realref, args[0]._mpfr, gmp.MPFR_RNDN)
            elif isinstance(args[0], mpz):
                gmp.mpfr_set_z(realref, args[0]._mpz, gmp.MPFR_RNDN)
            elif isinstance(args[0], mpq):
                gmp.mpfr_set_q(realref, args[0]._mpq, gmp.MPFR_RNDN)
            elif isinstance(args[0], float):
                gmp.mpfr_set_d(realref, args[0], gmp.MPFR_RNDN)
            elif isinstance(args[0], (int, long)):
                _pyint_to_mpfr(args[0], realref)

            if nargs >= 2:
                # Check if second argument is real
                if isinstance(args[1], mpfr):
                    gmp.mpfr_set(imagref, args[1]._mpfr, gmp.MPFR_RNDN)
                elif isinstance(args[1], mpz):
                    gmp.mpfr_set_z(imagref, args[1]._mpz, gmp.MPFR_RNDN)
                elif isinstance(args[1], mpq):
                    gmp.mpfr_set_q(imagref, args[1]._mpq, gmp.MPFR_RNDN)
                elif isinstance(args[1], float):
                    gmp.mpfr_set_d(imagref, args[1], gmp.MPFR_RNDN)
                elif isinstance(args[1], (int, long)):
                    _pyint_to_mpfr(args[1], imagref)
                else:
                    raise TypeError(
                        "invalid type for imaginary component in mpc()")
            else:
                gmp.mpfr_set_ui(imagref, 0, gmp.MPFR_RNDN)
        else:
            raise TypeError("mpc() requires numeric or string argument")
Пример #9
0
 def __complex__(self):
     return complex(
         gmp.mpfr_get_d(gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN),
         gmp.mpfr_get_d(gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN))
Пример #10
0
    def __init__(self, *args):
        nargs = len(args)
        # if nargs == 1 and isinstance(args[0], self.__class__):
        #     self._mpc = args[0]._mpc
        #     return

        if nargs == 0:
            self._mpc = ffi.gc(_new_mpc(), _del_mpc)
            gmp.mpc_set_ui(self._mpc, 0, gmp.MPC_RNDNN)
        elif isinstance(args[0], str):  # unicode?
            # First argument is a string
            if nargs == 1:
                prec, base = 0, 10
            elif nargs == 2:
                prec, base = args[1], 10
            elif nargs == 3:
                prec, base = args[1], args[2]
            else:
                raise TypeError("function takes at most 3 arguments (4 given)")

            prec = _check_prec(prec)

            self._mpc = ffi.gc(_new_mpc(prec), _del_mpc)
            _str_to_mpc(args[0], base, self._mpc)
        elif isinstance(args[0], (mpc, complex)):
            # First argument is complex
            if nargs == 1:
                prec = (0, 0)
            elif nargs == 2:
                prec = _check_prec(args[1])
            else:
                raise TypeError("function takes at most 2 arguments (3 given)")

            self._mpc = ffi.gc(_new_mpc(prec), _del_mpc)

            if isinstance(args[0], mpc):
                gmp.mpc_set(self._mpc, args[0]._mpc, gmp.MPC_RNDNN)
            elif isinstance(args[0], complex):
                gmp.mpc_set_d_d(self._mpc, args[0].real, args[0].imag, gmp.MPC_RNDNN)
        elif isinstance(args[0], (mpfr, mpq, mpz, float, int, long)):
            # First argument is real

            if nargs <= 2:
                prec = (0, 0)
            elif nargs == 3:
                prec = _check_prec(args[2])
            else:
                raise TypeError("function takes at most 3 arguments (4 given)")

            self._mpc = ffi.gc(_new_mpc(prec), _del_mpc)
            realref = gmp.mpc_realref(self._mpc)
            imagref = gmp.mpc_imagref(self._mpc)

            if isinstance(args[0], mpfr):
                gmp.mpfr_set(realref, args[0]._mpfr, gmp.MPFR_RNDN)
            elif isinstance(args[0], mpz):
                gmp.mpfr_set_z(realref, args[0]._mpz, gmp.MPFR_RNDN)
            elif isinstance(args[0], mpq):
                gmp.mpfr_set_q(realref, args[0]._mpq, gmp.MPFR_RNDN)
            elif isinstance(args[0], float):
                gmp.mpfr_set_d(realref, args[0], gmp.MPFR_RNDN)
            elif isinstance(args[0], (int, long)):
                _pyint_to_mpfr(args[0], realref)

            if nargs >= 2:
                # Check if second argument is real
                if isinstance(args[1], mpfr):
                    gmp.mpfr_set(imagref, args[1]._mpfr, gmp.MPFR_RNDN)
                elif isinstance(args[1], mpz):
                    gmp.mpfr_set_z(imagref, args[1]._mpz, gmp.MPFR_RNDN)
                elif isinstance(args[1], mpq):
                    gmp.mpfr_set_q(imagref, args[1]._mpq, gmp.MPFR_RNDN)
                elif isinstance(args[1], float):
                    gmp.mpfr_set_d(imagref, args[1], gmp.MPFR_RNDN)
                elif isinstance(args[1], (int, long)):
                    _pyint_to_mpfr(args[1], imagref)
                else:
                    raise TypeError("invalid type for imaginary component in mpc()")
            else:
                gmp.mpfr_set_ui(imagref, 0, gmp.MPFR_RNDN)
        else:
            raise TypeError("mpc() requires numeric or string argument")
Пример #11
0
 def __complex__(self):
     return complex(
         gmp.mpfr_get_d(gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN),
         gmp.mpfr_get_d(gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN),
     )