� I�gݯ� ��dZdZddlZddlmZddlZdaddlmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZddlmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!e dkrd �Z"nd �Z"d �Z#Gd �d e$��Z% e&n#e'$rd�Z&YnwxYwe&d��Z(e&d��Z)e&d��Z*e&d��Z+e&d��Z,e,Z-d�Z.d�Z/d�Z0de ddfZ1de ddfZ2de ddfZ3de ddfZ4de ddfZ5de ddfZ6de ddfZ7de ddfZ8de ddfZ9de ddfZ:dZ;d�Z<Gd �d!��Z=dgd"�e>dd#��D��zZ?e=��e?gZ@e)d$e*d%e,d&e+d'iZAd(�ZBd)�ZC eDeEfZFn#e'$reDfZFYnwxYwd*�ZGd+�ZHe d,krd-eIe��vrejJZBejJZCe dkr ejKxZBZCereGZKeHZLneBZKeCZLde-fd.�ZMeNd/�e>d0d1��D����ZOe d,krd2eIe��vrejPZMe dkrejMZMde-fd3�ZQd4�ZRdkd5�ZSd6�ZTde-fd7�ZUde-fd8�ZVde-fd9�ZWde-fd:�ZXd;e-fd<�ZYd=e-fd>�ZZde-fd?�Z[d@e-fdA�Z\e-fdB�Z]dC�Z^dD�Z_dE�Z`dF�ZadG�ZbdH�ZcdI�ZddJ�ZedK�ZfdL�ZgdM�Zhde-fdN�Zide-fdO�Zjde-fdP�ZkdQ�Zlde-dfdR�Zmde-fdS�Znde-d@fdT�Zode-fdU�Zpe-fdV�Zqde-fdW�Zre-fdX�Zse d,krepZteqZunerZtesZudY�ZvdZ�Zwe-fd[�Zxe-fd\�Zye-fd]�Zze,e+e+e,e)e*e*e)e(e(iZ{e,e,e+e+e)e*e*e)e(e(iZ|e-fd^�Z}d_�Z~d`�Z dldb�Z�dmdd�Z�e9e9e:e8de�Z�e-fdf�Z�dg�Z�dh�Z�e-fdi�Z�e-fdj�Z�e dkr> ddl�m�cm�cm�Z�e�jmZme�jnZne�jtZte�jxZxe�j�Z�dS#e�$rYdSwxYwdS)nzH Low-level functions for arbitrary-precision floating-point arithmetic. � plaintext�N)�bisect�) �MPZ�MPZ_TYPE�MPZ_ZERO�MPZ_ONE�MPZ_TWO�MPZ_FIVE�BACKEND�STRICT� HASH_MODULUS� HASH_BITS�gmpy�sage� sage_utils) � giant_steps� trailtable�bctable�lshift�rshift�bitcount�trailing� sqrt_fixed�numeral�isqrt� isqrt_fast�sqrtrem� bin_to_radixrc�6�|\}}}}|t|��||fS�N��hex��x�sign�man�exp�bcs �c/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/mpmath/libmp/libmpf.py� to_pickabler+s%�����c�3���S��X�X�s�B�&�&�c�F�|\}}}}|t|��dd�||fS)N�r"r$s r*r+r+#s-�����c�3���S��X�X�a�b�b�\�3��*�*r,c�8�|\}}}}|t|d��||fS)N�)rr$s r*� from_pickabler1's'����D�#�s�B� �#�c�2�,�,��R� (�(r,c��eZdZdS)� ComplexResultN)�__name__� __module__� __qualname__�r,r*r3r3+s�������Dr,r3c��|Sr!r7)r%s r*�<lambda>r91s��q�r,�n�f�c�u�dc �|�tdttt|��dz ��dz ����S)zZReturn number of accurate decimals that can be represented with a precision of n bits.r�r�y O� @��max�int�round�r:s r*� prec_to_dpsrF;s5�� �q�#�e�C��F�F�#5�5�6�6�q�8�9�9� :� :�:r,c �|�tdttt|��dzdz������S)zJReturn the number of bits required to represent n decimals accurately.rr@rArEs r*� dps_to_precrH@s4�� �q�#�e�S��V�V�A�X�'9�9�:�:�;�;� <� <�<r,c�:�t|��}|dkrdS|dzS)z�Return the number of decimal digits required to represent a number with n-bit precision so that it can be uniquely reconstructed from the representation.���)rF)r:�dpss r*�repr_dpsrNEs&�� �a�.�.�C� �b�y�y��r� ��7�Nr,rL�����i����i8��������i���������g�c�x�|tkrR|dkr9||dz z }|dzr'|dzs|t|dk|zr|dz dzS|dz St| ||�� S|tkr||z S|tkr| |z S|t kr|dkr||z S| |z S|t kr|dkr| |z S||z SdS)Nrrr.�,)� round_nearest�h_mask� round_int� round_floor� round_ceiling� round_down�round_up)r%r:�rnd�ts r*rVrVks�� �m��� ��6�6��a��c� �A��1�u� �1�q�5� �a�&��3��-��*:�&:� ��1��a�x���!�t� ��q�b�!�S�)�)�)� )� �k����A�v� � �m����"���|�� �j��� ��6�6���6�M��"���|�� �h��� ��6�6��b�Q�Y�<� ��A�v� ��r,c��eZdZd�ZdS)� h_mask_bigc�"�t|dz zdz S)Nr�r )�selfr:s r*� __getitem__zh_mask_big.__getitem__�s���!�A�#���!�!r,N)r4r5r6rbr7r,r*r^r^�s#������"�"�"�"�"r,r^c�.�g|]}t|dz zdz ��S)rr`)�.0�_s r*� <listcomp>rf�s%��@�@�@�Q�g��!��n�a�'�@�@�@r,rS)rr)rr)rr)rrc���|stS||z }|dkrm|tkr;||dz z }|dzr(|dzs|t|dk|zr |dz dz}n&|dz }n t||r||z}n| |z }||z }|}|dzsdtt |dz��}|s6|dzs|dz}|dz }|dz}|dz�tt |dz��}||z}||z }||z}|dkrd}||||fS)a� Create a raw mpf tuple with value (-1)**sign * man * 2**exp and normalized mantissa. The mantissa is rounded in the specified direction if its size exceeds the precision. Trailing zero bits are also stripped from the mantissa to ensure that the representation is canonical. Conditions on the input: * The input must represent a regular (finite) number * The sign bit must be 0 or 1 * The mantissa must be positive * The exponent must be an integer * The bitcount must be exact If these conditions are not met, use from_man_exp, mpf_pos, or any of the conversion functions to create normalized raw mpf tuples. rrr.rS�����fzerorTrU� shifts_downrrC�r&r'r(r)�precr[r:r\s r*� _normalizero�sj��$ ��� � �T� �A��1�u�u� �-� � ���!�� �A��1�u� �1�q�5� �c�F�1�S�5�M�!�,<�&<� ��!�t�Q�h�����d��� �� �d� #� � �A�I�C�C��T�A�I�,�C� �q��� �� ��7� � �s�3��9�~�~� &��� +��C�i� ��� ���q����a����C�i� ��3�s�S�y�>�>�*�A� �� �� �q��� �a���  �a�x�x� �� ��c�2� �r,c���|stS||kr||||fS||z }|tkr;||dz z }|dzr(|dzs|t|dk|zr |dz dz}n&|dz }n t||r||z}n| |z }||z }|}|dzsdtt |dz��}|s6|dzs|dz}|dz }|dz}|dz�tt |dz��}||z}||z }||z}|dkrd}||||fS)zSsame as normalize, but with the added condition that man is odd or zero rr.rSrhrirjrms r*� _normalize1rq�sy�� ��� � �T�z�z��S�#�r�!�!� �T� �A� �m��� �A�a�C�L�� �q�5� �q�1�u� �#��q��u� �a�(8�"8� ��a�4��(�C�C��Q�$�C�C� �S� �$� �� �� ������ �l���1�H�C� �B� ��7� � �s�3��9�~�~� &��� +��C�i� ��� ���q����a����C�i� ��3�s�S�y�>�>�*�A� �� �� �q��� �a���  �a�x�x� �� ��c�2� �r,c���t|��tksJ�t|��tvsJ�t|��tvsJ�|t|��ksJ�t ||||||��S)�zAdditional checks on the components of an mpf. Enable tests by setting the environment variable MPMATH_STRICT to Y.)�typer� _exp_typesrro�r&r'r(r)rnr[s r*�strict_normalizerw�sx�� ��9�9�� � � � � ��8�8�z� !� !� !� !� ��9�9� � "� "� "� "� ��#��� � � � � �d�C��b�$�� 4� 4�4r,c���t|��tksJ�t|��tvsJ�t|��tvsJ�|t|��ksJ�|r|dzsJ�t ||||||��S)rsr)rtrrurrqrvs r*�strict_normalize1rys��� ��9�9�� � � � � ��8�8�z� !� !� !� !� ��9�9� � "� "� "� "� ��#��� � � � ��!��q��!�!� !� �t�S�#�r�4�� 5� 5�5r,r�_mpmath_normalizec���t|��}d}|dkrd}| }|dkrtt|��}nt|��}|s�|stS|dzsx|dzr||dz |dz|dz fSt t|dz��}|s6|dzs|dz}|dz }|dz}|dz�t t|dz��}||z}||z }||z}||||fSt ||||||��S)z�Create raw mpf from (man, exp) pair. The mantissa may be signed. If no precision is specified, the mantissa is stored exactly.rr�r.rhri)rrrCrrkr� normalize)r'r(rnr[r&r)r\s r*� from_man_expr~#s@�� �c�(�(�C� �D� �Q�w�w����d�� �T�z�z� �S��X�X� ��� �c�]�]�� �$�� ��L��Q�w� ��Q�w� 9��c�Q�h��a���a��8�8��3�s�S�y�>�>�*�A�� /���)���A�I�C��1�H�C��!�G�B���)���s�3��9�~�~�.�� �A�I�C� �1�H�C� �!�G�B��c�3��#�#� �T�3��R��s� 3� 3�3r,c#�:K�|]}|t|d��fV��dS)rN)r~)rdr:s r*� <genexpr>r�Bs1����B�B�Q�!�\�!�Q�'�'�(�B�B�B�B�B�Br,i����i�_mpmath_createc�V�|s|tvr t|St|d||��S)zcCreate a raw mpf from an integer. If no precision is specified, the mantissa is stored exactly.r)� int_cacher~)r:rnr[s r*�from_intr�Js3�� � � � �>�>��Q�<� � ��1�d�C� (� (�(r,c�D�|\}}}}|s|rtd|z���||fS)z:Return (man, exp) of a raw mpf. Raise an error if inf/nan.z*mantissa and exponent are undefined for %s�� ValueError��sr&r'r(r)s r*� to_man_expr�Rs>����D�#�s�B� �M�S�M��E��K�L�L�L� ��8�Or,c���|\}}}}|s|rtd���|dkr |r| |zS||zS|s|r|| z S|| z S|rt| | |��St|| |��S)z�Convert a raw mpf to the nearest int. Rounding is done down by default (same as int(float) in Python), but can be changed. If the input is inf/nan, an exception is raised.z cannot convert inf or nan to intr)r�rV)r�r[r&r'r(r)s r*�to_intr�Ys�����D�#�s�B� �=�S�=��;�<�<�<� �a�x�x� � !��D�S�=� ��c�z�� �!� � !��c�T�]�#� #��C�4�=� � �)��#���t�S�)�)�)���s�d�C�(�(�(r,c�`�|\}}}}|s|r|S|dkr|S||z}|dkrp|tkr|rtStS|tkr|rtStS|t kr(|dks |t krtS|rtStSt�t|t||��|��S�Nrr) rXrk�fonerW�fnonerTr �NotImplementedError�mpf_pos�min)r�r[r&r'r(r)�mags r*� mpf_round_intr�os�����D�#�s�B� ��S���� �a�x�x��� �b�&�C� �Q�w�w� �-� � �� !�E�\� �[� �K� � �� "�E�\�!�\� �M� !� !��Q�w�w�#��.�.��,�� #�e�|�"�{�%� %� �1�c�"�c�l�l�C� (� (�(r,c�V�t|t��}|rt|||��}|Sr!)r�rWr��r�rnr[�vs r*� mpf_floorr��s/���a��%�%�A� �"� �A�t�S� !� !�� �Hr,c�V�t|t��}|rt|||��}|Sr!)r�rXr�r�s r*�mpf_ceilr���/���a��'�'�A� �"� �A�t�S� !� !�� �Hr,c�V�t|t��}|rt|||��}|Sr!)r�rTr�r�s r*�mpf_nintr��r�r,c�@�t|t|��||��Sr!)�mpf_subr�)r�rnr[s r*�mpf_fracr��s�� �1�i��l�l�D�#� .� .�.r,�5c�P�||krtS tj|��\}}n7#|tkr tcYS|t kr t cYStcYSxYw|tkrtS|t krt St t|dz��|dz ||��S)z�Create a raw mpf from a Python float, rounding if necessary. If prec >= 53, the result is guaranteed to represent exactly the same number as the input. If prec is not specified, use prec=53.lr�)�fnan�math�frexp�math_float_inf�finf�fninfr~rC)r%rnr[�m�es r*� from_floatr��s���  �A�v�v�� ���z�!�}�}���1�1��� �� � �t� � � � ��� � ������ � � �����N���4�K��^�O���E�\� ��A�u�I����"��d�C� 8� 8�8s�'�A�A�A�qc��t|��}||krt|||��Sddl}|�|��r[|�|��\}}t t |�|d����t |dz ��||��S|�|��rtS|� |��rtStS)z�Create a raw mpf from a numpy float, rounding if necessary. If prec >= 113, the result is guaranteed to represent exactly the same number as the input. If prec is not specified, use prec=113.rNr�) �floatr��numpy�isfiniter�r~rC�ldexp�isposinfr��isneginfr�r�)r%rnr[�y�npr�r�s r*� from_npfloatr��s��� �a���A��A�v�v��!�T�3�'�'�'����� �{�{�1�~�~�J��x�x��{�{���1��C�����C� 0� 0�1�1�3�q��u�:�:�t�S�I�I�I� �{�{�1�~�~�"�d�{� �{�{�1�~�~�#�e�|� �Kr,c�R�|���rtS|���r"|���rtnt S|�7t t|���d��dz��}tt|��||��S)z�Create a raw mpf from a Decimal, rounding if necessary. If prec is not specified, use the equivalent bit precision of the number of significant digits in x.Nrr@) �is_nanr�� is_infinite� is_signedr�r�rC�len�as_tuple�from_str�str)r%rnr[s r*� from_Decimalr��s~�� �x�x�z�z��$�;��}�}���?�� � � � �?�u�u�4�?� �|��3�q�z�z�|�|�A��'�'�(:�:�;�;�� �C��F�F�D�#� &� &�&r,Fc�n�|\}}}}|sA|tkrdS|tkrtS|tkrt Sttz S|dkrt ||||d|��\}}}}|r| } t j||��S#t$r%|r�||zdkr|r t cYStcYSYdSwxYw)a  Convert a raw mpf to a Python float. The result is exact if the bitcount of s is <= 53 and no underflow/overflow occurs. If the number is too large or too small to represent as a regular float, it will be converted to inf or 0.0. Setting strict=True forces an OverflowError to be raised instead. Warning: with a directed rounding mode, the correct nearest representable floating-point number in the specified direction might not be computed in case of overflow or (gradual) underflow. gr�r)rkr�r�r�� normalize1r�r�� OverflowError)r��strictr[r&r'r(r)s r*�to_floatr��s�����D�#�s�B� �-� ��:�:�c�c� ��9�9�^�+� ��:�:�~�o�-��n�,�,� �B�w�w�'��c�3��B��D�D���c�3�� ���d�� ��z�#�s�#�#�#�� � � � � � � � ��8�a�<�<�� &�&��&�&�&�%�%�%�%��s�s� ���s�0B� B4�'B4�3B4c�Z�tt|��t|��||��S)zDCreate a raw mpf from a rational number p/q, round if necessary.)�mpf_divr�)�p�qrnr[s r*� from_rationalr��s$�� �8�A�;�;��� � �T�3� 7� 7�7r,c�z�|\}}}}|r| }|dkrtd|z���|dkr |d|zzdfS|d| zfS)z]Convert a raw mpf to a rational number. Return integers (p, q) such that s = p/q exactly.rOz&cannot convert %s to a rational numberrrr�r�s r*� to_rationalr��sj����D�#�s�B� ���d�� �R�x�x��A�C�G�H�H�H� �a�x�x��a��f�~�q� � ��A���I�~�r,c�f�|\}}}}||z}|r|dkr| |zS| | z S|dkr||zS|| z S)z.Convert a raw mpf to a fixed-point big integerrr7)r�rnr&r'r(r)�offsets r*�to_fixedr��sd����D�#�s�B� �4�Z�F� �0� �Q�;�;����/�/�!$��6�'�2�2� �Q�;�;�s�f�}�,�"��w�/�/r,c�p�ts ddl}|jatt|��| |t��S)zQReturn a raw mpf chosen randomly from [0, 1), with prec bits in the mantissa.rN)� getrandbits�randomr~rW)rnr�s r*�mpf_randr�s;�� �)�� � � ��(� � � �D�)�)�D�5�$� � D� D�Dr,c�^�|dr|ds|tks |tkrdS||kS)z�Test equality of two raw mpfs. This is simply tuple comparison unless either number is nan, in which case the result is False.rF)r��r�r\s r*�mpf_eqr�s:�� �Q�4��q��t�� ��9�9��T� � ��5� ��6�Mr,c��tjdkr�|\}}}}|sU|tkrtjjS|t krtjjS|tkrtjj S|tz}|dkr |tz}ntdz d|z tzz }||ztz}|r| }|dkrd}t|��S tt|d�����S#t$rt|��cYSwxYw)N)rLr.rrrOrP)r�)�sys� version_infor�� hash_info�nanr��infr�rrrC�hashr�r�)r��ssign�sman�sexp�sbc�hs r*�mpf_hashr�!s�� ��6�!�!�!"���t�T�3�� 5��D�y�y���!2�2��D�y�y���!2�2��E�z�z�3�=�#4�"4�4� �<� �� �1�9�9��)�#�D�D��q�=�R�$�Y�)�$;�<�D� �$�Y�,� &�� ��q�b�!� ��7�7��A��1�v�v� � ����1�-�-�-�.�.� .��� � � ���7�7�N�N�N�  ���s� C*�*D�Dc���|\}}}}|\}}}} |r|sf|tkrt|�� S|tkrt|��S||krdS|tkrdS|tkrdS|tkrdSdS||kr|sdSdS||kr||krdS||kr|rdSdS|rdSdS||z} | |z} |r| | krdS| | krdSn| | krdS| | krdSt ||dt ��} | drdSdS)z�Compare the raw mpfs s and t. Return -1 if s < t, 0 if s == t, and 1 if s > t. (Same convention as Python's cmp() function.)rrrO�)rk�mpf_signr�r�r�r�rW) r�r\r�r�r�r��tsign�tman�texp�tbc�a�b�deltas r*�mpf_cmpr�>sk����E�4��s���E�4��s� ��t�� ��:�:�x��{�{�l�*� ��:�:�h�q�k�k�)� ��6�6�!�!� ��9�9�Q�Q� ��9�9�Q�Q� ��:�:�a�a��r� ��~�~���Q�Q��r� �t�|�|� �4�<�<��1� �$�;�;�� �R�R��Q�� �Q�Q��R� �d� �A� �d� �A� �� �q�5�5��� �q�5�5���5� �q�5�5��� �q�5�5��� �A�q�!�[� )� )�E� �Q�x���r� �1r,c�Z�|tks |tkrdSt||��dkS�NFr�r�r�r�s r*�mpf_ltr�r�+���D�y�y�A��I�I��u� �1�a�=�=�1� �r,c�Z�|tks |tkrdSt||��dkSr�r�r�s r*�mpf_ler�w�+���D�y�y�A��I�I��u� �1�a�=�=�A� �r,c�Z�|tks |tkrdSt||��dkSr�r�r�s r*�mpf_gtr�|r�r,c�Z�|tks |tkrdSt||��dkSr�r�r�s r*�mpf_ger��r�r,c��|dx}}|dd�D]&}t||��r|}t||��r|}�'||fSr�)r�r�)�seqr�rBr%s r*� mpf_min_maxr��sU���A���C�#� ����W�#�#�� �!�S�>�>�"��3� �!�S�>�>�"��3�� ��8�Or,c�L�|r!|\}}}}|s|r|St||||||��S|S)zPCalculate 0+s for a raw mpf (i.e., just round s to the specified precision).)r��r�rnr[r&r'r(r)s r*r�r��sK�� �9����c�3��� �� ��H��$��S�"�d�C�8�8�8� �Hr,c��|\}}}}|s(|r$|tkrtS|tkrtS|S|s d|z |||fStd|z |||||��S)z�Negate a raw mpf (return -s), rounding the result to the specified precision. The prec argument can be omitted to do the operation exactly.r)r�r�r�r�s r*�mpf_negr�sx����D�#�s�B� �� � '��D�y�y��,��E�z�z�$�;��� �&��$���S�"�%�%� �a��f�c�3��D�#� 6� 6�6r,c��|\}}}}|s|r|tkrtS|S|s |rd|||fS|Std|||||��S)z�Return abs(s) of the raw mpf s, rounded to the specified precision. The prec argument can be omitted to generate an exact result.r)r�r�r�r�s r*�mpf_absr�sp����D�#�s�B� ��S�� ��:�:��K��� �� � %��s�C��$� $��� �a��c�2�t�S� 1� 1�1r,c�V�|\}}}}|s|tkrdS|tkrdSdSd|zS)z�Return -1, 0, or 1 (as a Python int, not a raw mpf) depending on whether s is negative, zero, or positive. (Nan is taken to give 0.)rrOr)r�r�r�s r*r�r��sC����D�#�s�B� �� ��9�9�Q�Q� ��:�:�b�b��q� �4�<�r,c�z�|\}}}}|\} } } } | |z} |�r�| �r�|| z } | �ro| dkr�| dkrU|rS||z| z | z }||dzkr?|dz} || z}| |kr|dz }n|dz}t|||| z t|��||��S|| kr | || zz}n!|r | || zz }n|| z| z }|dkrd}n| }d}t|��}t||| ||p||��S| dkr�| dkrU|rS| | z|z |z }||dzkr?|dz} | | z} || kr| dz } n| dz} t| | | | z t| ��||��S|| kr || | zz}n#| r || | zz }n | | z|z }|dkrd}n| }d}t|��}t|||||p||��S|| kr| |z}n|r| |z }n|| z }|dkrd}n| }d}t|��}t||| ||p||��S|rt|��}|s/|r||ks| s| s|StS| rt| | | | |p| |��S|S| r|S|rt|||||p||��S|S)z� Add the two raw mpf values s and t. With prec=0, no rounding is performed. Note that this can produce a very large mantissa (potentially too large to fit in memory) if exponents are far apart. r�d�ri����)r�rr}rr�)r�r\rnr[�_subr�r�r�r�r�r�r�r�r�r�r'r)s r*�mpf_addr �s�����E�4��s���E�4��s� �T�M�E� �A@��A@����� �2 I���z�z��C�<�<�D�<��$�J��,�t�3�E��t�a�x�'�'�!%�������� �E�>�>�4�1�9�4�4�+/�1�9�4�)�%��t�F�{�$�T�N�N�D�#� 7� 7�7��E�>�>��$�&�.�1�C�C��<�D�D�F�N�$;�c�c�%)�V�^�t�$;�c��a�x�x� !���"�d�� !���c�]�]��!�%��d�B�� ��C�H�H�H��!����D�=�=�T�=��$�J��,�t�3�E��t�a�x�'�'�!%�������� �E�>�>�4�1�9�4�4�+/�1�9�4�)�%��t�F�{�$�T�N�N�D�#� 7� 7�7��E�>�>��$�6�'�/�2�C�C��=�D�D�V�G�O�$<�c�c�%)�f�W�_��$<�c��a�x�x� !���"�d�� !���c�]�]��!�%��d�B�� ��C�H�H�H� �E�>�>���+�C�C�� (�D�4�K�c�c� �4�K�c��a�x�x�����d���� �c�]�]�����T�2�t�z�r�3�?�?�?� �� �A�J�J�� �� � ��A�v�v��v�T�v����K� � H��e�T�4��d�k�c�3�G�G� G��� ���� �D��%��t�S�$�+�#�s�C�C�C� �Hr,c�(�t||||d��S)zxReturn the difference of two raw mpfs, s-t. This function is simply a wrapper of mpf_add that changes the sign of t.r)r )r�r\rnr[s r*r�r�s�� �1�a��s�A� &� &�&r,c��d}d}|dzpd}d}|D]�}|\} } } } | ri| r|s| } | |z } | |kr9| |kr*|r#| tt|����z |kr| }| }�M|| | zz }�V| } | | z |kr|s| | }}�i|| z| z}| }�t| r)|rt|��}t|pt|d��}��|r|St ||||��S)a> Sum a list of mpf values efficiently and accurately (typically no temporary roundoff occurs). If prec=0, the final result will not be rounded either. There may be roundoff error or cancellation if extremely large exponent differences occur. With absolute=True, sums the absolute values. rr.i@BNr)r�absrr rkr~)�xsrnr[�absoluter'r(�max_extra_prec�specialr%�xsign�xman�xexp�xbcr�s r*�mpf_sumr"sM�� �C� �C��!�V�&�w�N��G� �6�6��!"���t�T�3� � 6�� �X� ��u���3�J�E��s�{�{��N�*�*��+�"'���S���(:�(:�":�^�"K�"K��C��C�C��D�E�M�*�C�C������9�~�-�-��.�#'��S����%�<�4�/�C��C�C� � 6�� ��A�J�J���g�.���1�5�5�G������ ��S�$�� ,� ,�,r,c�~�|\}}}}|\}} } } ||z } || z} | r1t| ��}|rt| | || z|||��S| | || z|fS| o|}| o| }|s |stSt||fvrtS| s| r||}}|tkrtStt d�t |��t |��zS)�Multiply two raw mpfs�rrO)rr�rkr�r�r�r��r�r\rnr[r�r�r�r�r�r�r�r�r&r'r)� s_special� t_specials r*� gmpy_mpf_mulrRs�����E�4��s���E�4��s� �5�=�D� �t�)�C� �.� �c�]�]�� � .��d�C��d��B��c�B�B� B��#�t�D�y�"�-� -���#�t�I���#�t�I� ��Y��� � ��1�v�~�~�d�{� �'�d�'�1�a�q�A��E�z�z�$�;��u� � �h�q�k�k�H�Q�K�K�7� 8�8r,c���|\}}}}|st|t|��||��S|stS|dkr|dz}| }||z}t|||t |��||��S)�Multiply by a Python integer.rr)�mpf_mulr�rkr}r�r�r:rnr[r&r'r(r)s r*�gmpy_mpf_mul_intr!gs����D�#�s�B� �2��q�(�1�+�+�t�S�1�1�1� ��� ��1�u�u� �� �� �B���1�H�C� �T�3��X�c�]�]�D�#� >� >�>r,c��|\}}}}|\}} } } ||z } || z} | r?|| zdz }|t| |z ��z }|rt| | || z|||��S| | || z|fS| o|}| o| }|s |stSt||fvrtS| s| r||}}|tkrtStt d�t |��t |��zS)rrr)rCr�rkr�r�r�r�rs r*�python_mpf_mulr#ts����E�4��s���E�4��s� �5�=�D� �t�)�C� �.� �3�Y��]�� �c�#�r�'�l�l��� � .��d�C��d��B��c�B�B� B��#�t�D�y�"�-� -���#�t�I���#�t�I� ��Y��� � ��1�v�~�~�d�{� �'�d�'�1�a�q�A��E�z�z�$�;��u� � �h�q�k�k�H�Q�K�K�7� 8�8r,c�T�|\}}}}|st|t|��||��S|stS|dkr|dz}| }||z}|dkr!|tt |��dz z }n|t |��dz z }|t ||z ��z }t ||||||��S)rrrr|)rr�rkrrCrr}r s r*�python_mpf_mul_intr%�s�����D�#�s�B� �2��q�(�1�+�+�t�S�1�1�1� ��� ��1�u�u� �� �� �B���1�H�C��4�x�x� �g�c�!�f�f�o��!�!��� �h�q�k�k�A�o����#�c�2�g�,�,��B� �T�3��R��s� 3� 3�3r,c�*�|\}}}}|s|S||||z|fS)z8Quickly multiply the raw mpf s by 2**n without rounding.r7)r�r:r&r'r(r)s r*� mpf_shiftr'�s0����D�#�s�B� ���� ��c�!�e�R� �r,c�|�|\}}}}|s|tkr tdfSt�t|| |z ��||zfS)z?Convert x = y*2**n to (y, n) with abs(y) in [0.5, 1) if nonzeror)rkr�r'r$s r*� mpf_frexpr)�sP����D�#�s�B� �� ��:�:��1�:� �� � �Q���C�� � �"�S�&� (�(r,c��|\}}}}|\}} } } |r| s�|tkr+|tkrt�|tkrtStS|tkrt�| o|} | o| } | r | rtS|tks |tkrtS| sD|tkrtSttd�t |��t |��zStS||z }| dkrt |||| z |||��S||z | zdz}|dkrd}t||z| ��\}}|r4|dzdz}|dz }t |||| z |z t|��||��St|||| z |z t|��||��S)zFloating-point divisionrrr�) rk�ZeroDivisionErrorr�r�r�r�r��divmodrr})r�r\rnr[r�r�r�r�r�r�r�r�rrr&�extra�quot�rems r*r�r��s�����E�4��s���E�4��s� ��t�� ��:�:��E�z�z�!2�2��D�y�y��+��L� ��:�:�#� #��X�'�4� ��X�'�4� � � �� ��K� ��9�9��T� � ��K�� A��E�z�z�� ��u�%�%�h�q�k�k�H�Q�K�K�&?�@� @�� � �5�=�D� �q�y�y��$��d�4�i��d�C�@�@�@� �3�J�� �q� �E� �q�y�y����t�U�{�D�)�)�I�D�#� �R��a��1�}�� �� ���$��d�4�i��o�x��~�~�t�S�Q�Q�Q� �T�4��d��5��(�4�.�.�$�� L� L�Lr,c�`�|\}}}}|r|stt|��|||��S|dkr|dz}| }||zdz}t||z|��\} } | r2| dzdz} |dz }t|| | |z t | ��||��St || | |z t | ��||��S)z>Floating-point division n/t with a Python integer as numeratorrrr�)r�r�r,r�rr}) r:r\rnr[r&r'r(r)r-r.r/s r*� mpf_rdiv_intr1�s�����D�#�s�B� �2�C�2��x��{�{�A�t�S�1�1�1��1�u�u� �� �� �B�� �2�I��M�E��q�%�x��%�%�I�D�#� �M��a��1�}�� �� ���$��s�d�5�j�(�4�.�.�$��L�L�L� �T�4�#��e��X�d�^�^�T�3� G� G�Gr,c�J�|\}}}}|\}} } } |s|s| s | rtS||kr | ||zkr|S| dkr|| | zkrtSt|| ��} d|z|z}d|z| z} ||| z z| | | z zz} | dkrd}n| } d}t|| | t | ��||��S)NrrOr)r�rkr�r}r)r�r\rnr[r�r�r�r�r�r�r�r��baser'r&s r*�mpf_modr4�s�����E�4��s���E�4��s� ��t��d����� � ��~�~�$��c��/�/��� �q�y�y�T�D��H�_�_�� � �t�T�?�?�D� ��;�� �D� ��;�� �D� �D��I� �4�D��I�#6� 7�C� �a�x�x�����d���� �T�3��h�s�m�m�T�3� ?� ?�?r,c��|\}}}}|sl|rj|tkr|dkr|S|dkrtStS|tkr1|dkrttg|dzS|dkrtStStSt |��}|dkrt S|dkrt |||��S|dkrh|\}}}}|stS||z}|dkrdt||zdfS||zdz }|tt ||z ��z }td|||z|||��S|dkrtt |||��S|dkr8t|| |dzt|��} tt | ||��S||z} |dkr| t||zdfS||zdkr)||z}t| |||zt|��||��S|tkpt|| } |dt|��zzdz} t \}} }} |dzr_| |z} ||z}||dz z }|tt | |z ��z}|| kr| r | || z z } n | || z z } ||| z z }| }|dz}|sn]||z}||z}||zdz }|tt ||z ��z}|| kr| r ||| z z }n | || z z }||| z z }| }|dz}��t!| | ||||��S)z=Compute s**n, where s is a raw mpf and n is a Python integer.rrr.rOr�i�r)r�r�rkr�rCr�r�r rr�r�� mpf_pow_int�reciprocal_rndrrTrlr})r�r:rnr[r&r'r(r)re�inverse� result_sign� rounds_down�workprec�pm�pe�pbcs r*r6r6s�����D�#�s�B� � �S� � ��9�9��1�u�u�Q�h��A�v�v�d�{��L� ��:�:��1�u�u�d�E�]�1�q�5�1�1��A�v�v�d�{��L�� � �A���A��A�v�v�d�{��A�v�v�g�a��s�+�+�+��A�v�v����3��R�� ��L��#�g�� �!�8�8��w��C���+� +� �"�W�q�[�� �g�c�#�r�'�l�l�#�#���!�S�#�c�'�2�t�S�9�9�9��B�w�w�w�t�Q��c�2�2�2��1�u�u��a�!��T�!�V�^�C�-@�A�A���t�W�d�C�0�0�0���(�K� �a�x�x��W�c�!�e�Q�/�/� �!�t�d�{�{� �� ���+�s�C��E�8�C�=�=�$��L�L�L��-�'�&��C���%�� �a��� � �m�#�a�'�H��N�A�r�2�s�� �q�5� ��C��B��C��B� �2��6�M�C����B�#�I���/�/�C��X�~�~��4���H� �-�B�B��C�S��\�2�3�B��c�H�n�$���� ��F�A�� ���#�g���#�g�� �"�W�q�[�� �'�#�c�R�i�.�.�)� )�� ��=�=�� 1��b��k�*�����2�h�;�/�0�� �2��=� �C��B� ��F��7�: �[�"�b�#�t�S� 9� 9�9r,c� �|tkrt|||��S|\}}}}|t||z|z dz df}|r|ttfv|z } n|t tfv|z } | rt ||||��St|||��S)a For nonzero x, calculate x + eps with directed rounding, where eps < prec relatively and eps has the given sign (0 for positive, 1 for negative). With rounding to nearest, this is taken to simply normalize x to the given precision. r)rTr�r rYrXrZr ) r%�eps_signrnr[r&r'r(r)�eps�aways r*� mpf_perturbrCfs��� �m����q�$��$�$�$���D�#�s�B� �W�c�"�f�T�k�!�m�Q� /�C� �=�� �M�2�2�h�>�����-�0�0�H�<�� �%��q�#�t�S�)�)�)��q�$��$�$�$r,c�@�|drd}t|��}nd}|\}}}}|sdSt|tjdd��z��dz}||z}t |��dkr�dd lm} m} tt |����d z} t|��} t| | | ����} t| | | ��| ��} t| ��} t|tt| |��|��}|\}}}}| }nd}t||z |z d��}t|tjdd��z d z��}t!||��}t#||d|��}t%|d|� ��}|t'|��|z dz z }|||fS) a0Helper function for representing the floating-point number s as a decimal with dps digits. Returns (sign, string, exponent) where sign is '' or '-', string is the digit string, and exponent is the decimal exponent as an int. If inexact, the decimal representation is rounded toward zero.r�-�)rF�0r� r.i� r)�mpf_ln2�mpf_ln10r�g�?)r3�size)rrCr��logr � libelefunrIrJrr�rr�r�r6�ftenrBr�rrr�)r�rMr&�_signr'r(r)�bitprec� exp_from_1rIrJ�expprec�tmpr��exponent�fixprec�fixdps�sf�sd�digitss r*� to_digits_exprZ�s��� ��t���� �A�J�J�������E�3��R� ���z��#����A���&�'�'�"�,�G��r��J� �:������0�0�0�0�0�0�0�0��3�s�8�8�$�$�q�(���s�m�m���c�7�7�7�+�+�,�,���c�8�8�G�,�,�g�6�6�� �3�K�K�� �A�{�4��G�4�4�g� >� >�����s�C������� �'�C�-�"�$�a�(�(�G� ��4�8�B�q�>�>�)�C�/� 0� 0�F� �!�W� � �B� �b�'�2�v� .� .�B� �R�b�s� +� +� +�F� ��F� � �f�$�q�(�(�H� ��� !�!r,Tc�D�|dsI|tkr|rd}nd}|r|dz }|S|tkrdS|tkrdS|tkrdSt�|�t |d z d ��}|�|}t ||d z��\}}} |s|d d vr| dz } d}�n=t|��|kr�||d vr�|d|�}|dz } | d kr#|| d kr| dz} | d kr || d k�| d kr=|d| �tt|| ��dz��zd|| z dz zz}ndd|dz zz}| dz } n |d|�}|| cxkr|kr;nn8| d krdt| ��z|z}d} n| dz} | |kr |d| |z zz }d } nd} |d| �dz|| d�z}|r&|� d��}|ddkr|dz }| d kr |r|s||zS| d kr||zdzt| ��zS| d kr||zdzt| ��zSdS)a{ Convert a raw mpf to a decimal floating-point literal with at most `dps` decimal digits in the mantissa (not counting extra zeros that may be inserted for visual purposes). The number will be printed in fixed-point format if the position of the leading digit is strictly between min_fixed (default = min(-dps/3,-5)) and max_fixed (default = dps). To force fixed-point format always, set min_fixed = -inf, max_fixed = +inf. To force floating-point format, set min_fixed >= max_fixed. The literal is formatted so that it can be parsed back to a number by to_str, float() or Decimal(). rz0.0z.0ze+0�+inf�-infr�NrL�����r�56789�9rG�1�.rOze+r�) rkr�r�r�r�r�rZr�r�rC�rstrip) r�rM� strip_zeros� min_fixed� max_fixed�show_zero_exponentr\r&rYrT�i�splits r*�to_strrj�s���( �Q�4� � ��:�:�� ��A�A��A�!� ��U� ���H� ��9�9�V�V� ��:�:�f�f� ��9�9�U�U�����c�C��F�)�R�&8�&8�)���c�)�+�1�c�!�e�4�4��D�&�(� �'� �!�9�� � � ��M�H���� �v�;�;�� � �����!7�!7��D�S�D�\�F��a��A��q�&�&�V�A�Y�#�-�-��Q����q�&�&�V�A�Y�#�-�-��A�v�v������c�#�f�Q�i�.�.�1�*<�&=�&=�=��s�Q�w�QR�{�@S�S����s�c�A�g��.���A� ����D�S�D�\�F� �x� +� +� +� +�)� +� +� +� +� +��!�|�|��c�8�)�n�n�,��6����� �1� ���3�;�;��c�5��9�o�-�F��H�H��E���%��.�3�&������7�� � ��]�]�3�'�'�F��b�z�S� � ��#� ���1�}�}��}�%7�}��v� �9M��1�}�}�T�F�]�T�1�C��M�M�A�A��!�|�|�D�6�M�C�/�#�h�-�-�?�?�|�|r,rHc��|����d��}t|��|�d��}t |��dkrd}n|d}t |d��}|�d��}t |��dkr:|d|d�d��}}|t |��z}||z}t t ||����}||fS)zHelper function for from_str.�lr�rrrbr.rG)�lowerrcr�rir�rCr)r%r3�partsr(r�r�s r*�str_to_man_expro s��� ��� � ������A� �!�H�H�H� �G�G�C�L�L�E� �5�z�z�Q������ �!�H���%��(�m�m�� �G�G�C�L�L�E� �5�z�z�Q����Q�x��q�����-�-�1�� �s�1�v�v� �� ��E�� �C��4�L�L���A� �c�6�Mr,)r�r\r]r�c ��|������}|tvr t|Sd|vrn|�d��\}}|�d��|�d��}}t t |��t |��||��St|d���\}}t|��dkr=t||dz��}t|tt||dz��||��}n4|dkrt|d|zz||��}nt |d| z||��}|S)aVCreate a raw mpf from a decimal literal, rounding in the specified direction if the input number cannot be represented exactly as a binary floating-point number with the given number of bits. The literal syntax accepted is the same as for Python floats. TODO: the rounding does not work properly for large exponents. �/rlrH�r3i�r) rm�strip� special_strrircr�rCror r�rr6rN)r%rnr[r�r�r'r(r�s r*r�r�!s*�� ��� � �����A��K����1�~�� �a�x�x��w�w�s�|�|���1��x�x��}�}�a�h�h�s�m�m�1���S��V�V�S��V�V�T�3�7�7�7��a�b�)�)�)�H�C�� �3�x�x�#�~�~� �S�$�r�'� "� "�� �A�{�4��d�2�g�6�6��c� B� B��� �!�8�8���r�3�w���c�2�2�A�A��c�2��t�8�T�3�7�7�A� �Hr,c��t|d���\}}t|��}d}|dkr| }d}t|��}t|||||t��S)Nr.rrrr)rorrr}rW)r%r'r(r&r)s r*� from_bstrrvDs`���a�a�(�(�(�H�C�� �c�(�(�C� �D� �Q�w�w��d���� �#���B� �T�3��R��[� 9� 9�9r,c�p�|\}}}}ddg|t|t|��d���zd|zzS)NrFrEr.)rKr3ze%i)rrr$s r*�to_bstrrxNsB����D�#�s�B� �s�8�D�>�G�C�h�s�m�m�!�D�D�D� D��PS� � T�Tr,c��|\}}}}|rtd���|s|S|dzr|dz}|dz}|dz }n|dkrt|||dz|||��Stdd|z|z dz��}||dzz }|dvrt||z��}n$t ||z��\}}|r |dzdz}|dz }t |||z dz||��S)zb Compute the square root of a nonnegative mpf value. The result is correctly rounded. z square root of a negative numberrr.r�fd)r3r�rBrrr~) r�rnr[r&r'r(r)�shiftr/s r*�mpf_sqrtr|Xs�� ��D�#�s�B� �@��>�?�?�?� ���� �Q�w�<� �q��� �� �� �a���� �����$��S�!�V�R��s�;�;�;� ��1�T�6�"�9�Q�;� � �E� �U�Q�Y��E� �d�{�{��C��J������3��:�&�&���S� � ���6�1�*�C� �Q�J�E� ��c�%�i�!�^�T�3� 7� 7�7r,c���|tkrt|||��S|tkrt|||��Stt||��t||��|dz��}t |||��S)zICompute the Euclidean norm sqrt(x**2 + y**2) of two raw mpfs x and y.r)rkrr rr|)r%r�rnr[�hypot2s r*� mpf_hypotrtso�� �E�z�z�'�!�T�3�/�/�/��E�z�z�'�!�T�3�/�/�/� �W�Q�q�\�\�7�1�Q�<�<��a�� 8� 8�F� �F�D�#� &� &�&r,r!)TNNF)rH)��__doc__� __docformat__r�rr�r��backendrrrr r r r r rrrrr� libintmathrrrrrrrrrrrrrr+r1r�r3�intern� NameErrorrTrWrXrZrY� round_fastrFrHrNrk�fnzeror�r��ftworN�fhalfr�r�r�r�rVr^�range� h_mask_smallrUrlrorqrC�longrurwry�dirrzr}r�r~�dictr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr�r r�rrr!r#r%r� mpf_mul_intr'r)r�r1r4r7� negative_rndr6rCrZrjrortr�rvrxr|r�sage.libs.mpmath.ext_libmp�libs�mpmath� ext_libmp�ext_lib� ImportErrorr7r,r*�<module>r�sw ����� � � � � ������� � � � �� �F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F������������������������������� �f���'�'�'�'�+�+�+�)�)�)� � � � � �J� � � �� �F�F������ �[�F�F�F�������s� � � ��f�S�k�k� ���s� � � � �6�#�;�;�� �V�C�[�[� � � �;�;�;� =�=�=� ��� �H�a���� �X�q�!� �� �7�A�q��� �G�Q���� �7�A�q��� �8�Q���� �G�R���� �8�T�2��� �8�T�2��� �H�d�B��������2"�"�"�"�"�"�"�"��s�@�@�%�%��3�-�-�@�@�@�@� � �*�,�,� � %�� �5�-���u�h�u�&� �5�5�5�n'�'�'�R��t��J�J��������J�J�J�����5�5�5�6�6�6� �f���,���D� � �9�9��'�J��(�K� �f���)�3�3�J�� �� �I�"�J�J��I��J� !%�*�4�4�4�4�> �D�B�B�%�%��S�/�/�B�B�B� B� B� � �f���)�S�S��Y�Y�6�6��&�L� �f����*�L��J�)�)�)�)����)�)�)�)�,)�)�)�,�Z� � � � � �J� � � � � �J� � � � � �J�/�/�/�/��z�9�9�9�9�&�*� � � � ��:�'�'�'�'��*�#�#�#�#�J#-�8�8�8�8� � � � 0� 0� 0�&E�E�E�������:2 �2 �2 �h��� ��� ��� ��� ����:� � � � ��j� 7� 7� 7� 7��j� 2� 2� 2� 2�����j�q�] �] �] �] �~�j�'�'�'�'� �J��.-�.-�.-�.-�`�:�9�9�9�9�*&0� ?� ?� ?� ?� �Z�9�9�9�9�,(2�4�4�4�4�( �f����G�"�K�K��G�$�K� � � �)�)�)�'�#M�#M�#M�#M�J",�H�H�H�H� '�@�@�@�@�0 �x� �Z� � ��+��-� �� �z� �X� � ��+��-� � �!+�Q:�Q:�Q:�Q:�h%�%�%�62"�2"�2"�h@D��S@�S@�S@�S@�j����*�$�u�D�A�A� �$� � � � �F:�:�:�U�U�U�%�8�8�8�8�8)�'�'�'�'� �f��� �4�4�4�4�4�4�4�4�4�4�4�4��/���/���/���/���#����� � � � � ��� �����s6�0A3�3A>�=A>�E � E�E�/L2�2L;�:L;
Memory