o
    §ƒðjS  ã                   @  s¨  d Z ddlmZ ddlmZ ddlmZ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
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" 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/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 e4e&fdd„ƒZ9e4e&fdd„ƒZ:e4e&fdd„ƒZ;e4d d!„ ƒZ<G d"d#„ d#e2ƒZ=G d$d%„ d%ee2eƒZ>d&S )'z!Sparse rational function fields. é    )Úannotations)Úreduce)ÚaddÚmulÚltÚleÚgtÚge)ÚExpr)ÚMod)ÚExp1)ÚS)ÚSymbol)ÚCantSympifyÚsympify)ÚExpBase)ÚDomain)ÚDomainElement©ÚFractionField)ÚPolynomialRing)Úconstruct_domain)ÚlexÚMonomialOrder)ÚCoercionFailed)Úbuild_options)Ú_parallel_dict_from_expr)ÚPolyRingÚPolyElement)ÚDefaultPrinting)Úpublic)Úis_sequence)Úpollutec                 C  s   t | ||ƒ}|f|j S )zFConstruct new rational function field returning (field, x1, ..., xn). ©Ú	FracFieldÚgens©ÚsymbolsÚdomainÚorderÚ_field© r+   ú”/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/polys/fields.pyÚfield   s   r-   c                 C  s   t | ||ƒ}||jfS )zHConstruct new rational function field returning (field, (x1, ..., xn)). r#   r&   r+   r+   r,   Úxfield$   s   
r.   c                 C  s(   t | ||ƒ}tdd„ |jD ƒ|jƒ |S )zSConstruct new rational function field and inject generators into global namespace. c                 S  s   g | ]}|j ‘qS r+   )Úname)Ú.0Úsymr+   r+   r,   Ú
<listcomp>.   s    zvfield.<locals>.<listcomp>)r$   r"   r'   r%   r&   r+   r+   r,   Úvfield*   s   r3   c              	   O  sì   d}t | ƒs| gd} }ttt| ƒƒ} t||ƒ}g }| D ]	}| | ¡ ¡ qt||ƒ\}}|jdu rEt	dd„ |D ƒg ƒ}t
||d�\|_}	t|j|j|jƒ}
g }tdt|ƒdƒD ]}| |
t|||d … ƒƒ¡ qX|rr|
|d fS |
|fS )	a«  Construct a field deriving generators and domain
    from options and input expressions.

    Parameters
    ==========

    exprs   : py:class:`~.Expr` or sequence of :py:class:`~.Expr` (sympifiable)

    symbols : sequence of :py:class:`~.Symbol`/:py:class:`~.Expr`

    options : keyword arguments understood by :py:class:`~.Options`

    Examples
    ========

    >>> from sympy import exp, log, symbols, sfield

    >>> x = symbols("x")
    >>> K, f = sfield((x*log(x) + 4*x**2)*exp(1/x + log(x)/3)/x**2)
    >>> K
    Rational function field in x, exp(1/x), log(x), x**(1/3) over ZZ with lex order
    >>> f
    (4*x**2*(exp(1/x)) + x*(exp(1/x))*(log(x)))/((x**(1/3))**5)
    FTNc                 S  s   g | ]}t | ¡ ƒ‘qS r+   )ÚlistÚvalues)r0   Úrepr+   r+   r,   r2   Y   s    zsfield.<locals>.<listcomp>)Úoptr   é   )r!   r4   Úmapr   r   ÚextendÚas_numer_denomr   r(   Úsumr   r$   r%   r)   ÚrangeÚlenÚappendÚtuple)Úexprsr'   ÚoptionsÚsingler7   ÚnumdensÚexprÚrepsÚcoeffsÚ_r*   ÚfracsÚir+   r+   r,   Úsfield1   s&   

 rK   c                   @  sÖ   e Zd ZU dZded< ded< ded< ded	< d
