o
    8Va$*                     @   s>   d Z ddlmZ ddlmZ G dd dZG dd deZdS )	z-Computations with ideals of polynomial rings.    )reduce)CoercionFailedc                   @   s  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZeZd6d7 ZeZ d8d9 Z!d:d; Z"d<d= Z#d>S )?Ideala  
    Abstract base class for ideals.

    Do not instantiate - use explicit constructors in the ring class instead:

    >>> from sympy import QQ
    >>> from sympy.abc import x
    >>> QQ.old_poly_ring(x).ideal(x+1)
    <x + 1>

    Attributes

    - ring - the ring this ideal belongs to

    Non-implemented methods:

    - _contains_elem
    - _contains_ideal
    - _quotient
    - _intersect
    - _union
    - _product
    - is_whole_ring
    - is_zero
    - is_prime, is_maximal, is_primary, is_radical
    - is_principal
    - height, depth
    - radical

    Methods that likely should be overridden in subclasses:

    - reduce_element
    c                 C      t )z&Implementation of element containment.NotImplementedErrorselfx r   9/usr/lib/python3/dist-packages/sympy/polys/agca/ideals.py_contains_elem+      zIdeal._contains_elemc                 C   r   )z$Implementation of ideal containment.r   )r	   Ir   r   r   _contains_ideal/   r   zIdeal._contains_idealc                 C   r   )z!Implementation of ideal quotient.r   r	   Jr   r   r   	_quotient3   r   zIdeal._quotientc                 C   r   )z%Implementation of ideal intersection.r   r   r   r   r   
_intersect7   r   zIdeal._intersectc                 C   r   )z*Return True if ``self`` is the whole ring.r   r	   r   r   r   is_whole_ring;   r   zIdeal.is_whole_ringc                 C   r   )z*Return True if ``self`` is the zero ideal.r   r   r   r   r   is_zero?   r   zIdeal.is_zeroc                 C   s   |  |o	| | S )z!Implementation of ideal equality.)r   r   r   r   r   _equalsC   s   zIdeal._equalsc                 C   r   )z)Return True if ``self`` is a prime ideal.r   r   r   r   r   is_primeG   r   zIdeal.is_primec                 C   r   )z+Return True if ``self`` is a maximal ideal.r   r   r   r   r   
is_maximalK   r   zIdeal.is_maximalc                 C   r   )z+Return True if ``self`` is a radical ideal.r   r   r   r   r   
is_radicalO   r   zIdeal.is_radicalc                 C   r   )z+Return True if ``self`` is a primary ideal.r   r   r   r   r   
is_primaryS   r   zIdeal.is_primaryc                 C   r   )z-Return True if ``self`` is a principal ideal.r   r   r   r   r   is_principalW   r   zIdeal.is_principalc                 C   r   )z Compute the radical of ``self``.r   r   r   r   r   radical[   r   zIdeal.radicalc                 C   r   )zCompute the depth of ``self``.r   r   r   r   r   depth_   r   zIdeal.depthc                 C   r   )zCompute the height of ``self``.r   r   r   r   r   heightc   r   zIdeal.heightc                 C   s
   || _ d S N)ring)r	   r"   r   r   r   __init__k      
zIdeal.__init__c                 C   s,   t |tr|j| jkrtd| j|f dS )z.Helper to check ``J`` is an ideal of our ring.z J must be an ideal of %s, got %sN)
isinstancer   r"   
ValueErrorr   r   r   r   _check_idealn   s
   zIdeal._check_idealc                 C   s   |  | j|S )aD  
        Return True if ``elem`` is an element of this ideal.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import QQ
        >>> QQ.old_poly_ring(x).ideal(x+1, x-1).contains(3)
        True
        >>> QQ.old_poly_ring(x).ideal(x**2, x**3).contains(x)
        False
        )r   r"   convert)r	   elemr   r   r   containst   s   zIdeal.containsc                    s*   t |tr
 |S t fdd|D S )a  
        Returns True if ``other`` is is a subset of ``self``.

        Here ``other`` may be an ideal.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import QQ
        >>> I = QQ.old_poly_ring(x).ideal(x+1)
        >>> I.subset([x**2 - 1, x**2 + 2*x + 1])
        True
        >>> I.subset([x**2 + 1, x + 1])
        False
        >>> I.subset(QQ.old_poly_ring(x).ideal(x**2 - 1))
        True
        c                 3   s    | ]}  |V  qd S r!   )r   .0r
   r   r   r   	<genexpr>       zIdeal.subset.<locals>.<genexpr>)r%   r   r   all)r	   otherr   r   r   subset   s   

