o
    §ƒðjm,  ã                   @   s¼   d 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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 dd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )z0Tools for constructing domains for expressions. é    )Úprod)Úsympify)Úpure_complex)Úordered)ÚZZÚQQÚZZ_IÚQQ_IÚEX)ÚComplexField)Ú	RealField)Úbuild_options)Úparallel_dict_from_basic)Úpublicc                    sz  d } } }}g }|j du rdd„ }ndd„ }| D ]W}|jr%|js$d}q|jr5|r- dS d}| |¡ qt|ƒ}	|	rcd}|	\}
}|
jrP|jrP|
jrM|jsOd}qd}|
jrZ| |
¡ |jrb| |¡ q||ƒro|rl dS d}q dS |r}tdd„ |D ƒƒnd	}|rŒt| |ƒ\‰ }ˆ |fS |r–|r–t|d
�‰ n|ržt	|d
�‰ n|s£|j
rª|r§tnt‰ n|r®tnt‰ ‡ fdd„| D ƒ}ˆ |fS )z?Handle simple domains, e.g.: ZZ, QQ, RR and algebraic domains. FTc                 S   s   | j o| jS ©N©Ú	is_numberÚis_algebraic©Úcoeff© r   ú™/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/polys/constructor.pyÚ<lambda>   s    z#_construct_simple.<locals>.<lambda>c                 S   s   dS )NFr   r   r   r   r   r      s    Nc                 s   ó   � | ]}|j V  qd S r   ©Ú_prec©Ú.0Úcr   r   r   Ú	<genexpr>>   ó   € z$_construct_simple.<locals>.<genexpr>é5   ©Úprecc                    s   g | ]}ˆ   |¡‘qS r   )Ú
from_sympy)r   r   ©Údomainr   r   Ú
<listcomp>L   s    z%_construct_simple.<locals>.<listcomp>)Ú	extensionÚis_RationalÚ
is_IntegerÚis_FloatÚappendr   ÚmaxÚ_construct_algebraicr   r   Úfieldr	   r   r   r   )ÚcoeffsÚoptÚ	rationalsÚfloatsÚ	complexesÚ
algebraicsÚfloat_numbersr   r   Ú
is_complexÚxÚyÚmax_precÚresultr   r%   r   Ú_construct_simple   s^   

€

€õ
r<   c           	         sÈ   ddl m} tƒ ‰‡ ‡fdd„‰ ˆ | ƒ}ttˆƒƒ‰|ˆddd�\‰}}tdd„ t|ˆƒD ƒƒ}t ˆ|f¡ˆj	 
¡ ‰‰‡‡fd	d
„|D ƒ}ttˆ|ƒƒ‰‡‡‡‡fdd„‰‡fdd
„|D ƒ}ˆ|fS )zDWe know that coefficients are algebraic so construct the extension. r   )Úprimitive_elementc                    sp   g }| D ]1}|j rdt |¡f}n|jrdˆ |jƒf}n|jr'dˆ |jƒf}n	d|f}ˆ |¡ | |¡ q|S )NÚQú+Ú*Úe)r)   r   r$   Úis_AddÚargsÚis_MulÚaddr,   )rC   ÚtreesÚaÚtree)Úbuild_treesÚextsr   r   rI   W   s   
z)_construct_algebraic.<locals>.build_treesT)ÚexÚpolysc                 s   s   � | ]	\}}|| V  qd S r   r   )r   ÚsÚextr   r   r   r   j   ó   € z'_construct_algebraic.<locals>.<genexpr>c                    s   g | ]
}ˆ j  |ˆt¡‘qS r   )ÚdtypeÚ	from_listr   )r   Úh)r&   Úgr   r   r'   n   s    z(_construct_algebraic.<locals>.<listcomp>c                    sv   | \}}|dkrˆj  |gˆt¡S |dkr"t‡ fdd„|D ƒˆjƒS |dkr1t‡ fdd„|D ƒƒS |dkr9ˆ| S t‚)Nr>   r?   c                 3   ó   � | ]}ˆ |ƒV  qd S r   r   ©r   rG   ©Úconvert_treer   r   r   v   ó   € z=_construct_algebraic.<locals>.convert_tree.<locals>.<genexpr>r@   c                 3   rT   r   r   rU   rV   r   r   r   x   rX   rA   )rP   rQ   r   ÚsumÚzeror   ÚRuntimeError)rH   ÚoprC   )rW   r&   Úexts_maprS   r   r   rW   q   s   z*_construct_algebraic.<locals>.convert_treec                    s   g | ]}ˆ |ƒ‘qS r   r   )r   rH   rV   r   r   r'   ~   s    )Úsympy.polys.numberfieldsr=   ÚsetÚlistr   rY   Úzipr   Úalgebraic_fieldÚrepÚto_listÚdict)	r0   r1   r=   rF   ÚspanÚHÚrootÚexts_domr;   r   )rI   rW   r&   rJ   r]   rS   r   r.   Q   s   r.   c                 C   s‚  g g }}| D ]}|  ¡ \}}| |¡ | |¡ qt|| ƒ\}}|s&dS |jdu rLtdd„ |D ƒƒr6dS tƒ }	|D ]}
|
j}|	|@ rG dS |	|O }	q;t|ƒ}t|ƒd }|d|… }||d… }|jrhd}ndd| }}|D ]}t|ƒdks}||vr�d} nqqtƒ } |s¬t	||ƒD ]\}}|| }| 
¡ D ]\}}|| }|  |¡ |||< q˜qŒnt	||ƒD ]\}}|  t| ¡ ƒ¡ |  t| ¡ ƒ¡ q±d } }}g }| D ]L}|jrÝ|jsÜd}qÒ|jrèd}| |¡ qÒt|ƒ}|du�rd}|\}}|j�r
|j�r
|j�r|j�s	d}qÒd}|j�r| |¡ |j�r| |¡ qÒ|�r+td	d„ |D ƒƒnd
}|�r9|�r9t|d�}n|�rBt|d�}n|�rN|�rKt}nt}n|�rTt}nt}g }|�s‚|j|Ž }|D ]}| 
¡ D ]\}}| |¡||< �qh| ||ƒ¡ �qb||fS |j|Ž }t	||ƒD ]0\}}| 
¡ D ]\}}| |¡||< �q”| 
¡ D ]\}}| |¡||< �q¥| |||fƒ¡ �qŒ||fS )z<Handle composite domains, e.g.: ZZ[X], QQ[X], ZZ(X), QQ(X). Nc                 s   s   � | ]	}|j o	|jV  qd S r   r   )r   Úgenr   r   r   r   ’   rO   z'_construct_composite.<locals>.<genexpr>é   TF)r   é   c                 s   r   r   r   r   r   r   r   r   ×   r    r!   r"   )Úas_numer_denomr,   r   Ú	compositeÚanyr_   Úfree_symbolsÚlenr/   ra   ÚitemsrE   Úupdater`   Úvaluesr)   r*   r+   r   r-   r   r   r	   r   r   r   Ú	poly_ringr$   Ú
frac_field)r0   r1   ÚnumersÚdenomsr   ÚnumerÚdenomrL   ÚgensÚall_symbolsrj   ÚsymbolsÚnÚkÚ	fractionsÚzerosÚmonomr2   r3   r4   r6   r7   r8   r9   r:   Úgroundr;   r&   r   r   r   Ú_construct_compositeƒ   s¼   



