� ��g���ddlmZddlmZmZmZddlmZddlm Z m Z m Z m Z m Z ddlmZmZddlmZmZddlmZmZd�Zd d d �d �Zd 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||gdd���\}}n#t$r||z cYSwxYwt |�t||z ��zS)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 �f/home/asafur/pinokio/api/open-webui.git/app/env/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�ATF)�quick� polynomialc����������ddlm�td|��t|�����\}} t ||g�zg|�Ri|��\}�n#t $r|cYSwxYw�j} | jr| � ���_ntd| z����fd�|dd�D���t������fd��d�������fd � �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\}}}}�||dd���}| �|�|��|�|��f���Tt)| d����\} } | jsC| �d���\}} | �d���\}} t/||��}nt/d ��}| |jz| |jzz 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>Ss%���<�<�<�Q����c�i���<�<�<��Nc�t���|dkrdgSg}ttt�j����|��D]_}dgt�j��z�|D]}�|xxdz cc<�t �fd��D����r|�����`�fd�|D���|dz ��zS)z� Compute all monomials with degree less than ``n`` that are not divisible by any element of ``leading_monomials``. r�c3�<�K�|]}t�|��duV��dS�N)r)r'�lmg�ms �r� <genexpr>z5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>bsB�����&�&�C�<��3�'�'�4�/�&�&�&�&�&�&r*c�H��g|]}t|��j�j���Sr$)r �as_expr�gens)r'�sr(s �rr)z6ratsimpmodprime.<locals>.staircase.<locals>.<listcomp>fs,���:�:�:�1�#��� � �#�S�X�.�:�:�:r*)r�range�lenr5�all�append)�n�S�mi�ir1�leading_monomialsr(� staircases @���rr@z"ratsimpmodprime.<locals>.staircaseVs����� ��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|dz } n|} ||z| k�r>||f�vr�n6��||f���|����|���td||��f��tdt ���zt ����tdt ���zt ������z} t t��fd�tt �����D�����j | z��} t t��fd�tt �����D�����j | z��} t|| z|| zz ��j | z�j d � ��d} t | �j � ��� ��}�|��zd d � ��}|�r�td �|���D�����sZ| �|��}| �|��}|�t!t#t%��zdgt ���t ���zz��������}|�t!t#t%��zdgt ���t ���zz��������}t |�j ��}t |�j ��}|dkrt'd���|�| | |��zf��||z|kr |dg}n|dz }|dz }|dz }||z| k��>|dkr,�||||||z ��\}}}�|||||z |��\}}}|||fS)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. rr-z%s / %s: %s, %szc:%d)�clszd:%dc3�:�K�|]}�|�|zV��dSr/r$)r'r>�Cs�M1s ��rr2z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>��/�����:�:�a�B�q�E�B�q�E�M�:�:�:�:�:�:r*c3�:�K�|]}�|�|zV��dSr/r$)r'r>�Ds�M2s ��rr2z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>�rFr*T)r&�polys)r5�� particularrc3�"K�|] }|dkV�� dS)rNr$)r'r6s rr2z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>�s&����<�<�!�q�A�v�<�<�<�<�<�<r*zIdeal not prime?�����)� total_degree�addrrr8rr �sumr7r5r r&�coeffsr9�values�subs�dict�list�zip� ValueErrorr:)�a�b�allsol�N�D�c�d�steps�maxdeg�bound�ng�c_hat�d_hatrr<�solrDrHrErI�G�_ratsimpmodprimer(rr!r@�testeds @@@@�������rrhz)ratsimpmodprime.<locals>._ratsimpmodprimehs��������:�!�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: %sTFrKc��t|d�����t|d�����zS)Nrr-)r8�terms)�xs r�<lambda>z!ratsimpmodprime.<locals>.<lambda>�s3���Q�q�T�Z�Z�\�\�):�):�S��1������=N�=N�)N�r*)�key)�convert)rr)�sympy.solvers.solversr!rrrr rrj�has_assoc_Field� get_fieldr�setr r5r&r rr8r:rT�min�is_Field� clear_denomsr�q�p)rrgrrr5�args�num�denomrJrjr^r_r[�newsolrdrer<rcrf�cn�dnrrhr?r(r!r@ris `` @@@@@@rr"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.corerrr�sympy.core.numbersr� sympy.polysrr r r r �sympy.polys.monomialsr r�sympy.polys.polyerrorsrr�sympy.utilities.miscrrrr"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*
Memory