zIdeal.subsetc                 K   s   |  | | j|fi |S )a~  
        Compute the ideal quotient of ``self`` by ``J``.

        That is, if ``self`` is the ideal `I`, compute the set
        `I : J = \{x \in R | xJ \subset I \}`.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> from sympy import QQ
        >>> R = QQ.old_poly_ring(x, y)
        >>> R.ideal(x*y).quotient(R.ideal(x))
        <y>
        )r'   r   r	   r   Zoptsr   r   r   quotient   s   
zIdeal.quotientc                 C      |  | | |S )a  
        Compute the intersection of self with ideal J.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> from sympy import QQ
        >>> R = QQ.old_poly_ring(x, y)
        >>> R.ideal(x).intersect(R.ideal(y))
        <x*y>
        )r'   r   r   r   r   r   	intersect   s   

zIdeal.intersectc                 C   r   )z
        Compute the ideal saturation of ``self`` by ``J``.

        That is, if ``self`` is the ideal `I`, compute the set
        `I : J^\infty = \{x \in R | xJ^n \subset I \text{ for some } n\}`.
        r   r   r   r   r   saturate      zIdeal.saturatec                 C   r4   )aD  
        Compute the ideal generated by the union of ``self`` and ``J``.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import QQ
        >>> QQ.old_poly_ring(x).ideal(x**2 - 1).union(QQ.old_poly_ring(x).ideal((x+1)**2)) == QQ.old_poly_ring(x).ideal(x+1)
        True
        )r'   _unionr   r   r   r   union   s   

zIdeal.unionc                 C   r4   )a  
        Compute the ideal product of ``self`` and ``J``.

        That is, compute the ideal generated by products `xy`, for `x` an element
        of ``self`` and `y \in J`.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> from sympy import QQ
        >>> QQ.old_poly_ring(x, y).ideal(x).product(QQ.old_poly_ring(x, y).ideal(y))
        <x*y>
        )r'   _productr   r   r   r   product   s   

zIdeal.productc                 C   s   |S )z
        Reduce the element ``x`` of our ring modulo the ideal ``self``.

        Here "reduce" has no specific meaning: it could return a unique normal
        form, simplify the expression a bit, or just do nothing.
        r   r   r   r   r   reduce_element   r7   zIdeal.reduce_elementc                 C   sZ   t |ts#| j| }t ||jr|S t ||jjr||S ||S | | | |S r!   )r%   r   r"   Zquotient_ringZdtyper(   r'   r9   )r	   eRr   r   r   __add__   s   



zIdeal.__add__c                 C   sF   t |tsz| j|}W n ty   t Y S w | | | |S r!   )r%   r   r"   idealr   NotImplementedr'   r;   r	   r=   r   r   r   __mul__   s   


zIdeal.__mul__c                 C   s*   |dk rt tdd | g| | jdS )Nr   c                 S   s   | | S r!   r   )r
   yr   r   r   <lambda>  s    zIdeal.__pow__.<locals>.<lambda>   )r   r   r"   r@   )r	   Zexpr   r   r   __pow__
  s   zIdeal.__pow__c                 C   s$   t |tr|j| jkrdS | |S )NF)r%   r   r"   r   rB   r   r   r   __eq__  s   
zIdeal.__eq__c                 C   s
   | |k S r!   r   rB   r   r   r   __ne__  r$   zIdeal.__ne__N)$__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r#   r'   r*   r1   r3   r5   r6   r9   r;   r<   r?   __radd__rC   __rmul__rG   rH   rI   r   r   r   r   r      sD    "
		r   c                   @   s|   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )ModuleImplementedIdealzs
    Ideal implementation relying on the modules code.

    Attributes:

    - _module - the underlying module
    c                 C   s   t | | || _d S r!   )r   r#   _module)r	   r"   moduler   r   r   r#   "  s   
