Exemple #1
0
def _new_mpfr(prec=0):
    """Return an initialized mpfr_t."""
    global in_mpfr_cache

    if isinstance(prec, (int, long)):
        if not (prec == 0 or gmp.MPFR_PREC_MIN <= prec <= gmp.MPFR_PREC_MAX):
            raise ValueError("invalid prec %i (wanted %s <= prec <= %s)" % (
                prec, gmp.MPFR_PREC_MIN, gmp.MPFR_PREC_MAX))
    else:
        raise TypeError('an integer is required')

    if in_mpfr_cache:
        in_mpfr_cache -= 1
        # Set default precision
        if prec == 0:
            gmp.mpfr_set_prec(mpfr_cache[in_mpfr_cache], gmp.mpfr_get_default_prec())
        else:
            gmp.mpfr_set_prec(mpfr_cache[in_mpfr_cache], prec)
        return mpfr_cache[in_mpfr_cache]
    else:
        mpfr = ffi.new("mpfr_t")
        if prec == 0:
            gmp.mpfr_init(mpfr)
        else:
            gmp.mpfr_init2(mpfr, prec)
        return mpfr
Exemple #2
0
def _init_mpc_cache():
    global mpc_cache, in_mpc_cache
    mpc_cache = []
    in_mpc_cache = cache_size
    for _ in xrange(cache_size):
        mpc = ffi.new("mpc_t")
        gmp.mpc_init2(mpc, gmp.mpfr_get_default_prec())
        mpc_cache.append(mpc)
Exemple #3
0
def _init_mpq_cache():
    global mpq_cache, in_mpq_cache
    mpq_cache = []
    in_mpq_cache = cache_size
    for _ in xrange(cache_size):
        mpq = ffi.new("mpq_t")
        gmp.mpq_init(mpq)
        mpq_cache.append(mpq)
Exemple #4
0
def _mpq_to_str(a, base):
    l = (gmp.mpz_sizeinbase(gmp.mpq_numref(a), base) +
         gmp.mpz_sizeinbase(gmp.mpq_denref(a), base) + 3)
    p = ffi.new('char[]', l)
    gmp.mpq_get_str(p, base, a)
    if PY3:
        return ffi.string(p).decode('UTF-8')
    else:
        return ffi.string(p)
Exemple #5
0
def _mpq_to_str(a, base):
    l = (gmp.mpz_sizeinbase(gmp.mpq_numref(a), base) +
         gmp.mpz_sizeinbase(gmp.mpq_denref(a), base) + 3)
    p = ffi.new('char[]', l)
    gmp.mpq_get_str(p, base, a)
    if PY3:
        return ffi.string(p).decode('UTF-8')
    else:
        return ffi.string(p)
Exemple #6
0
def _del_mpc(mpc):
    global in_mpc_cache

    if in_mpc_cache < cache_size:
        mpc_cache[in_mpc_cache] = mpc
        # FIXME This doesn't seem to be working properly
        if ffi.sizeof(mpc[0]) <= cache_obsize:
            mpc_cache[in_mpc_cache] = mpc
        else:
            mpc_cache[in_mpc_cache] = ffi.new('mpc_t')
Exemple #7
0
def lgamma(x):
    """
    lgamma(x) -> (number, int)

    Return a tuple containing the logarithm of the absolute value of
    gamma(x) and the sign of gamma(x)
    """
    res, x = _init_check_mpfr(x)
    sgn = ffi.new('int *')
    gmp.mpfr_lgamma(res, sgn, x, gmp.MPFR_RNDN)
    return (mpfr._from_c_mpfr(res), int(sgn[0]))
Exemple #8
0
def _del_mpz(mpz):
    global in_mpz_cache

    if in_mpz_cache < cache_size:
        if ffi.sizeof(mpz[0]) <= cache_obsize:
            mpz_cache[in_mpz_cache] = mpz
        else:
            mpz_cache[in_mpz_cache] = ffi.new('mpz_t')
        in_mpz_cache += 1
    else:
        gmp.mpz_clear(mpz)
Exemple #9
0
def _new_mpq():
    """Return an initialized mpq_t."""
    global in_mpq_cache

    if in_mpq_cache:
        in_mpq_cache -= 1
        return mpq_cache[in_mpq_cache]
    else:
        mpq = ffi.new("mpq_t")
        gmp.mpq_init(mpq)
        return mpq
Exemple #10
0
def _mpfr_to_str(a):
    precision = int(log10(2) * gmp.mpfr_get_prec(a) + 2)
    buf = ffi.new('char []', precision + 10)
    fmtstr = "%.{0}Rg".format(precision)
    buflen = gmp.mpfr_sprintf(buf, fmtstr.encode('UTF-8'), a)
    if PY3:
        pybuf = ffi.string(buf).decode('UTF-8')
    else:
        pybuf = ffi.string(buf)
    if gmp.mpfr_number_p(a) and '.' not in pybuf:
        pybuf = pybuf + '.0'
    return pybuf
