def __repr__(self): if self._repr_str is None: if self.precision == gmp.mpfr_get_default_prec(): self._repr_str = "mpfr('%s')" % self else: self._repr_str = "mpfr('%s',%s)" % (self, self.precision) return self._repr_str
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
def __repr__(self): prec = self.precision if prec[0] == prec[1] == gmp.mpfr_get_default_prec(): # return "mpc('" + self.__str__() + "')" return "mpc('{0}')".format(self) else: return "mpc('{0}',({1[0]},{1[1]}))".format(self, prec)
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)
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