
    \h                         S SK Jr  S SKJrJrJr  S SKJr  S SKJ	r	J
r
JrJrJr  S SKJrJr  S SKJrJr  S SKJrJr  S rS	S
S.S jrg)    )combinations_with_replacement)symbolsAddDummy)Rational)cancelComputationFailedparallel_poly_from_exprreducedPoly)Monomialmonomial_div)DomainErrorPolificationFailed)debugdebugfc                     [        U 5      R                  5       u  p [        X/SSS9u  p4[	        U6 [        XB-  5      -   $ ! [         a    X-  s $ f = f)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        N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/simplify/ratsimp.pyratsimpr   	   s^     $<&&(DAq#T%8 7VAC[    s
s   A AATF)quick
polynomialc          	        ^^^^^^^^ SSK Jm  [        SU 5        [        U 5      R	                  5       u  pg [        Xg/T-   /UQ70 UD6u  nmTR                  n	U	R                  (       a  U	R                  5       Tl        O[        SU	-  5      eUSS  V
s/ s H  oR                  TR                  5      PM     sn
m[        5       mUUU4S jmSUUUUUUU4S jjm[        UTTR                  TR                  S	9S
   n[        UTTR                  TR                  S	9S
   nU(       a  Xg-  R                  5       $ T" [!        UTR                  TR                  S9[!        UTR                  TR                  S9/ 5      u  pnT(       ds  U(       al  [#        S[%        U5      5        / nU HB  u  nnnnT" UUSSS9nUR'                  UR)                  U5      UR)                  U5      45        MD     [+        US S9u  pU	R,                  (       d1  UR/                  SS9u  nnUR/                  SS9u  nn[1        UU5      nO[1        S
5      nUUR2                  -  UUR4                  -  -  $ ! [         a    U s $ f = fs  sn
f )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 %s   Nc                   >^ U S:X  a  S/$ / n[        [        [        TR                  5      5      U 5       H_  nS/[        TR                  5      -  mU H  nTU==   S-  ss'   M     [	        U4S jT 5       5      (       d  MN  UR                  T5        Ma     U Vs/ s H%  n[        U5      R                  " TR                  6 PM'     snT" U S-
  5      -   $ s  snf )zs
Compute all monomials with degree less than ``n`` that are
not divisible by any element of ``leading_monomials``.
r      c              3   @   >#    U  H  n[        TU5      S L v   M     g 7fN)r   ).0lmgms     r   	<genexpr>5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>b   s$      &$ 58<3'4/$s   )r   rangelengensallappendr   as_expr)	nSmiisr*   leading_monomialsopt	staircases	        @r   r:   "ratsimpmodprime.<locals>.staircaseV   s    
 63J/c#((m0DaHBCM!A!	  &$& & & I 9::1##SXX.:Yq1u=MMM:s   ,Cc                 0  >^^^^ XpeSnU R                  5       UR                  5       -   nT(       a  US-
  n	OUn	X4-   U	::  Ga  X44T;   a  GOTR                  X445        T" U5      mT" U5      m[        SX4TT45        [        S[	        T5      -  [
        S9m[        S[	        T5      -  [
        S9mTT-   n
[        [        UU4S j[        [	        T5      5       5       5      TR                  U
-   5      n[        [        UU4S j[        [	        T5      5       5       5      TR                  U
-   5      n[        X-  X-  -
  TTR                  U
-   TR                  S	S
9S   n[        UTR                  S9R                  5       nT" UTT-   S	S	S9nU(       Ga=  [        S UR                  5        5       5      (       Gd  UR                  U5      nUR                  U5      nUR                  [!        [#        [%        TT-   S/[	        T5      [	        T5      -   -  5      5      5      5      nUR                  [!        [#        [%        TT-   S/[	        T5      [	        T5      -   -  5      5      5      5      n[        UTR                  5      n[        UTR                  5      nUS:X  a  ['        S5      eUR)                  XUTT-   45        X4-   U:w  a  US   /nOUS-  nUS-  nUS-  nX4-   U	::  a  GM  US:  a  T" XVX#XG-
  5      u  pVnT" XVX#U-
  U5      u  pVnXVU4$ )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.
r   r%   z%s / %s: %s, %szc:%d)clszd:%dc              3   :   >#    U  H  nTU   TU   -  v   M     g 7fr'    )r(   r6   CsM1s     r   r+   <ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>        :>aBqEBqEM>   c              3   :   >#    U  H  nTU   TU   -  v   M     g 7fr'   r?   )r(   r6   DsM2s     r   r+   rB      rC   rD   T)orderpolys)r/   
particularr   c              3   *   #    U  H	  oS :H  v   M     g7f)r   Nr?   )r(   r7   s     r   r+   rB      s     <|!Av|s   zIdeal not prime?)total_degreeaddr   r   r.   r   r   sumr-   r/   r   rH   coeffsr0   valuessubsdictlistzip
ValueErrorr1   )aballsolNDcdstepsmaxdegboundngc_hatd_hatr   r4   solr@   rF   rA   rG   G_ratsimpmodprimer9   r   r!   r:   testeds                   @@@@r   rg   )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3<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_ eunb 19+A&QYGLA&+A&e)QGLA&V|    )rH   r%   )domainz*Looking for best minimal solution. Got: %sTFrJ   c                 t    [        U S   R                  5       5      [        U S   R                  5       5      -   $ )Nr   r%   )r.   terms)xs    r   <lambda>!ratsimpmodprime.<locals>.<lambda>   s'    QqTZZ\):S1=N)Nrj   )key)convert)r   r   )sympy.solvers.solversr!   r   r   r   r
   r   rk   has_assoc_Field	get_fieldr   LMrH   setr   r/   r   r   r.   r1   rS   minis_Fieldclear_denomsr   qp)r   rf   r   r   r/   argsnumdenomrI   rk   r   r]   r^   rZ   newsolrc   rd   r4   rb   re   cndnr   rg   r8   r9   r!   r:   rh   s    ``                    @@@@@@r   r"   r"      s9   < ,	
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   I( $I:(I76I7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?   rj   r   <module>r      s2    3 * * ' Y Y 8 B .!, +/5 rj   