ó
    Š*£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        ÚS/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/simplify/ratsimp.pyÚratsimpr   	   s^   € ô �$‹<×&Ñ&Ó(�D€AðÜ�q˜# T°%Ñ8‰ˆô �ˆ7”V˜A™C“[Ñ Ð øô ó Ø‰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  oªR                  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  p¼n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  p¼U	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Ø�3ˆJØˆÜ/´´c¸#¿(¹(³mÓ0DÀaÖHˆBØ�”C˜Ÿ™“MÑ!ˆAÛ�Ø�!“˜‘	•ñ äô &Ù$ó&÷ &ó &à—‘˜–ñ Iñ 9:Ó:º°1”˜“×#Ò# S§X¡XÓ.¹Ñ:¹YÀqÈ1ÁuÓ=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O£T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)                  X¼U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>›   ó   øé € Ð:ª> a�B�q‘E˜B˜q™E–Mª>ùó   ƒ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   é € Ð<ª|¨! Ažvª|ù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Øˆà—‘Ó! A§N¡NÓ$4Ñ4ˆÞØ˜Q‘J‰EàˆEØ‰e�uŒnØˆv˜ÓÙØ�J‰J˜�vÔá˜1“ˆBÙ˜1“ˆBÜÐ$ q¨R° nÔ5ä˜¤# b£'Ñ)¬uÑ5ˆBÜ˜¤# b£'Ñ)¬uÑ5ˆBØ�b‘ˆBäÜÕ:¬5´°R³¬>Ó:Ó:¸C¿H¹HÀr¹MóKˆEäÜÕ:¬5´°R³¬>Ó:Ó:¸C¿H¹HÀr¹MóKˆEô ˜™	 A¡IÑ-¨q°#·(±(¸R±-Ø!Ÿi™i¨tñ5Ø56ñ8ˆAô �Q˜SŸX™XÑ&×-Ñ-Ó/ˆAÙ˜˜2 ™7¨t¸4Ñ@ˆCçœ3Ñ<¨s¯z©z¬|Ó<×<Ò<Ø—J‘J˜s“O�Ø—J‘J˜s“O�ð
 —F‘Fœ4¤¤S¨¨b©°1°#¼¸R»Ä3ÀrÃ7Ñ9JÑ2KÓ%LÓ MÓNÓO�Ø—F‘Fœ4¤¤S¨¨b©°1°#¼¸R»Ä3ÀrÃ7Ñ9JÑ2KÓ%LÓ MÓNÓO�ä˜˜CŸH™HÓ%�Ü˜˜CŸH™HÓ%�Ø˜“6Ü$Ð%7Ó8Ð8à—‘˜u¨Q°°R±Ð8Ô9Ø‘5˜F“?Ø$ R™j˜\�Fàà�Q‰JˆEØ�‰FˆAØ�‰FˆAð_ ‰e�uŽnðb �1‹9Ù+¨A°&¸Q¹YÓG‰LˆA�&Ù+¨A°&¸e¹)ÀQÓG‰LˆA�&à�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'   € ¬¨Q¨q©T¯Z©Z«\Ó):¼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Ô"ô ˜“×,Ñ,Ó.�J€CðÜ,¨c¨\¸AÑ-=ÐMÀÒMÈÑM‰
ˆˆsð �Z‰Z€Fà××Ø×%Ñ%Ó'ˆ�
äØ<¸vÑEóGð 	Gð 38¸¸±)Ó<²)¨QŸ™˜cŸi™iž±)Ñ<ÐÜ‹U€F÷N÷$Zõ Zô| �#�q˜#Ÿ(™(¨#¯)©)Ñ
4°QÑ
7€CÜ�E˜1˜cŸh™h¨c¯i©iÑ8¸Ñ;€EæØ‘	×!Ñ!Ó#Ð#á#ÜˆS�#—(‘( 3§:¡:Ñ.´°U¸C¿H¹HÈSÏZÉZÑ0XÐZ\ó^�L€Aˆ&æ–VÜÐ;¼SÀ»[ÔIØˆÛ#)ÑˆE�5˜!˜RÙ˜˜2¨$°eÑ<ˆCà�M‰M˜5Ÿ:™: c›?¨E¯J©J°s«OÐ<Ö=ñ $*ô �6ÑNÑO‰ˆà�?�?Ø—‘ t�Ð,‰ˆˆAØ—‘ t�Ð,‰ˆˆAÜ�R˜Ó‰ä�Q‹Kˆàˆa�c‰c‰E�A�a—c‘c‘E‰?Ðøô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   