o
    oh                     @  s   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
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 ddlmZ eG dd dZdgZdS )z)Implementation of :class:`Domain` class.     )annotations)Any)AlgebraicNumber)Basicsympify)ordered)GROUND_TYPES)DomainElement)lex)UnificationFailedCoercionFailedDomainError)_unify_gens_not_a_coeff)public)is_sequencec                   @  s  e Zd ZU dZdZded< 	 dZded< 	 dZded< 	 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
ed< dZ,d
ed< dd Z-dd Z.dd Z/dd Z0dd Z1e2dd Z3dd Z4dd Z5dd Z6ddd Z7d!d" Z8d#d$ Z9d%d& Z:d'd( Z;d)d* Z<d+d, Z=d-d. Z>d/d0 Z?d1d2 Z@d3d4 ZAd5d6 ZBd7d8 ZCd9d: ZDd;d< ZEd=d> ZFd?d@ ZGdAdB ZHdCdD ZIdEdF ZJdGdH ZKdIdJ ZLdKdL ZMdMdN ZNdOdP ZOddQdRZPdSdT ZQdUdV ZRdWdX ZSdYdZ ZTd[d\ ZUd]d^ ZVd_d` ZWeXdadbdcZYeXdadddeZZdfdg Z[dhdi Z\ddjdkdlZ]ddndoZ^ddqdrZ_dsdt Z`dudv Zadwdx Zbdydz Zcd{d| Zdd}d~ Zedd Zfdd Zgdd Zhdd Zidd Zjdd Zkdd Zldd Zmdd Zndd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd Zudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd Zdd ZdddZeZdd Zdd ZdddZdd ZdS )Domainay  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) cannot. 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

    Nztype | Nonedtyper   zerooneFTz
str | Nonerepaliasc                 C     t NNotImplementedErrorself r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/domains/domain.py__init__g     zDomain.__init__c                 C     | j S r   )r   r   r   r   r   __str__j     zDomain.__str__c                 C  s   t | S r   )strr   r   r   r   __repr__m  s   zDomain.__repr__c                 C  s   t | jj| jfS r   )hash	__class____name__r   r   r   r   r   __hash__p  s   zDomain.__hash__c                 G  
   | j | S r   r   r   argsr   r   r   news     
z
Domain.newc                 C  r"   )z#Alias for :py:attr:`~.Domain.dtype`r,   r   r   r   r   tpv  s   z	Domain.tpc                 G  r+   )z7Construct an element of ``self`` domain from ``args``. )r/   r-   r   r   r   __call__{     
zDomain.__call__c                 G  r+   r   r,   r-   r   r   r   normal  r0   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. Nfrom_z*Cannot convert %s of type %s from %s to %s)r   r(   r)   getattrr   type)r   elementbasemethod_convertresultr   r   r   convert_from  s   


zDomain.convert_fromc              	   C  s  |durt |rtd| | ||S | |r|S t |r%td| ddlm}m}m}m} ||r<| ||S t	|t
rI| |||S tdkret	||jrY| ||S t	||jre| ||S t	|trw|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``. Nz%s is not in any domainr   )ZZQQ	RealFieldComplexFieldpythonF)tol	is_groundT)strictz"Cannot convert %s of type %s to %s)r   r   r=   of_typesympy.polys.domainsr>   r?   r@   rA   
isinstanceintr   r1   floatcomplexr	   parentis_Numericalr6   convertLCr   
from_sympy	TypeError
ValueErrorr   r   r7   )r   r8   r9   r>   r?   r@   rA   rL   r   r   r   rN     sX   









