o
    ôj  ã                   @   s”   d dl mZ d dlmZmZ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mZ d dlmZmZ d dlmZmZ dd	„ Zd
ddœdd„ZdS )é    )Úcombinations_with_replacement)ÚsymbolsÚAddÚDummy)ÚRational)ÚcancelÚComputationFailedÚparallel_poly_from_exprÚreducedÚPoly)ÚMonomialÚmonomial_div)ÚDomainErrorÚPolificationFailed)ÚdebugÚdebugfc                 C   sZ   t | ƒ ¡ \}}zt||gddd�\}}W n ty"   ||  Y S w t|Ž t || ƒ S )z×
    Put an expression over a common denominator, cancel and reduce.

    Examples
    ========

    >>> from sympy import ratsimp
    >>> from sympy.abc import x, y
    >>> ratsimp(1/x + 1/y)
    (x + y)/(x*y)
    TF)ÚfieldÚexpand)r   Úas_numer_denomr
   r   r   )ÚexprÚfÚgÚQÚr© r   úi/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sympy/simplify/ratsimp.pyÚratsimp	   s   ÿr   TF)ÚquickÚ
polynomialc                   sø  ddl m‰ td| ƒ t| ƒ ¡ \}}zt||gˆ  g|¢R i |¤Ž\}‰W n ty1   |  Y S w ˆj}	|	jr>|	 	¡ ˆ_nt
d|	 ƒ‚‡fdd„|dd… D ƒ‰tƒ ‰‡‡‡fd	d
„‰d‡ ‡‡‡‡‡‡fdd„	‰t|ˆ ˆjˆjd�d }t|ˆ ˆjˆjd�d }|r‰||  ¡ S ˆt|ˆjˆjd�t|ˆjˆjd�g ƒ\}
}}ˆsÕ|rÕtdt|ƒƒ g }|D ]\}}}}ˆ||ddd�}| | |¡| |¡f¡ q¯t|dd„ d�\}
}|	jsî|
jdd�\}}
|jdd�\}}t||ƒ}ntdƒ}|
|j ||j  S )aÚ  
    Simplifies a rational expression ``expr`` modulo the prime ideal
    generated by ``G``.  ``G`` should be a Groebner basis of the
    ideal.

    Examples
    ========

    >>> from sympy.simplify.ratsimp import ratsimpmodprime
    >>> from sympy.abc import x, y
    >>> eq = (x + y**5 + y)/(x - y)
    >>> ratsimpmodprime(eq, [x*y**5 - x - y], x, y, order='lex')
    (-x**2 - x*y - x - y)/(-x**2 + x*y)

    If ``polynomial`` is ``False``, the algorithm computes a rational
    simplification which minimizes the sum of the total degrees of
    the numerator and the denominator.

    If ``polynomial`` is ``True``, this function just brings numerator and
    denominator into a canonical form. This is much faster, but has
    potentially worse results.

    References
    ==========

    .. [1] M. Monagan, R. Pearce, Rational Simplification Modulo a Polynomial
        Ideal, https://dl.acm.org/doi/pdf/10.1145/1145768.1145809
        (specifically, the second algorithm)
    r   )ÚsolveÚratsimpmodprimez.Cannot compute rational simplification over %sc                    s   g | ]}|  ˆ j¡‘qS r   )ÚLMÚorder)Ú.0r   ©Úoptr   r   Ú
<listcomp>S   s    z#ratsimpmodprime.<locals>.<listcomp>é   Nc                    s”   | dkrdgS g }t ttˆjƒƒ| ƒD ]'}dgtˆjƒ ‰ |D ]
}ˆ |  d7  < qt‡ fdd„ˆD ƒƒr:| ˆ ¡ q‡fdd„|D ƒˆ| d ƒ S )z‹
        Compute all monomials with degree less than ``n`` that are
        not divisible by any element of ``leading_monomials``.
        r   é   c                 3   s   � | ]
}t ˆ |ƒd u V  qd S ©N)r   )r#   Úlmg©Úmr   r   Ú	<genexpr>b   s   € z5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>c                    s   g | ]
}t |ƒjˆ jŽ ‘qS r   )r   Úas_exprÚgens©r#   Úsr$   r   r   r&   f   s    z6ratsimpmodprime.<locals>.staircase.<locals>.<listcomp>)r   ÚrangeÚlenr/   ÚallÚappend)ÚnÚSÚmiÚi)Úleading_monomialsr%   Ú	staircaser+   r   r;   V   s   ÿ
€z"ratsimpmodprime.<locals>.staircasec              
      sª  | |}}d}|   ¡ |  ¡  }ˆr|d }	n|}	|| |	k�r1||fˆ