ed< ded< efdd„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd1dd „Zd1d!d"„Zd#d$„ Zd%d&„ Zd'd(„ ZeZd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ ZdS )2r$   z2Multivariate distributed rational function field. r   Úringztuple[FracElement, ...]r%   ztuple[Expr, ...]r'   ÚintÚngensr   r(   r   r)   c                 C  sä   t |||ƒ}|j}|j}|j}|j}| j||||f}t | ¡}||_t	|ƒ|_
||_||_||_||_||_t||jƒj|_| |j¡|_| |j¡|_| ¡ |_t|j|jƒD ]\}}	t|tƒro|j}
t||
ƒsot||
|	ƒ qX|S ©N)r   r'   rN   r(   r)   Ú__name__ÚobjectÚ__new__Ú_hash_tupleÚhashÚ_hashrL   ÚFracElementÚzeroÚraw_newÚdtypeÚoneÚ_gensr%   ÚzipÚ
isinstancer   r/   ÚhasattrÚsetattr)Úclsr'   r(   r)   rL   rN   rS   ÚobjÚsymbolÚ	generatorr/   r+   r+   r,   rR   q   s2   




€zFracField.__new__c                   s   t ‡ fdd„ˆ jjD ƒƒS )z(Return a list of polynomial generators. c                   s   g | ]}ˆ   |¡‘qS r+   ©rY   ©r0   Úgen©Úselfr+   r,   r2   •   s    z#FracField._gens.<locals>.<listcomp>)r@   rL   r%   rg   r+   rg   r,   r[   “   s   zFracField._gensc                 C  ó   | j | j| jfS rO   )r'   r(   r)   rg   r+   r+   r,   Ú__getnewargs__—   ó   zFracField.__getnewargs__c                 C  s   | j S rO   )rU   rg   r+   r+   r,   Ú__hash__š   s   zFracField.__hash__c                 C  s,   |   |¡r| j | ¡ ¡S td| j|f ƒ‚)Nzexpected a %s, got %s instead)Ú
is_elementrL   ÚindexÚto_polyÚ
ValueErrorrY   )rh   rf   r+   r+   r,   rn   �   s   
zFracField.indexc                 C  s2   t |tƒo| j| j| j| jf|j|j|j|jfkS rO   )r]   r$   r'   rN   r(   r)   ©rh   Úotherr+   r+   r,   Ú__eq__£   s
   
ÿÿzFracField.__eq__c                 C  ó
   | |k S rO   r+   rq   r+   r+   r,   Ú__ne__¨   ó   
