예제 #1
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
예제 #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)
예제 #3
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)
예제 #4
0
def sin_cos(x):
    """
    sin_cos(x) -> (number, number)

    Return a tuple containing the sine and cosine of x; x in radians.
    """
    if isinstance(x, mpfr):
        res1 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        res2 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
    elif isinstance(x, (int, long)):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        res1 = _new_mpfr()
        res2 = _new_mpfr()
        mpfr_x = res1
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        if isinstance(x, mpc):
            res1 = _new_mpc()
            res2 = _new_mpc()
            mpc_x = x._mpc
        elif isinstance(x, complex):
            res1 = _new_mpc()
            res2 = _new_mpc()
            mpc_x = res1
            gmp.mpc_set_d_d(mpc_x, x.real, x.imag, gmp.MPC_RNDNN)
        else:
            raise TypeError
        gmp.mpc_sin_cos(res1, res2, mpc_x, gmp.MPC_RNDNN, gmp.MPC_RNDNN)
        return (mpc._from_c_mpc(res1), mpc._from_c_mpc(res2))
    gmp.mpfr_sin_cos(res1, res2, mpfr_x, gmp.MPFR_RNDN)
    return (mpfr._from_c_mpfr(res1), mpfr._from_c_mpfr(res2))
예제 #5
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")
예제 #6
0
파일: mpc.py 프로젝트: sn6uv/gmpy_cffi
    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")