o
    8VaD                     @   s   d Z ddlmZmZmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZmZ eG dd dZdgZdS )z)Implementation of :class:`Domain` class.     )AnyOptionalType)Basicsympify)HAS_GMPYis_sequenceordered)
deprecated)DomainElement)lex)UnificationFailedCoercionFailedDomainError)_unify_gens_not_a_coeff)default_sort_keypublicc                   @   s  e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZ	 dZ		 dZ
	 d ZZd ZZd ZZd ZZd ZZd ZZd ZZd ZZd ZZd ZZd ZZ d Z!Z"dZ#dZ$dZ%dZ&dZ'dZ(	 dZ)dZ*dZ+e,e-ddddd	d
 Z.e,e-dddddd Z/dd Z0dd Z1dd Z2dd Z3dd Z4e,dd Z5dd Z6dd Z7dd Z8dd d!Z9d"d# Z:d$d% Z;d&d' Z<d(d) Z=d*d+ Z>d,d- Z?d.d/ Z@d0d1 ZAd2d3 ZBd4d5 ZCd6d7 ZDd8d9 ZEd:d; ZFd<d= ZGd>d? ZHd@dA ZIdBdC ZJdDdE ZKdFdG ZLdHdI ZMdJdK ZNdLdM ZOdNdO ZPddPdQZQdRdS ZRdTdU ZSdVdW ZTdXdY ZUdZd[ ZVd\d] ZWd^d_ ZXeYd`dadbZZeYd`dcddZ[dedf Z\dgdh Z]didj Z^dkdl Z_dmdn Z`dodp Zadqdr Zbdsdt Zcdudv Zddwdx Zedydz Zfd{d| Zgd}d~ Zhdd Zidd Zjdd Zkdd Zldd Zmdd Zndd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd Zudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dddZ~e~Zdd Zdd ZdddZdd ZdS )Domainaz  Superclass for all domains in the polys domains system.

    See :ref:`polys-domainsintro` for an introductory explanation of the
    domains system.

    The :py:class:`~.Domain` class is an abstract base class for all of the
    concrete domain types. There are many different :py:class:`~.Domain`
    subclasses each of which has an associated ``dtype`` which is a class
    representing the elements of the domain. The coefficients of a
    :py:class:`~.Poly` are elements of a domain which must be a subclass of
    :py:class:`~.Domain`.

    Examples
    ========

    The most common example domains are the integers :ref:`ZZ` and the
    rationals :ref:`QQ`.

    >>> from sympy import Poly, symbols, Domain
    >>> x, y = symbols('x, y')
    >>> p = Poly(x**2 + y)
    >>> p
    Poly(x**2 + y, x, y, domain='ZZ')
    >>> p.domain
    ZZ
    >>> isinstance(p.domain, Domain)
    True
    >>> Poly(x**2 + y/2)
    Poly(x**2 + 1/2*y, x, y, domain='QQ')

    The domains can be used directly in which case the domain object e.g.
    (:ref:`ZZ` or :ref:`QQ`) can be used as a constructor for elements of
    ``dtype``.

    >>> from sympy import ZZ, QQ
    >>> ZZ(2)
    2
    >>> ZZ.dtype  # doctest: +SKIP
    <class 'int'>
    >>> type(ZZ(2))  # doctest: +SKIP
    <class 'int'>
    >>> QQ(1, 2)
    1/2
    >>> type(QQ(1, 2))  # doctest: +SKIP
    <class 'sympy.polys.domains.pythonrational.PythonRational'>

    The corresponding domain elements can be used with the arithmetic
    operations ``+,-,*,**`` and depending on the domain some combination of
    ``/,//,%`` might be usable. For example in :ref:`ZZ` both ``//`` (floor
    division) and ``%`` (modulo division) can be used but ``/`` (true
    division) can not. Since :ref:`QQ` is a :py:class:`~.Field` its elements
    can be used with ``/`` but ``//`` and ``%`` should not be used. Some
    domains have a :py:meth:`~.Domain.gcd` method.

    >>> ZZ(2) + ZZ(3)
    5
    >>> ZZ(5) // ZZ(2)
    2
    >>> ZZ(5) % ZZ(2)
    1
    >>> QQ(1, 2) / QQ(2, 3)
    3/4
    >>> ZZ.gcd(ZZ(4), ZZ(2))
    2
    >>> QQ.gcd(QQ(2,7), QQ(5,3))
    1/21
    >>> ZZ.is_Field
    False
    >>> QQ.is_Field
    True

    There are also many other domains including:

        1. :ref:`GF(p)` for finite fields of prime order.
        2. :ref:`RR` for real (floating point) numbers.
        3. :ref:`CC` for complex (floating point) numbers.
        4. :ref:`QQ(a)` for algebraic number fields.
        5. :ref:`K[x]` for polynomial rings.
        6. :ref:`K(x)` for rational function fields.
        7. :ref:`EX` for arbitrary expressions.

    Each domain is represented by a domain object and also an implementation
    class (``dtype``) for the elements of the domain. For example the
    :ref:`K[x]` domains are represented by a domain object which is an
    instance of :py:class:`~.PolynomialRing` and the elements are always
    instances of :py:class:`~.PolyElement`. The implementation class
    represents particular types of mathematical expressions in a way that is
    more efficient than a normal SymPy expression which is of type
    :py:class:`~.Expr`. The domain methods :py:meth:`~.Domain.from_sympy` and
    :py:meth:`~.Domain.to_sympy` are used to convert from :py:class:`~.Expr`
    to a domain element and vice versa.

    >>> from sympy import Symbol, ZZ, Expr
    >>> x = Symbol('x')
    >>> K = ZZ[x]           # polynomial ring domain
    >>> K
    ZZ[x]
    >>> type(K)             # class of the domain
    <class 'sympy.polys.domains.polynomialring.PolynomialRing'>
    >>> K.dtype             # class of the elements
    <class 'sympy.polys.rings.PolyElement'>
    >>> p_expr = x**2 + 1   # Expr
    >>> p_expr
    x**2 + 1
    >>> type(p_expr)
    <class 'sympy.core.add.Add'>
    >>> isinstance(p_expr, Expr)
    True
    >>> p_domain = K.from_sympy(p_expr)
    >>> p_domain            # domain element
    x**2 + 1
    >>> type(p_domain)
    <class 'sympy.polys.rings.PolyElement'>
    >>> K.to_sympy(p_domain) == p_expr
    True

    The :py:meth:`~.Domain.convert_from` method is used to convert domain
    elements from one domain to another.

    >>> from sympy import ZZ, QQ
    >>> ez = ZZ(2)
    >>> eq = QQ.convert_from(ez, ZZ)
    >>> type(ez)  # doctest: +SKIP
    <class 'int'>
    >>> type(eq)  # doctest: +SKIP
    <class 'sympy.polys.domains.pythonrational.PythonRational'>

    Elements from different domains should not be mixed in arithmetic or other
    operations: they should be converted to a common domain first.  The domain
    method :py:meth:`~.Domain.unify` is used to find a domain that can
    represent all the elements of two given domains.

    >>> from sympy import ZZ, QQ, symbols
    >>> x, y = symbols('x, y')
    >>> ZZ.unify(QQ)
    QQ
    >>> ZZ[x].unify(QQ)
    QQ[x]
    >>> ZZ[x].unify(QQ[y])
    QQ[x,y]

    If a domain is a :py:class:`~.Ring` then is might have an associated
    :py:class:`~.Field` and vice versa. The :py:meth:`~.Domain.get_field` and
    :py:meth:`~.Domain.get_ring` methods will find or create the associated
    domain.

    >>> from sympy import ZZ, QQ, Symbol
    >>> x = Symbol('x')
    >>> ZZ.has_assoc_Field
    True
    >>> ZZ.get_field()
    QQ
    >>> QQ.has_assoc_Ring
    True
    >>> QQ.get_ring()
    ZZ
    >>> K = QQ[x]
    >>> K
    QQ[x]
    >>> K.get_field()
    QQ(x)

    See also
    ========

    DomainElement: abstract base class for domain elements
    construct_domain: construct a minimal domain for some expressions

    NFTis_Fieldi1  z1.1)Z