zFracField.__ne__c                 C  s   t |tƒo	|j| kS )zBTrue if ``element`` is an element of this field. False otherwise. )r]   rV   r-   ©rh   Úelementr+   r+   r,   rm   «   s   zFracField.is_elementNc                 C  s   |   ||¡S rO   rd   ©rh   ÚnumerÚdenomr+   r+   r,   rX   ¯   ó   zFracField.raw_newc                 C  s*   |d u r| j j}| |¡\}}|  ||¡S rO   )rL   rZ   ÚcancelrX   ry   r+   r+   r,   Únew²   s   zFracField.newc                 C  s   | j  |¡S rO   )r(   Úconvertrw   r+   r+   r,   Ú
domain_new·   r|   zFracField.domain_newc                 C  s€   z
|   | j |¡¡W S  ty?   | j}|js>|jr>| j}| ¡ }| |¡}| | 	|¡¡}| | 
|¡¡}|  ||¡ Y S ‚ w rO   )r~   rL   Ú
ground_newr   r(   Úis_FieldÚhas_assoc_FieldÚ	get_fieldr   rz   r{   rX   )rh   rx   r(   rL   Úground_fieldrz   r{   r+   r+   r,   r�   º   s   
õzFracField.ground_newc                 C  sb  t |tƒr6| |jkr|S t | jtƒr| jj|jkr|  |¡S t | jtƒr2| jj ¡ |jkr2|  |¡S t	dƒ‚t |t
ƒr}| ¡ \}}t | jtƒrU|j| jjkrU| j |¡}nt | jtƒrk|j| jj ¡ krk| j |¡}n| | j¡}| j |¡}|  ||¡S t |tƒr™t|ƒdkr™tt| jj|ƒƒ\}}|  ||¡S t |tƒr¢t	dƒ‚t |tƒr¬|  |¡S |  |¡S )NÚ
conversionr8   Úparsing)r]   rV   r-   r(   r   r�   r   rL   Úto_fieldÚNotImplementedErrorr   Úclear_denomsÚto_ringÚset_ringrX   r@   r>   r4   r9   Úring_newr~   Ústrr
   Ú	from_expr)rh   rx   r{   rz   r+   r+   r,   Ú	field_newÊ   s:   








zFracField.field_newc                   s6   | j ‰tdd„ ˆ ¡ D ƒƒ‰‡ ‡‡‡fdd„‰ ˆ |ƒS )Nc                 s  s,   � | ]}|j st|tƒr|| ¡ fV  qd S rO   )Úis_Powr]   r   Úas_base_expre   r+   r+   r,   Ú	<genexpr>ó   s   € ÿÿz*FracField._rebuild_expr.<locals>.<genexpr>c                   s2  ˆ  | ¡}|d ur|S | jrttttˆ | jƒƒƒS | jr'ttttˆ | jƒƒƒS | j	s1t
| ttfƒri|  ¡ \}}ˆD ]\}\}}||krWt||ƒdkrWˆ  |¡t|| ƒ   S q9|jrh|tjurhˆ |ƒt|ƒ S nˆ  d|  ¡d ur{dˆ  d|  ¡ S zˆ | ¡W S  ty˜   ˆjs—ˆjr—ˆ ¡  | ¡ Y S ‚ w )Nr   é   )ÚgetÚis_Addr   r   r4   r9   ÚargsÚis_Mulr   r‘   r]   r   r   r’   r   rM   Ú
is_Integerr   ÚOner   r   r‚   rƒ   r„   )rE   rc   ÚbÚerf   ÚbgÚeg©Ú_rebuildr(   ÚmappingÚpowersr+   r,   r    ö   s2   
€€üz)FracField._rebuild_expr.<locals>._rebuild)r(   r@   Úkeys)rh   rE   r¡   r+   rŸ   r,   Ú_rebuild_exprñ   s   zFracField._rebuild_exprc                 C  sT   t tt| j| jƒƒƒ}z
|  t|ƒ|¡}W n ty$   td| |f ƒ‚w |  	|¡S )NzGexpected an expression convertible to a rational function in %s, got %s)
Údictr4   r\   r'   r%   r¤   r   r   rp   r�   )rh   rE   r¡   Úfracr+   r+   r,   r�     s   ÿ
zFracField.from_exprc                 C  s   t | ƒS rO   r   rg   r+   r+   r,   Ú	to_domain  s   zFracField.to_domainc                 C  s   t | j| j| jƒS rO   )r   r'   r(   r)   rg   r+   r+   r,   r‹   !  s   zFracField.to_ringrO   )rP   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   rR   r[   rj   rl   rn   rs   ru   rm   rX   r~   r€   r�   r�   Ú__call__r¤   r�   r§   r‹   r+   r+   r+   r,   r$   g   s4   
 "

%#
r$   c                   @  s>  e Zd ZdZdKdd„ZdKdd„Zdd„ Zd	d
„ Zdd„ Zd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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Z d;d<„ Z!d=d>„ Z"d?d@„ Z#dAdB„ Z$dCdD„ Z%dKdEdF„Z&dKdGdH„Z'dKdIdJ„Z(dS )LrV   z=Element of multivariate distributed rational function field. Nc                 C  s4   |d u r	|j j}n|stdƒ‚|| _|| _|| _d S )Nzzero denominator)rL   rZ   ÚZeroDivisionErrorr-   rz   r{   )rh   r-   rz   r{   r+   r+   r,   Ú__init__'  s   

