Exemplo n.º 1
0
def _init_check_mpfr(x):
    """
    Returns a new mpfr and a pointer to a c mpfr storing the value of x
    """
    if isinstance(x, mpfr):
        res = _new_mpfr(gmp.mpfr_get_prec(x._mpfr))
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN)
    elif isinstance(x, (int, long)):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        res = _new_mpfr()
        mpfr_x = res        # avoid initialising another c mpfr
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError
    return res, mpfr_x
Exemplo n.º 2
0
def sinh_cosh(x):
    """
    sinh_cosh(x) -> (number, number)

    Return a tuple containing the hyperbolic sine and cosine of x.
    """
    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
        gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN)
    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:
        raise TypeError
    gmp.mpfr_sinh_cosh(res1, res2, mpfr_x, gmp.MPFR_RNDN)
    return (mpfr._from_c_mpfr(res1), mpfr._from_c_mpfr(res2))
Exemplo n.º 3
0
    def __init__(self, *args):
        nargs = len(args)
        if nargs == 1 and isinstance(args[0], self.__class__):
            self._mpfr = args[0]._mpfr
            return

        if nargs > 3:
            raise TypeError("mpfr() requires 0 to 3 arguments")

        if nargs >= 2:
            a = self._mpfr = ffi.gc(_new_mpfr(prec=args[1]), _del_mpfr)
        else:
            a = self._mpfr = ffi.gc(_new_mpfr(), _del_mpfr)

        if nargs == 0:
            gmp.mpfr_set_zero(a, 1)
        elif nargs == 3:
            if isinstance(args[0], str):
                _str_to_mpfr(args[0], args[2], a)
            else:
                raise TypeError('function takes at most 2 arguments (%i given)' % nargs)
        else:
            if isinstance(args[0], str):
                _str_to_mpfr(args[0], 10, a)
            elif isinstance(args[0], float):
                gmp.mpfr_set_d(a, args[0], gmp.MPFR_RNDN)
            elif isinstance(args[0], (int, long)):
                _pyint_to_mpfr(args[0], a)
            elif isinstance(args[0], mpz):
                gmp.mpfr_set_z(a, args[0]._mpz, gmp.MPFR_RNDN)
            elif isinstance(args[0], mpq):
                gmp.mpfr_set_q(a, args[0]._mpq, gmp.MPFR_RNDN)
            else:
                raise TypeError('cannot construct mpfr from %s.' % args[0])
Exemplo n.º 4
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)
Exemplo n.º 5
0
def _pyint_to_mpfr(n, a):
    if -sys.maxsize - 1 <= n <= sys.maxsize:
        gmp.mpfr_set_si(a, n, gmp.MPFR_RNDN)
    elif sys.maxsize < n <= MAX_UI:
        gmp.mpfr_set_ui(a, n, gmp.MPFR_RNDN)
    else:
        assert isinstance(n, long)
        tmp_mpz = ffi.new('mpz_t')
        gmp.mpz_init(tmp_mpz)
        _pylong_to_mpz(n, tmp_mpz)
        gmp.mpfr_set_z(a, tmp_mpz, gmp.MPFR_RNDN)
        gmp.mpz_clear(tmp_mpz)
Exemplo n.º 6
0
def _pyint_to_mpfr(n, a):
    if -sys.maxsize - 1 <= n <= sys.maxsize:
        gmp.mpfr_set_si(a, n, gmp.MPFR_RNDN)
    elif sys.maxsize < n <= MAX_UI:
        gmp.mpfr_set_ui(a, n, gmp.MPFR_RNDN)
    else:
        assert isinstance(n, long)
        tmp_mpz = ffi.new('mpz_t')
        gmp.mpz_init(tmp_mpz)
        _pylong_to_mpz(n, tmp_mpz)
        gmp.mpfr_set_z(a, tmp_mpz, gmp.MPFR_RNDN)
        gmp.mpz_clear(tmp_mpz)
Exemplo n.º 7
0
def atan2(y, x):
    """
    atan2(y, x) -> number

    Return the arc-tangent of (y/x).
    """
    # X
    if isinstance(x, mpfr):
        mpfr_x = x._mpfr
    elif isinstance(x, float):
        mpfr_x = _new_mpfr()
        gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN)
    elif isinstance(x, (int, long)):
        mpfr_x = _new_mpfr()
        _pyint_to_mpfr(x, mpfr_x)
    elif isinstance(x, mpz):
        mpfr_x = _new_mpfr()
        gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN)
    elif isinstance(x, mpq):
        mpfr_x = _new_mpfr()
        gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError

    # Y
    if isinstance(y, mpfr):
        mpfr_y = y._mpfr
    elif isinstance(y, float):
        mpfr_y = _new_mpfr()
        gmp.mpfr_set_d(mpfr_y, y, gmp.MPFR_RNDN)
    elif isinstance(y, (int, long)):
        mpfr_y = _new_mpfr()
        _pyint_to_mpfr(y, mpfr_y)
    elif isinstance(y, mpz):
        mpfr_y = _new_mpfr()
        gmp.mpfr_set_z(mpfr_y, y._mpz, gmp.MPFR_RNDN)
    elif isinstance(y, mpq):
        mpfr_y = _new_mpfr()
        gmp.mpfr_set_q(mpfr_y, y._mpq, gmp.MPFR_RNDN)
    else:
        raise TypeError

    res = _new_mpfr(min(gmp.mpfr_get_prec(mpfr_x), gmp.mpfr_get_prec(mpfr_x)))
    gmp.mpfr_atan2(res, mpfr_y, mpfr_x, gmp.MPFR_RNDN)
    return mpfr._from_c_mpfr(res)
Exemplo n.º 8
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))
Exemplo n.º 9
0
 def __rpow__(self, other):
     res = _new_mpfr()
     if isinstance(other, mpq):
         # There is no mpfr_pow_q
         gmp.mpfr_set_q(res, other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_pow(res, res, self._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         # There is no mpfr_pow_z
         gmp.mpfr_set_z(res, other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_pow(res, res, self._mpfr, 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, res, self._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         # There is no mpfr_si_pow
         _pyint_to_mpfr(other, res)
         gmp.mpfr_pow(res, res, self._mpfr, gmp.MPFR_RNDN)
     else:
         return NotImplemented
     return mpfr._from_c_mpfr(res)
Exemplo n.º 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")
Exemplo n.º 11
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")