§
    PŠtj  ã                   ó’   — 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dS )é    )Úcombinations_with_replacement)ÚsymbolsÚAddÚDummy)ÚRational)ÚcancelÚComputationFailedÚparallel_poly_from_exprÚreducedÚPoly)ÚMonomialÚmonomial_div)ÚDomainErrorÚPolificationFailed)ÚdebugÚdebugfc                 óÞ   — t          | ¦  «                             ¦   «         \  }}	 t          ||gdd¬¦  «        \  }}n# t          $ r ||z  cY S w xY wt	          |Ž t          ||z  ¦  «        z   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Úrs        úT/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/simplify/ratsimp.pyÚratsimpr   	   s…   € õ �$‰<Œ<×&Ò&Ñ(Ô(�D€A€qðÝ�q˜1˜# T°%Ð8Ñ8Ô8‰ˆˆ1ˆ1øÝð ð ð Ø�‰sˆ
ˆ
ˆ
ðøøøõ �ˆ7•V˜A˜a™C‘[”[Ñ Ð s   ¦> ¾AÁATF)ÚquickÚ
polynomialc                ó   ‡‡‡‡‡‡‡‡— ddl mŠ t          d| ¦  «         t          | ¦  «                             ¦   «         \  }}	 t          ||g‰z   g|¢R i |¤Ž\  }Šn# t          $ r | cY S w xY w‰j        }	|	j        r|	 	                    ¦   «         ‰_        nt          d|	z  ¦  «        ‚ˆfd„|dd…         D ¦   «         Št          ¦   «         Šˆˆˆfd„Šdˆˆˆˆˆˆˆfd	„	Št          |‰‰j        ‰j        ¬
¦  «        d         }t          |‰‰j        ‰j        ¬
¦  «        d         }|r||z                       ¦   «         S  ‰t          |‰j        ‰j        ¬¦  «        t          |‰j        ‰j        ¬¦  «        g ¦  «        \  }
}}‰sŒ|rŠt!          dt#          |¦  «        ¦  «         g }|D ]S\  }}}} ‰||dd¬¦  «        }|                     |                     |¦  «        |                     |¦  «        f¦  «         ŒTt)          |d„ ¬¦  «        \  }
}|	j        sC|
                     d¬¦  «        \  }}
|                     d¬¦  «        \  }}t/          ||¦  «        }nt/          d¦  «        }|
|j        z  ||j        z  z  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                 óD   •— g | ]}|                      ‰j        ¦  «        ‘ŒS © )ÚLMÚorder)Ú.0r   Úopts     €r   ú
<listcomp>z#ratsimpmodprime.<locals>.<listcomp>S   s%   ø€ Ð<Ð<Ð<¨Q˜Ÿš˜cœi™œÐ<Ð<Ð<ó    é   Nc                 ót  •‡— | dk    rdgS g }t          t          t          ‰j        ¦  «        ¦  «        | ¦  «        D ]_}dgt          ‰j        ¦  «        z  Š|D ]}‰|xx         dz  cc<   Œt	          ˆfd„‰D ¦   «         ¦  «        r|                     ‰¦  «         Œ`ˆfd„|D ¦   «          ‰| dz
  ¦  «        z   S )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 V — Œd S ©N)r   )r'   ÚlmgÚms     €r   ú	<genexpr>z5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>b   sB   øè è € ð &ð &°C•<  3Ñ'Ô'¨4Ð/ð &ð &ð &ð &ð &ð &r*   c                 óH   •— g | ]} t          |¦  «        j        ‰j        Ž ‘ŒS r$   )r   Úas_exprÚgens)r'   Úsr(   s     €r   r)   z6ratsimpmodprime.<locals>.staircase.<locals>.<listcomp>f   s,   ø€ Ð:Ð:Ð:°1Ð#•˜‘”Ô# S¤XÐ.Ð:Ð:Ð:r*   )r   ÚrangeÚlenr5   ÚallÚappend)ÚnÚSÚmiÚir1   Úleading_monomialsr(   Ú	staircases       @€€€r   r@   z"ratsimpmodprime.<locals>.staircaseV   só   øø€ ð
 �Š6ˆ6Ø�3ˆJØˆÝ/µµc¸#¼(±m´mÑ0DÔ0DÀaÑHÔHð 	ð 	ˆBØ�•C˜œ‘M”MÑ!ˆAØð ð �Ø�!��”˜‘	��‘�Ýð &ð &ð &ð &Ø$ð&ñ &ô &ñ &ô &ð à—’˜‘”�øà:Ð:Ð:Ð:¸Ð:Ñ:Ô:¸Y¸YÀqÈ1ÁuÑ=MÔ=MÑMÐMr*   c                 ó|  •‡‡‡‡— | |}}d}|                       ¦   «         |                      ¦   «         z   }‰r|dz
  }	n|}	||z   |	k    �r>||f‰v r�n6‰                     ||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	        ¬¦  «                             ¦   «         } ‰|‰‰z   d	d	¬¦  «        }|�r†t          d„ |                     ¦   «         D ¦   «         ¦  «        �sZ|                     |¦  «        }|                     |¦  «        }|                     t!          t#          t%          ‰‰z   dgt	          ‰¦  «        t	          ‰¦  «        z   z  ¦  «        ¦  «        ¦  «        ¦  «        }|                     t!          t#          t%          ‰‰z   dgt	          ‰¦  «        t	          ‰¦  «        z   z  ¦  «        ¦  «        ¦  «        ¦  «        }t          |‰j	        ¦  «        }t          |‰j	        ¦  «        }|dk    rt'          d¦  «        ‚|                     |||‰‰z   f¦  «         ||z   |k    r	|d         g}n|dz  }|dz  }|dz  }||z   |	k    �°>|dk    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   ó:   •K  — | ]}‰|         ‰|         z  V — Œd S r/   r$   )r'   r>   ÚCsÚM1s     €€r   r2   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>›   ó/   øè è € Ð:Ð: a�B�q”E˜B˜qœE‘MÐ:Ð:Ð:Ð:Ð:Ð:r*   c              3   ó:   •K  — | ]}‰|         ‰|         z  V — Œd S r/   r$   )r'   r>   ÚDsÚM2s     €€r   r2   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>�   rF   r*   T)r&   Úpolys)r5   ©Ú