zModuleImplementedIdeal.__init__c                 C      | j |gS r!   )rQ   r*   r   r   r   r   r   &  s   z%ModuleImplementedIdeal._contains_elemc                 C   s   t |tst| j|jS r!   )r%   rP   r   rQ   Zis_submoduler   r   r   r   r   )  s   
z&ModuleImplementedIdeal._contains_idealc                 C   &   t |tst| | j| j|jS r!   )r%   rP   r   	__class__r"   rQ   r5   r   r   r   r   r   .     
z!ModuleImplementedIdeal._intersectc                 K   s$   t |tst| jj|jfi |S r!   )r%   rP   r   rQ   Zmodule_quotientr2   r   r   r   r   3  s   
z ModuleImplementedIdeal._quotientc                 C   rT   r!   )r%   rP   r   rU   r"   rQ   r9   r   r   r   r   r8   8  rV   zModuleImplementedIdeal._unionc                 C   s   dd | j jD S )z
        Return generators for ``self``.

        Examples
        ========

        >>> from sympy import QQ
        >>> from sympy.abc import x, y
        >>> list(QQ.old_poly_ring(x, y).ideal(x, y, x**2 + y).gens)
        [x, y, x**2 + y]
        c                 s   s    | ]}|d  V  qdS )r   Nr   r+   r   r   r   r-   J  s    z.ModuleImplementedIdeal.gens.<locals>.<genexpr>rQ   gensr   r   r   r   rX   =  s   zModuleImplementedIdeal.gensc                 C   
   | j  S )a%  
        Return True if ``self`` is the zero ideal.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import QQ
        >>> QQ.old_poly_ring(x).ideal(x).is_zero()
        False
        >>> QQ.old_poly_ring(x).ideal().is_zero()
        True
        )rQ   r   r   r   r   r   r   L  s   
zModuleImplementedIdeal.is_zeroc                 C   rY   )a  
        Return True if ``self`` is the whole ring, i.e. one generator is a unit.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import QQ, ilex
        >>> QQ.old_poly_ring(x).ideal(x).is_whole_ring()
        False
        >>> QQ.old_poly_ring(x).ideal(3).is_whole_ring()
        True
        >>> QQ.old_poly_ring(x, order=ilex).ideal(2 + x).is_whole_ring()
        True
        )rQ   Zis_full_moduler   r   r   r   r   \  s   
z$ModuleImplementedIdeal.is_whole_ringc                    s0   ddl m  dd fdd| jjD  d S )Nr   sstr<,c                 3   s    | ]\} |V  qd S r!   r   r+   rZ   r   r   r-   p  r.   z2ModuleImplementedIdeal.__repr__.<locals>.<genexpr>>)Zsympyr[   joinrQ   rX   r   r   rZ   r   __repr__n  s   $zModuleImplementedIdeal.__repr__c                    s6   t  tst| | j| jj fdd| jjD  S )Nc                    s(   g | ]\} j jD ]\}|| gq	qS r   rW   )r,   r
   rD   r   r   r   
<listcomp>w  s   ( z3ModuleImplementedIdeal._product.<locals>.<listcomp>)r%   rP   r   rU   r"   rQ   Z	submodulerX   r   r   ra   r   r:   s  s
   
zModuleImplementedIdeal._productc                 C   rS   )a  
        Express ``e`` in terms of the generators of ``self``.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import QQ
        >>> I = QQ.old_poly_ring(x).ideal(x**2 + 1, x)
        >>> I.in_terms_of_generators(1)
        [1, -x]
        )rQ   in_terms_of_generatorsrB   r   r   r   rc   y  s   z-ModuleImplementedIdeal.in_terms_of_generatorsc                 K   s   | j j|gfi |d S )Nr   )rQ   r<   )r	   r
   optionsr   r   r   r<     s   z%ModuleImplementedIdeal.reduce_elementN)rJ   rK   rL   rM   r#   r   r   r   r   r8   propertyrX   r   r   r`   r:   rc   r<   r   r   r   r   rP     s     
rP   N)rM   	functoolsr   Zsympy.polys.polyerrorsr   r   rP   r   r   r   r   <module>   s      