useinsteadZissueZdeprecated_since_versionc                 C      | j S N)r   self r   </usr/lib/python3/dist-packages/sympy/polys/domains/domain.py	has_Fieldd     zDomain.has_Fieldis_Ringc                 C   r   r   )r   r   r   r   r   has_Ringi  r   zDomain.has_Ringc                 C      t r   NotImplementedErrorr   r   r   r   __init__n     zDomain.__init__c                 C   r   r   )repr   r   r   r   __str__q     zDomain.__str__c                 C   s   t | S r   )strr   r   r   r   __repr__t     zDomain.__repr__c                 C   s   t | jj| jfS r   )hash	__class____name__dtyper   r   r   r   __hash__w  s   zDomain.__hash__c                 G   
   | j | S r   r.   r   argsr   r   r   newz     
z
Domain.newc                 C   r   )z#Alias for :py:attr:`~.Domain.dtype`r1   r   r   r   r   tp}  r   z	Domain.tpc                 G   r0   )z7Construct an element of ``self`` domain from ``args``. )r4   r2   r   r   r   __call__     
zDomain.__call__c                 G   r0   r   r1   r2   r   r   r   normal  r5   zDomain.normalc                 C   sb   |j durd|j  }nd|jj }t| |}|dur%|||}|dur%|S td|t||| f )z=Convert ``element`` to ``self.dtype`` given the base domain. NZfrom_z)can't convert %s of type %s from %s to %s)aliasr,   r-   getattrr   type)r   elementbasemethod_convertresultr   r   r   convert_from  s   