zFracElement.__init__c                 C  s   |   | j||¡S rO   )Ú	__class__r-   ©Úfrz   r{   r+   r+   r,   rX   1  rk   zFracElement.raw_newc                 C  s   | j | |¡Ž S rO   )rX   r}   r°   r+   r+   r,   r~   4  rk   zFracElement.newc                 C  s   | j dkr	tdƒ‚| jS )Nr”   zf.denom should be 1)r{   rp   rz   ©r±   r+   r+   r,   ro   7  s   
zFracElement.to_polyc                 C  s
   | j  ¡ S rO   )r-   r§   rg   r+   r+   r,   Úparent<  rv   zFracElement.parentc                 C  ri   rO   )r-   rz   r{   rg   r+   r+   r,   rj   ?  rk   zFracElement.__getnewargs__c                 C  s,   | j }|d u rt| j| j| jfƒ | _ }|S rO   )rU   rT   r-   rz   r{   )rh   rU   r+   r+   r,   rl   D  s   zFracElement.__hash__c                 C  s   |   | j ¡ | j ¡ ¡S rO   )rX   rz   Úcopyr{   rg   r+   r+   r,   r´   J  ó   zFracElement.copyc                 C  s8   | j |kr| S |j}| j |¡}| j |¡}| ||¡S rO   )r-   rL   rz   rŒ   r{   r~   )rh   Ú	new_fieldÚnew_ringrz   r{   r+   r+   r,   Ú	set_fieldM  s   
zFracElement.set_fieldc                 G  s   | j j|Ž | jj|Ž  S rO   )rz   Úas_exprr{   )rh   r'   r+   r+   r,   r¹   V  rµ   zFracElement.as_exprc                 C  sH   t |tƒr| j|jkr| j|jko| j|jkS | j|ko#| j| jjjkS rO   )r]   rV   r-   rz   r{   rL   rZ   ©r±   Úgr+   r+   r,   rs   Y  s   zFracElement.__eq__c                 C  rt   rO   r+   rº   r+   r+   r,   ru   _  rv   zFracElement.__ne__c                 C  s
   t | jƒS rO   )Úboolrz   r²   r+   r+   r,   Ú__bool__b  rv   zFracElement.__bool__c                 C  s   | j  ¡ | j ¡ fS rO   )r{   Úsort_keyrz   rg   r+   r+   r,   r¾   e  s   zFracElement.sort_keyc                 C  s"   | j  |¡r||  ¡ | ¡ ƒS tS rO   )r-   rm   r¾   ÚNotImplemented)Úf1Úf2Úopr+   r+   r,   Ú_cmph  s   zFracElement._cmpc                 C  ó   |   |t¡S rO   )rÃ   r   ©rÀ   rÁ   r+   r+   r,   Ú__lt__n  r|   zFracElement.__lt__c                 C  rÄ   rO   )rÃ   r   rÅ   r+   r+   r,   Ú__le__p  r|   zFracElement.__le__c                 C  rÄ   rO   )rÃ   r   rÅ   r+   r+   r,   Ú__gt__r  r|   zFracElement.__gt__c                 C  rÄ   rO   )rÃ   r	   rÅ   r+   r+   r,   Ú__ge__t  r|   zFracElement.__ge__c                 C  s   |   | j| j¡S ©z"Negate all coefficients in ``f``. ©rX   rz   r{   r²   r+   r+   r,   Ú__pos__w  s   zFracElement.__pos__c                 C  s   |   | j | j¡S rÊ   rË   r²   r+   r+   r,   Ú__neg__{  s   zFracElement.__neg__c                 C  sŠ   | j j}z| |¡}W n4 ty?   |js<|jr<| ¡ }z| |¡}W n ty.   Y Y dS w d| |¡| |¡f Y S Y dS w d|d fS )Néÿÿÿÿ)r   NNr”   )	r-   r(   r   r   r‚   rƒ   r„   rz   r{   )rh   rx   r(   r…   r+   r+   r,   Ú_extract_ground  s    ûõ
zFracElement._extract_groundc                 C  s  | j }|s| S | s|S | |¡r5| j|jkr!|  | j|j | j¡S |  | j|j | j|j  | j|j ¡S |j |¡rH|  | j| j|  | j¡S t|tƒrpt|jt	ƒr[|jj |j kr[n-t|j jt	ƒrn|j jj |krn| 
| ¡S tS t|tƒrˆt|jtƒrƒ|jj|jkrƒn| 
| ¡S |  
|¡S )z(Add rational functions ``f`` and ``g``. )r-   rm   r{   r~   rz   rL   r]   rV   r(   r   Ú__radd__r¿   r   r   ©r±   r»   r-   r+   r+   r,   Ú__add__“  s,   
(




zFracElement.__add__c                 C  s„   | j j |¡r|  | j| j|  | j¡S |  |¡\}}}|dkr-|  | j| j|  | j¡S |s1tS |  | j| | j|  | j| ¡S ©Nr”   ©r-   rL   rm   r~   rz   r{   rÏ   r¿   ©r±   ÚcrÂ   Úg_numerÚg_denomr+   r+   r,   rÐ   ²  s   "zFracElement.__radd__c                 C  sn  | j }|s| S | s| S | |¡r6| j|jkr"|  | j|j | j¡S |  | j|j | j|j  | j|j ¡S |j |¡rI|  | j| j|  | j¡S t|tƒrqt|jt	ƒr\|jj |j kr\n-t|j jt	ƒro|j jj |kro| 
| ¡S tS t|tƒr‰t|jtƒr„|jj|jkr„n| 
| ¡S |  |¡\}}}|dkr¢|  | j| j|  | j¡S |s¦tS |  | j| | j|  | j| ¡S )z-Subtract rational functions ``f`` and ``g``. r”   )r-   rm   r{   r~   rz   rL   r]   rV   r(   r   Ú__rsub__r¿   r   r   rÏ   ©r±   r»   r-   rÂ   r×   rØ   r+   r+   r,   Ú__sub__¿  s6   
(



"zFracElement.__sub__c                 C  sŠ   | j j |¡r|  | j | j|  | j¡S |  |¡\}}}|dkr/|  | j | j|  | j¡S |s3tS |  | j | | j|  | j| ¡S rÓ   rÔ   rÕ   r+   r+   r,   rÙ   å  s   $zFracElement.__rsub__c                 C  sä   | j }| r|s
|jS | |¡r|  | j|j | j|j ¡S |j |¡r-|  | j| | j¡S t|tƒrUt|j	t
ƒr@|j	j |j kr@n-t|j j	t
ƒrS|j j	j |krS| | ¡S tS t|tƒrmt|j	tƒrh|j	j|jkrhn| | ¡S |  |¡S )z-Multiply rational functions ``f`` and ``g``. )r-   rW   rm   r~   rz   r{   rL   r]   rV   r(   r   Ú__rmul__r¿   r   r   rÑ   r+   r+   r,   Ú__mul__ò  s$   





zFracElement.__mul__c                 C  sn   | j j |¡r|  | j| | j¡S |  |¡\}}}|dkr'|  | j| | j¡S |s+tS |  | j| | j| ¡S rÓ   rÔ   rÕ   r+   r+   r,   rÜ     s   zFracElement.__rmul__c                 C  s   | j }|st‚| |¡r|  | j|j | j|j ¡S |j |¡r*|  | j| j| ¡S t|tƒrRt|j	t
ƒr=|j	j |j kr=n-t|j j	t
ƒrP|j j	j |krP| | ¡S tS t|tƒrjt|j	tƒre|j	j|jkren| | ¡S |  |¡\}}}|dkr€|  | j| j| ¡S |s„tS |  | j| | j| ¡S )z0Computes quotient of fractions ``f`` and ``g``. r”   )r-   r­   rm   r~   rz   r{   rL   r]   rV   r(   r   Ú__rtruediv__r¿   r   r   rÏ   rÚ   r+   r+   r,   Ú__truediv__  s.   




zFracElement.__truediv__c                 C  sv   | st ‚| jj |¡r|  | j| | j¡S |  |¡\}}}|dkr+|  | j| | j¡S |s/tS |  | j| | j| ¡S rÓ   )	r­   r-   rL   rm   r~   r{   rz   rÏ   r¿   rÕ   r+   r+   r,   rÞ   :  s   zFracElement.__rtruediv__c                 C  sD   |dkr|   | j| | j| ¡S | st‚|   | j|  | j|  ¡S )z+Raise ``f`` to a non-negative power ``n``. r   )rX   rz   r{   r­   )r±   Únr+   r+   r,   Ú__pow__I  s
   zFracElement.__pow__c                 C  s:   |  ¡ }|  | j |¡| j | j| j |¡  | jd ¡S )a  Computes partial derivative in ``x``.

        Examples
        ========

        >>> from sympy.polys.fields import field
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y, z = field("x,y,z", ZZ)
        >>> ((x**2 + y)/(z + 1)).diff(x)
        2*x/(z + 1)

        r8   )ro   r~   rz   Údiffr{   )r±   Úxr+   r+   r,   râ   R  s   2zFracElement.diffc                 G  sP   dt |ƒ  k r| jjkrn n|  tt| jj|ƒƒ¡S td| jjt |ƒf ƒ‚)Nr   z1expected at least 1 and at most %s values, got %s)r>   r-   rN   Úevaluater4   r\   r%   rp   )r±   r5   r+   r+   r,   r¬   c  s    zFracElement.__call__c                 C  sx   t |tƒr|d u rdd„ |D ƒ}| j |¡| j |¡}}n| ¡ }| j ||¡| j ||¡}}|j ¡ }| ||¡S )Nc                 S  ó   g | ]
