Exemple #1
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))
Exemple #2
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
Exemple #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])
Exemple #4
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)
Exemple #5
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)
Exemple #6
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)
Exemple #7
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")
Exemple #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")