zDomain.convert_fromc           
   	   C   s  t |r
td| |dur| ||S | |r|S ddlm}m}m}m} ||r2| ||S t	|t
r?| |||S tr]|}t	||jrO| ||S |}t	||jr]| ||S t	|tro|dd}	| |	||	S t	|tr|dd}	| |	||	S t	|tr| || S | jrt|ddr| | S t	|trz| |W S  ttfy   Y n$w t|szt|dd	}t	|tr| |W S W n ttfy   Y nw td
|t|| f )z'Convert ``element`` to ``self.dtype``. z%s is not in any domainNr   )ZZQQ	RealFieldComplexFieldF)tol	is_groundT)strictz!can't convert %s of type %s to %s)r   r   rB   of_typesympy.polys.domainsrC   rD   rE   rF   
isinstanceintr   r6   floatcomplexr   parentis_Numericalr;   convertLCr   
from_sympy	TypeError
ValueErrorr   r   r<   )
r   r=   r>   rC   rD   rE   rF   ZintegersZ	rationalsrP   r   r   r   rR     sX   









zDomain.convertc                 C   s   t || jS )z%Check if ``a`` is of type ``dtype``. )rL   r6   )r   r=   r   r   r   rJ        zDomain.of_typec                 C   s2   zt |rt| | W dS  ty   Y dS w )z'Check if ``a`` belongs to this domain. FT)r   r   rR   r   ar   r   r   __contains__  s   zDomain.__contains__c                 C   r    )a	  Convert domain element *a* to a SymPy expression (Expr).

        Explanation
        ===========

        Convert a :py:class:`~.Domain` element *a* to :py:class:`~.Expr`. Most
        public SymPy functions work with objects of type :py:class:`~.Expr`.
        The elements of a :py:class:`~.Domain` have a different internal
        representation. It is not possible to mix domain elements with
        :py:class:`~.Expr` so each domain has :py:meth:`~.Domain.to_sympy` and
        :py:meth:`~.Domain.from_sympy` methods to convert its domain elements
        to and from :py:class:`~.Expr`.

        Parameters
        ==========

        a: domain element
            An element of this :py:class:`~.Domain`.

        Returns
        =======

        expr: Expr
            A normal sympy expression of type :py:class:`~.Expr`.

        Examples
        ========

        Construct an element of the :ref:`QQ` domain and then convert it to
        :py:class:`~.Expr`.

        >>> from sympy import QQ, Expr
        >>> q_domain = QQ(2)
        >>> q_domain
        2
        >>> q_expr = QQ.to_sympy(q_domain)
        >>> q_expr
        2

        Although the printed forms look similar these objects are not of the
        same type.

        >>> isinstance(q_domain, Expr)
        False
        >>> isinstance(q_expr, Expr)
        True

        Construct an element of :ref:`K[x]` and convert to
        :py:class:`~.Expr`.

        >>> from sympy import Symbol
        >>> x = Symbol('x')
        >>> K = QQ[x]
        >>> x_domain = K.gens[0]  # generator x as a domain element
        >>> p_domain = x_domain**2/3 + 1
        >>> p_domain
        1/3*x**2 + 1
        >>> p_expr = K.to_sympy(p_domain)
        >>> p_expr
        x**2/3 + 1

        The :py:meth:`~.Domain.from_sympy` method is used for the opposite
        conversion from a normal SymPy expression to a domain element.

        >>> p_domain == p_expr
        False
        >>> K.from_sympy(p_expr) == p_domain
        True
        >>> K.to_sympy(p_domain) == p_expr
        True
        >>> K.from_sympy(K.to_sympy(p_domain)) == p_domain
        True
        >>> K.to_sympy(K.from_sympy(p_expr)) == p_expr
        True

        The :py:meth:`~.Domain.from_sympy` method makes it easier to construct
        domain elements interactively.

        >>> from sympy import Symbol
        >>> x = Symbol('x')
        >>> K = QQ[x]
        >>> K.from_sympy(x**2/3 + 1)
        1/3*x**2 + 1

        See also
        ========

        from_sympy
        convert_from
        r!   rX   r   r   r   to_sympy  s   [zDomain.to_sympyc                 C   r    )a  Convert a SymPy expression to an element of this domain.

        Explanation
        ===========

        See :py:meth:`~.Domain.to_sympy` for explanation and examples.

        Parameters
        ==========

        expr: Expr
            A normal sympy expression of type :py:class:`~.Expr`.

        Returns
        =======

        a: domain element
            An element of this :py:class:`~.Domain`.

        See also
        ========

        to_sympy
        convert_from
        r!   rX   r   r   r   rT   A  s   zDomain.from_sympyc                 C      t |S r   )sumr2   r   r   r   r]   ]  r*   z
