Ë
    7^(h  ã                   ó�   — 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„ Zd	d
dœd„Zy)é    )Úcombinations_with_replacement)ÚsymbolsÚAddÚDummy)ÚRational)ÚcancelÚComputationFailedÚparallel_poly_from_exprÚreducedÚPoly)ÚMonomialÚmonomial_div)ÚDomainErrorÚPolificationFailed)ÚdebugÚdebugfc                 ó¸   — t        | «      j                  «       \  }}	 t        ||gdd¬«      \  }}t	        |Ž t        ||z  «      z   S # t        $ r ||z  cY S w xY w)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        úT/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/simplify/ratsimp.pyÚratsimpr   	   sh   € ô �$‹<×&Ñ&Ó(�D€A€qðÜ�q˜1˜# T°%Ô8‰ˆˆ1ô �ˆ7”V˜A˜a™C“[Ñ Ð øô ò Ø�‰sŠ
ðús   žA ÁAÁATF)ÚquickÚ
polynomialc                óö  ‡‡‡‡‡‡‡‡— ddl mŠ t        d| «       t        | «      j	                  «       \  }}	 t        ||g‰z   g|¢­i |¤Ž\  }Š‰j                  }	|	j                  r|	j                  «       ‰_        nt        d|	z  «      ‚|dd D �
cg c]  }
|
j                  ‰j                  «      ‘Œ c}
Št        «       Šˆˆˆfd„Šdˆˆˆˆˆˆˆfd„	Št        |‰‰j                  ‰j                  ¬	«      d
   }t        |‰‰j                  ‰j                  ¬	«      d
   }|r||z  j                  «       S  ‰t!        |‰j                  ‰j                  ¬«      t!        |‰j                  ‰j                  ¬«      g «      \  }}}‰ss|rqt#        dt%        |«      «       g }|D ]D  \  }}}} ‰||dd¬«      }|j'                  |j)                  |«      |j)                  |«      f«       ŒF t+        |d„ ¬«      \  }}|	j,                  s7|j/                  d¬«      \  }}|j/                  d¬«      \  }}t1        ||«      }nt1        d
«      }||j2                  z  ||j4                  z  z  S # t        $ r | cY S w xY wc c}
w )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                 óœ  •‡— | dk(  rdgS g }t        t        t        ‰j                  «      «      | «      D ]U  }dgt        ‰j                  «      z  Š|D ]  }‰|xx   dz  cc<   Œ t	        ˆfd„‰D «       «      sŒE|j                  ‰«       ŒW |D �cg c]$  } t        |«      j                  ‰j                  Ž ‘Œ& c} ‰| dz
  «      z   S c c}w )z‹
        Compute all monomials with degree less than ``n`` that are
        not divisible by any element of ``leading_monomials``.
        r   é   c              3   ó:   •K  — | ]  }t        ‰|«      d u –— Œ y ­w©N)r   )Ú.0ÚlmgÚms     €r   ú	<genexpr>z5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>b   s!   øè ø€ ò &°C”<  3Ó'¨4Ô/ñ &ùs   ƒ)r   ÚrangeÚlenÚgensÚallÚappendr   Úas_expr)	ÚnÚSÚmiÚiÚsr*   Úleading_monomialsÚoptÚ	staircases	        @€€€r   r9   z"ratsimpmodprime.<locals>.staircaseV   sÅ   ù€ ð
 �Š6Ø�3ˆJØˆÜ/´´c¸#¿(¹(³mÓ0DÀaÓHò 	ˆBØ�”C˜Ÿ™“MÑ!ˆAØò �Ø�!“˜‘	”ðäó &Ø$ô&õ &à—‘˜•ð	ð 9:Ö:°1Ð#”˜“×#Ñ# S§X¡XÒ.Ò:¹YÀqÈ1ÁuÓ=MÑMÐMùÒ:s   Â)C	c                 óB  •‡‡‡‡— | |}}d}| j                  «       |j                  «       z   }‰r|dz
  }	n|}	||z   |	k  �r±||f‰v r�n©‰j                  ||f«        ‰|«      Š ‰|«      Št        d||‰‰f«       t        dt	        ‰«      z  t
        ¬«      Št        dt	        ‰«      z  t
        ¬«      Š‰‰z   }
t        t        ˆˆfd„t        t	        ‰«      «      D «       «      ‰j                  |
z   «      }t        t        ˆˆfd„t        t	        ‰«      «      D «       «      ‰j                  |
z   «      }t        | |z  ||z  z
  ‰‰j                  |
