� H�g,����dZdZddlZddlZddlmZddlmZmZddl m Z ddl m Z m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^mZdd l m_Z_dd l m`Z`eajbZcejdd ��Zeed krdd lfmgZhddlfmicmjcmkZln ddlmmnZhddl mmZlddlmmoZompZpmqZqGd�dehe��ZrGd�d��ZsetdkrddluZueujv��dSdS)z[ This module defines the mpf, mpc classes, and standard functions for operating with them. � plaintext�N�)�StandardBaseContext)� basestring�BACKEND)�libmp)U�MPZ�MPZ_ZERO�MPZ_ONE� int_types�repr_dps� round_floor� round_ceiling� dps_to_prec� round_nearest� prec_to_dps� ComplexResult� to_pickable� from_pickable� normalize�from_int� from_float�from_str�to_int�to_float�to_str� from_rational� from_man_exp�fone�fzero�finf�fninf�fnan�mpf_abs�mpf_pos�mpf_neg�mpf_add�mpf_sub�mpf_mul� mpf_mul_int�mpf_div� mpf_rdiv_int� mpf_pow_int�mpf_mod�mpf_eq�mpf_cmp�mpf_lt�mpf_gt�mpf_le�mpf_ge�mpf_hash�mpf_rand�mpf_sum�bitcount�to_fixed� mpc_to_str�mpc_to_complex�mpc_hash�mpc_pos�mpc_is_nonzero�mpc_neg� mpc_conjugate�mpc_abs�mpc_add� mpc_add_mpf�mpc_sub� mpc_sub_mpf�mpc_mul� mpc_mul_mpf� mpc_mul_int�mpc_div� mpc_div_mpf�mpc_pow� mpc_pow_mpf� mpc_pow_int� mpc_mpf_div�mpf_pow�mpf_pi� mpf_degree�mpf_e�mpf_phi�mpf_ln2�mpf_ln10� mpf_euler� mpf_catalan� mpf_apery� mpf_khinchin� mpf_glaisher� mpf_twinprime� mpf_mertensr )� function_docs)�rationalz\^\(?(?P<re>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?)??(?P<im>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?j)?\)?$�sage)�Context)�PythonMPContext)� ctx_mp_python)�_mpf�_mpc� mpnumericc�z�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z d�Z d �Z d<d �Z d �Z d �Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zed���Zed���Zd=d�Zd=d�Zd=d�Zd=d�Z d>d"�Z!d?d$�Z"d%�Z#d&�Z$d'�Z%d(Z&d)Z'd@d+�Z(d,�Z)d-�Z*d.�Z+d/�Z,d0�Z-d1�Z.d2�Z/d3�Z0d4�Z1d5�Z2d6�Z3d7�Z4d8�Z5d9�Z6 d:gdfd;�Z7d S)A� MPContextzH Context for multiprecision arithmetic with a global precision. c���tj|��d|_d|_|j|j|jg|_tj |_ |� ��tj|��tj |_ |� ��i|_|��� t j|jj_t j|jj_t j|jj_t j|jj_n|#t.$rot j|jj_t j|jj_t j|jj_t j|jj_YnwxYwt j|j_t j|j_t j|j_dS�NF)� BaseMPContext�__init__� trap_complex�pretty�mpf�mpc�constant�typesr^�mpq�_mpq�defaultr� init_builtins� hyp_summators� _init_aliasesr]� bernoulli�im_func�func_doc�primepi�psi�atan2�AttributeError�__func__�digamma�cospi�sinpi��ctxs �]/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/mpmath/ctx_mp.pyrkzMPContext.__init__?si����s�#�#�#� ����� ��W�c�g�s�|�4�� ��<��� � � � � � ��$�S�)�)�)��,��� ��������� ������ >�-:�-D�C�M� !� *�+8�+@�C�K� � (�'4�'8�C�G�O� $�)6�)<�C�I� � &� &��� >� >� >�.;�.E�C�M� "� +�,9�,A�C�K� � )�(5�(9�C�G� � %�*7�*=�C�I� � '� '� '�  >���� -�4�� ��*�0�� ��*�0�� ���s�6A,D#�#A6F�Fc��|j}|j}|�t��|_|�t ��|_|�t tf��|_|�t��|_ |�t��|_ |�t��|_|�d�dd��}||_|�t"dd��|_|�t&dd��|_|�t*dd��|_|�t.d d ��|_|�t2d d ��|_|�t6d d��|_|�t:dd��|_|�t>dd��|_ |�tBdd��|_"|�tFdd��|_$|�tJdd��|_&|�tNdd��|_(|�tRdd��|_*|�+tXj-tXj.��|_/|�+tXj0tXj1��|_2|�+tXj3tXj4��|_5|�+tXj6tXj7��|_8|�+tXj9tXj:��|_;|�+tXj<tXj=��|_>|�+tXj?tXj@��|_A|�+tXjBtXjC��|_D|�+tXjEtXjF��|_G|�+tXjHtXjI��|_J|�+tXjKtXjL��|_M|�+tXjNtXjO��|_P|�+tXjQtXjR��|_S|�+tXjTtXjU��|_V|�+tXjWtXjX��|_Y|�+tXj6tXj7��|_8|�+tXjZtXj[��|_\|�+tXj]tXj^��|__|�+tXj`tXja��|_b|�+tXjctXjd��|_e|�+tXjftXjg��|_h|�+tXjitXjj��|_k|�+tXjltXjm��|_n|�+tXjotXjp��|_q|�+tXjrtXjs��|_t|�+tXjutXjv��x|_w|_x|�+tXjytXjz��|_{|�+tXj|tXj}��|_~|�+tXjtXj���|_�|�+tXj�tXj���x|_�|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_�|�+tXj�d��|_�|�+tXj�d��|_�|�+tXj�tXj���|_�|�+tXj�tXj���|_��tW|d|j/��|_/�tW|d|j;��|_;�tW|d|j5��|_5�tW|d |jG��|_G�tW|d!|jD��|_DdS)"Nc��dtd|z dfS)Nrr)r )�prec�rnds r��<lambda>z)MPContext.init_builtins.<locals>.<lambda>ms��a��!�D�&�!�-D��zepsilon of working precision�eps�pizln(2)�ln2zln(10)�ln10zGolden ratio phi�phiz e = exp(1)�ezEuler's constant�eulerzCatalan's constant�catalanzKhinchin's constant�khinchinzGlaisher's constant�glaisherzApery's constant�aperyz1 deg = pi / 180�degreezTwin prime constant� twinprimezMertens' constant�mertens� _sage_sqrt� _sage_exp�_sage_ln� _sage_cos� _sage_sin)�rnro�make_mpfr�oner �zero�make_mpc�jr!�infr"�ninfr#�nanrpr�rPr�rTr�rUr�rSr�rRr�rVr�rWr�rYr�rZr�rXr�rQr�r[r�r\r��_wrap_libmp_functionr�mpf_sqrt�mpc_sqrt�sqrt�mpf_cbrt�mpc_cbrt�cbrt�mpf_log�mpc_log�ln�mpf_atan�mpc_atan�atan�mpf_exp�mpc_exp�exp�mpf_expj�mpc_expj�expj� mpf_expjpi� mpc_expjpi�expjpi�mpf_sin�mpc_sin�sin�mpf_cos�mpc_cos�cos�mpf_tan�mpc_tan�tan�mpf_sinh�mpc_sinh�sinh�mpf_cosh�mpc_cosh�cosh�mpf_tanh�mpc_tanh�tanh�mpf_asin�mpc_asin�asin�mpf_acos�mpc_acos�acos� mpf_asinh� mpc_asinh�asinh� mpf_acosh� mpc_acosh�acosh� mpf_atanh� mpc_atanh�atanh� mpf_sin_pi� mpc_sin_pir�� mpf_cos_pi� mpc_cos_pir�� mpf_floor� mpc_floor�floor�mpf_ceil�mpc_ceil�ceil�mpf_nint�mpc_nint�nint�mpf_frac�mpc_frac�frac� mpf_fibonacci� mpc_fibonacci�fib� fibonacci� mpf_gamma� mpc_gamma�gamma� mpf_rgamma� mpc_rgamma�rgamma� mpf_loggamma� mpc_loggamma�loggamma� mpf_factorial� mpc_factorial�fac� factorial�mpf_psi0�mpc_psi0r�� mpf_harmonic� mpc_harmonic�harmonic�mpf_ei�mpc_ei�ei�mpf_e1�mpc_e1�e1�mpf_ci�mpc_ci�_ci�mpf_si�mpc_si�_si� mpf_ellipk� mpc_ellipk�ellipk� mpf_ellipe� mpc_ellipe�_ellipe�mpf_agm1�mpc_agm1�agm1�mpf_erf�_erf�mpf_erfc�_erfc�mpf_zeta�mpc_zeta�_zeta� mpf_altzeta� mpc_altzeta�_altzeta�getattr)r�rnror�s r�ruzMPContext.init_builtins`s����g���g���,�,�t�$�$����<�<��&�&���� � �e�D�\�*�*����,�,�t�$�$����<�<��&�&����,�,�t�$�$����l�l�D�D� *�E�3�3��������f�d�D�1�1����,�,�w���7�7����<�<��(�F�;�;����,�,�w�(:�E�B�B���� � �U�L�#�6�6����L�L��,>��H�H�� ��l�l�;�0D�i�P�P�� ��|�|�L�2G��T�T�� ��|�|�L�2G��T�T�� ��L�L��,>��H�H�� ��\�\�*�.@�(�K�K�� �� � �]�4I�;�W�W�� ��l�l�;�0C�Y�O�O�� ��+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����)�)�%�-���G�G����+�+�E�N�E�N�K�K����*�*�5�=�%�-�H�H����+�+�E�N�E�N�K�K����-�-�e�.>��@P�Q�Q�� ��*�*�5�=�%�-�H�H����*�*�5�=�%�-�H�H����*�*�5�=�%�-�H�H����+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����,�,�U�_�e�o�N�N�� ��,�,�U�_�e�o�N�N�� ��,�,�U�_�e�o�N�N�� ��,�,�U�-=�u�?O�P�P�� ��,�,�U�-=�u�?O�P�P�� ��,�,�U�_�e�o�N�N�� ��+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K����+�+�E�N�E�N�K�K���"%�":�":�5�;N�PU�Pc�"d�"d�d���#�-��,�,�U�_�e�o�N�N�� ��-�-�e�.>��@P�Q�Q�� ��/�/��0B�E�DV�W�W�� �"%�":�":�5�;N�PU�Pc�"d�"d�d���#�-��.�.�u�~�u�~�N�N�� ��/�/��0B�E�DV�W�W�� ��)�)�%�,�� �E�E����)�)�%�,�� �E�E����*�*�5�<���F�F����*�*�5�<���F�F����-�-�e�.>��@P�Q�Q�� ��.�.�u�/?��AQ�R�R�� ��+�+�E�N�E�N�K�K����+�+�E�M�4�@�@����,�,�U�^�T�B�B�� ��,�,�U�^�U�^�L�L�� ��/�/��0A�5�CT�U�U�� ��3� �c�h�7�7����#�{�C�G�4�4�����j�#�&�1�1����#�{�C�G�4�4����#�{�C�G�4�4����r�c�,�|�|��S�N)r9)r��xr�s r�r9zMPContext.to_fixed�s���z�z�$���r�c���|�|��}|�|��}|�tj|j|jg|j�R���S)z� Computes the Euclidean norm of the vector `(x, y)`, equal to `\sqrt{x^2 + y^2}`. Both `x` and `y` must be real.)�convertr�r� mpf_hypot�_mpf_�_prec_rounding)r�r&�ys r��hypotzMPContext.hypot�sO�� �K�K��N�N�� �K�K��N�N���|�|�E�O�A�G�Q�W�R�s�?Q�R�R�R�S�S�Sr�c�\�t|�|����}|dkr|�|��St|d��st�|j\}}t j||j||d���\}}|�|� |��S|� ||f��S)Nrr*T)r�) �int�_rer �hasattr�NotImplementedErrorr+r� mpf_expintr*r�r��r��n�zr��rounding�real�imags r��_gamma_upper_intzMPContext._gamma_upper_int�s��� ����� � �O�O�� ��6�6��6�6�!�9�9� ��q�'�"�"� &�%� %��+���h��%�a���$���M�M�M� ��d� �<��<�<��%�%� %��<�<��t� �-�-� -r�c�2�t|��}|dkr|�|��St|d��st�|j\}}t j||j||��\}}|�|�|��S|� ||f��S)Nrr*) r/r r1r2r+rr3r*r�r�r4s r�� _expint_intzMPContext._expint_int�s��� ��F�F�� ��6�6��6�6�!�9�9� ��q�'�"�"� &�%� %��+���h��%�a���$��A�A� ��d� �<��<�<��%�%� %��<�<��t� �-�-� -r�c�>�t|d��r\ |�tj|j|g|j�R���S#t $r|jr�|jtjf}Yn wxYw|j }|� tj ||g|j�R���S�Nr*) r1r�r� mpf_nthrootr*r+rrlr �_mpc_r�� mpc_nthroot�r�r&r5s r��_nthrootzMPContext._nthroot�s��� �1�g� � � � +��|�|�E�$5�a�g�q�$V�3�CU�$V�$V�$V�W�W�W�� � +� +� +��#����W�e�k�*���� +���� ��A��|�|�E�-�a��H�S�5G�H�H�H�I�I�Is�/A�%A*�)A*c��|j\}}t|d��r/|�tj||j||����St|d��r/|�tj||j||����SdS�Nr*r@) r+r1r�r� mpf_besseljnr*r�� mpc_besseljnr@)r�r5r6r�r7s r��_besseljzMPContext._besselj�s����+���h� �1�g� � � P��<�<�� 2�1�a�g�t�X� N� N�O�O� O� �Q�� � � P��<�<�� 2�1�a�g�t�X� N� N�O�O� O� P� Pr�rc���|j\}}t|d��rWt|d��rG tj|j|j||��}|�|��S#t $rYnwxYwt|d��r|jtjf}n|j}t|d��r|jtjf}n|j}|� tj ||||����Sr>) r+r1r�mpf_agmr*r�rr r@r��mpc_agm)r��a�br�r7�vs r��_agmzMPContext._agm�s����+���h� �1�g� � � �7�1�g�#6�#6� � ��M�!�'�1�7�D�(�C�C���|�|�A���&�� � � � ��� ���� �1�g� � � �Q�W�e�k�$:����'�a� �1�g� � � �Q�W�e�k�$:����'�a��|�|�E�M�!�Q��h�?�?�@�@�@s�5A"�" A/�.A/c�p�|�tjt|��g|j�R���Sr%)r�r� mpf_bernoullir/r+�r�r5s r�rxzMPContext.bernoulli�s0���|�|�E�/��A���L��9K�L�L�L�M�M�Mr�c�p�|�tjt|��g|j�R���Sr%)r�r� mpf_zeta_intr/r+rRs r�� _zeta_intzMPContext._zeta_int�s0���|�|�E�.�s�1�v�v�K��8J�K�K�K�L�L�Lr�c���|�|��}|�|��}|�tj|j|jg|j�R���Sr%)r(r�r� mpf_atan2r*r+)r�r,r&s r�r}zMPContext.atan2sM�� �K�K��N�N�� �K�K��N�N���|�|�E�O�A�G�Q�W�R�s�?Q�R�R�R�S�S�Sr�c�4�|�|��}t|��}|�|��r0|�t j||jg|j�R���S|�t j ||j g|j�R���Sr%) r(r/� _is_real_typer�r�mpf_psir*r+r��mpc_psir@)r��mr6s r�r|z MPContext.psis��� �K�K��N�N�� ��F�F�� � � �Q� � � P��<�<�� �a��� N�3�;M� N� N� N�O�O� O��<�<�� �a��� N�3�;M� N� N� N�O�O� Or�c � �t|��|jvr|�|��}|�|��\}}t |d��rHt j|j||��\}}|�|��|�|��fSt |d��rHt j |j ||��\}}|� |��|� |��fS|j |fi|��|j |fi|��fSrE)�typerqr(� _parse_precr1r� mpf_cos_sinr*r�� mpc_cos_sinr@r�r�r��r�r&�kwargsr�r7�c�ss r��cos_sinzMPContext.cos_sins�� ��7�7�#�)� #� #�� � �A���A�����0�0���h� �1�g� � � >��$�Q�W�d�H�=�=�D�A�q��<�<��?�?�C�L�L��O�O�3� 3� �Q�� � � >��$�Q�W�d�H�=�=�D�A�q��<�<��?�?�C�L�L��O�O�3� 3��3�7�1�'�'��'�'�����)=�)=�f�)=�)=�=� =r�c � �t|��|jvr|�|��}|�|��\}}t |d��rHt j|j||��\}}|�|��|�|��fSt |d��rHt j |j ||��\}}|� |��|� |��fS|j |fi|��|j |fi|��fSrE)r^rqr(r_r1r�mpf_cos_sin_pir*r��mpc_cos_sin_pir@r�r�r�rbs r�� cospi_sinpizMPContext.cospi_sinpis�� ��7�7�#�)� #� #�� � �A���A�����0�0���h� �1�g� � � >��'����x�@�@�D�A�q��<�<��?�?�C�L�L��O�O�3� 3� �Q�� � � >��'����x�@�@�D�A�q��<�<��?�?�C�L�L��O�O�3� 3��3�7�1�'�'��'�'�����)=�)=�f�)=�)=�=� =r�c�F�|���}|j|_|S)zP Create a copy of the context, with the same working precision. )� __class__r�)r�rLs r��clonezMPContext.clone)s�� �M�M�O�O�������r�c�V�t|d��st|��turdSdS)Nr@FT�r1r^�complex�r�r&s r�rYzMPContext._is_real_type4s.�� �1�g� � � �$�q�'�'�W�"4�"4��5��tr�c�V�t|d��st|��turdSdS)Nr@TFrorqs r��_is_complex_typezMPContext._is_complex_type9s.�� �1�g� � � �$�q�'�'�W�"4�"4��4��ur�c��t|d��r|jtkSt|d��rt|jvSt |t ��st |t j��rdS|�|��}t|d��st|d��r|� |��Std���)a� Return *True* if *x* is a NaN (not-a-number), or for a complex number, whether either the real or complex part is NaN; otherwise return *False*:: >>> from mpmath import * >>> isnan(3.14) False >>> isnan(nan) True >>> isnan(mpc(3.14,2.72)) False >>> isnan(mpc(3.14,nan)) True r*r@Fzisnan() needs a number as input) r1r*r#r@� isinstancer r^rrr(�isnan� TypeErrorrqs r�rvzMPContext.isnan>s���" �1�g� � � #��7�d�?� "� �1�g� � � #��1�7�?� "� �a�� #� #� �z�!�X�\�'B�'B� ��5� �K�K��N�N�� �1�g� � � �'�!�W�"5�"5� ��9�9�Q�<�<� ��9�:�:�:r�c�^�|�|��s|�|��rdSdS)a� Return *True* if *x* is a finite number, i.e. neither an infinity or a NaN. >>> from mpmath import * >>> isfinite(inf) False >>> isfinite(-inf) False >>> isfinite(3) True >>> isfinite(nan) False >>> isfinite(3+4j) True >>> isfinite(mpc(3,inf)) False >>> isfinite(mpc(nan,3)) False FT)�isinfrvrqs r��isfinitezMPContext.isfiniteZs1��, �9�9�Q�<�<� �3�9�9�Q�<�<� ��5��tr�c��|sdSt|d��r|j\}}}}|o|dkSt|d��r"|j o|�|j��St |��t vr|dkSt||j��r|j \}}|sdS|dko|dkS|�|� |����S)z< Determine if *x* is a nonpositive integer. Tr*rr@r) r1r*r9�isnpintr8r^r rurr�_mpq_r()r�r&�sign�manr��bc�p�qs r�r|zMPContext.isnpintts���� ��4� �1�g� � � %�!"�� �D�#�s�B��$�C�1�H� $� �1�g� � � 6��v�:�5�#�+�+�a�f�"5�"5� 5� ��7�7�i� � ���6�M� �a��� !� !� %��7�D�A�q�� ��t���6�$�a�1�f� $��{�{�3�;�;�q�>�>�*�*�*r�c���dd|jz�d��dzd|jz�d��dzd|jz�d��dzg}d �|��S) NzMpmath settings:z mp.prec = %s�z [default: 53]z mp.dps = %sz [default: 15]z mp.trap_complex = %sz[default: False]� )r��ljust�dpsrl�join)r��liness r��__str__zMPContext.__str__�sy��#� ��� (� /� /�� 3� 3�o� E� �s�w� &� -� -�b� 1� 1�O� C� %��(8� 8� ?� ?�� C� C�FX� X� �� �y�y����r�c�*�t|j��Sr%)r �_precr�s r�� _repr_digitszMPContext._repr_digits�s���� �"�"�"r�c��|jSr%)�_dpsr�s r�� _str_digitszMPContext._str_digits�s ���x�r�Fc�.��t|�fd�d|��S)a� The block with extraprec(n): <code> increases the precision n bits, executes <code>, and then restores the precision. extraprec(n)(f) returns a decorated version of the function f that increases the working precision by n bits before execution, and restores the parent precision afterwards. With normalize_output=True, it rounds the return value to the parent precision. c���|�zSr%��r�r5s �r�r�z%MPContext.extraprec.<locals>.<lambda>�s ���q�1�u�r�N��PrecisionManager�r�r5�normalize_outputs ` r�� extrapreczMPContext.extraprec�s ���  ��_�_�_�_�d�<L�M�M�Mr�c�.��t|d�fd�|��S)z� This function is analogous to extraprec (see documentation) but changes the decimal precision instead of the number of bits. Nc���|�zSr%r���dr5s �r�r�z$MPContext.extradps.<locals>.<lambda>�s ���Q��U�r�r�r�s ` r��extradpszMPContext.extradps�s ���  ��T�?�?�?�?�<L�M�M�Mr�c�.��t|�fd�d|��S)a� The block with workprec(n): <code> sets the precision to n bits, executes <code>, and then restores the precision. workprec(n)(f) returns a decorated version of the function f that sets the precision to n bits before execution, and restores the precision afterwards. With normalize_output=True, it rounds the return value to the parent precision. c����Sr%r�r�s �r�r�z$MPContext.workprec.<locals>.<lambda>�s���q�r�Nr�r�s ` r��workpreczMPContext.workprec�s ��� ��[�[�[�[�$�8H�I�I�Ir�c�.��t|d�fd�|��S)z� This function is analogous to workprec (see documentation) but changes the decimal precision instead of the number of bits. Nc����Sr%r�r�s �r�r�z#MPContext.workdps.<locals>.<lambda>�s���Q�r�r�r�s ` r��workdpszMPContext.workdps�s ���  ��T�;�;�;�;�8H�I�I�Ir�Nr�c�"�����������fd�}|S)a� Return a wrapped copy of *f* that repeatedly evaluates *f* with increasing precision until the result converges to the full precision used at the point of the call. This heuristically protects against rounding errors, at the cost of roughly a 2x slowdown compared to manually setting the optimal precision. This method can, however, easily be fooled if the results from *f* depend "discontinuously" on the precision, for instance if catastrophic cancellation can occur. Therefore, :func:`~mpmath.autoprec` should be used judiciously. **Examples** Many functions are sensitive to perturbations of the input arguments. If the arguments are decimal numbers, they may have to be converted to binary at a much higher precision. If the amount of required extra precision is unknown, :func:`~mpmath.autoprec` is convenient:: >>> from mpmath import * >>> mp.dps = 15 >>> mp.pretty = True >>> besselj(5, 125 * 10**28) # Exact input -8.03284785591801e-17 >>> besselj(5, '1.25e30') # Bad 7.12954868316652e-16 >>> autoprec(besselj)(5, '1.25e30') # Good -8.03284785591801e-17 The following fails to converge because `\sin(\pi) = 0` whereas all finite-precision approximations of `\pi` give nonzero values:: >>> autoprec(sin)(pi) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NoConvergence: autoprec: prec increased to 2910 without convergence As the following example shows, :func:`~mpmath.autoprec` can protect against cancellation, but is fooled by too severe cancellation:: >>> x = 1e-10 >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x) 1.00000008274037e-10 1.00000000005e-10 1.00000000005e-10 >>> x = 1e-50 >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x) 0.0 1.0e-50 0.0 With *catch*, an exception or list of exceptions to intercept may be specified. The raised exception is interpreted as signaling insufficient precision. This permits, for example, evaluating a function where a too low precision results in a division by zero:: >>> f = lambda x: 1/(exp(x)-1) >>> f(1e-30) Traceback (most recent call last): ... ZeroDivisionError >>> autoprec(f, catch=ZeroDivisionError)(1e-30) 1.0e+30 c�P��� j}� �� �|��}n� } |dz� _ � |i|��}n#�$r � j}YnwxYw|dz} |� _ � |i|��}n#�$r � j}YnwxYw||krn�� �||z ��� �|��z }|| krna� rt d|�d|�d| ����|}||kr� �d|z���|t |dz��z }t||��}��|� _n #|� _wxYw| S) N� �rzautoprec: target=z, prec=z , accuracy=z2autoprec: prec increased to %i without convergence�)r��_default_hyper_maxprecr��mag�print� NoConvergencer/�min) �argsrcr��maxprec2�v1�prec2�v2�err�catchr��f�maxprec�verboses �����r��f_autoprec_wrappedz.MPContext.autoprec.<locals>.f_autoprec_wrapped s�����8�D����5�5�d�;�;���"�� ��"�9���!���D�+�F�+�+�B�B���!�!�!���B�B�B�!�����r� ��1�$�C�H�%��Q��/��/�/���� �%�%�%� �W����%�����R�x�x���'�'�"�R�%�.�.�3�7�7�2�;�;�6�C��t�e�}�}���3���#�t�t�U�U�U�S�D�D�2�3�3�3��B���(�(�!�/�/�L�� �!�!�!��S��q��\�\�)�E���x�0�0�E�)1�, �����4��������3�JsP� D�8�D� A�D�A�D�A!� D�! A0�-D�/A0�0B!D� D"r�)r�r�r�r�r�r�s````` r��autopreczMPContext.autoprec�s>�������H$ �$ �$ �$ �$ �$ �$ �$ �$ �J"�!r��c �8����t|t��r&dd����fd�|D����zSt|t��r&dd����fd�|D����zSt |d��rt |j�fi���St |d��rdt|j�fi���zd zSt|t��rt|��St|�j ��r|j �fi���St|��S) a3 Convert an ``mpf`` or ``mpc`` to a decimal string literal with *n* significant digits. The small default value for *n* is chosen to make this function useful for printing collections of numbers (lists, matrices, etc). If *x* is a list or tuple, :func:`~mpmath.nstr` is applied recursively to each element. For unrecognized classes, :func:`~mpmath.nstr` simply returns ``str(x)``. The companion function :func:`~mpmath.nprint` prints the result instead of returning it. The keyword arguments *strip_zeros*, *min_fixed*, *max_fixed* and *show_zero_exponent* are forwarded to :func:`~mpmath.libmp.to_str`. 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. >>> from mpmath import * >>> nstr([+pi, ldexp(1,-500)]) '[3.14159, 3.05494e-151]' >>> nprint([+pi, ldexp(1,-500)]) [3.14159, 3.05494e-151] >>> nstr(mpf("5e-10"), 5) '5.0e-10' >>> nstr(mpf("5e-10"), 5, strip_zeros=False) '5.0000e-10' >>> nstr(mpf("5e-10"), 5, strip_zeros=False, min_fixed=-11) '0.00000000050000' >>> nstr(mpf(0), 5, show_zero_exponent=True) '0.0e+0' z[%s]z, c3�6�K�|]}�j|�fi���V��dSr%��nstr��.0rdr�rcr5s ���r�� <genexpr>z!MPContext.nstr.<locals>.<genexpr>]�9�����&K�&K�A�x�s�x��1�'?�'?��'?�'?�&K�&K�&K�&K�&K�&Kr�z(%s)c3�6�K�|]}�j|�fi���V��dSr%r�r�s ���r�r�z!MPContext.nstr.<locals>.<genexpr>_r�r�r*r@�(�))ru�listr��tupler1rr*r:r@r�repr�matrix�__nstr__�str)r�r&r5rcs` ``r�r�zMPContext.nstr4sK�����P �a�� � � M��T�Y�Y�&K�&K�&K�&K�&K�&K��&K�&K�&K�K�K�L� L� �a�� � � M��T�Y�Y�&K�&K�&K�&K�&K�&K��&K�&K�&K�K�K�L� L� �1�g� � � 0��!�'�1�/�/��/�/� /� �1�g� � � A���A�G�Q�9�9�&�9�9�9�S�@� @� �a�� $� $� ���7�7�N� �a��� $� $� +��1�:�a�*�*�6�*�*� *��1�v�v� r�c��|r�t|t��r�d|���vr�|����dd��}t�|��}|�d��}|sd}|�d���d��}|�|� |��|� |����St|d��r4|j \}}||kr|� |��Std���td t|��z���) Nr�� ��rer�im�_mpi_z,can only create mpf from zero-width intervalzcannot create mpf from )rur�lower�replace� get_complex�match�group�rstripror(r1r�r�� ValueErrorrwr�)r�r&�stringsr�r�r�rLrMs r��_convert_fallbackzMPContext._convert_fallbackjs%�� � A�z�!�Z�0�0� A��a�g�g�i�i����G�G�I�I�%�%�c�2�.�.��#�)�)�!�,�,���[�[��&�&�����B��[�[��&�&�-�-�c�2�2���w�w�s�{�{�2���� � �B���@�@�@� �1�g� � � Q��7�D�A�q��A�v�v��|�|�A���&� �!O�P�P�P��1�D��G�G�;�<�<�<r�c��|j|i|��Sr%)r()r�r�rcs r�� mpmathifyzMPContext.mpmathify|s���s�{�D�+�F�+�+�+r�c��|r�|�d��rdS|j\}}d|vr|d}d|vr%|d}||jkrdSt|��}n(d|vr$|d}||jkrdSt |��}||fS|jS)N�exact)rr�r7r�r�)�getr+r�r/r)r�rcr�r7r�s r�r_zMPContext._parse_precs��� � "��z�z�'�"�"� ��v� �/�N�D�(��V�#�#�!�*�-�������f�~���3�7�?�?�!�6��t�9�9�D�D��&����U�m���#�'�>�>�!�6�"�3�'�'����>� !��!�!r�z'the exact result does not fit in memoryz�hypsum() failed to converge to the requested %i bits of accuracy using a working precision of %i bits. Try with a higher maxprec, maxterms, or set zeroprec.Tc ��t|d��r|||df}|j} nt|d��r |||df}|j} ||jvr"t j|��d|j|<|j|} |j} |�d|�| ����} d} d}i}d }t|��D]�\}}||d krW||krP|d krJd }t|d|���D]\}}||d kr|d kr||krd }� |std ����h|� |��\}}t|�� }| }||kr<|d kr6|dkr0||vr||xx|z cc<n|||<t| || z dz��} |t|��z }�� | | krt|j| | | zfz���| | z}|rt#d�|D����}ni}| || | |||fi|��\}}}| }d }| |kr#|���D]}|�|| krd }n�|| dz dz kp| }|r>|rnK|�d��} | �$|| kr|r|�d ��S|jS| dz} |dz }| dz } ��t+|��t,ur,|r|�|��S|�|��S|S)Nr*�Rr@�Crr��2�r�ZFTzpole in hypergeometric series��<c3�K�|]}|dfV�� dSr%r�)r�r5s r�r�z#MPContext.hypsum.<locals>.<genexpr>�s&����B�B�Q��4��B�B�B�B�B�Br���zeroprecr�)r1r*r@rvr�make_hyp_summatorr�r�r�� enumerate�ZeroDivisionError� nint_distancer/�max�absr�� _hypsum_msg�dict�valuesror�r^r�r�r�)!r�r�r��flags�coeffsr6�accurate_smallrc�keyrN�summatorr�r�r��epsshift�magnitude_check�max_total_jump�ird�ok�ii�ccr5r��wp�mag_dict�zv� have_complex� magnitude�cancel�jumps_resolved�accurater�s! r��hypsumzMPContext.hypsum�s��� �1�g� � � ��Q��s�"�C���A�A� �Q�� � � ��Q��s�"�C���A� �c�'� '� '�%*�%<�S�%A�%A�!�%D�C� �c� "��$�S�)���x���*�*�Y��(B�(B�4�(H�(H�I�I��� ��������f�%�%� %� %�D�A�q��Q�x�3�����6�6�a�1�f�f��B�"+�F�2�A�2�J�"7�"7�&�&���B� ��9��+�+��a���A��G�G�!%�B���Q�/�0O�P�P�P���$�$�Q�'�'�D�A�q��Q����A���A��A�v�v�!�q�&�&�Q��U�U���'�'�#�A�&�&�&�!�+�&�&�&�&�)*�O�A�&�� �1�t�8�b�=�9�9� � �c�!�f�f� $�N�N�# ��7�"�"� ���D�$�y�.�3I�!I�J�J�J�� �!�B�� ��B�B�/�B�B�B�B�B�����*2�(�6�1�d�B��(�+.�+.�&,�+.�+.� '�B� �i��Z�F�!�N��>�)�)�!���*�*���A�� �q�4�x�x�).����(0���2��a��/�E�~�3E�H�� ,����!�:�:�j�1�1���'���(�(�'�,�#&�7�7�1�:�:�-�#&�8�O� ��N�I� ��M�H� ��N�I�G# �J ��8�8�u� � �� (��|�|�B�'�'�'��|�|�B�'�'�'��Ir�c��|�|��}|�tj|j|����S)a� Computes `x 2^n` efficiently. No rounding is performed. The argument `x` must be a real floating-point number (or possible to convert into one) and `n` must be a Python ``int``. >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> ldexp(1, 10) mpf('1024.0') >>> ldexp(1, -3) mpf('0.125') )r(r�r� mpf_shiftr*rBs r��ldexpzMPContext.ldexp�s3�� �K�K��N�N���|�|�E�O�A�G�Q�7�7�8�8�8r�c��|�|��}tj|j��\}}|�|��|fS)a= Given a real number `x`, returns `(y, n)` with `y \in [0.5, 1)`, `n` a Python integer, and such that `x = y 2^n`. No rounding is performed. >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> frexp(7.5) (mpf('0.9375'), 3) )r(r� mpf_frexpr*r�)r�r&r,r5s r��frexpzMPContext.frexps=�� �K�K��N�N����q�w�'�'���1��|�|�A����!�!r�c �^�|�|��\}}|�|��}t|d��r)|�t |j||����St|d��r)|�t|j||����Std���)a� Negates the number *x*, giving a floating-point result, optionally using a custom precision and rounding mode. See the documentation of :func:`~mpmath.fadd` for a detailed description of how to specify precision and rounding. **Examples** An mpmath number is returned:: >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> fneg(2.5) mpf('-2.5') >>> fneg(-5+2j) mpc(real='5.0', imag='-2.0') Precise control over rounding is possible:: >>> x = fadd(2, 1e-100, exact=True) >>> fneg(x) mpf('-2.0') >>> fneg(x, rounding='f') mpf('-2.0000000000000004') Negating with and without roundoff:: >>> n = 200000000000000000000001 >>> print(int(-mpf(n))) -200000000000000016777216 >>> print(int(fneg(n))) -200000000000000016777216 >>> print(int(fneg(n, prec=log(n,2)+1))) -200000000000000000000001 >>> print(int(fneg(n, dps=log(n,10)+1))) -200000000000000000000001 >>> print(int(fneg(n, prec=inf))) -200000000000000000000001 >>> print(int(fneg(n, dps=inf))) -200000000000000000000001 >>> print(int(fneg(n, exact=True))) -200000000000000000000001 r*r@�2Arguments need to be mpf or mpc compatible numbers) r_r(r1r�r&r*r�r?r@r�)r�r&rcr�r7s r��fnegzMPContext.fnegs���\����0�0���h� �K�K��N�N�� �1�g� � � B��<�<�����x� @� @�A�A� A� �1�g� � � B��<�<�����x� @� @�A�A� A��M�N�N�Nr�c �2�|�|��\}}|�|��}|�|��} t|d��r~t|d��r/|�t |j|j||����St|d��r/|�t|j|j||����St|d��r~t|d��r/|�t|j|j||����St|d��r/|�t|j|j||����Sn)#ttf$rt|j ���wxYwtd���)a� Adds the numbers *x* and *y*, giving a floating-point result, optionally using a custom precision and rounding mode. The default precision is the working precision of the context. You can specify a custom precision in bits by passing the *prec* keyword argument, or by providing an equivalent decimal precision with the *dps* keyword argument. If the precision is set to ``+inf``, or if the flag *exact=True* is passed, an exact addition with no rounding is performed. When the precision is finite, the optional *rounding* keyword argument specifies the direction of rounding. Valid options are ``'n'`` for nearest (default), ``'f'`` for floor, ``'c'`` for ceiling, ``'d'`` for down, ``'u'`` for up. **Examples** Using :func:`~mpmath.fadd` with precision and rounding control:: >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> fadd(2, 1e-20) mpf('2.0') >>> fadd(2, 1e-20, rounding='u') mpf('2.0000000000000004') >>> nprint(fadd(2, 1e-20, prec=100), 25) 2.00000000000000000001 >>> nprint(fadd(2, 1e-20, dps=15), 25) 2.0 >>> nprint(fadd(2, 1e-20, dps=25), 25) 2.00000000000000000001 >>> nprint(fadd(2, 1e-20, exact=True), 25) 2.00000000000000000001 Exact addition avoids cancellation errors, enforcing familiar laws of numbers such as `x+y-x = y`, which don't hold in floating-point arithmetic with finite precision:: >>> x, y = mpf(2), mpf('1e-1000') >>> print(x + y - x) 0.0 >>> print(fadd(x, y, prec=inf) - x) 1.0e-1000 >>> print(fadd(x, y, exact=True) - x) 1.0e-1000 Exact addition can be inefficient and may be impossible to perform with large magnitude differences:: >>> fadd(1, '1e-100000000000000000000', prec=inf) Traceback (most recent call last): ... OverflowError: the exact result does not fit in memory r*r@r) r_r(r1r�r'r*r�rCr@rBr�� OverflowError�_exact_overflow_msg�r�r&r,rcr�r7s r��faddzMPContext.faddFs���p����0�0���h� �K�K��N�N�� �K�K��N�N�� 9��q�'�"�"� W��1�g�&�&�S��<�<������$��(Q�(Q�R�R�R��1�g�&�&�W��<�<� �A�G�Q�W�d�H�(U�(U�V�V�V��q�'�"�"� S��1�g�&�&�W��<�<� �A�G�Q�W�d�H�(U�(U�V�V�V��1�g�&�&�S��<�<������$��(Q�(Q�R�R�R����M�*� 9� 9� 9��� 7�8�8� 8� 9�����M�N�N�N� �AE!�>E!�AE!�!>E!�!&Fc �@�|�|��\}}|�|��}|�|��} t|d��r�t|d��r/|�t |j|j||����St|d��r6|�t|jtf|j ||����St|d��r~t|d��r/|�t|j |j||����St|d��r/|�t|j |j ||����Sn)#ttf$rt|j ���wxYwtd���)a� Subtracts the numbers *x* and *y*, giving a floating-point result, optionally using a custom precision and rounding mode. See the documentation of :func:`~mpmath.fadd` for a detailed description of how to specify precision and rounding. **Examples** Using :func:`~mpmath.fsub` with precision and rounding control:: >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> fsub(2, 1e-20) mpf('2.0') >>> fsub(2, 1e-20, rounding='d') mpf('1.9999999999999998') >>> nprint(fsub(2, 1e-20, prec=100), 25) 1.99999999999999999999 >>> nprint(fsub(2, 1e-20, dps=15), 25) 2.0 >>> nprint(fsub(2, 1e-20, dps=25), 25) 1.99999999999999999999 >>> nprint(fsub(2, 1e-20, exact=True), 25) 1.99999999999999999999 Exact subtraction avoids cancellation errors, enforcing familiar laws of numbers such as `x-y+y = x`, which don't hold in floating-point arithmetic with finite precision:: >>> x, y = mpf(2), mpf('1e1000') >>> print(x - y + y) 0.0 >>> print(fsub(x, y, prec=inf) + y) 2.0 >>> print(fsub(x, y, exact=True) + y) 2.0 Exact addition can be inefficient and may be impossible to perform with large magnitude differences:: >>> fsub(1, '1e-100000000000000000000', prec=inf) Traceback (most recent call last): ... OverflowError: the exact result does not fit in memory r*r@r)r_r(r1r�r(r*r�rDr r@rEr�rrrs r��fsubzMPContext.fsub�s���`����0�0���h� �K�K��N�N�� �K�K��N�N�� 9��q�'�"�"� \��1�g�&�&�S��<�<������$��(Q�(Q�R�R�R��1�g�&�&�\��<�<����%�0@�!�'�4�QY�(Z�(Z�[�[�[��q�'�"�"� S��1�g�&�&�W��<�<� �A�G�Q�W�d�H�(U�(U�V�V�V��1�g�&�&�S��<�<������$��(Q�(Q�R�R�R����M�*� 9� 9� 9��� 7�8�8� 8� 9�����M�N�N�Ns!�AE(�AE(�AE(�(>E(�(&Fc �2�|�|��\}}|�|��}|�|��} t|d��r~t|d��r/|�t |j|j||����St|d��r/|�t|j|j||����St|d��r~t|d��r/|�t|j|j||����St|d��r/|�t|j|j||����Sn)#ttf$rt|j ���wxYwtd���)a� Multiplies the numbers *x* and *y*, giving a floating-point result, optionally using a custom precision and rounding mode. See the documentation of :func:`~mpmath.fadd` for a detailed description of how to specify precision and rounding. **Examples** The result is an mpmath number:: >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> fmul(2, 5.0) mpf('10.0') >>> fmul(0.5j, 0.5) mpc(real='0.0', imag='0.25') Avoiding roundoff:: >>> x, y = 10**10+1, 10**15+1 >>> print(x*y) 10000000001000010000000001 >>> print(mpf(x) * mpf(y)) 1.0000000001e+25 >>> print(int(mpf(x) * mpf(y))) 10000000001000011026399232 >>> print(int(fmul(x, y))) 10000000001000011026399232 >>> print(int(fmul(x, y, dps=25))) 10000000001000010000000001 >>> print(int(fmul(x, y, exact=True))) 10000000001000010000000001 Exact multiplication with complex numbers can be inefficient and may be impossible to perform with large magnitude differences between real and imaginary parts:: >>> x = 1+2j >>> y = mpc(2, '1e-100000000000000000000') >>> fmul(x, y) mpc(real='2.0', imag='4.0') >>> fmul(x, y, rounding='u') mpc(real='2.0', imag='4.0000000000000009') >>> fmul(x, y, exact=True) Traceback (most recent call last): ... OverflowError: the exact result does not fit in memory r*r@r) r_r(r1r�r)r*r�rGr@rFr�rrrs r��fmulzMPContext.fmul�s���f����0�0���h� �K�K��N�N�� �K�K��N�N�� 9��q�'�"�"� W��1�g�&�&�S��<�<������$��(Q�(Q�R�R�R��1�g�&�&�W��<�<� �A�G�Q�W�d�H�(U�(U�V�V�V��q�'�"�"� S��1�g�&�&�W��<�<� �A�G�Q�W�d�H�(U�(U�V�V�V��1�g�&�&�S��<�<������$��(Q�(Q�R�R�R����M�*� 9� 9� 9��� 7�8�8� 8� 9�����M�N�N�Nrc � �|�|��\}}|std���|�|��}|�|��}t|d��r�t|d��r/|�t |j|j||����St|d��r6|�t|jtf|j ||����St|d��r~t|d��r/|�t|j |j||����St|d��r/|�t|j |j ||����Std���)a� Divides the numbers *x* and *y*, giving a floating-point result, optionally using a custom precision and rounding mode. See the documentation of :func:`~mpmath.fadd` for a detailed description of how to specify precision and rounding. **Examples** The result is an mpmath number:: >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> fdiv(3, 2) mpf('1.5') >>> fdiv(2, 3) mpf('0.66666666666666663') >>> fdiv(2+4j, 0.5) mpc(real='4.0', imag='8.0') The rounding direction and precision can be controlled:: >>> fdiv(2, 3, dps=3) # Should be accurate to at least 3 digits mpf('0.6666259765625') >>> fdiv(2, 3, rounding='d') mpf('0.66666666666666663') >>> fdiv(2, 3, prec=60) mpf('0.66666666666666667') >>> fdiv(2, 3, rounding='u') mpf('0.66666666666666674') Checking the error of a division by performing it at higher precision:: >>> fdiv(2, 3) - fdiv(2, 3, prec=100) mpf('-3.7007434154172148e-17') Unlike :func:`~mpmath.fadd`, :func:`~mpmath.fmul`, etc., exact division is not allowed since the quotient of two floating-point numbers generally does not have an exact floating-point representation. (In the future this might be changed to allow the case where the division is actually exact.) >>> fdiv(2, 3, exact=True) Traceback (most recent call last): ... ValueError: division is not an exact operation z"division is not an exact operationr*r@r) r_r�r(r1r�r+r*r�rIr r@rJrs r��fdivzMPContext.fdivsf��b����0�0���h�� C��A�B�B� B� �K�K��N�N�� �K�K��N�N�� �1�g� � � X��q�'�"�"� O��|�|�G�A�G�Q�W�d�H�$M�$M�N�N�N��q�'�"�"� X��|�|�G�Q�W�e�,<�a�g�t�X�$V�$V�W�W�W� �1�g� � � O��q�'�"�"� S��|�|�K�����$��$Q�$Q�R�R�R��q�'�"�"� O��|�|�G�A�G�Q�W�d�H�$M�$M�N�N�N��M�N�N�Nr�c�F�t|��}|tvrt|��|jfS|tjurm|j\}}t||��\}}d|z|kr|dz }n |s ||jfStt|||zz ����t|��z }||fSt|d��r|j }|j} n�t|d��r;|j \}} | \} } } }| r| |z} n{| tkr|j} nhtd���|�|��}t|d��st|d��r|�|��St#d���|\}}}}||z}|dkrd}|}n�|rg|dkr ||z}|j}nN|dkr |dz dz}d}n=| dz }||z }|dzr|dz }||z|z }n|||zz}|dz }|t|��z}|r| }n$|tkr |j}d}ntd���|t%|| ��fS) a� Return `(n,d)` where `n` is the nearest integer to `x` and `d` is an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision (measured in bits) lost to cancellation when computing `x-n`. >>> from mpmath import * >>> n, d = nint_distance(5) >>> print(n); print(d) 5 -inf >>> n, d = nint_distance(mpf(5)) >>> print(n); print(d) 5 -inf >>> n, d = nint_distance(mpf(5.00000001)) >>> print(n); print(d) 5 -26 >>> n, d = nint_distance(mpf(4.99999999)) >>> print(n); print(d) 5 -26 >>> n, d = nint_distance(mpc(5,10)) >>> print(n); print(d) 5 4 >>> n, d = nint_distance(mpc(5,0.000001)) >>> print(n); print(d) 5 -19 r�rr*r@zrequires a finite numberzrequires an mpf/mpcr�����)r^r r/r�r^rrr}�divmodr8r�r1r*r@r r�r(r�rwr�)r�r&�typxr�r�r5�rr�r��im_distr��isign�iman�iexp�ibcr~rr�r�r��re_dist�ts r�r�zMPContext.nint_distanceYs���B�A�w�w�� �9� � ��q�6�6�3�8�#� #� �X�\� !� !��7�D�A�q��!�Q�<�<�D�A�q���s�a�x�x��Q����� #��#�(�{�"���Q�q��s�U���$�$�x��{�{�2�A��a�4�K� �1�g� � � 7���B��h�G�G� �Q�� � � 7��W�F�B��%'� "�E�4��s�� =���*����u����(��� �!;�<�<�<�� � �A���A��q�'�"�"� 7�g�a��&9�&9� 7��(�(��+�+�+�� 5�6�6�6����c�3���"�f�� ��7�7��A��G�G� � 9��a�x�x��3�J���(��������!�V�Q�J������T�!�V���1�H���q�5�"���F�A��a�4�3�,�C�C��A�q�D�M�C��q�D���h�s�m�m�+��� ��B��� �5�[�[��h�G��A�A��7�8�8� 8��#�g�w�'�'�'�'r�c�d�|j} |j}|D]}||z}� ||_n #||_wxYw| S)aT Calculates a product containing a finite number of factors (for infinite products, see :func:`~mpmath.nprod`). The factors will be converted to mpmath numbers. >>> from mpmath import * >>> mp.dps = 15; mp.pretty = False >>> fprod([1, 2, 0.5, 7]) mpf('7.0') )r�r�)r��factors�origrNr�s r��fprodzMPContext.fprod�sW���x�� ���A�� � ���Q���� ��C�H�H��t�C�H�O�O�O�O��r� s�#� ,c�P�|�t|j����S)z� Returns an ``mpf`` with value chosen randomly from `[0, 1)`. The number of randomly generated bits in the mantissa is equal to the working precision. )r�r6r�r�s r��randzMPContext.rand�s �� �|�|�H�S�Y�/�/�0�0�0r�c�D���|���fd���d�����S)a Given Python integers `(p, q)`, returns a lazy ``mpf`` representing the fraction `p/q`. The value is updated with the precision. >>> from mpmath import * >>> mp.dps = 15 >>> a = fraction(1,100) >>> b = mpf(1)/100 >>> print(a); print(b) 0.01 0.01 >>> mp.dps = 30 >>> print(a); print(b) # a will be accurate 0.01 0.0100000000000000002081668171172 >>> mp.dps = 15 c�(��t��||��Sr%)r)r�r�r�r�s ��r�r�z$MPContext.fraction.<locals>.<lambda>�s���m�A�q�$��.L�.L�r��/)rp)r�r�r�s ``r��fractionzMPContext.fraction�s9����$�|�|�L�L�L�L�L��q�q�!�!� ��� r�c�F�t|�|����Sr%�r�r(rqs r��absminzMPContext.absmin�����3�;�;�q�>�>�"�"�"r�c�F�t|�|����Sr%r6rqs r��absmaxzMPContext.absmax�r8r�c��t|d��r4|j\}}|�|��|�|��gS|S)Nr�)r1r�r�)r�r&rLrMs r�� _as_pointszMPContext._as_points�sC�� �1�g� � � 6��7�D�A�q��L�L��O�O�S�\�\�!�_�_�5� 5��r�rc� ����|��rt|d��st�t|��}�j}t j|j|||||��\}}�fd�|D��}�fd�|D��}||fS)Nr@c�:��g|]}��|����Sr��r�)r�r&r�s �r�� <listcomp>z+MPContext._zetasum_fast.<locals>.<listcomp>�#��� *� *� *�!�c�l�l�1�o�o� *� *� *r�c�:��g|]}��|����Sr�r?)r�r,r�s �r�r@z+MPContext._zetasum_fast.<locals>.<listcomp>rAr�)�isintr1r2r/r�r� mpc_zetasumr@) r�rerLr5� derivatives�reflectr��xs�yss ` r�� _zetasum_fastzMPContext._zetasum_fast s����� � �!� � � &���G�!4�!4� &�%� %� ��F�F���y���"�1�7�A�q�+�w��M�M���B� *� *� *� *�r� *� *� *�� *� *� *� *�r� *� *� *���2�v� r�)r�F)Nr�F)r�)T)8�__name__� __module__� __qualname__�__doc__rkrur9r-r:r<rCrHrOrxrUr}r|rfrjrmrYrsrvrzr|r��propertyr�r�r�r�r�r�r�r�r�r�r_rr�r r rrrrrrr�r.r0r4r7r:r<rIr�r�r�rgrg:s���������1�1�1�BT5�T5�T5�l � � �T�T�T� .� .� .� .� .� .� J� J� J�P�P�P� A� A� A� A�N�N�N�M�M�M�T�T�T� P�P�P� >� >� >� >� >� >������� ��� ;�;�;�8���4+�+�+�( � � ��#�#��X�#�����X��N�N�N�N�$N�N�N�N�J�J�J�J�"J�J�J�J�i"�i"�i"�i"�V4�4�4�4�l=�=�=�$,�,�,�"�"�"�*D���K�S�S�S�S�j9�9�9�""�"�"� 4O�4O�4O�lHO�HO�HO�T@O�@O�@O�DCO�CO�CO�J@O�@O�@O�D`(�`(�`(�D���*1�1�1����*#�#�#�#�#�#�����"23��U������r�rgc�(�eZdZdd�Zd�Zd�Zd�ZdS)r�Fc�>�||_||_||_||_dSr%)r��precfun�dpsfunr�)�selfr�rRrSr�s r�rkzPrecisionManager.__init__s%������� ��� � 0����r�c�J���tj�����fd���}|S)Nc�����jj} �jr*���jj���j_n)���jj���j_�jrR�|i|��}t |��tur%td�|D����|�j_S| |�j_S�|i|��|�j_S#|�j_wxYw)Nc��g|]}| ��Sr�r�)r�rLs r�r@z8PrecisionManager.__call__.<locals>.g.<locals>.<listcomp>'s��_�_�_�Q�q�b�_�_�_r�)r�r�rRrSr�r�r^r�)r�rcr-rNr�rTs ��r��gz$PrecisionManager.__call__.<locals>.gs�����8�=�D� %��<�=�$(�L�L����$?�$?�D�H�M�M�#'�;�;�t�x�|�#<�#<�D�H�L��(�.���4�*�6�*�*�A��A�w�w�%�'�'�$�_�_�!�_�_�_�5�5� !%��� � � �2�!%��� � ��1�d�-�f�-�-� $��� � ����� �$�$�$�$s�BC�3C�C�C$)� functools�wraps)rTr�rXs`` r��__call__zPrecisionManager.__call__s>���� ��� � � %� %� %� %� %� � � %� �r�c���|jj|_|jr+|�|jj��|j_dS|�|jj��|j_dSr%)r�r��origprRrSr�)rTs r�� __enter__zPrecisionManager.__enter__.sQ���X�]�� � �<� 5� �L�L����7�7�D�H�M�M�M��;�;�t�x�|�4�4�D�H�L�L�Lr�c�(�|j|j_dSri)r]r�r�)rT�exc_type�exc_val�exc_tbs r��__exit__zPrecisionManager.__exit__4s��� ��� ��ur�NrJ)rKrLrMrkr[r^rcr�r�r�r�r�sU������1�1�1�1� ���&5�5�5� ����r�r��__main__)wrN� __docformat__rYr��ctx_baser� libmp.backendrrr�rr r r r r rrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrCrDrErFrGrHrIrJrKrLrMrNrOrPrQrRrSrTrUrVrWrXrYrZr[r\r]r^�object�__new__�new�compiler��sage.libs.mpmath.ext_mainr`rj�libs�mpmath�ext_main� _mpf_modulerbrarcrdrergr�rK�doctest�testmodr�r�r��<module>rss0����� ����� � � � �)�)�)�)�)�)�.�.�.�.�.�.�.�.�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������.������������ �n���b�j�K�L�L� � �f���B�B�B�B�B�B�3�3�3�3�3�3�3�3�3�3�3�3�3�?�?�?�?�?�?�.�.�.�.�.�.�0�0�0�0�0�0�0�0�0�0�Y�Y�Y�Y�Y� �2�Y�Y�Y�v&!�!�!�!�!�!�!�!�H �z����N�N�N��G�O�������r�
Memory