Domain.sumc                 C      dS z.Convert ``ModularInteger(int)`` to ``dtype``. Nr   K1rY   K0r   r   r   from_FF`     zDomain.from_FFc                 C   r^   r_   r   r`   r   r   r   from_FF_pythond  rd   zDomain.from_FF_pythonc                 C   r^   )z.Convert a Python ``int`` object to ``dtype``. Nr   r`   r   r   r   from_ZZ_pythonh  rd   zDomain.from_ZZ_pythonc                 C   r^   )z3Convert a Python ``Fraction`` object to ``dtype``. Nr   r`   r   r   r   from_QQ_pythonl  rd   zDomain.from_QQ_pythonc                 C   r^   )z.Convert ``ModularInteger(mpz)`` to ``dtype``. Nr   r`   r   r   r   from_FF_gmpyp  rd   zDomain.from_FF_gmpyc                 C   r^   )z,Convert a GMPY ``mpz`` object to ``dtype``. Nr   r`   r   r   r   from_ZZ_gmpyt  rd   zDomain.from_ZZ_gmpyc                 C   r^   )z,Convert a GMPY ``mpq`` object to ``dtype``. Nr   r`   r   r   r   from_QQ_gmpyx  rd   zDomain.from_QQ_gmpyc                 C   r^   )z,Convert a real element object to ``dtype``. Nr   r`   r   r   r   from_RealField|  rd   zDomain.from_RealFieldc                 C   r^   )z(Convert a complex element to ``dtype``. Nr   r`   r   r   r   from_ComplexField  rd   zDomain.from_ComplexFieldc                 C   r^   )z*Convert an algebraic number to ``dtype``. Nr   r`   r   r   r   from_AlgebraicField  rd   zDomain.from_AlgebraicFieldc                 C   s   |j r| |j|jS dS )#Convert a polynomial to ``dtype``. N)rH   rR   rS   domr`   r   r   r   from_PolynomialRing  s   zDomain.from_PolynomialRingc                 C   r^   )z*Convert a rational function to ``dtype``. Nr   r`   r   r   r   from_FractionField  rd   zDomain.from_FractionFieldc                 C   s   |  |j|jS )z.Convert an ``ExtensionElement`` to ``dtype``. )rB   r%   Zringr`   r   r   r   from_MonogenicFiniteExtension  s   z$Domain.from_MonogenicFiniteExtensionc                 C   s   |  |jS z&Convert a ``EX`` object to ``dtype``. )rT   exr`   r   r   r   from_ExpressionDomain  rW   zDomain.from_ExpressionDomainc                 C   s
   |  |S rs   )rT   r`   r   r   r   from_ExpressionRawDomain  r8   zDomain.from_ExpressionRawDomainc                 C   s"   |  dkr| | |jS dS )rn   r   N)ZdegreerR   rS   ro   r`   r   r   r   from_GlobalPolynomialRing  s   z Domain.from_GlobalPolynomialRingc                 C   s   |  ||S r   )rq   r`   r   r   r   from_GeneralizedPolynomialRing  s   z%Domain.from_GeneralizedPolynomialRingc                 C   sP   | j rt| jt|@ s|j r#t|jt|@ r#td| |t|f | |S )Nz+can't unify %s with %s, given %s generators)is_Compositesetsymbolsr   tupleunify)rb   ra   r{   r   r   r   unify_with_symbols  s   0
zDomain.unify_with_symbolsc                 C   sx  |dur
|  ||S | |kr| S | jr| S |jr|S | jr| S |jr$|S | js*|jr_|jr2|| } }|jrNtt| j|jgd | jkrI|| } }|| S || j	}| j
|}| |S | jse|jr| jrk| jn| }|jrs|jn|}| jr{| jnd}|jr|jnd}||}t||}| jr| jn|j}| jr|js|jr| jr|jr|js|jr|jr| }| jr|jr| js|jr| j}	n|j}	ddlm}
 |	|
kr|	||S |	|||S dd }|jr|| } }| jr|js|jr|| j| |S | S |jr|| } }| jr*|jr|| j| |S |js|jr(ddlm} || j| j d	S | S |j!r3|| } }| j!r`|jr?|" }|jrG|# }|j!r^| j| j|jgt| j$|j$R  S | S | jrf| S |jrl|S | jrz|j%rx| " } | S |jr| j%r|" }|S | j%r| S |j%r|S | j&r| S |j&r|S | j'r|j'r| t(| j)|j)t*d
S ddl+m,} |S )aZ  
        Construct a minimal domain that contains elements of ``K0`` and ``K1``.

        Known domains (from smallest to largest):

        - ``GF(p)``
        - ``ZZ``
        - ``QQ``
        - ``RR(prec, tol)``
        - ``CC(prec, tol)``
        - ``ALG(a, b, c)``
        - ``K[x, y, z]``
        - ``K(x, y, z)``
        - ``EX``

        N   r   r   )GlobalPolynomialRingc                 S   s(   t |j|j}t |j|j}| ||dS )NprecrG   )max	precision	tolerance)clsrb   ra   r   rG   r   r   r   	mkinexact  s   zDomain.unify.<locals>.mkinexact)rF   r   )key)EX)-r~   is_EXRAWis_EXis_FiniteExtensionlistr	   modulusZ