Exemple #11
0
def _mpfr_to_str(a):
    precision = int(log10(2) * gmp.mpfr_get_prec(a) + 2)
    buf = ffi.new('char []', precision + 10)
    fmtstr = "%.{0}Rg".format(precision)
    buflen = gmp.mpfr_sprintf(buf, fmtstr.encode('UTF-8'), a)
    if PY3:
        pybuf = ffi.string(buf).decode('UTF-8')
    else:
        pybuf = ffi.string(buf)
    if gmp.mpfr_number_p(a) and '.' not in pybuf:
        pybuf = pybuf + '.0'
    return pybuf
Exemple #12
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)
Exemple #13
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)
Exemple #14
0
def _mpz_to_str(a, base):
    """
    Return string representation of a in base base.

    :type a: mpz_t
    :param base: 2..62
    :type base: int
    :rtype: str
    """

    l = gmp.mpz_sizeinbase(a, base) + 2
    p = ffi.new('char[]', l)
    gmp.mpz_get_str(p, base, a)
    if PY3:
        return ffi.string(p).decode('UTF-8')
    else:
        return ffi.string(p)
Exemple #15
0
def _mpz_to_str(a, base):
    """
    Return string representation of a in base base.

    :type a: mpz_t
    :param base: 2..62
    :type base: int
    :rtype: str
    """

    l = gmp.mpz_sizeinbase(a, base) + 2
    p = ffi.new('char[]', l)
    gmp.mpz_get_str(p, base, a)
    if PY3:
        return ffi.string(p).decode('UTF-8')
    else:
        return ffi.string(p)
Exemple #16
0
def _mpz_to_pylong(a):
    """
    Convert a to a python long.

    :type a: mpz_t
    :rtype: long
    """

    size = ffi.sizeof('uint64_t')
    numb = 8 * size
    count = (gmp.mpz_sizeinbase(a, 2) + numb - 1) // numb
    p = ffi.new('uint64_t[]', count)
    gmp.mpz_export(p, ffi.NULL, 1, size, 0, 0, a)
    res = 0
    for n in p:
        res = (res << numb) + n

    return res * gmp.mpz_sgn(a)
Exemple #17
0
def _mpz_to_pylong(a):
    """
    Convert a to a python long.

    :type a: mpz_t
    :rtype: long
    """

    size = ffi.sizeof('uint64_t')
    numb = 8 * size
    count = (gmp.mpz_sizeinbase(a, 2) + numb - 1) // numb
    p = ffi.new('uint64_t[]', count)
    gmp.mpz_export(p, ffi.NULL, 1, size, 0, 0, a)
    res = 0
    for n in p:
        res = (res << numb) + n

    return res * gmp.mpz_sgn(a)
Exemple #18
0
def _new_mpc(prec=(0,0)):
    """Return an initialized mpc_t."""
    global in_mpc_cache

    # prec is assumed to be checked already
    rprec, iprec = prec

    if not all(p == 0 or gmp.MPFR_PREC_MIN <= p <= gmp.MPFR_PREC_MAX
               for p in prec):
            raise ValueError(
                "invalid prec (wanted prec == 0 or %s <= prec <= %s)" % (
                    gmp.MPFR_PREC_MIN, gmp.MPFR_PREC_MAX))

    if in_mpc_cache:
        in_mpc_cache -= 1
        # Set default precision
        if rprec == iprec:
            if rprec  == 0:
                gmp.mpc_set_prec(mpc_cache[in_mpc_cache], gmp.mpfr_get_default_prec())
            else:
                gmp.mpc_set_prec(mpc_cache[in_mpc_cache], rprec)
        else:
            if rprec == 0:
                rprec = gmp.mpfr_get_default_prec()
            if iprec == 0:
                iprec = gmp.mpfr_get_default_prec()
            gmp.mpc_clear(mpc_cache[in_mpc_cache])
            gmp.mpc_init3(mpc_cache[in_mpc_cache], rprec, iprec)
        return mpc_cache[in_mpc_cache]
    else:
        mpc = ffi.new("mpc_t")
        if rprec == 0:
            rprec = gmp.mpfr_get_default_prec()
        if iprec == 0:
            iprec = gmp.mpfr_get_default_prec()
        if rprec == iprec:
            gmp.mpc_init2(mpc, rprec)
        else:
            gmp.mpc_init3(mpc, rprec, iprec)
        return mpc
Exemple #19
0
 def precision(self):
     rprec, iprec = ffi.new("mpfr_prec_t *"), ffi.new("mpfr_prec_t *")
     gmp.mpc_get_prec2(rprec, iprec, self._mpc)
     return int(rprec[0]), int(iprec[0])
Exemple #20
0
 def precision(self):
     rprec, iprec = ffi.new('mpfr_prec_t *'), ffi.new('mpfr_prec_t *')
     gmp.mpc_get_prec2(rprec, iprec, self._mpc)
     return int(rprec[0]), int(iprec[0])