zDomain.convertc                 C  s   t || jS )z%Check if ``a`` is of type ``dtype``. )rH   r1   )r   r8   r   r   r   rF        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   rN   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   rT   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   rT   r   r   r   rP   :  s   zDomain.from_sympyc                 C  s   t || jdS )N)start)sumr   r-   r   r   r   rY   V  s   z
Domain.sumc                 C     dS z.Convert ``ModularInteger(int)`` to ``dtype``. Nr   K1rU   K0r   r   r   from_FFY     zDomain.from_FFc                 C  rZ   r[   r   r\   r   r   r   from_FF_python]  r`   zDomain.from_FF_pythonc                 C  rZ   )z.Convert a Python ``int`` object to ``dtype``. Nr   r\   r   r   r   from_ZZ_pythona  r`   zDomain.from_ZZ_pythonc                 C  rZ   )z3Convert a Python ``Fraction`` object to ``dtype``. Nr   r\   r   r   r   from_QQ_pythone  r`   zDomain.from_QQ_pythonc                 C  rZ   )z.Convert ``ModularInteger(mpz)`` to ``dtype``. Nr   r\   r   r   r   from_FF_gmpyi  r`   zDomain.from_FF_gmpyc                 C  rZ   )z,Convert a GMPY ``mpz`` object to ``dtype``. Nr   r\   r   r   r   from_ZZ_gmpym  r`   zDomain.from_ZZ_gmpyc                 C  rZ   )z,Convert a GMPY ``mpq`` object to ``dtype``. Nr   r\   r   r   r   from_QQ_gmpyq  r`   zDomain.from_QQ_gmpyc                 C  rZ   )z,Convert a real element object to ``dtype``. Nr   r\   r   r   r   from_RealFieldu  r`   zDomain.from_RealFieldc                 C  rZ   )z(Convert a complex element to ``dtype``. Nr   r\   r   r   r   from_ComplexFieldy  r`   zDomain.from_ComplexFieldc                 C  rZ   )z*Convert an algebraic number to ``dtype``. Nr   r\   r   r   r   from_AlgebraicField}  r`   zDomain.from_AlgebraicFieldc                 C  s   |j r| |j|jS dS )#Convert a polynomial to ``dtype``. N)rD   rN   rO   domr\   r   r   r   from_PolynomialRing  s   zDomain.from_PolynomialRingc                 C  rZ   )z*Convert a rational function to ``dtype``. Nr   r\   r   r   r   from_FractionField  r`   zDomain.from_FractionFieldc                 C  s   |  |j|jS )z.Convert an ``ExtensionElement`` to ``dtype``. )r=   r   ringr\   r   r   r   from_MonogenicFiniteExtension  s   z$Domain.from_MonogenicFiniteExtensionc                 C  s   |  |jS z&Convert a ``EX`` object to ``dtype``. )rP   exr\   r   r   r   from_ExpressionDomain  rS   zDomain.from_ExpressionDomainc                 C  s
   |  |S rp   )rP   r\   r   r   r   from_ExpressionRawDomain  r3   zDomain.from_ExpressionRawDomainc                 C  s"   |  dkr| | |jS dS )rj   r   N)degreerN   rO   rk   r\   r   r   r   from_GlobalPolynomialRing  s   z Domain.from_GlobalPolynomialRingc                 C  s   |  ||S r   )rm   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,Cannot unify %s with %s, given %s generators)is_Compositesetsymbolsr   tupleunify)r^   r]   ry   r   r   r   unify_with_symbols  s   0
zDomain.unify_with_symbolsc                 C  s   | j r| jn| }|j r|jn|}| j r| jnd}|j r|jnd}||}t||}| j r0| jn|j}| jr9|js?|jrO| jrO|jrE|jsO|jrO|j	rO|
 }| j r_|j r[| js[|jr_| j}	n|j}	ddlm}
 |	|
krq|	||S |	|||S )z2Unify two domains where at least one is composite.r   r   )GlobalPolynomialRing)rw   rk   ry   r{   r   orderis_FractionFieldis_PolynomialRingis_Fieldhas_assoc_Ringget_ringr(   &sympy.polys.domains.old_polynomialringr}   )r^   r]   	K0_ground	K1_ground
K0_symbols
K1_symbolsdomainry   r~   clsr}   r   r   r   unify_composite  s8   


