� ��g�+���dZddlZddlmZddlmZddlmZddlZddl m Z m Z ej jZej jZGd�d��Zeeeefe_dS) a� PythonMPQ: Rational number type based on Python integers. This class is intended as a pure Python fallback for when gmpy2 is not installed. If gmpy2 is installed then its mpq type will be used instead. The mpq type is around 20x faster. We could just use the stdlib Fraction class here but that is slower: from fractions import Fraction from sympy.external.pythonmpq import PythonMPQ nums = range(1000) dens = range(5, 1005) rats = [Fraction(n, d) for n, d in zip(nums, dens)] sum(rats) # <--- 24 milliseconds rats = [PythonMPQ(n, d) for n, d in zip(nums, dens)] sum(rats) # <--- 7 milliseconds Both mpq and Fraction have some awkward features like the behaviour of division with // and %: >>> from fractions import Fraction >>> Fraction(2, 3) % Fraction(1, 4) 1/6 For the QQ domain we do not want this behaviour because there should be no remainder when dividing rational numbers. SymPy does not make use of this aspect of mpq when gmpy2 is installed. Since this class is a fallback for that case we do not bother implementing e.g. __mod__ so that we can be sure we are not using it when gmpy2 is installed either. �N)�gcd)�Decimal)�Fraction)�Tuple�Typec� ��eZdZUdZdZd#d�Zed���Ze�fd���Zd�Z d�Z d �Z d �Z d �Z d �Zd �Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d Z"e#e$d!fe%d"<�xZ&S)$� PythonMPQaRational number implementation that is intended to be compatible with gmpy2's mpq. Also slightly faster than fractions.Fraction. PythonMPQ should be treated as immutable although no effort is made to prevent mutation (since that might slow down calculations). �� numerator� denominatorNc�f�|�[t|t��rEt|t��r0t||��}||z}||z}|�||��Sn�t|t��r|�|d��St|t ��r |�|j|j��St|tttf��rt|��}t|t��r |�|j|j��Std���)z3Construct PythonMPQ with gcd computation and checksN�z/PythonMPQ() requires numeric or string argument) � isinstance�intr� _new_check�_newr r r r�float�strr� TypeError)�clsr r �divisors �h/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/external/pythonmpq.py�__new__zPythonMPQ.__new__9s!�� � "��)�S�)�)� >�j��c�.J�.J� >��i��5�5���g�%� ���'� ��~�~�i��=�=�=���)�S�)�)� L��x�x� �1�-�-�-��I�y�1�1� L��x�x� � 3�Y�5J�K�K�K��)�g�u�c�%:�;�;� 0�$�Y�/�/� ��)�X�.�.� L��x�x� � 3�Y�5J�K�K�K��I�J�J�J�c�t�|std|�d|�����|dkr| }| }|�||��S)z@Construct PythonMPQ, check divide by zero and canonicalize signsz Zero divisor �/r)�ZeroDivisionErrorr)rr r s rrzPythonMPQ._new_check]sV��� '�#�$M�I�$M�$M� �$M�$M�N�N� N� �1�_�_�"� �I�&�,�K��x�x� �;�/�/�/rc�f��t���|��}||_||_|S)z+Construct PythonMPQ efficiently (no checks))�superrr r )rr r �obj� __class__s �rrzPythonMPQ._newgs-����g�g�o�o�c�"�"��!�� �%���� rc�B�|j|j}}|dkr| |z S||zS)z'Convert to int (truncates towards zero)rr )�self�p�qs r�__int__zPythonMPQ.__int__os/���~�t�/�1�� �q�5�5��R��U�8�O��!�t� rc� �|j|jz S)z Convert to float (approximately)r �r#s r� __float__zPythonMPQ.__float__vs���~�� 0�0�0rc�*�t|j��S)zTrue/False if nonzero/zero)�boolr r(s r�__bool__zPythonMPQ.__bool__zs���D�N�#�#�#rc���t|t��r |j|jko|j|jkSt||j��r"|�t|����St S)z=Compare equal with PythonMPQ, int, float, Decimal or Fraction)rr r r �_compatible_types�__eq__�NotImplemented�r#�others rr/zPythonMPQ.__eq__~sm�� �e�Y� '� '� "��N�e�o�5�:��$��(9�9� ;� ��t�5� 6� 6� "��;�;�y��/�/�0�0� 0�!� !rc�� t|jdt��}ttt |j����|z��}n#t $r t}YnwxYw|jdkr|n| }|dkrdn|S)zhash - same as mpq/Fraction�����r�����)�powr �_PyHASH_MODULUS�hash�absr � ValueError� _PyHASH_INF)r#�dinv�hash_�results r�__hash__zPythonMPQ.__hash__�s��� ;��t�'��_�=�=�D���c�$�.�1�1�2�2�T�9�:�:�E�E��� � � ��E�E�E� �����.�A�-�-���E�6���r�\�\�r�r�v�-s�A�A#�"A#c�<�t|��|j|jffS)zDeconstruct for pickling)�typer r r(s r� __reduce__zPythonMPQ.__reduce__�s���D�z�z�D�N�D�,<�=�=�=rc�J�|jdkr|j�d|j��S|j�S)�Convert to stringrr)r r r(s r�__str__zPythonMPQ.__str__�s4�� � �q� � ��n�9�9�t�'7�9�9� 9��n�&� &rc�(�d|j�d|j�d�S)rDzMPQ(�,�)r r(s r�__repr__zPythonMPQ.__repr__�s ��:�d�n�:�:�t�'7�:�:�:�:rc��t||j��stS|j|jz}|j|jz}|||��S)zHelper for lt/le/gt/ge)rr.r0r r )r#r2�op�lhs�rhss r�_cmpzPythonMPQ._cmp�sK���%��!7�8�8� "�!� !��n�u�0�0���o�� 0�0���r�#�s�|�|�rc�B�|�|tj��S)z self < other)rN�operator�ltr1s r�__lt__zPythonMPQ.__lt__�����y�y��� �,�,�,rc�B�|�|tj��S)z self <= other)rNrP�ler1s r�__le__zPythonMPQ.__le__�rSrc�B�|�|tj��S)z self > other)rNrP�gtr1s r�__gt__zPythonMPQ.__gt__�rSrc�B�|�|tj��S)z self >= other)rNrP�ger1s r�__ge__zPythonMPQ.__ge__�rSrc�\�|�t|j��|j��S)zabs(q))rr9r r r(s r�__abs__zPythonMPQ.__abs__�s#���y�y��T�^�,�,�d�.>�?�?�?rc��|S)z+q�r(s r�__pos__zPythonMPQ.__pos__�s��� rc�D�|�|j |j��S)z-q)rr r r(s r�__neg__zPythonMPQ.__neg__�s���y�y�$�.��$�*:�;�;�;rc��t|t��r{|j|j}}|j|j}}t ||��}|dkr||z||zz}||z}nn||z||z} } || z|| zz| | z}}t ||��} || z||| zz}}n6t|t ��r|j|j|zz}|j}nt S|�||��S)zq1 + q2r�rr r r rrr0r� r#r2�ap�aq�bp�bq�gr$r%�q1�q2�g2s r�__add__zPythonMPQ.__add__�s��� �e�Y� '� '� "��^�T�%5��B��_�e�&7��B��B�� � �A��A�v�v��r�E�B�r�E�M���r�E����Q���A��B���"�u�r�"�u�}�b��e�1����A�Y�Y���R��!�q�B�w�-�1��� ��s� #� #� "����!1�E�!9�9�A�� �A�A�!� !��y�y��A���rc��t|t��r/|j|j|zz}|j}|�||��St S)zz1 + q2)rrr r rr0�r#r2r$r%s r�__radd__zPythonMPQ.__radd__�sH�� �e�S� !� !� "����!1�E�!9�9�A�� �A��9�9�Q��?�?� "�!� !rc��t|t��r{|j|j}}|j|j}}t ||��}|dkr||z||zz }||z}nn||z||z} } || z|| zz | | z}}t ||��} || z||| zz}}n6t|t ��r|j|j|zz }|j}nt S|�||��S)zq1 - q2rrerfs r�__sub__zPythonMPQ.__sub__�s��� �e�Y� '� '� "��^�T�%5��B��_�e�&7��B��B�� � �A��A�v�v��r�E�B�r�E�M���r�E����Q���A��B���"�u�r�"�u�}�b��e�1����A�Y�Y���R��!�q�B�w�-�1��� ��s� #� #� "����!1�%�!7�7�A�� �A�A�!� !��y�y��A���rc��t|t��r/|j|z|jz }|j}|�||��St S)zz1 - q2)rrr r rr0rqs r�__rsub__zPythonMPQ.__rsub__sH�� �e�S� !� !� "�� �5�(�4�>�9�A�� �A��9�9�Q��?�?� "�!� !rc��t|t��rS|j|j}}|j|j}}t ||��}t ||��}||z||zz||z||zz} }nIt|t ��r-t ||j��} |j|| zz}|j| z} nt S|�|| ��S)zq1 * q2re� r#r2rgrhrirj�x1�x2r$r%�xs r�__mul__zPythonMPQ.__mul__s��� �e�Y� '� '� "��^�T�%5��B��_�e�&7��B��R����B��R����B���V�b�"�f�%��B���R��'8�q�A�A� ��s� #� #� "��E�4�+�,�,�A����q��)�A�� �!�#�A�A�!� !��y�y��A���rc��t|t��rBt|j|��}|j||zz}|j|z}|�||��St S)zz1 * q2)rrrr r rr0�r#r2r{r$r%s r�__rmul__zPythonMPQ.__rmul__ s[�� �e�S� !� !� "��D�$�e�,�,�A����q��)�A�� �!�#�A��9�9�Q��?�?� "�!� !rc�p�|j|j}}|dkr||| }}}|�||z||z��S)zq ** zr)r r r)r#�expr$r%s r�__pow__zPythonMPQ.__pow__*sD���~�t�/�1�� ��7�7��1�s�d�#�q�A����q�#�v�q�#�v�.�.�.rc��t|t��rS|j|j}}|j|j}}t ||��}t ||��}||z||zz||z||zz} }nIt|t ��r-t ||j��} |j| z}|j|| zz} nt S|�|| ��S)zq1 / q2)rr r r rrr0rrxs r� __truediv__zPythonMPQ.__truediv__3s��� �e�Y� '� '� "��^�T�%5��B��_�e�&7��B��R����B��R����B���V�b�"�f�%��B���R��'8�q�A�A� ��s� #� #� "��E�4�>�*�*�A����!�A�� �%��(�+�A�A�!� !����q�!�$�$�$rc��t|t��rBt|j|��}|j||zz}|j|z}|�||��St S)zz / q)rrrr r rr0r~s r� __rtruediv__zPythonMPQ.__rtruediv__Ds\�� �e�S� !� !� "��D�N�E�*�*�A�� �%��(�+�A����!�A��?�?�1�a�(�(� (�!� !rr`.r.)N)'�__name__� __module__� __qualname__�__doc__� __slots__r� classmethodrrr&r)r,r/r?rBrErIrNrRrVrYr\r^rarcrorrrtrvr|rr�r�r�r.�tTupler�__annotations__� __classcell__)r!s@rr r .s����������-�I�"K�"K�"K�"K�H�0�0��[�0�������[�����1�1�1�$�$�$�"�"�"� .� .� .�>�>�>�'�'�'�;�;�;����-�-�-�-�-�-�-�-�-�-�-�-�@�@�@����<�<�<����B"�"�"����,"�"�"����""�"�"�/�/�/�%�%�%�""�"�"�,.��v�d�C�i�(�-�-�-�-�-�-�-rr )r�rP�mathr�decimalr� fractionsr�sys�typingrr�r� hash_info�modulusr7�infr;r rr.r`rr�<module>r�s�����@���������������������� � � � �(�(�(�(�(�(�(�(��-�'���m�� �`.�`.�`.�`.�`.�`.�`.�`.�N )�#�w��A� ���r
Memory