v r'�n
ˆ
 ||f¡ ˆ	|ƒ‰ˆ	|ƒ‰td||ˆˆfƒ tdtˆƒ td�‰ tdtˆƒ td�‰ˆ ˆ }
tt‡ ‡fdd„ttˆƒƒD ƒƒˆj	|
 ƒ}tt‡‡fd	d„ttˆƒƒD ƒƒˆj	|
 ƒ}t
| | ||  ˆˆj	|
 ˆjd
d�d }t|ˆj	d� ¡ }ˆ|ˆ ˆ d
d
d�}|�rtdd„ | ¡ D ƒƒ�s| |¡}| |¡}| tttˆ ˆ dgtˆ ƒtˆƒ  ƒƒƒ¡}| tttˆ ˆ dgtˆ ƒtˆƒ  ƒƒƒ¡}t|ˆj	ƒ}t|ˆj	ƒ}|dk�rtdƒ‚| |||ˆ ˆ f¡ || |k�r|d g}n|d7 }|d7 }|d7 }|| |	ks|dk�rPˆ|||||| ƒ\}}}ˆ||||| |ƒ\}}}|||fS )ak  
        Computes a rational simplification of ``a/b`` which minimizes
        the sum of the total degrees of the numerator and the denominator.

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

        The algorithm proceeds by looking at ``a * d - b * c`` modulo
        the ideal generated by ``G`` for some ``c`` and ``d`` with degree
        less than ``a`` and ``b`` respectively.
        The coefficients of ``c`` and ``d`` are indeterminates and thus
        the coefficients of the normalform of ``a * d - b * c`` are
        linear polynomials in these indeterminates.
        If these linear polynomials, considered as system of
        equations, have a nontrivial solution, then `\frac{a}{b}
        \equiv \frac{c}{d}` modulo the ideal generated by ``G``. So,
        by construction, the degree of ``c`` and ``d`` is less than
        the degree of ``a`` and ``b``, so a simpler representation
        has been found.
        After a simpler representation has been found, the algorithm
        tries to reduce the degree of the numerator and denominator
        and returns the result afterwards.

        As an extension, if quick=False, we look at all possible degrees such
        that the total degree is less than *or equal to* the best current
        solution. We retain a list of all solutions of minimal degree, and try
        to find the best one at the end.
        r   r(   z%s / %s: %s, %szc:%d)Úclszd:%dc                 3   ó    � | ]}ˆ | ˆ|  V  qd S r)   r   ©r#   r9   )ÚCsÚM1r   r   r-   ›   ó   € z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>c                 3   r=   r)   r   r>   )ÚDsÚM2r   r   r-   �   rA   T)r"   Úpolys)r/   ©Ú
particularr   c                 s   s   � | ]}|d kV  qdS )r   Nr   r0   r   r   r   r-   ¥   s   € zIdeal not prime?éÿÿÿÿ)Útotal_degreeÚaddr   r   r3   r   r   Úsumr2   r/   r
   r"   Úcoeffsr4   ÚvaluesÚsubsÚdictÚlistÚzipÚ
ValueErrorr5   )ÚaÚbÚallsolÚNÚDÚcÚdÚstepsZmaxdegÚboundÚngÚc_hatÚd_hatr   r7   Úsol)ÚGÚ_ratsimpmodprimer%   r   r   r;   Útested)r?   rB   r@   rC   r   r`   h   sd   

&ÿ&ÿÿÿ

..

Ñ
1
z)ratsimpmodprime.<locals>._ratsimpmodprime)r"   r(   )Údomainz*Looking for best minimal solution. Got: %sTFrE   c                 S   s    t | d  ¡ ƒt | d  ¡ ƒ S )Nr   r(   )r3   Úterms)Úxr   r   r   Ú<lambda>Õ   s     z!ratsimpmodprime.<locals>.<lambda>)Úkey)Úconvert)r   r   )Úsympy.solvers.solversr   r   r   r   r	   r   rb   Úhas_assoc_FieldÚ	get_fieldr   Úsetr
   r/   r"   r   r   r3   r5   rM   ÚminÚis_FieldÚclear_denomsr   ÚqÚp)r   r_   r   r   r/   ÚargsÚnumÚdenomrD   rb   rW   rX   rT   Znewsolr\   r]   r7   r[   r^   ÚcnÚdnr   r   )r_   r`   r:   r%   r   r   r;   ra   r   r       sL   
&ÿÿ^"
ÿr    N)Ú	itertoolsr   Ú
sympy.corer   r   r   Úsympy.core.numbersr   Úsympy.polysr   r   r	   r
   r   Úsympy.polys.monomialsr   r   Úsympy.polys.polyerrorsr   r   Úsympy.utilities.miscr   r   r   r    r   r   r   r   Ú<module>   s    