z   ‰j                  d	¬
«      d   }t        |‰j                  ¬«      j                  «       } ‰|‰‰z   d	d	¬«      }|�r8t        d„ |j                  «       D «       «      �s|j                  |«      }|j                  |«      }|j                  t!        t#        t%        ‰‰z   dgt	        ‰«      t	        ‰«      z   z  «      «      «      «      }|j                  t!        t#        t%        ‰‰z   dgt	        ‰«      t	        ‰«      z   z  «      «      «      «      }t        |‰j                  «      }t        |‰j                  «      }|dk(  rt'        d«      ‚|j)                  |||‰‰z   f«       ||z   |k7  r|d   g}n|dz  }|dz  }|dz  }||z   |	k  r�Œ±|dkD  r& ‰||||||z
  «      \  }}} ‰|||||z
  |«      \  }}}|||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   ó4   •K  — | ]  }‰|   ‰|   z  –— Œ y ­wr'   © )r(   r5   ÚCsÚM1s     €€r   r+   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>›   ó   øè ø€ Ò: a�B�q‘E˜B˜q™E•MÑ:ùó   ƒc              3   ó4   •K  — | ]  }‰|   ‰|   z  –— Œ y ­wr'   r=   )r(   r5   ÚDsÚM2s     €€r   r+   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>�   r@   rA   T)ÚorderÚpolys)r.   ©Ú
particularr   c              3   ó&   K  — | ]	  }|d k(  –— Œ y­w)r   Nr=   )r(   r6   s     r   r+   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>¥   s   è ø€ Ò<¨!˜q A�vÑ<ùs   ‚zIdeal not prime?éÿÿÿÿ)Útotal_degreeÚaddr   r   r-   r   r   Úsumr,   r.   r   rE   Úcoeffsr/   ÚvaluesÚsubsÚdictÚlistÚzipÚ
ValueErrorr0   )ÚaÚbÚallsolÚNÚDÚcÚdÚstepsÚmaxdegÚboundÚngÚc_hatÚd_hatr   r3   Úsolr>   rC   r?   rD   ÚGÚ_ratsimpmodprimer8   r   r!   r9   Útesteds                   @@@@€€€€€€€r   rd   z)ratsimpmodprime.<locals>._ratsimpmodprimeh   s÷  ü€ ð: �!ˆ1ˆØˆà—‘Ó! A§N¡NÓ$4Ñ4ˆÙØ˜Q‘J‰EàˆEØ�!‰e�u‹nØ�1ˆv˜ÑÙØ�J‰J˜˜1�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ô ˜˜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 e¨Q°°R±Ð8Ô9Ø�q‘5˜F’?Ø$ R™j˜\�Fàà�Q‰JˆEØ�‰FˆAØ�‰FˆAð_ �!‰e�uŒnðb �1Š9Ù+¨A¨q°&¸!¸QÀ¹YÓG‰LˆAˆq�&Ù+¨A¨q°&¸!¸e¹)ÀQÓG‰LˆAˆq�&à�!�Vˆ|Ðó    )rE   r%   )Údomainz*Looking for best minimal solution. Got: %sTFrG   c                 ót   — t        | d   j                  «       «      t        | d   j                  «       «      z   S )Nr   r%   )r-   Úterms)Úxs    r   ú<lambda>z!ratsimpmodprime.<locals>.<lambda>Õ   s)   € ¬¨Q¨q©T¯Z©Z«\Ó):¼SÀÀ1ÁÇÁÃÓ=NÑ)N€ rf   )Úkey)Úconvert)r   r   )Úsympy.solvers.solversr!   r   r   r   r
   r   rg   Úhas_assoc_FieldÚ	get_fieldr   ÚLMrE   Úsetr   r.   r   r   r-   r0   rP   ÚminÚis_FieldÚclear_denomsr   ÚqÚp)r   rc   r   r   r.   ÚargsÚnumÚdenomrF   rg   r   rZ   r[   rW   Únewsolr`   ra   r3   r_   rb   ÚcnÚdnr   rd   r7   r8   r!   r9   re   s    ``                    @@@@@@r   r"   r"      sF  ÿ€ õ< ,ä	Ð
˜TÔ"ô ˜“×,Ñ,Ó.�J€CˆðÜ,¨c°5¨\¸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áØ�E‘	×!Ñ!Ó#Ð#á#ÜˆS�#—(‘( 3§:¡:Ô.´°U¸C¿H¹HÈSÏZÉZÔ0XÐZ\ó^�L€A€qˆ&á‘VÜÐ;¼SÀ»[ÔIØˆØ#)ò 	>ÑˆE�5˜!˜RÙ˜˜2¨$°eÔ<ˆCà�M‰M˜5Ÿ:™: c›?¨E¯J©J°s«OÐ<Õ=ð	>ô �6ÑNÔO‰ˆˆ1à�?Š?Ø—‘ t�Ó,‰ˆˆAØ—‘ t�Ó,‰ˆˆAÜ�R˜Ó‰ä�Q‹Kˆàˆa�c‰c‰E�A�a—c‘c‘E‰?Ðøôo ò ØŠðüò =s   ¸I% Â"I6É%I3É2I3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=   rf   r   ú<module>r…      s2   ðÝ 3ß *Ñ *Ý 'ß YÕ Yß 8ß Bß .ò!ð, +/¸5õ rf   