Source code for sympy.polys.domains.gmpyrationalfield
"""Implementation of :class:`GMPYRationalField` class. """fromsympy.polys.domains.groundtypesimport(GMPYRational,SymPyRational,gmpy_numer,gmpy_denom,factorialasgmpy_factorial,)fromsympy.polys.domains.rationalfieldimportRationalFieldfromsympy.polys.polyerrorsimportCoercionFailedfromsympy.utilitiesimportpublic
[docs]@publicclassGMPYRationalField(RationalField):"""Rational field based on GMPY's ``mpq`` type. This will be the implementation of :ref:`QQ` if ``gmpy`` or ``gmpy2`` is installed. Elements will be of type ``gmpy.mpq``. """dtype=GMPYRationalzero=dtype(0)one=dtype(1)tp=type(one)alias='QQ_gmpy'def__init__(self):pass
[docs]defget_ring(self):"""Returns ring associated with ``self``. """fromsympy.polys.domainsimportGMPYIntegerRingreturnGMPYIntegerRing()
[docs]defto_sympy(self,a):"""Convert ``a`` to a SymPy object. """returnSymPyRational(int(gmpy_numer(a)),int(gmpy_denom(a)))
[docs]deffrom_sympy(self,a):"""Convert SymPy's Integer to ``dtype``. """ifa.is_Rational:returnGMPYRational(a.p,a.q)elifa.is_Float:fromsympy.polys.domainsimportRRreturnGMPYRational(*map(int,RR.to_rational(a)))else:raiseCoercionFailed("expected ``Rational`` object, got %s"%a)
[docs]deffrom_ZZ_python(K1,a,K0):"""Convert a Python ``int`` object to ``dtype``. """returnGMPYRational(a)
[docs]deffrom_QQ_python(K1,a,K0):"""Convert a Python ``Fraction`` object to ``dtype``. """returnGMPYRational(a.numerator,a.denominator)
[docs]deffrom_ZZ_gmpy(K1,a,K0):"""Convert a GMPY ``mpz`` object to ``dtype``. """returnGMPYRational(a)
[docs]deffrom_QQ_gmpy(K1,a,K0):"""Convert a GMPY ``mpq`` object to ``dtype``. """returna
[docs]deffrom_GaussianRationalField(K1,a,K0):"""Convert a ``GaussianElement`` object to ``dtype``. """ifa.y==0:returnGMPYRational(a.x)
[docs]deffrom_RealField(K1,a,K0):"""Convert a mpmath ``mpf`` object to ``dtype``. """returnGMPYRational(*map(int,K0.to_rational(a)))
[docs]defexquo(self,a,b):"""Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """returnGMPYRational(a)/GMPYRational(b)
[docs]defquo(self,a,b):"""Quotient of ``a`` and ``b``, implies ``__truediv__``. """returnGMPYRational(a)/GMPYRational(b)
[docs]defrem(self,a,b):"""Remainder of ``a`` and ``b``, implies nothing. """returnself.zero
[docs]defdiv(self,a,b):"""Division of ``a`` and ``b``, implies ``__truediv__``. """returnGMPYRational(a)/GMPYRational(b),self.zero
[docs]defnumer(self,a):"""Returns numerator of ``a``. """returna.numerator
[docs]defdenom(self,a):"""Returns denominator of ``a``. """returna.denominator
[docs]deffactorial(self,a):"""Returns factorial of ``a``. """returnGMPYRational(gmpy_factorial(int(a)))