set_domaindropsymboldomainr}   ry   ro   r{   r   orderis_FractionFieldis_PolynomialRingr   has_assoc_Ringget_ringr,   &sympy.polys.domains.old_polynomialringr   is_ComplexFieldis_RealFieldis_GaussianRingis_GaussianFieldZ sympy.polys.domains.complexfieldrF   r   r   is_AlgebraicField	get_fieldZas_AlgebraicFieldZorig_extis_RationalFieldis_IntegerRingis_FiniteFieldr   modr   rK   r   )rb   ra   r{   Z	K0_groundZ	K1_groundZ
K0_symbolsZ
K1_symbolsr   r   r   r   r   rF   r   r   r   r   r}     s   









&zDomain.unifyc                 C   s   t |to
| j|jkS )z0Returns ``True`` if two domains are equivalent. )rL   r   r.   r   otherr   r   r   __eq__9  s   zDomain.__eq__c                 C   s
   | |k S )z1Returns ``False`` if two domains are equivalent. r   r   r   r   r   __ne__=  r8   zDomain.__ne__c                 C   s<   g }|D ]}t |tr|| | q|| | q|S )z5Rersively apply ``self`` to all elements of ``seq``. )rL   r   appendmap)r   seqrA   eltr   r   r   r   A  s   
z
Domain.mapc                 C      t d|  )z)Returns a ring associated with ``self``. z#there is no ring associated with %sr   r   r   r   r   r   M  rW   zDomain.get_ringc                 C   r   )z*Returns a field associated with ``self``. z$there is no field associated with %sr   r   r   r   r   r   Q  rW   zDomain.get_fieldc                 C   s   | S )z2Returns an exact domain associated with ``self``. r   r   r   r   r   	get_exactU  rd   zDomain.get_exactc                 C   s   t |dr
| j| S | |S )z0The mathematical way to make a polynomial ring. __iter__)hasattr	poly_ringr   r{   r   r   r   __getitem__Y  s   


zDomain.__getitem__)r   c                G      ddl m} || ||S z(Returns a polynomial ring, i.e. `K[X]`. r   )PolynomialRing)Z"sympy.polys.domains.polynomialringr   )r   r   r{   r   r   r   r   r   `     zDomain.poly_ringc                G   r   z'Returns a fraction field, i.e. `K(X)`. r   )FractionField)Z!sympy.polys.domains.fractionfieldr   )r   r   r{   r   r   r   r   
frac_fielde  r   zDomain.frac_fieldc                 O   "   ddl m} || g|R i |S r   )r   r   )r   r{   kwargsr   r   r   r   old_poly_ringj     zDomain.old_poly_ringc                 O   r   r   )Z%sympy.polys.domains.old_fractionfieldr   )r   r{   r   r   r   r   r   old_frac_fieldo  r   zDomain.old_frac_fieldc                 G   r   )z6Returns an algebraic field, i.e. `K(\alpha, \ldots)`. z$can't create algebraic field over %sr   )r   	extensionr   r   r   algebraic_fieldt  rW   zDomain.algebraic_fieldc                 G   r    )z$Inject generators into this domain. r!   r   r   r   r   injectx  rd   zDomain.injectc                 G   s   | j r| S t)z"Drop generators from this domain. )	is_Simpler"   r   r   r   r   r   |  s   zDomain.dropc                 C   s   | S )zReturns True if ``a`` is zero. r   rX   r   r   r   is_zero     zDomain.is_zeroc                 C   s
   || j kS )zReturns True if ``a`` is one. )onerX   r   r   r   is_one  r8   zDomain.is_onec                 C   s   |dkS )z#Returns True if ``a`` is positive. r   r   rX   r   r   r   is_positive     zDomain.is_positivec                 C   s   |dk S )z#Returns True if ``a`` is negative. r   r   rX   r   r   r   is_negative  r   zDomain.is_negativec                 C   s   |dkS )z'Returns True if ``a`` is non-positive. r   r   rX   r   r   r   is_nonpositive  r   zDomain.is_nonpositivec                 C   s   |dkS )z'Returns True if ``a`` is non-negative. r   r   rX   r   r   r   is_nonnegative  r   zDomain.is_nonnegativec                 C   s   |  |r	| j S | jS r   )r   r   rX   r   r   r   canonical_unit  s   
