+
    i                         ^ RI Ht ^ RIHtHtHt ^ RIHt ^ RIH	t	H
t
HtHtHt ^ RIHtHt ^ RIHtHt ^ RIHtHt R tR	R
RR/R ltR# )    )combinations_with_replacement)symbolsAddDummy)Rational)cancelComputationFailedparallel_poly_from_exprreducedPoly)Monomialmonomial_div)DomainErrorPolificationFailed)debugdebugfc                    \        V 4      P                  4       w  r \        W.RRR7      w  r4\	        T!  \        YB,          4      ,           #   \         d    Y,          u # i ; i)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rs   &    v/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/simplify/ratsimp.pyratsimpr   	   s^     $<&&(DAq#T%8 7VAC[    s
s   A A#"A#quickT
polynomialFc          	       aaaaaaaa ^ RI Ho \        RV 4       \        V 4      P	                  4       w  rg \        Wg.S,           .VO5/ VB w  poSP                  p	T	P                  '       d   T	P                  4       Sn        M\        RT	,          4      hTR,           U
u. uF  qP                  SP                  4      NK  	  up
o\        4       oTTT3R loRTTTTTTT3R llo\        TSSP                  SP                  R7      ^,          p\        TSSP                  SP                  R7      ^,          pT'       d   Yg,          P                  4       # S! \!        TSP                  SP                  R7      \!        TSP                  SP                  R7      . 4      w  rpS'       g{   T'       ds   \#        R	\%        T4      4       . pT FE  w  ppppS! TTR
RR7      pTP'                  TP)                  T4      TP)                  T4      34       KG  	  \+        TR R7      w  rT	P,                  '       g8   TP/                  R
R7      w  ppTP/                  R
R7      w  pp\1        TT4      pM\1        ^4      pTTP2                  ,          TTP4                  ,          ,          #   \         d    T u # i ; iu up
i )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)
)solveratsimpmodprimez.Cannot compute rational simplification over %s:   NNc                0  <a V ^ 8X  d   ^.# . p\        \        \        SP                  4      4      V 4       F  p^ .\        SP                  4      ,          oV F  pSV;;,          ^,          uu&   K  	  \        ;QJ d    V3R lS 4       F  '       d   K   RM	  RM! V3R lS 4       4      '       g   K  VP                  S4       K  	  V Uu. uF&  p\        V4      P                  ! SP                  !  NK(  	  upS! V ^,
          4      ,           # u upi )zs
Compute all monomials with degree less than ``n`` that are
not divisible by any element of ``leading_monomials``.
c              3   @   <"   T F  p\        SV4      R J x  K  	  R # 5iN)r   ).0lmgms   & r   	<genexpr>5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>b   s$      &$ 58<3'4/$s   FT)r   rangelengensallappendr   as_expr)	nSmiisr)   leading_monomialsopt	staircases	   &    @r   r9   "ratsimpmodprime.<locals>.staircaseV   s    
 63J/c#((m0DaHBCM!A!	 s &$&sss &$& & & I 9::1##SXX.:Yq1u=MMM:s   ,Dc                  <aaaa Yre^ pV P                  4       VP                  4       ,           pS'       d   V^,
          p	MTp	W4,           V	8:  EdE   W43S9   d   EM:SP                  W434       S! V4      oS! V4      o\        RW4SS34       \        R\	        S4      ,          \
        R7      o\        R\	        S4      ,          \
        R7      oSS,           p
\        \        VV3R l\        \	        S4      4       4       4      SP                  V
,           4      p\        \        VV3R l\        \	        S4      4       4       4      SP                  V
,           4      p\        W,          W,          ,
          SSP                  V
,           SP                  RR7      ^,          p\        VSP                  R	7      P                  4       pS! VSS,           RRR
7      pV'       Ed   \        ;QJ d*    R VP                  4        4       F  '       d   K   RM	  RM! R VP                  4        4       4      '       Eg>   VP                  V4      pVP                  V4      pVP                  \!        \#        \%        SS,           ^.\	        S4      \	        S4      ,           ,          4      4      4      4      pVP                  \!        \#        \%        SS,           ^.\	        S4      \	        S4      ,           ,          4      4      4      4      p\        VSP                  4      p\        VSP                  4      pV^ 8X  d   \'        R4      hVP)                  WVSS,           34       W4,           V8w  d   VR,          .pMV^,          pV^,          pV^,          pEKR  V^ 8  d(   S! WVW#WG,
          4      w  rVpS! WVW#V,
          V4      w  rVpWVV3# )a  
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.
z%s / %s: %s, %szc:%d)clszd:%dc              3   R   <"   T F  pSV,          SV,          ,          x  K  	  R # 5ir&    )r'   r5   CsM1s   & r   r*   <ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>        :>aBqEBqEMM>   $'c              3   R   <"   T F  pSV,          SV,          ,          x  K  	  R # 5ir&   r>   )r'   r5   DsM2s   & r   r*   rA      rB   rC   T)orderpolys)r.   
particularr   c              3   *   "   T F	  q^ 8H  x  K  	  R# 5i)r   Nr>   )r'   r6   s   & r   r*   rA      s     <|!Av|s   FzIdeal not prime?)total_degreeaddr   r   r-   r   r   sumr,   r.   r   rG   coeffsr/   valuessubsdictlistzip
ValueErrorr0   )aballsolNDcdstepsmaxdegboundngc_hatd_hatr   r3   solr?   rE   r@   rF   G_ratsimpmodprimer8   r   r!   r9   testeds   &&&&&           @@@@r   rf   )ratsimpmodprime.<locals>._ratsimpmodprimeh   s   : 1!ANN$44QJEEeunvJJv1B1B$qRn5#b')u5B#b')u5BbB:5R>::CHHrMKE:5R>::CHHrMKE 	AI-q#((R-!iit5568A QSXX&--/A27t4@Cs33<szz|<333<szz|<<<JJsOJJsO
 FF4Sb1#R3r79J2K%L MNOFF4Sb1#R3r79J2K%L MNOCHH%CHH%6$%788uQR895F?$Rj\FQJEFAFA19+A&QYGLA&+A&e)QGLA&V|    )rG   )domainz*Looking for best minimal solution. Got: %sTFrI   c                     \        V ^ ,          P                  4       4      \        V ^,          P                  4       4      ,           # )r   )r-   terms)xs   &r   <lambda>!ratsimpmodprime.<locals>.<lambda>   s'    QqTZZ\):S1=N)Nri   )key)convert)r   r   )sympy.solvers.solversr!   r   r   r   r
   r   rj   has_assoc_Field	get_fieldr   LMrG   setr   r.   r   r   r-   r0   rR   minis_Fieldclear_denomsr   qp)r   re   r   r   r.   argsnumdenomrH   rj   r   r\   r]   rY   newsolrb   rc   r3   ra   rd   cndnr   rf   r7   r8   r!   r9   rg   s   &fd$*,                 @@@@@@r   r"   r"      s<   < ,	
T" ,,.JC,c\A-=MMM
s ZZF%%'
<vEG 	G 38)<)Qcii)<UFN$Z Z| #q#((#))
4Q
7CE1chhcii8;E	!!###S#((3::.UCHHSZZ0XZ\^LA&V;S[I#)E5!R2$e<CMM5::c?EJJsO<= $* 6NO???t,At,ARQKaccEAaccE?o   =s   J( "#J;(J87J8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>   ri   r   <module>r      s;    3 * * ' Y Y 8 B .!,$ 5 ri   