def integralize(exp): """ Coerce an expression into an integer if possible, else return an error. Returns the corresponding integer if ``exp`` is integral. Returns ``exp`` if ``exp`` is an expression which is not known to be integral. Otherwise, an exception is raised. """ if isinstance(exp, float): exp = create_RealNumber(exp) elif isinstance(exp, Expression) and not exp.is_constant(): return exp try: if isinstance(exp, (int, IntegerFactorization)): return exp + Integer(0) elif exp.is_integer(): return Integer(exp) mp = exp.minpoly() if mp.degree() == 1: return Integer(mp.any_root()) except Exception: pass raise TypeError("attempt to coerce non-integer to Integer")
def _sympysage_float(self): """ EXAMPLES:: sage: from sympy.core.numbers import RealNumber as RN sage: assert SR(-1.34)._sympy_() == RN('-1.34') sage: assert SR(-1.34) == RN('-1.34')._sage_() """ from sage.rings.real_mpfr import create_RealNumber return create_RealNumber(str(self))
def _sympysage_float(self): """ EXAMPLES:: sage: from sympy.core.numbers import RealNumber as RN sage: assert SR(-1.34)._sympy_() == RN('-1.34') sage: assert SR(-1.34) == RN('-1.34')._sage_() """ from sage.rings.real_mpfr import create_RealNumber return create_RealNumber(str(self))
def int_or_real(x): r""" Convert ``x`` to a Sage integer or real number. If ``x`` is integral, an ``Integer`` is returned. Otherwise, a real number is returned. """ if isinteger(x): return Integer(x) else: return create_RealNumber(x)
def int_or_real(x): r""" Convert ``x`` to a Sage integer or real number. If ``x`` is integral, an ``Integer`` is returned. Otherwise, a real number is returned. """ if isinteger(x): return Integer(x) else: return create_RealNumber(x)
def round(x): """ Return ``x`` rounded to an ``Integer``. """ return create_RealNumber(x).round()
def int_or_real(x): if isinteger(x): return Integer(x) else: return create_RealNumber(x)