123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- """Implementation of :class:`RationalField` class. """
- from sympy.external.gmpy import MPQ
- from sympy.polys.domains.groundtypes import SymPyRational
- from sympy.polys.domains.characteristiczero import CharacteristicZero
- from sympy.polys.domains.field import Field
- from sympy.polys.domains.simpledomain import SimpleDomain
- from sympy.polys.polyerrors import CoercionFailed
- from sympy.utilities import public
- @public
- class RationalField(Field, CharacteristicZero, SimpleDomain):
- r"""Abstract base class for the domain :ref:`QQ`.
- The :py:class:`RationalField` class represents the field of rational
- numbers $\mathbb{Q}$ as a :py:class:`~.Domain` in the domain system.
- :py:class:`RationalField` is a superclass of
- :py:class:`PythonRationalField` and :py:class:`GMPYRationalField` one of
- which will be the implementation for :ref:`QQ` depending on whether either
- of ``gmpy`` or ``gmpy2`` is installed or not.
- See also
- ========
- Domain
- """
- rep = 'QQ'
- alias = 'QQ'
- is_RationalField = is_QQ = True
- is_Numerical = True
- has_assoc_Ring = True
- has_assoc_Field = True
- dtype = MPQ
- zero = dtype(0)
- one = dtype(1)
- tp = type(one)
- def __init__(self):
- pass
- def get_ring(self):
- """Returns ring associated with ``self``. """
- from sympy.polys.domains import ZZ
- return ZZ
- def to_sympy(self, a):
- """Convert ``a`` to a SymPy object. """
- return SymPyRational(int(a.numerator), int(a.denominator))
- def from_sympy(self, a):
- """Convert SymPy's Integer to ``dtype``. """
- if a.is_Rational:
- return MPQ(a.p, a.q)
- elif a.is_Float:
- from sympy.polys.domains import RR
- return MPQ(*map(int, RR.to_rational(a)))
- else:
- raise CoercionFailed("expected `Rational` object, got %s" % a)
- def algebraic_field(self, *extension):
- r"""Returns an algebraic field, i.e. `\mathbb{Q}(\alpha, \ldots)`.
- Parameters
- ==========
- *extension: One or more Expr
- Generators of the extension. These should be expressions that are
- algebraic over `\mathbb{Q}`.
- Returns
- =======
- :py:class:`~.AlgebraicField`
- A :py:class:`~.Domain` representing the algebraic field extension.
- Examples
- ========
- >>> from sympy import QQ, sqrt
- >>> QQ.algebraic_field(sqrt(2))
- QQ<sqrt(2)>
- """
- from sympy.polys.domains import AlgebraicField
- return AlgebraicField(self, *extension)
- def from_AlgebraicField(K1, a, K0):
- """Convert a :py:class:`~.ANP` object to :ref:`QQ`.
- See :py:meth:`~.Domain.convert`
- """
- if a.is_ground:
- return K1.convert(a.LC(), K0.dom)
- def from_ZZ(K1, a, K0):
- """Convert a Python ``int`` object to ``dtype``. """
- return MPQ(a)
- def from_ZZ_python(K1, a, K0):
- """Convert a Python ``int`` object to ``dtype``. """
- return MPQ(a)
- def from_QQ(K1, a, K0):
- """Convert a Python ``Fraction`` object to ``dtype``. """
- return MPQ(a.numerator, a.denominator)
- def from_QQ_python(K1, a, K0):
- """Convert a Python ``Fraction`` object to ``dtype``. """
- return MPQ(a.numerator, a.denominator)
- def from_ZZ_gmpy(K1, a, K0):
- """Convert a GMPY ``mpz`` object to ``dtype``. """
- return MPQ(a)
- def from_QQ_gmpy(K1, a, K0):
- """Convert a GMPY ``mpq`` object to ``dtype``. """
- return a
- def from_GaussianRationalField(K1, a, K0):
- """Convert a ``GaussianElement`` object to ``dtype``. """
- if a.y == 0:
- return MPQ(a.x)
- def from_RealField(K1, a, K0):
- """Convert a mpmath ``mpf`` object to ``dtype``. """
- return MPQ(*map(int, K0.to_rational(a)))
- def exquo(self, a, b):
- """Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
- return MPQ(a) / MPQ(b)
- def quo(self, a, b):
- """Quotient of ``a`` and ``b``, implies ``__truediv__``. """
- return MPQ(a) / MPQ(b)
- def rem(self, a, b):
- """Remainder of ``a`` and ``b``, implies nothing. """
- return self.zero
- def div(self, a, b):
- """Division of ``a`` and ``b``, implies ``__truediv__``. """
- return MPQ(a) / MPQ(b), self.zero
- def numer(self, a):
- """Returns numerator of ``a``. """
- return a.numerator
- def denom(self, a):
- """Returns denominator of ``a``. """
- return a.denominator
- QQ = RationalField()
|