def from_diofant(self, a): """Convert Diofant's Rational to `dtype`. """ if a.is_Rational: return PythonRational(a.p, a.q) elif a.is_Float: from diofant.polys.domains import RR p, q = RR.to_rational(a) return PythonRational(int(p), int(q)) else: raise CoercionFailed("expected `Rational` object, got %s" % a)
def test_Domain_convert(): assert QQ.convert(10e-52) == QQ( 1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576) R, x = ring("x", ZZ) assert ZZ.convert(x - x) == 0 assert ZZ.convert(x - x, R.to_domain()) == 0 F3 = FF(3) assert F3.convert(Float(2.0)) == F3.dtype(2) assert F3.convert(PythonRational(2, 1)) == F3.dtype(2) pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2))) assert F3.convert(2.0) == F3.dtype(2) pytest.raises(CoercionFailed, lambda: F3.convert(2.1))
def test_pickling_polys_elements(): for c in (PythonRational, PythonRational(1, 7)): check(c) gf = PythonFiniteField(17) # TODO: fix pickling of ModularInteger # for c in (gf.dtype, gf(5)): # check(c) mp = MPContext()
def from_RealField(self, a, K0): """Convert a mpmath `mpf` object to `dtype`. """ p, q = K0.to_rational(a) return PythonRational(int(p), int(q))
def from_QQ_gmpy(self, a, K0): """Convert a GMPY `mpq` object to `dtype`. """ return PythonRational(PythonInteger(a.numer()), PythonInteger(a.denom()))
def from_ZZ_gmpy(self, a, K0): """Convert a GMPY `mpz` object to `dtype`. """ return PythonRational(PythonInteger(a))
def from_ZZ_python(self, a, K0): """Convert a Python `int` object to `dtype`. """ return PythonRational(a)