zDomain.canonical_unitc                 C   r\   )z.Absolute value of ``a``, implies ``__abs__``. )absrX   r   r   r   r     r   z
Domain.absc                 C   s   | S )z,Returns ``a`` negated, implies ``__neg__``. r   rX   r   r   r   neg  r   z
Domain.negc                 C   s   |
 S )z-Returns ``a`` positive, implies ``__pos__``. r   rX   r   r   r   pos  r   z
Domain.posc                 C   s   || S )z.Sum of ``a`` and ``b``, implies ``__add__``.  r   r   rY   br   r   r   add  r   z
Domain.addc                 C   s   || S )z5Difference of ``a`` and ``b``, implies ``__sub__``.  r   r   r   r   r   sub  r   z
Domain.subc                 C   s   || S )z2Product of ``a`` and ``b``, implies ``__mul__``.  r   r   r   r   r   mul  r   z
Domain.mulc                 C   s   || S )z2Raise ``a`` to power ``b``, implies ``__pow__``.  r   r   r   r   r   pow  r   z
Domain.powc                 C   r    )a  Exact quotient of *a* and *b*. Analogue of ``a / b``.

        Explanation
        ===========

        This is essentially the same as ``a / b`` except that an error will be
        raised if the division is inexact (if there is any remainder) and the
        result will always be a domain element. When working in a
        :py:class:`~.Domain` that is not a :py:class:`~.Field` (e.g. :ref:`ZZ`
        or :ref:`K[x]`) ``exquo`` should be used instead of ``/``.

        The key invariant is that if ``q = K.exquo(a, b)`` (and ``exquo`` does
        not raise an exception) then ``a == b*q``.

        Examples
        ========

        We can use ``K.exquo`` instead of ``/`` for exact division.

        >>> from sympy import ZZ
        >>> ZZ.exquo(ZZ(4), ZZ(2))
        2
        >>> ZZ.exquo(ZZ(5), ZZ(2))
        Traceback (most recent call last):
            ...
        ExactQuotientFailed: 2 does not divide 5 in ZZ

        Over a :py:class:`~.Field` such as :ref:`QQ`, division (with nonzero
        divisor) is always exact so in that case ``/`` can be used instead of
        :py:meth:`~.Domain.exquo`.

        >>> from sympy import QQ
        >>> QQ.exquo(QQ(5), QQ(2))
        5/2
        >>> QQ(5) / QQ(2)
        5/2

        Parameters
        ==========

        a: domain element
            The dividend
        b: domain element
            The divisor

        Returns
        =======

        q: domain element
            The exact quotient

        Raises
        ======

        ExactQuotientFailed: if exact division is not possible.
        ZeroDivisionError: when the divisor is zero.

        See also
        ========

        quo: Analogue of ``a // b``
        rem: Analogue of ``a % b``
        div: Analogue of ``divmod(a, b)``

        Notes
        =====

        Since the default :py:attr:`~.Domain.dtype` for :ref:`ZZ` is ``int``
        (or ``mpz``) division as ``a / b`` should not be used as it would give
        a ``float``.

        >>> ZZ(4) / ZZ(2)
        2.0
        >>> ZZ(5) / ZZ(2)
        2.5

        Using ``/`` with :ref:`ZZ` will lead to incorrect results so
        :py:meth:`~.Domain.exquo` should be used instead.

        r!   r   r   r   r   exquo  s   QzDomain.exquoc                 C   r    )aG  Quotient of *a* and *b*. Analogue of ``a // b``.

        ``K.quo(a, b)`` is equivalent to ``K.div(a, b)[0]``. See
        :py:meth:`~.Domain.div` for more explanation.

        See also
        ========

        rem: Analogue of ``a % b``
        div: Analogue of ``divmod(a, b)``
        exquo: Analogue of ``a / b``
        r!   r   r   r   r   quo     z