\}}|  ¡ |f‘qS r+   ©ro   ©r0   ÚXÚar+   r+   r,   r2   k  ó    z(FracElement.evaluate.<locals>.<listcomp>)	r]   r4   rz   rä   r{   ro   rL   rˆ   r~   )r±   rã   ré   rz   r{   r-   r+   r+   r,   rä   i  s   
zFracElement.evaluatec                 C  sn   t |tƒr|d u rdd„ |D ƒ}| j |¡| j |¡}}n| ¡ }| j ||¡| j ||¡}}|  ||¡S )Nc                 S  rå   r+   ræ   rç   r+   r+   r,   r2   v  rê   z$FracElement.subs.<locals>.<listcomp>)r]   r4   rz   Úsubsr{   ro   r~   )r±   rã   ré   rz   r{   r+   r+   r,   rë   t  s   zFracElement.subsc                 C  s   t ‚rO   )r‰   )r±   rã   ré   r+   r+   r,   Úcompose~  s   zFracElement.composerO   ))rP   r¨   r©   rª   r®   rX   r~   ro   r³   rj   rU   rl   r´   r¸   r¹   rs   ru   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,   rV   $  sN    


	&!	


rV   N)?rª   Ú
__future__r   Ú	functoolsr   Úoperatorr   r   r   r   r   r	   Úsympy.core.exprr
   Úsympy.core.modr   Úsympy.core.numbersr   Úsympy.core.singletonr   Úsympy.core.symbolr   Úsympy.core.sympifyr   r   Ú&sympy.functions.elementary.exponentialr   Úsympy.polys.domains.domainr   Ú!sympy.polys.domains.domainelementr   Ú!sympy.polys.domains.fractionfieldr   Ú"sympy.polys.domains.polynomialringr   Úsympy.polys.constructorr   Úsympy.polys.orderingsr   r   Úsympy.polys.polyerrorsr   Úsympy.polys.polyoptionsr   Úsympy.polys.polyutilsr   Úsympy.polys.ringsr   r   Úsympy.printing.defaultsr   Úsympy.utilitiesr    Úsympy.utilities.iterablesr!   Úsympy.utilities.magicr"   r-   r.   r3   rK   r$   rV   r+   r+   r+   r,   Ú<module>   sH     
5 >