particularr   c              3   ó"   K  — | ]
}|d k    V — ŒdS )r   Nr$   )r'   r6   s     r   r2   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>¥   s&   è è € Ð<Ð<¨!˜q AšvÐ<Ð<Ð<Ð<Ð<Ð<r*   zIdeal not prime?éÿÿÿÿ)Útotal_degreeÚaddr   r   r8   r   r   Úsumr7   r5   r   r&   Úcoeffsr9   ÚvaluesÚsubsÚdictÚlistÚzipÚ
ValueErrorr:   )ÚaÚbÚallsolÚNÚDÚcÚdÚstepsÚmaxdegÚboundÚngÚc_hatÚd_hatr   r<   ÚsolrD   rH   rE   rI   ÚGÚ_ratsimpmodprimer(   r   r!   r@   Útesteds                   @@@@€€€€€€€r   rh   z)ratsimpmodprime.<locals>._ratsimpmodprimeh   sÛ  øøøøø€ ð: �!ˆ1ˆØˆà—’Ñ!Ô! A§N¢NÑ$4Ô$4Ñ4ˆØð 	Ø˜Q‘JˆEˆEàˆEØ�!‰e�uŠn‰nØ�1ˆv˜ÐÐÙØ�JŠJ˜˜1�vÑÔÐà�˜1‘”ˆBØ�˜1‘”ˆBÝÐ$ q¨!¨R° nÑ5Ô5Ð5å˜¥# b¡'¤'Ñ)­uÐ5Ñ5Ô5ˆBÝ˜¥# b¡'¤'Ñ)­uÐ5Ñ5Ô5ˆBØ�b‘ˆBåÝÐ:Ð:Ð:Ð:Ð:­5µ°R±´©>¬>Ð:Ñ:Ô:Ñ:Ô:¸C¼HÀr¹MñKô KˆEåÝÐ:Ð:Ð:Ð:Ð:­5µ°R±´©>¬>Ð:Ñ:Ô:Ñ:Ô:¸C¼HÀr¹MñKô KˆEõ ˜˜E™	 A¨¡IÑ-¨q°#´(¸R±-Ø!œi¨tð5ñ 5ô 5Ø56ô8ˆAõ �Q˜SœXÐ&Ñ&Ô&×-Ò-Ñ/Ô/ˆAØ�%˜˜2 ™7¨t¸4Ð@Ñ@Ô@ˆCàñ �3Ð<Ð<¨s¯zªz©|¬|Ð<Ñ<Ô<Ñ<Ô<ñ Ø—J’J˜s‘O”O�Ø—J’J˜s‘O”O�ð
 —F’F�4¥¥S¨¨b©°1°#½¸R¹¼Å3ÀrÁ7Ä7Ñ9JÑ2KÑ%LÔ%LÑ MÔ MÑNÔNÑOÔO�Ø—F’F�4¥¥S¨¨b©°1°#½¸R¹¼Å3ÀrÁ7Ä7Ñ9JÑ2KÑ%LÔ%LÑ MÔ MÑNÔNÑOÔO�å˜˜CœHÑ%Ô%�Ý˜˜CœHÑ%Ô%�Ø˜’6�6Ý$Ð%7Ñ8Ô8Ð8à—’˜u e¨Q°°R±Ð8Ñ9Ô9Ð9Ø�q‘5˜F’?�?Ø$ Rœj˜\�Fàà�Q‰JˆEØ�‰FˆAØ�‰FˆAð_ �!‰e�uŠn‰nðb �1Š9ˆ9Ø+Ð+¨A¨q°&¸!¸QÀ¹YÑGÔG‰LˆAˆq�&Ø+Ð+¨A¨q°&¸!¸e¹)ÀQÑGÔG‰LˆAˆq�&à�!�Vˆ|Ðr*   )r&   r-   )Údomainz*Looking for best minimal solution. Got: %sTFrK   c                 ó    — t          | d                              ¦   «         ¦  «        t          | d                              ¦   «         ¦  «        z   S )Nr   r-   )r8   Úterms)Úxs    r   ú<lambda>z!ratsimpmodprime.<locals>.<lambda>Õ   s3   € ­¨Q¨q¬T¯ZªZ©\¬\Ñ):Ô):½SÀÀ1ÄÇÂÁÄÑ=NÔ=NÑ)N€ r*   )Úkey)Úconvert)r   r   )Úsympy.solvers.solversr!   r   r   r   r
   r   rj   Úhas_assoc_FieldÚ	get_fieldr   Úsetr   r5   r&   r   r   r8   r:   rT   ÚminÚis_FieldÚclear_denomsr   ÚqÚp)r   rg   r   r   r5   ÚargsÚnumÚdenomrJ   rj   r^   r_   r[   Únewsolrd   re   r<   rc   rf   ÚcnÚdnr   rh   r?   r(   r!   r@   ri   s    ``                   @@@@@@r   r"   r"      s=  øøøøøøøø€ ð< ,Ð+Ð+Ð+Ð+Ð+å	Ð
˜TÑ"Ô"Ð"õ ˜‘”×,Ò,Ñ.Ô.�J€CˆðÝ,¨c°5¨\¸AÑ-=ÐMÀÐMÐMÐMÈÐMÐM‰
ˆˆsˆsøÝð ð ð Øˆˆˆðøøøð ŒZ€FàÔð GØ×%Ò%Ñ'Ô'ˆŒ
ˆ
åØ<¸vÑEñGô Gð 	Gð =Ð<Ð<Ð<°%¸¸¸´)Ð<Ñ<Ô<ÐÝ‰UŒU€FðNð Nð Nð Nð Nð Nð Nð$Zð Zð Zð Zð Zð Zð Zð Zð Zð Zð Zð Zõ| �#�q˜#œ(¨#¬)Ð
4Ñ
4Ô
4°QÔ
7€CÝ�E˜1˜cœh¨c¬iÐ8Ñ8Ô8¸Ô;€Eàð $Ø�E‘	×!Ò!Ñ#Ô#Ð#à#Ð#ÝˆS�#”( 3¤:Ð.Ñ.Ô.µ°U¸C¼HÈSÌZÐ0XÑ0XÔ0XÐZ\ñ^ô ^�L€A€qˆ&àð P�Vð PÝÐ;½SÀ¹[¼[ÑIÔIÐIØˆØ#)ð 	>ð 	>ÑˆE�5˜!˜RØ�%˜˜2¨$°eÐ<Ñ<Ô<ˆCà�MŠM˜5Ÿ:š: c™?œ?¨E¯JªJ°s©O¬OÐ<Ñ=Ô=Ð=Ð=Ý�6ÐNÐNÐOÑOÔO‰ˆˆ1àŒ?ð Ø—’ t�Ñ,Ô,‰ˆˆAØ—’ t�Ñ,Ô,‰ˆˆAÝ�R˜ÑÔˆˆå�Q‰KŒKˆàˆaŒc‰E�A�a”c‘E‰?Ðs   ÁA ÁA,Á+A,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   ú<module>r‡      sù   ðØ 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YØ 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ .Ð .Ð .Ð .Ð .Ð .Ð .Ð .ð!ð !ð !ð, +/¸5ð ð ð ð ð ð ð r*   