Domain.quoc                 C   r    )aN  Modulo division of *a* and *b*. Analogue of ``a % b``.

        ``K.rem(a, b)`` is equivalent to ``K.div(a, b)[1]``. See
        :py:meth:`~.Domain.div` for more explanation.

        See also
        ========

        quo: Analogue of ``a // b``
        div: Analogue of ``divmod(a, b)``
        exquo: Analogue of ``a / b``
        r!   r   r   r   r   rem  r   z
Domain.remc                 C   r    )a[	  Quotient and remainder for *a* and *b*. Analogue of ``divmod(a, b)``

        Explanation
        ===========

        This is essentially the same as ``divmod(a, b)`` except that is more
        consistent when working over some :py:class:`~.Field` domains such as
        :ref:`QQ`. When working over an arbitrary :py:class:`~.Domain` the
        :py:meth:`~.Domain.div` method should be used instead of ``divmod``.

        The key invariant is that if ``q, r = K.div(a, b)`` then
        ``a == b*q + r``.

        The result of ``K.div(a, b)`` is the same as the tuple
        ``(K.quo(a, b), K.rem(a, b))`` except that if both quotient and
        remainder are needed then it is more efficient to use
        :py:meth:`~.Domain.div`.

        Examples
        ========

        We can use ``K.div`` instead of ``divmod`` for floor division and
        remainder.

        >>> from sympy import ZZ, QQ
        >>> ZZ.div(ZZ(5), ZZ(2))
        (2, 1)

        If ``K`` is a :py:class:`~.Field` then the division is always exact
        with a remainder of :py:attr:`~.Domain.zero`.

        >>> QQ.div(QQ(5), QQ(2))
        (5/2, 0)

        Parameters
        ==========

        a: domain element
            The dividend
        b: domain element
            The divisor

        Returns
        =======

        (q, r): tuple of domain elements
            The quotient and remainder

        Raises
        ======

        ZeroDivisionError: when the divisor is zero.

        See also
        ========

        quo: Analogue of ``a // b``
        rem: Analogue of ``a % b``
        exquo: Analogue of ``a / b``

        Notes
        =====

        If ``gmpy`` is installed then the ``gmpy.mpq`` type will be used as
        the :py:attr:`~.Domain.dtype` for :ref:`QQ`. The ``gmpy.mpq`` type
        defines ``divmod`` in a way that is undesirable so
        :py:meth:`~.Domain.div` should be used instead of ``divmod``.

        >>> a = QQ(1)
        >>> b = QQ(3, 2)
        >>> a               # doctest: +SKIP
        mpq(1,1)
        >>> b               # doctest: +SKIP
        mpq(3,2)
        >>> divmod(a, b)    # doctest: +SKIP
        (mpz(0), mpq(1,1))
        >>> QQ.div(a, b)    # doctest: +SKIP
        (mpq(2,3), mpq(0,1))

        Using ``//`` or ``%`` with :ref:`QQ` will lead to incorrect results so
        :py:meth:`~.Domain.div` should be used instead.

        r!   r   r   r   r   div-  s   Tz
Domain.divc                 C   r    )z5Returns inversion of ``a mod b``, implies something. r!   r   r   r   r   invert  rd   zDomain.invertc                 C   r    )z!Returns ``a**(-1)`` if possible. r!   rX   r   r   r   revert  rd   zDomain.revertc                 C   r    )zReturns numerator of ``a``. r!   rX   r   r   r   numer  rd   zDomain.numerc                 C   r    )zReturns denominator of ``a``. r!   rX   r   r   r   denom  rd   zDomain.denomc                 C   s   |  ||\}}}||fS )z&Half extended GCD of ``a`` and ``b``. )gcdex)r   rY   r   sthr   r   r   
half_gcdex  s   zDomain.half_gcdexc                 C   r    )z!Extended GCD of ``a`` and ``b``. r!   r   r   r   r   r     rd   zDomain.gcdexc                 C   s.   |  ||}| ||}| ||}|||fS )z.Returns GCD and cofactors of ``a`` and ``b``. )gcdr   )r   rY   r   r   ZcfaZcfbr   r   r   	cofactors  s   
zDomain.cofactorsc                 C   r    )z Returns GCD of ``a`` and ``b``. r!   r   r   r   r   r     rd   z
Domain.gcdc                 C   r    )z Returns LCM of ``a`` and ``b``. r!   r   r   r   r   lcm  rd   z
Domain.lcmc                 C   r    )z#Returns b-base logarithm of ``a``. r!   r   r   r   r   log  rd   z
Domain.logc                 C   r    )zReturns square root of ``a``. r!   rX   r   r   r   sqrt  rd   zDomain.sqrtc                 K   s   |  |j|fi |S )z*Returns numerical approximation of ``a``. )r[   evalf)r   rY   r   optionsr   r   r   r     s   zDomain.evalfc                 C   s   |S r   r   rX   r   r   r   real  r$   zDomain.realc                 C   r   r   )zerorX   r   r   r   imag  r'   zDomain.imagc                 C   s   ||kS )z+Check if ``a`` and ``b`` are almost equal. r   )r   rY   r   r   r   r   r   almosteq  r   zDomain.almosteqc                 C   s   t d)z*Return the characteristic of this domain. zcharacteristic()r!   r   r   r   r   characteristic  r   zDomain.characteristicr   )r-   
__module____qualname____doc__r.   r   r   r   r   r   Zhas_assoc_Fieldr   Zis_FFr   Zis_ZZr   Zis_QQr   Zis_ZZ_Ir   Zis_QQ_Ir   Zis_RRr   Zis_CCr   Zis_Algebraicr   Zis_Polyr   Zis_FracZis_SymbolicDomainr   Zis_SymbolicRawDomainr   r   Zis_ExactrQ   r   ry   Zis_PIDZhas_CharacteristicZeror%   r:   propertyr
   r   r   r#   r&   r)   r/   r4   r6   r7   r9   rB   rR   rJ   rZ   r[   rT   r]   rc   re   rf   rg   rh   ri   rj   rk   rl   rm   rp   rq   rr   ru   rv   rw   rx   r~   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   nr   r   r   r   r   r   r   r   r      s    +

;]
 SV

r   N)r   typingr   r   r   Z
sympy.corer   r   Zsympy.core.compatibilityr   r   r	   Zsympy.core.decoratorsr
   Z!sympy.polys.domains.domainelementr   Zsympy.polys.orderingsr   Zsympy.polys.polyerrorsr   r   r   Zsympy.polys.polyutilsr   r   Zsympy.utilitiesr   r   r   __all__r   r   r   r   <module>   s,             
A