zDomain.unify_compositec                 C  s  |dur
|  ||S | |kr| S | jr|js+|  | kr&td| |f | |S | jr0| S |jr5|S | jr:| S |jr?|S | jsE|jrz|jrM|| } }|jritt	| j
|j
gd | j
krd|| } }|| S || j}| j|}| |S | js|jr| |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rddlm} || j| jdS | S |jr|| } }| jr|jr| }|jr| }|jr| j| j|jgt| j|jR  S | S | jr| S |jr|S | jr|j r|  } | S |jr'| j r%| }|S | j r-| S |j r3|S | j!r9| S |j!r?|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``

        NzCannot unify %s with %s   c                 S  s(   t |j|j}t |j|j}| ||dS )NprecrC   )max	precision	tolerance)r   r^   r]   r   rC   r   r   r   	mkinexact  s   zDomain.unify.<locals>.mkinexactr   )rA   r   )EX)$r|   has_CharacteristicZerocharacteristicr   r   is_EXRAWis_EXis_FiniteExtensionlistr   modulus
set_domaindropsymbolr   r{   rw   is_ComplexFieldis_RealFieldr(   is_GaussianRingis_GaussianField sympy.polys.domains.complexfieldrA   r   r   is_AlgebraicField	get_fieldas_AlgebraicFieldrk   r   orig_extis_RationalFieldis_IntegerRingrG   r   )r^   r]   ry   r   rA   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. )rH   r   r   r   otherr   r   r   __eq__D  s   zDomain.__eq__c                 C  s
   | |k S )z1Returns ``False`` if two domains are equivalent. r   r   r   r   r   __ne__I  r3   zDomain.__ne__c                 C  s<   g }|D ]}t |tr|| | q|| | q|S )z5Rersively apply ``self`` to all elements of ``seq``. )rH   r   appendmap)r   seqr<   eltr   r   r   r   M  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   Y  rS   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   ]  rS   zDomain.get_fieldc                 C  s   | S )z2Returns an exact domain associated with ``self``. r   r   r   r   r   	get_exacta  r`   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   ry   r   r   r   __getitem__e  s   


zDomain.__getitem__)r~   c                G     ddl m} || ||S z(Returns a polynomial ring, i.e. `K[X]`. r   )PolynomialRing)"sympy.polys.domains.polynomialringr   )r   r~   ry   r   r   r   r   r   l     zDomain.poly_ringc                G  r   z'Returns a fraction field, i.e. `K(X)`. r   )FractionField)!sympy.polys.domains.fractionfieldr   )r   r~   ry   r   r   r   r   
frac_fieldq  r   zDomain.frac_fieldc                 O  "   ddl m} || g|R i |S r   )r   r   )r   ry   kwargsr   r   r   r   old_poly_ringv     zDomain.old_poly_ringc                 O  r   r   )%sympy.polys.domains.old_fractionfieldr   )r   ry   r   r   r   r   r   old_frac_field{  r   zDomain.old_frac_fieldr   c                G  r   )z6Returns an algebraic field, i.e. `K(\alpha, \ldots)`. z%Cannot create algebraic field over %sr   )r   r   	extensionr   r   r   algebraic_field  rS   zDomain.algebraic_fieldc                 C  s0   ddl m} |||}t||d}| j||dS )a  
        Convenience method to construct an algebraic extension on a root of a
        polynomial, chosen by root index.

        Parameters
        ==========

        poly : :py:class:`~.Poly`
            The polynomial whose root generates the extension.
        alias : str, optional (default=None)
            Symbol name for the generator of the extension.
            E.g. "alpha" or "theta".
        root_index : int, optional (default=-1)
            Specifies which root of the polynomial is desired. The ordering is
            as defined by the :py:class:`~.ComplexRootOf` class. The default of
            ``-1`` selects the most natural choice in the common cases of
            quadratic and cyclotomic fields (the square root on the positive
            real or imaginary axis, resp. $\mathrm{e}^{2\pi i/n}$).

        Examples
        ========

        >>> from sympy import QQ, Poly
        >>> from sympy.abc import x
        >>> f = Poly(x**2 - 2)
        >>> K = QQ.alg_field_from_poly(f)
        >>> K.ext.minpoly == f
        True
        >>> g = Poly(8*x**3 - 6*x - 1)
        >>> L = QQ.alg_field_from_poly(g, "alpha")
        >>> L.ext.minpoly == g
        True
        >>> L.to_sympy(L([1, 1, 1]))
        alpha**2 + alpha + 1

        r   )CRootOfr   )sympy.polys.rootoftoolsr   r   r   )r   polyr   
root_indexr   rootalphar   r   r   alg_field_from_poly  s   %
zDomain.alg_field_from_polyzetac                 C  s2   ddl m} |r|t|7 }| j|||||dS )a  
        Convenience method to construct a cyclotomic field.

        Parameters
        ==========

        n : int
            Construct the nth cyclotomic field.
        ss : boolean, optional (default=False)
            If True, append *n* as a subscript on the alias string.
        alias : str, optional (default="zeta")
            Symbol name for the generator.
        gen : :py:class:`~.Symbol`, optional (default=None)
            Desired variable for the cyclotomic polynomial that defines the
            field. If ``None``, a dummy variable will be used.
        root_index : int, optional (default=-1)
            Specifies which root of the polynomial is desired. The ordering is
            as defined by the :py:class:`~.ComplexRootOf` class. The default of
            ``-1`` selects the root $\mathrm{e}^{2\pi i/n}$.

        Examples
        ========

        >>> from sympy import QQ, latex
        >>> K = QQ.cyclotomic_field(5)
        >>> K.to_sympy(K([-1, 1]))
        1 - zeta
        >>> L = QQ.cyclotomic_field(7, True)
        >>> a = L.to_sympy(L([-1, 1]))
        >>> print(a)
        1 - zeta7
        >>> print(latex(a))
        1 - \zeta_{7}

        r   )cyclotomic_poly)r   r   )sympy.polys.specialpolysr   r%   r   )r   nssr   genr   r   r   r   r   cyclotomic_field  s   $zDomain.cyclotomic_fieldc                 G  r   )z$Inject generators into this domain. r   r   r   r   r   inject  r`   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   rT   r   r   r   is_zero     zDomain.is_zeroc                 C  s
   || j kS )zReturns True if ``a`` is one. )r   rT   r   r   r   is_one  r3   zDomain.is_onec                 C  s   |dkS )z#Returns True if ``a`` is positive. r   r   rT   r   r   r   is_positive     zDomain.is_positivec                 C  s   |dk S )z#Returns True if ``a`` is negative. r   r   rT   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   rT   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   rT   r   r   r   is_nonnegative  r   zDomain.is_nonnegativec                 C  s   |  |r	| j S | jS r   )r   r   rT   r   r   r   canonical_unit  s   