þ

ýý€
€

€

õr„   c                 C   s,   t g }}| D ]
}| | |¡¡ q||fS )z6The last resort case, i.e. use the expression domain. )r
   r,   r$   )r0   r1   r&   r;   r   r   r   r   Ú_construct_expression   s   
r…   c                 K   s  t |ƒ}t| dƒr&t| tƒr#| sg g }}nttt|  ¡ ƒŽ ƒ\}}n| }n| g}ttt|ƒƒ}t	||ƒ}|durJ|durB|\}}n%t
||ƒ\}}n|jdu rRd}nt||ƒ}|dur`|\}}nt
||ƒ\}}t| dƒr€t| tƒr||ttt||ƒƒƒfS ||fS ||d fS )aT	  Construct a minimal domain for a list of expressions.

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

    Given a list of normal SymPy expressions (of type :py:class:`~.Expr`)
    ``construct_domain`` will find a minimal :py:class:`~.Domain` that can
    represent those expressions. The expressions will be converted to elements
    of the domain and both the domain and the domain elements are returned.

    Parameters
    ==========

    obj: list or dict
        The expressions to build a domain for.

    **args: keyword arguments
        Options that affect the choice of domain.

    Returns
    =======

    (K, elements): Domain and list of domain elements
        The domain K that can represent the expressions and the list or dict
        of domain elements representing the same expressions as elements of K.

    Examples
    ========

    Given a list of :py:class:`~.Integer` ``construct_domain`` will return the
    domain :ref:`ZZ` and a list of integers as elements of :ref:`ZZ`.

    >>> from sympy import construct_domain, S
    >>> expressions = [S(2), S(3), S(4)]
    >>> K, elements = construct_domain(expressions)
    >>> K
    ZZ
    >>> elements
    [2, 3, 4]
    >>> type(elements[0])  # doctest: +SKIP
    <class 'int'>
    >>> type(expressions[0])
    <class 'sympy.core.numbers.Integer'>

    If there are any :py:class:`~.Rational` then :ref:`QQ` is returned
    instead.

    >>> construct_domain([S(1)/2, S(3)/4])
    (QQ, [1/2, 3/4])

    If there are symbols then a polynomial ring :ref:`K[x]` is returned.

    >>> from sympy import symbols
    >>> x, y = symbols('x, y')
    >>> construct_domain([2*x + 1, S(3)/4])
    (QQ[x], [2*x + 1, 3/4])
    >>> construct_domain([2*x + 1, y])
    (ZZ[x,y], [2*x + 1, y])

    If any symbols appear with negative powers then a rational function field
    :ref:`K(x)` will be returned.

    >>> construct_domain([y/x, x/(1 - y)])
    (ZZ(x,y), [y/x, -x/(y - 1)])

    Irrational algebraic numbers will result in the :ref:`EX` domain by
    default. The keyword argument ``extension=True`` leads to the construction
    of an algebraic number field :ref:`QQ(a)`.

    >>> from sympy import sqrt
    >>> construct_domain([sqrt(2)])
    (EX, [EX(sqrt(2))])
    >>> construct_domain([sqrt(2)], extension=True)  # doctest: +SKIP
    (QQ<sqrt(2)>, [ANP([1, 0], [1, 0, -2], QQ)])

    See also
    ========

    Domain
    Expr
    Ú__iter__NFr   )r   ÚhasattrÚ
isinstancere   r`   ra   rr   Úmapr   r<   r…   rn   r„   )ÚobjrC   r1   Úmonomsr0   r;   r&   r   r   r   Úconstruct_domain
  s2   S








rŒ   N)Ú__doc__Úmathr   Ú
sympy.corer   Úsympy.core.evalfr   Úsympy.core.sortingr   Úsympy.polys.domainsr   r   r   r	   r
   Ú sympy.polys.domains.complexfieldr   Úsympy.polys.domains.realfieldr   Úsympy.polys.polyoptionsr   Úsympy.polys.polyutilsr   Úsympy.utilitiesr   r<   r.   r„   r…   rŒ   r   r   r   r   Ú<module>   s"    B2}