zDomain.canonical_unitc                 C  s   t |S )z.Absolute value of ``a``, implies ``__abs__``. )absrT   r   r   r   r      r   z
Domain.absc                 C  s   | S )z,Returns ``a`` negated, implies ``__neg__``. r   rT   r   r   r   neg  r   z
Domain.negc                 C  s   |
 S )z-Returns ``a`` positive, implies ``__pos__``. r   rT   r   r   r   pos  r   z
Domain.posc                 C  s   || S )z.Sum of ``a`` and ``b``, implies ``__add__``.  r   r   rU   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`` which is not a domain element.

        >>> ZZ(4) / ZZ(2) # doctest: +SKIP
        2.0
        >>> ZZ(5) / ZZ(2) # doctest: +SKIP
        2.5

        On the other hand with `SYMPY_GROUND_TYPES=flint` elements of :ref:`ZZ`
        are ``flint.fmpz`` and division would raise an exception:

        >>> ZZ(4) / ZZ(2) # doctest: +SKIP
        Traceback (most recent call last):
        ...
        TypeError: unsupported operand type(s) for /: 'fmpz' and 'fmpz'

        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   Yz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   quow     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  r`   zDomain.invertc                 C  r   )z!Returns ``a**(-1)`` if possible. r   rT   r   r   r   revert  r`   zDomain.revertc                 C  r   )zReturns numerator of ``a``. r   rT   r   r   r   numer  r`   zDomain.numerc                 C  r   )zReturns denominator of ``a``. r   rT   r   r   r   denom  r`   zDomain.denomc                 C  s   |  ||\}}}||fS )z&Half extended GCD of ``a`` and ``b``. )gcdex)r   rU   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      r`   zDomain.gcdexc                 C  s.   |  ||}| ||}| ||}|||fS )z.Returns GCD and cofactors of ``a`` and ``b``. )gcdr   )r   rU   r   r   cfacfbr   r   r   	cofactors  s   
zDomain.cofactorsc                 C  r   )z Returns GCD of ``a`` and ``b``. r   r   r   r   r   r     r`   z
Domain.gcdc                 C  r   )z Returns LCM of ``a`` and ``b``. r   r   r   r   r   lcm  r`   z
Domain.lcmc                 C  r   )z#Returns b-base logarithm of ``a``. r   r   r   r   r   log  r`   z
Domain.logc                 C  r   )aJ  Returns a (possibly inexact) square root of ``a``.

        Explanation
        ===========
        There is no universal definition of "inexact square root" for all
        domains. It is not recommended to implement this method for domains
        other then :ref:`ZZ`.

        See also
        ========
        exsqrt
        r   rT   r   r   r   sqrt  r   zDomain.sqrtc                 C  r   )a  Returns whether ``a`` is a square in the domain.

        Explanation
        ===========
        Returns ``True`` if there is an element ``b`` in the domain such that
        ``b * b == a``, otherwise returns ``False``. For inexact domains like
        :ref:`RR` and :ref:`CC`, a tiny difference in this equality can be
        tolerated.

        See also
        ========
        exsqrt
        r   rT   r   r   r   	is_square&  s   zDomain.is_squarec                 C  r   )a'  Principal square root of a within the domain if ``a`` is square.

        Explanation
        ===========
        The implementation of this method should return an element ``b`` in the
        domain such that ``b * b == a``, or ``None`` if there is no such ``b``.
        For inexact domains like :ref:`RR` and :ref:`CC`, a tiny difference in
        this equality can be tolerated. The choice of a "principal" square root
        should follow a consistent rule whenever possible.

        See also
        ========
        sqrt, is_square
        r   rT   r   r   r   exsqrt6  s   zDomain.exsqrtc                 K  s   |  |j|fi |S )z*Returns numerical approximation of ``a``. )rW   evalf)r   rU   r   optionsr   r   r   r  G  s   zDomain.evalfc                 C  s   |S r   r   rT   r   r   r   realM  r!   zDomain.realc                 C  r"   r   )r   rT   r   r   r   imagP  r$   zDomain.imagc                 C  s   ||kS )z+Check if ``a`` and ``b`` are almost equal. r   )r   rU   r   r   r   r   r   almosteqS  r   zDomain.almosteqc                 C  s   t d)z*Return the characteristic of this domain. zcharacteristic()r   r   r   r   r   r   W  r   zDomain.characteristicr   )Nr   )Fr   Nr   )r)   
__module____qualname____doc__r   __annotations__r   r   is_Ringr   r   has_assoc_Fieldis_FiniteFieldis_FFr   is_ZZr   is_QQr   is_ZZ_Ir   is_QQ_Ir   is_RRr   is_CCr   is_Algebraicr   is_Polyr   is_Fracis_SymbolicDomainr   is_SymbolicRawDomainr   r   is_ExactrM   r   rw   is_PIDr   r   r   r    r#   r&   r*   r/   propertyr1   r2   r4   r=   rN   rF   rV   rW   rP   rY   r_   ra   rb   rc   rd   re   rf   rg   rh   ri   rl   rm   ro   rr   rs   ru   rv   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   r  r  r  r  r  r   r  r  r	  r   r   r   r   r   r      s   
  +

;]
!

**[V

r   N)r  
__future__r   typingr   sympy.core.numbersr   
sympy.corer   r   sympy.core.sortingr   sympy.external.gmpyr   !sympy.polys.domains.domainelementr	   sympy.polys.orderingsr
   sympy.polys.polyerrorsr   r   r   sympy.polys.polyutilsr   r   sympy.utilitiesr   sympy.utilities.iterablesr   r   __all__r   r   r   r   <module>   s4              
S