Ë
    3^(hEr  ã                   óì  — d Z ddlmZ ddlmZmZ d„ Z G d„ de«      ZdId
„Z	dJd„Z
d„ Zd„ Zd„ Zd„ Zd„ ddfd„ ddfd„ ddfd„ ddfd„ ddfd„ ddfd„ ddfd„ d dfd!„ d"dfd#„ d$dfd%„ d&dfd'„ d(dfd)„ d*dfd+„ d,dfd-„ d.dfd/„ d0dfd1„ d2dfd3„ d4dfd5„ d6dfd7„ d8dfd9„ d:dfd;„ d<dfd=„ d>dfd?„ d@dfdA„ dBdfdC„ dDdfdE„ dFdfgZg ddd	d	fdG„Ze	e_	        e
e_
        ee_        edHk(  rddlZ ej&                  «        yy)Kzs
Implements the PSLQ algorithm for integer relation detection,
and derivative algorithms for constant recognition.
é   )Úxrange)Ú	int_typesÚ
sqrt_fixedc                 ó$   — | d|dz
  z  z   |z	  |z  S ©Nr   © )ÚxÚprecs     úS/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/mpmath/identification.pyÚround_fixedr   
   s   € Ø�!�d˜1‘f‘+Ñ 4Ñ'¨DÑ0Ð0ó    c                   ó   — e Zd Zy)ÚIdentificationMethodsN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r      s   „ Ør   r   Néè  Fc                 ó  — t        |«      }|dk  rt        d«      ‚| j                  }|dk  rt        d«      ‚|r|t        d|«      z  dk  rt	        d«       t        |dz  «      }|€| j                  d«      | z  }n| j                  |«      }d	}	||	z  }|rt	        d
|| j                  |«      fz  «       | j                  ||«      }|sJ ‚dg|D �
cg c]#  }
| j                  | j                  |
«      |«      ‘Œ% c}
z   }t        d„ |dd D «       «      }|st        d«      ‚||dz  k  r|rt	        d«       yt        d|z  dz  |«      }i }i }i }t        d|dz   «      D ]1  }t        d|dz   «      D ]  }||k(  |z  x|||f<   |||f<   d|||f<   Œ Œ3 dgdg|z  z   }t        d|dz   «      D ]5  }d}t        ||dz   «      D ]  }|||   dz  |z	  z  }Œ t        ||«      ||<   Œ7 |d   }|dd }t        d|dz   «      D ]  }||   |z  |z  ||<   ||   |z  |z  ||<   Œ  t        d|dz   «      D ]ˆ  }t        |dz   |«      D ]	  }d|||f<   Œ ||dz
  k  r#||   r||dz      |z  ||   z  |||f<   nd|||f<   t        d|«      D ]1  }||   ||dz      z  }|r||    ||   z  |z  |z  |||f<   Œ+d|||f<   Œ3 ŒŠ t        d|dz   «      D ]Æ  }t        |dz
  dd«      D ]±  }|||f   rt        |||f   |z  |||f   z  |«      }nŒ(||   |||   z  |z	  z   ||<   t        d|dz   «      D ]  }|||f   ||||f   z  |z	  z
  |||f<   Œ t        d|dz   «      D ]6  }|||f   ||||f   z  |z	  z
  |||f<   |||f   ||||f   z  |z	  z   |||f<   Œ8 Œ³ ŒÈ t        |«      D �]µ  }d}d}t        d|«      D ]-  }|||f   }||z  t        |«      z  ||dz
  z  z	  }||kD  sŒ*|}|}Œ/ ||dz      ||   c||<   ||dz   <   t        d|dz   «      D ]!  }||dz   |f   |||f   c|||f<   ||dz   |f<   Œ# t        d|dz   «      D ]!  }||dz   |f   |||f   c|||f<   ||dz   |f<   Œ# t        d|dz   «      D ]!  }|||dz   f   |||f   c|||f<   |||dz   f<   Œ# ||dz
  k  r–t        |||f   dz  |||dz   f   dz  z   |z	  |«      }|s �n“|||f   |z  |z  }|||dz   f   |z  |z  }t        ||dz   «      D ]=  }|||f   }|||dz   f   } ||z  || z  z   |z	  |||f<   | |z  || z  z   |z	  |||dz   f<   Œ? t        |dz   |dz   «      D ]Ë  }t        t        |dz
  |dz   «      dd«      D ]©  }	 t        |||f   |z  |||f   z  |«      }||   |||   z  |z	  z   ||<   t        d|dz   «      D ]  }|||f   ||||f   z  |z	  z
  |||f<   Œ t        d|dz   «      D ]6  }|||f   ||||f   z  |z	  z
  |||f<   |||f   ||||f   z  |z	  z   |||f<   Œ8 Œ« ŒÍ ||z  }!t        d|dz   «      D ]«  }t        ||   «      }"|"|k  rŠt        d|dz   «      D �cg c]  }t        t        |||f   |«      |z	  «      ‘Œ! }#}t        d„ |#D «       «      |k  r>|r6t	        d||| j                  |"| j                  d«      |z  z  d«      fz  «       |#c c S t        |"|!«      }!Œ­ t        d„ |j#                  «       D «       «      }$|$rdd|z  z  |$z  |z	  }%|%dz  }%n| j$                  }%|r7t	        d||| j                  |!| j                  d«      |z  z  d«      |%fz  «       |%|k\  s�Œ¶ n |rt	        d|fz  «       t	        d%z  «       yc c}
w # t         $ r Y  �ŒHw xY wc c}w )a¾  
    Given a vector of real numbers `x = [x_0, x_1, ..., x_n]`, ``pslq(x)``
    uses the PSLQ algorithm to find a list of integers
    `[c_0, c_1, ..., c_n]` such that

    .. math ::

        |c_1 x_1 + c_2 x_2 + ... + c_n x_n| < \mathrm{tol}

    and such that `\max |c_k| < \mathrm{maxcoeff}`. If no such vector
    exists, :func:`~mpmath.pslq` returns ``None``. The tolerance defaults to
    3/4 of the working precision.

    **Examples**

    Find rational approximations for `\pi`::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> pslq([-1, pi], tol=0.01)
        [22, 7]
        >>> pslq([-1, pi], tol=0.001)
        [355, 113]
        >>> mpf(22)/7; mpf(355)/113; +pi
        3.14285714285714
        3.14159292035398
        3.14159265358979

    Pi is not a rational number with denominator less than 1000::

        >>> pslq([-1, pi])
        >>>

    To within the standard precision, it can however be approximated
    by at least one rational number with denominator less than `10^{12}`::

        >>> p, q = pslq([-1, pi], maxcoeff=10**12)
        >>> print(p); print(q)
        238410049439
        75888275702
        >>> mpf(p)/q
        3.14159265358979

    The PSLQ algorithm can be applied to long vectors. For example,
    we can investigate the rational (in)dependence of integer square
    roots::

        >>> mp.dps = 30
        >>> pslq([sqrt(n) for n in range(2, 5+1)])
        >>>
        >>> pslq([sqrt(n) for n in range(2, 6+1)])
        >>>
        >>> pslq([sqrt(n) for n in range(2, 8+1)])
        [2, 0, 0, 0, 0, 0, -1]

    **Machin formulas**

    A famous formula for `\pi` is Machin's,

    .. math ::

        \frac{\pi}{4} = 4 \operatorname{acot} 5 - \operatorname{acot} 239

    There are actually infinitely many formulas of this type. Two
    others are

    .. math ::

        \frac{\pi}{4} = \operatorname{acot} 1

        \frac{\pi}{4} = 12 \operatorname{acot} 49 + 32 \operatorname{acot} 57
            + 5 \operatorname{acot} 239 + 12 \operatorname{acot} 110443

    We can easily verify the formulas using the PSLQ algorithm::

        >>> mp.dps = 30
        >>> pslq([pi/4, acot(1)])
        [1, -1]
        >>> pslq([pi/4, acot(5), acot(239)])
        [1, -4, 1]
        >>> pslq([pi/4, acot(49), acot(57), acot(239), acot(110443)])
        [1, -12, -32, 5, -12]

    We could try to generate a custom Machin-like formula by running
    the PSLQ algorithm with a few inverse cotangent values, for example
    acot(2), acot(3) ... acot(10). Unfortunately, there is a linear
    dependence among these values, resulting in only that dependence
    being detected, with a zero coefficient for `\pi`::

        >>> pslq([pi] + [acot(n) for n in range(2,11)])
        [0, 1, -1, 0, 0, 0, -1, 0, 0, 0]

    We get better luck by removing linearly dependent terms::

        >>> pslq([pi] + [acot(n) for n in range(2,11) if n not in (3, 5)])
        [1, -8, 0, 0, 4, 0, 0, 0]

    In other words, we found the following formula::

        >>> 8*acot(2) - 4*acot(7)
        3.14159265358979323846264338328
        >>> +pi
        3.14159265358979323846264338328

    **Algorithm**

    This is a fairly direct translation to Python of the pseudocode given by
    David Bailey, "The PSLQ Integer Relation Algorithm":
    http://www.cecm.sfu.ca/organics/papers/bailey/paper/html/node3.html

    The present implementation uses fixed-point instead of floating-point
    arithmetic, since this is significantly (about 7x) faster.
    é   zn cannot be less than 2é5   zprec cannot be less than 53é   z*Warning: precision for PSLQ may be too lowg      è?Né<   zPSLQ using prec %i and tol %sc              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©N©Úabs)Ú.0Úxxs     r   ú	<genexpr>zpslq.<locals>.<genexpr>§   s   è ø€ Ò'˜2Œs�2�wÑ'ùó   ‚r   z)PSLQ requires a vector of nonzero numberséd   z#STOPPING: (one number is too small)é   é   é    éÿÿÿÿc              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r   )r   Úvs     r   r   zpslq.<locals>.<genexpr>  s   è ø€ Ò+ !”s˜1—vÑ+ùr    z'FOUND relation at iter %i/%i, error: %sc              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r   )r   Úhs     r   r   zpslq.<locals>.<genexpr>'  s   è ø€ Ò1 ”c˜!—fÑ1ùr    z%i/%i:  Error: %8s   Norm: %szCANCELLING after step %i/%i.z2Could not find an integer relation. Norm bound: %s)ÚlenÚ
ValueErrorr
   ÚmaxÚprintÚintÚmpfÚconvertÚnstrÚto_fixedÚminr   r   Úranger   r   ÚZeroDivisionErrorÚvaluesÚinf)&Úctxr	   ÚtolÚmaxcoeffÚmaxstepsÚverboseÚnr
   ÚtargetÚextraÚxkÚminxÚgÚAÚBÚHÚiÚjÚsÚkÚtÚyÚsjj1ÚREPÚmÚszmaxr)   ÚszÚt0Út1Út2Út3Út4Úbest_errÚerrÚvecÚrecnormÚnorms&                                         r   Úpslqr[      sþ	  € ôf 	ˆA‹€AØˆ1‚uÜÐ2Ó3Ð3ð �8‰8€DØˆb‚yÜÐ6Ó7Ð7á�4œ3˜q ›8Ñ# aÒ'ÜÐ:Ô;ä�˜‘Ó€Fà
€{Ø�g‰g�a‹j˜F˜7Ñ#‰à�k‰k˜#Óˆà€EØˆE�M€DáÜÐ-°°s·x±xÀ³}Ð0EÑEÔFà
�,‰,�s˜DÓ
!€CÙ€Jˆ3ð 
ˆ¸AÖ>°b�#—,‘,˜sŸw™w r›{¨DÕ1Ò>Ñ>€Aô Ñ'  1 2 Ô'Ó'€DÙÜÐDÓEÐEØˆc�3‰h‚ÙÜÐ7Ô8Øä�A�t‘G˜a‘< Ó&€AØ
€AØ
€AØ
€Aô �A�q˜‘s‹^ò ˆÜ˜˜1˜Q™3“ò 	ˆAØ  !™t¨™nÐ,ˆAˆa�ˆc‰F�Q�q˜�s‘VØˆAˆa�ˆcŠFñ	ðð
 
ˆ�!��q‘Ñ€AÜ�A�q˜‘s‹^ò #ˆØˆÜ˜˜1˜Q™3“ò 	#ˆAØ�!�A‘$˜‘'˜T‘/Ñ"‰Að	#ä˜!˜TÓ"ˆˆ!Šð	#ð
 	
ˆ!‰€AØ	‰!ˆ€AÜ�A�q˜‘s‹^ò #ˆØ�!‘˜‘ Ñ"ˆˆ!‰Ø�!‘˜‘ Ñ"ˆˆ!Šð#ô �A�q˜‘s‹^ò ˆÜ˜˜!™˜Q“ò 	ˆAØˆAˆa�ˆcŠFð	à��!‘Š8Ø�ŠtØ˜A˜a™C™& D™.¨Q¨q©TÑ1��!�A�#’à��!�A�#‘Ü�q˜!“ò 	ˆAØ�Q‘4˜˜!˜A™#™‘;ˆDÙØ˜a™D˜5  1¡™:¨Ñ,¨tÑ3��!�A�#’à��!�A�#’ñ	ðô �A�q˜‘s‹^ò 5ˆÜ˜˜!™˜Q Ó#ò 	5ˆAà��1�ŠvÜ  1 Q 3¡¨4¡°!°A°a°C±&Ñ 8¸$Ó?‘ð Ø�Q‘4˜1˜Q˜q™T™6 T™>Ñ*ˆAˆa‰DÜ˜A˜q ™s“^ò 5�Ø˜1˜Q˜3™ 1 Q q¨ s¡V¡8¨tÑ#3Ñ4��!�A�#’ð5ä˜A˜q ™s“^ò 5�Ø˜1˜Q˜3™ 1 Q q¨ s¡V¡8¨tÑ#3Ñ4��!�A�#‘Ø˜1˜Q˜3™ 1 Q q¨ s¡V¡8¨tÑ#3Ñ4��!�A�#’ñ5ñ	5ð5ô �X‹ó MˆàˆØˆÜ�q˜!“ò 	ˆAØ�!�A�#‘ˆAØ�Q‘$œ˜Q›‘- T¨1¨Q©3¡ZÑ0ˆBØ�E‹zØ�Ø‘ð	ð ˜˜1™‘v˜q ™tˆˆˆ!‰ˆa��!‘‰fÜ˜˜!˜A™#“ÒCˆA°1°Q°q±S¸°U±8¸Q¸qÀ¸s¹VÐ 0  ! A #¡¨¨!¨A©#¨a¨%ªÐCÜ˜˜!˜A™#“ÒCˆA°1°Q°q±S¸°U±8¸Q¸qÀ¸s¹VÐ 0  ! A #¡¨¨!¨A©#¨a¨%ªÐCÜ˜˜!˜A™#“ÒCˆA°1°Q°q¸±s°U±8¸Q¸qÀ¸s¹VÐ 0  ! A #¡¨¨!¨A¨a©C¨%ªÐCà��A‘Š:Ü˜Q˜q ˜s™V Q™Y¨¨1¨Q¨q©S¨5©°1©Ñ4°tÑ;¸TÓBˆBñ ÚØ�A�a�C‘&˜D‘. RÑ'ˆBØ�A�a˜‘c�E‘(˜dÑ" rÑ)ˆBÜ˜A˜q ™s“^ò 2�Ø�q˜�s‘V�Ø�q˜˜1™�u‘X�Ø˜R™%  2¡™+¨$Ñ.��!�A�#‘Ø˜C ™F 2 b¡5™L¨TÑ1��!�A�a‘C�%’ð	2ô ˜˜!™˜Q˜q™SÓ!ò 	9ˆAÜœC  !¡ Q q¡S›M¨1¨bÓ1ò 9�ðÜ# Q q¨ s¡V¨t¡^°a¸¸!¸±fÑ$<¸dÓC�Að ˜‘t  ! A¡$¡¨4Ñ/Ñ0��!‘Ü  1 Q¡3›ò 9�AØ˜q ˜s™V q¨¨1¨Q¨3©¡x°4Ñ'7Ñ8�A�a˜�c’Fð9ä  1 Q¡3›ò 9�AØ˜q ˜s™V q¨¨1¨Q¨3©¡x°4Ñ'7Ñ8�A�a˜�c‘FØ˜q ˜s™V q¨¨1¨Q¨3©¡x°4Ñ'7Ñ8�A�a˜�c’Fñ9ñ9ð	9ð& ˜T‘>ˆÜ˜˜1˜Q™3“ò 	*ˆAÜ�a˜‘d“)ˆCà�SŠyô �a˜˜!™“öÀ!”sœ; q¨¨1¨¡v¨tÓ4¸Ñ<Õ=ð �ð äÑ+ sÔ+Ó+¨hÒ6ÙÜÐGØ  (¨C¯H©H°S¸3¿7¹7À1»:ÀtÑ;KÑ5KÈQÓ,OÐPñQô Rà”JÜ˜3 Ó)‰Hð	*ô  Ñ1 a§h¡h£jÔ1Ó1ˆÙØ˜1˜T™6‘] wÑ.°4Ñ7ˆDØ�S‰L‰Dà—7‘7ˆDÙÜÐ1Ø�h §¡¨°C·G±G¸A³JÀÑ4DÑ)DÀaÓ HÈ$ÐOñPô Qà�8ÔÙð[Mñ\ ÜÐ,°°X¨Ñ>Ô?ÜÐBÀTÑIÔJØùòc ?øôH )ò Ûðüò(s   Ã(]4Õ"]9Ù$^
Ý9	^	Þ^	c                 ó
  — | j                  |«      }|dk  rt        d«      ‚|dk(  rddgS | j                  d«      g}t        d|dz   «      D ]5  }|j                  ||z  «        | j                  |fi |¤Ž}|€Œ-|ddd…   c S  y)aù  
    ``findpoly(x, n)`` returns the coefficients of an integer
    polynomial `P` of degree at most `n` such that `P(x) \approx 0`.
    If no polynomial having `x` as a root can be found,
    :func:`~mpmath.findpoly` returns ``None``.

    :func:`~mpmath.findpoly` works by successively calling :func:`~mpmath.pslq` with
    the vectors `[1, x]`, `[1, x, x^2]`, `[1, x, x^2, x^3]`, ...,
    `[1, x, x^2, .., x^n]` as input. Keyword arguments given to
    :func:`~mpmath.findpoly` are forwarded verbatim to :func:`~mpmath.pslq`. In
    particular, you can specify a tolerance for `P(x)` with ``tol``
    and a maximum permitted coefficient size with ``maxcoeff``.

    For large values of `n`, it is recommended to run :func:`~mpmath.findpoly`
    at high precision; preferably 50 digits or more.

    **Examples**

    By default (degree `n = 1`), :func:`~mpmath.findpoly` simply finds a linear
    polynomial with a rational root::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> findpoly(0.7)
        [-10, 7]

    The generated coefficient list is valid input to ``polyval`` and
    ``polyroots``::

        >>> nprint(polyval(findpoly(phi, 2), phi), 1)
        -2.0e-16
        >>> for r in polyroots(findpoly(phi, 2)):
        ...     print(r)
        ...
        -0.618033988749895
        1.61803398874989

    Numbers of the form `m + n \sqrt p` for integers `(m, n, p)` are
    solutions to quadratic equations. As we find here, `1+\sqrt 2`
    is a root of the polynomial `x^2 - 2x - 1`::

        >>> findpoly(1+sqrt(2), 2)
        [1, -2, -1]
        >>> findroot(lambda x: x**2 - 2*x - 1, 1)
        2.4142135623731

    Despite only containing square roots, the following number results
    in a polynomial of degree 4::

        >>> findpoly(sqrt(2)+sqrt(3), 4)
        [1, 0, -10, 0, 1]

    In fact, `x^4 - 10x^2 + 1` is the *minimal polynomial* of
    `r = \sqrt 2 + \sqrt 3`, meaning that a rational polynomial of
    lower degree having `r` as a root does not exist. Given sufficient
    precision, :func:`~mpmath.findpoly` will usually find the correct
    minimal polynomial of a given algebraic number.

    **Non-algebraic numbers**

    If :func:`~mpmath.findpoly` fails to find a polynomial with given
    coefficient size and tolerance constraints, that means no such
    polynomial exists.

    We can verify that `\pi` is not an algebraic number of degree 3 with
    coefficients less than 1000::

        >>> mp.dps = 15
        >>> findpoly(pi, 3)
        >>>

    It is always possible to find an algebraic approximation of a number
    using one (or several) of the following methods:

        1. Increasing the permitted degree
        2. Allowing larger coefficients
        3. Reducing the tolerance

    One example of each method is shown below::

        >>> mp.dps = 15
        >>> findpoly(pi, 4)
        [95, -545, 863, -183, -298]
        >>> findpoly(pi, 3, maxcoeff=10000)
        [836, -1734, -2658, -457]
        >>> findpoly(pi, 3, tol=1e-7)
        [-4, 22, -29, -2]

    It is unknown whether Euler's constant is transcendental (or even
    irrational). We can use :func:`~mpmath.findpoly` to check that if is
    an algebraic number, its minimal polynomial must have degree
    at least 7 and a coefficient of magnitude at least 1000000::

        >>> mp.dps = 200
        >>> findpoly(euler, 6, maxcoeff=10**6, tol=1e-100, maxsteps=1000)
        >>>

    Note that the high precision and strict tolerance is necessary
    for such high-degree runs, since otherwise unwanted low-accuracy
    approximations will be detected. It may also be necessary to set
    maxsteps high to prevent a premature exit (before the coefficient
    bound has been reached). Running with ``verbose=True`` to get an
    idea what is happening can be useful.
    r   zn cannot be less than 1r$   Nr%   )r/   r+   r4   Úappendr[   )r8   r	   r=   ÚkwargsÚxsrF   Úas          r   Úfindpolyra   7  s“   € ðR 	�‰�‹
€AØˆ1‚uÜÐ2Ó3Ð3ØˆA‚vØ�1ˆvˆØ
�'‰'�!‹*ˆ€BÜ�1�Q�q‘S‹\ò ˆØ
�	‰	�!�Q‘$ŒØˆC�H‰H�RÑ"˜6Ñ"ˆØ‰=Ø‘T�r�T‘7ŠNñ	r   c                 óV   — | |}}|r
|||z  }}|rŒ
|dk7  r
| |z  } ||z  }|dk(  r| S | |fS r   r   )ÚpÚqr	   rK   s       r   Úfracgcdre   ¬  sN   € Øˆa€q€AÙ
Ø�!�a‘%ˆ1ˆò àˆA‚vØ	ˆa‰ˆØ	ˆa‰ˆØˆA‚vØˆØˆaˆ4€Kr   c                 óz  — | d   }| dd  } g }t        t        | «      «      D ]r  }| |   }|sŒt        | |«      }||   d   }|dk(  rd}nd|z   }t        |t        «      r|dkD  rt        |«      |z   }nd|z  |z   }nd|z  |z   }|j                  |«       Œt dj                  |«      }d	|v sd|v rd
|z   dz   }|xs dS )Nr$   r   Ú1Ú Ú*z(%s)z(%s/%s)z + ú+ú(ú)Ú0)r4   r*   re   Ú
isinstancer   Ústrr]   Újoin)	ÚrÚ	constantsrd   rH   rF   rc   ÚzÚcsÚterms	            r   Ú
pslqstringrv   ·  sã   € Ø	ˆ!‰€AØ	ˆ!ˆ"ˆ€AØ
€AÜ”3�q“6‹]ò ˆØˆa‰DˆÚÜ˜˜˜1“ˆAØ˜1‘˜a‘ˆBØ�SŠyØ‘à˜2‘X�Ü˜!œYÔ'Ø�q’5¤ Q£¨"¡™$Ø"(¨1¡*°Ñ!2™$à! A™¨Ñ+�Ø�H‰H�T�Nðð 	�
‰
�1‹€AØ
ˆa�x�3˜!‘8Ø�!‰G�c‰MˆØŠ8�€Or   c                 ó  — | d   }| dd  } g }g }t        t        | «      «      D ]   }| |   }|sŒt        | |«      }||   d   }t        |t        «      r;t        |«      dk(  r|}	n|›dt        |«      ›�}	||g|dk     j                  |	«       Œk|›dt        |d   «      ›d|d   ›d�}	||g|d   dk     j                  |	«       Œ¢ dj                  |«      }dj                  |«      }|r|r	d|›d	|›d�S |r|S |rd
|z  S y )Nr$   r   z**z**(ú/rl   ri   rk   z)/(z1/(%s))r4   r*   re   rn   r   r   r]   rp   )
rq   rr   rd   ÚnumÚdenrF   rc   rs   rt   rJ   s
             r   Ú
prodstringr{   Ï  s  € Ø	ˆ!‰€AØ	ˆ!ˆ"ˆ€AØ
€CØ
€CÜ”3�q“6‹]ò .ˆØˆa‰DˆÚÜ˜˜˜1“ˆAØ˜1‘˜a‘ˆBÜ˜!œYÔ'Ü�q“6˜Q’; B¡Ú02´C¸´FÐ$; Ø�c�˜1˜Q™3‘×'Ñ'¨Õ*â%'¬¨Q¨q©T­°A°a³DÐ9�Ø�c�˜1˜Q™4 ™6Ñ"×*Ñ*¨1Õ-ð.ð �(‰(�3‹-€CØ
�(‰(�3‹-€CÙ
Šsª#ªsÐ3Ð3Ù
�3ˆJÙ
�8˜c‘>Ð!€sr   c                 óÄ  — |dk  r	| | | }}}| | j                  |dz  d|z  |z  z
  «      z   d|z  z  }| | j                  |dz  d|z  |z  z
  «      z
  d|z  z  }t        ||z
  «      t        ||z
  «      k  r4|rd| ›d|dz  d|z  |z  z
  ›dd|z  ›d�}|S dd	|z  |z  ›d
d|z  ›d�}|S |rd| ›d|dz  d|z  |z  z
  ›dd|z  ›d�}|S dd	|z  |z  ›d
d|z  ›d�}|S )Nr$   r   r"   z((z+sqrt(z))/rl   z(sqrt(éüÿÿÿz)/z-sqrt(z(-sqrt()Úsqrtr   )r8   rJ   r`   ÚbÚcÚu1Úu2rH   s           r   Úquadraticstringrƒ   æ  s(  € Øˆ1‚uØ��A�2�q�bˆAˆ!ˆØˆ"ˆS�X‰X�a˜‘d˜1˜Q™3˜q™5‘jÓ!Ñ
! A a¡CÑ	(€BØˆ"ˆS�X‰X�a˜‘d˜1˜Q™3˜q™5‘jÓ!Ñ
! A a¡CÑ	(€BÜ
ˆ2ˆa‰4ƒy”3�r˜!‘t“9ÒÚ¨Aª2¨a°©d°1°Q±3°q±5«j¸¸1»Ð=ˆqð
 €Hð Ø&(¨¡d¨1£f¨Q¨q«SÐ1ˆqð €Hò ¨Aª2¨a°©d°1°Q±3°q±5«j¸¸1»Ð=ˆqà€Hð Ø')¨!¡t¨A£v¨a°«cÐ2ˆqØ€Hr   c                 ó   — ||z  S r   r   ©r8   r	   r€   s      r   ú<lambda>r†   ÷  ó
   € �1�Q‘3€ r   z$y/$cr$   c                 ó   — ||z  S r   r   r…   s      r   r†   r†   ø  r‡   r   z$c*$yc                 ó   — ||z  S r   r   r…   s      r   r†   r†   ù  r‡   r   z$c/$yc                 ó   — ||z  dz  S ©Nr   r   r…   s      r   r†   r†   ú  ó   € �A�a‘C˜!‘8€ r   zsqrt($y)/$cc                 ó   — ||z  dz  S r‹   r   r…   s      r   r†   r†   û  rŒ   r   z$c*sqrt($y)c                 ó   — ||z  dz  S r‹   r   r…   s      r   r†   r†   ü  rŒ   r   z$c/sqrt($y)c                 ó   — ||dz  z  S r‹   r   r…   s      r   r†   r†   ý  ó   € �1�Q˜‘T‘6€ r   zsqrt($y)/sqrt($c)c                 ó   — |dz  |z  S r‹   r   r…   s      r   r†   r†   þ  s   € �1�a‘4˜‘6€ r   zsqrt($c)*sqrt($y)c                 ó   — ||dz  z  S r‹   r   r…   s      r   r†   r†   ÿ  r�   r   zsqrt($c)/sqrt($y)c                 ó*   — | j                  ||z  «      S r   ©r~   r…   s      r   r†   r†      ó   € �3—8‘8˜A˜a™C“=€ r   z$y**2/$cc                 ó*   — | j                  ||z  «      S r   r”   r…   s      r   r†   r†     r•   r   z$c*$y**2c                 ó*   — | j                  ||z  «      S r   r”   r…   s      r   r†   r†     r•   r   z$c/$y**2c                 ó*   — || j                  |«      z  S r   r”   r…   s      r   r†   r†     ó   € �1�S—X‘X˜a“[‘=€ r   z$y**2/$c**2c                 ó*   — | j                  |«      |z  S r   r”   r…   s      r   r†   r†     s   € �3—8‘8˜A“;˜q‘=€ r   z$c**2*$y**2c                 ó*   — || j                  |«      z  S r   r”   r…   s      r   r†   r†     r™   r   z$c**2/$y**2c                 ó*   — | j                  ||z  «      S r   ©Úexpr…   s      r   r†   r†     ó   € �3—7‘7˜1˜Q™3“<€ r   z
log($y)/$cc                 ó*   — | j                  ||z  «      S r   r�   r…   s      r   r†   r†     rŸ   r   z
$c*log($y)c                 ó*   — | j                  ||z  «      S r   r�   r…   s      r   r†   r†     rŸ   r   z
$c/log($y)c                 ó*   — || j                  |«      z  S r   r�   r…   s      r   r†   r†   	  ó   € �1�S—W‘W˜Q“Z‘<€ r   z
log($y/$c)c                 ó*   — | j                  |«      |z  S r   r�   r…   s      r   r†   r†   
  s   € �3—7‘7˜1“:˜a‘<€ r   z
log($c*$y)c                 ó*   — || j                  |«      z  S r   r�   r…   s      r   r†   r†     r£   r   z
log($c/$y)c                 ó*   — | j                  ||z  «      S r   ©Úlnr…   s      r   r†   r†     ó   € �3—6‘6˜!˜A™#“;€ r   z
exp($y)/$cc                 ó*   — | j                  ||z  «      S r   r§   r…   s      r   r†   r†     r©   r   z
$c*exp($y)c                 ó*   — | j                  ||z  «      S r   r§   r…   s      r   r†   r†     r©   r   z
$c/exp($y)c                 ó*   — || j                  |«      z  S r   r§   r…   s      r   r†   r†     ó   € �1�S—V‘V˜A“Y‘;€ r   z
exp($y/$c)c                 ó*   — | j                  |«      |z  S r   r§   r…   s      r   r†   r†     s   € �3—6‘6˜!“9˜Q‘;€ r   z
exp($c*$y)c                 ó*   — || j                  |«      z  S r   r§   r…   s      r   r†   r†     r­   r   z
exp($c/$y)c           
      óÖ  ‡ ‡‡‡— g Šˆˆfd„}‰ j                  |«      }|dk(  r|rdgS y|dk  r5‰ j                  | ||||‰«      }|€|S |r|D �	cg c]  }	d|	z  ‘Œ	 c}	S d|z  S |r‰ j                  |«      }n‰ j                  dz  }|}
|r†t        |t        «      r=t        |j                  «       «      D ��cg c]  \  }}‰ j                  |«      |f‘Œ }}}n;t	        ˆ fd„t        ‰ «      D «       «      }|D �cg c]  }t        ||«      |f‘Œ }}ng }d|D ��cg c]  \  }}|‘Œ	 c}}vr‰ j                  d«      d	fg|z   }t        D �]‚  \  }}}|D �]u  \  }}|r|d	k(  rŒ |‰ ||«      }t        |«      |
d
z  kD  st        |«      |k  rŒ9‰ j                  |g|D �cg c]  }|d   ‘Œ	 c}z   ||
«      }d}	|�'t        d„ |D «       «      |
k  r|d   rt        ||«      }	nx‰ j                  ‰ j                  ||d
z  g||
«      }|�St        |«      dk(  rE|d
   r@|\  }}}t        t        |«      t        |«      t        |«      «      |
k  rt!        ‰ ||||«      }	|	ra|d	k(  r'd|v r#|j#                  d|	«      j#                  dd«      }	n"|j#                  d|	«      j#                  d|«      }	 ||	«       |s	‰d   c c S ‰s�Œkt%        d«       �Œx �Œ… |dk7  rág d¢}g }|D ]=  \  Š}	t'        ˆˆ fd„|D «       «      rŒ|j)                  ‰ j+                  ‰«      |	f«       Œ? |D �cg c]  }‰ j+                  |«      t-        |«      f‘Œ  c}|z   }‰ j                  ‰ j+                  |«      g|D �cg c]  }|d   ‘Œ	 c}z   ||
«      }|�3t        d„ |D «       «      |
k  r|d   r |t/        ||«      «       |s‰d   S |rt        ‰t        ¬«      S yc c}	w c c}}w c c}w c c}}w c c}w c c}w c c}w )an  
    Given a real number `x`, ``identify(x)`` attempts to find an exact
    formula for `x`. This formula is returned as a string. If no match
    is found, ``None`` is returned. With ``full=True``, a list of
    matching formulas is returned.

    As a simple example, :func:`~mpmath.identify` will find an algebraic
    formula for the golden ratio::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> identify(phi)
        '((1+sqrt(5))/2)'

    :func:`~mpmath.identify` can identify simple algebraic numbers and simple
    combinations of given base constants, as well as certain basic
    transformations thereof. More specifically, :func:`~mpmath.identify`
    looks for the following:

        1. Fractions
        2. Quadratic algebraic numbers
        3. Rational linear combinations of the base constants
        4. Any of the above after first transforming `x` into `f(x)` where
           `f(x)` is `1/x`, `\sqrt x`, `x^2`, `\log x` or `\exp x`, either
           directly or with `x` or `f(x)` multiplied or divided by one of
           the base constants
        5. Products of fractional powers of the base constants and
           small integers

    Base constants can be given as a list of strings representing mpmath
    expressions (:func:`~mpmath.identify` will ``eval`` the strings to numerical
    values and use the original strings for the output), or as a dict of
    formula:value pairs.

    In order not to produce spurious results, :func:`~mpmath.identify` should
    be used with high precision; preferably 50 digits or more.

    **Examples**

    Simple identifications can be performed safely at standard
    precision. Here the default recognition of rational, algebraic,
    and exp/log of algebraic numbers is demonstrated::

        >>> mp.dps = 15
        >>> identify(0.22222222222222222)
        '(2/9)'
        >>> identify(1.9662210973805663)
        'sqrt(((24+sqrt(48))/8))'
        >>> identify(4.1132503787829275)
        'exp((sqrt(8)/2))'
        >>> identify(0.881373587019543)
        'log(((2+sqrt(8))/2))'

    By default, :func:`~mpmath.identify` does not recognize `\pi`. At standard
    precision it finds a not too useful approximation. At slightly
    increased precision, this approximation is no longer accurate
    enough and :func:`~mpmath.identify` more correctly returns ``None``::

        >>> identify(pi)
        '(2**(176/117)*3**(20/117)*5**(35/39))/(7**(92/117))'
        >>> mp.dps = 30
        >>> identify(pi)
        >>>

    Numbers such as `\pi`, and simple combinations of user-defined
    constants, can be identified if they are provided explicitly::

        >>> identify(3*pi-2*e, ['pi', 'e'])
        '(3*pi + (-2)*e)'

    Here is an example using a dict of constants. Note that the
    constants need not be "atomic"; :func:`~mpmath.identify` can just
    as well express the given number in terms of expressions
    given by formulas::

        >>> identify(pi+e, {'a':pi+2, 'b':2*e})
        '((-2) + 1*a + (1/2)*b)'

    Next, we attempt some identifications with a set of base constants.
    It is necessary to increase the precision a bit.

        >>> mp.dps = 50
        >>> base = ['sqrt(2)','pi','log(2)']
        >>> identify(0.25, base)
        '(1/4)'
        >>> identify(3*pi + 2*sqrt(2) + 5*log(2)/7, base)
        '(2*sqrt(2) + 3*pi + (5/7)*log(2))'
        >>> identify(exp(pi+2), base)
        'exp((2 + 1*pi))'
        >>> identify(1/(3+sqrt(2)), base)
        '((3/7) + (-1/7)*sqrt(2))'
        >>> identify(sqrt(2)/(3*pi+4), base)
        'sqrt(2)/(4 + 3*pi)'
        >>> identify(5**(mpf(1)/3)*pi*log(2)**2, base)
        '5**(1/3)*pi*log(2)**2'

    An example of an erroneous solution being found when too low
    precision is used::

        >>> mp.dps = 15
        >>> identify(1/(3*pi-4*e+sqrt(8)), ['pi', 'e', 'sqrt(2)'])
        '((11/25) + (-158/75)*pi + (76/75)*e + (44/15)*sqrt(2))'
        >>> mp.dps = 50
        >>> identify(1/(3*pi-4*e+sqrt(8)), ['pi', 'e', 'sqrt(2)'])
        '1/(3*pi + (-4)*e + 2*sqrt(2))'

    **Finding approximate solutions**

    The tolerance ``tol`` defaults to 3/4 of the working precision.
    Lowering the tolerance is useful for finding approximate matches.
    We can for example try to generate approximations for pi::

        >>> mp.dps = 15
        >>> identify(pi, tol=1e-2)
        '(22/7)'
        >>> identify(pi, tol=1e-3)
        '(355/113)'
        >>> identify(pi, tol=1e-10)
        '(5**(339/269))/(2**(64/269)*3**(13/269)*7**(92/269))'

    With ``full=True``, and by supplying a few base constants,
    ``identify`` can generate almost endless lists of approximations
    for any number (the output below has been truncated to show only
    the first few)::

        >>> for p in identify(pi, ['e', 'catalan'], tol=1e-5, full=True):
        ...     print(p)
        ...  # doctest: +ELLIPSIS
        e/log((6 + (-4/3)*e))
        (3**3*5*e*catalan**2)/(2*7**2)
        sqrt(((-13) + 1*e + 22*catalan))
        log(((-6) + 24*e + 4*catalan)/e)
        exp(catalan*((-1/5) + (8/15)*e))
        catalan*(6 + (-6)*e + 15*catalan)
        sqrt((5 + 26*e + (-3)*catalan))/e
        e*sqrt(((-27) + 2*e + 25*catalan))
        log(((-1) + (-11)*e + 59*catalan))
        ((3/20) + (21/20)*e + (3/20)*catalan)
        ...

    The numerical values are roughly as close to `\pi` as permitted by the
    specified tolerance:

        >>> e/log(6-4*e/3)
        3.14157719846001
        >>> 135*e*catalan**2/98
        3.14166950419369
        >>> sqrt(e-13+22*catalan)
        3.14158000062992
        >>> log(24*e-6+4*catalan)-1
        3.14158791577159

    **Symbolic processing**

    The output formula can be evaluated as a Python expression.
    Note however that if fractions (like '2/3') are present in
    the formula, Python's :func:`~mpmath.eval()` may erroneously perform
    integer division. Note also that the output is not necessarily
    in the algebraically simplest form::

        >>> identify(sqrt(2))
        '(sqrt(8)/2)'

    As a solution to both problems, consider using SymPy's
    :func:`~mpmath.sympify` to convert the formula into a symbolic expression.
    SymPy can be used to pretty-print or further simplify the formula
    symbolically::

        >>> from sympy import sympify # doctest: +SKIP
        >>> sympify(identify(sqrt(2))) # doctest: +SKIP
        2**(1/2)

    Sometimes :func:`~mpmath.identify` can simplify an expression further than
    a symbolic algorithm::

        >>> from sympy import simplify # doctest: +SKIP
        >>> x = sympify('-1/(-3/2+(1/2)*5**(1/2))*(3/2-1/2*5**(1/2))**(1/2)') # doctest: +SKIP
        >>> x # doctest: +SKIP
        (3/2 - 5**(1/2)/2)**(-1/2)
        >>> x = simplify(x) # doctest: +SKIP
        >>> x # doctest: +SKIP
        2/(6 - 2*5**(1/2))**(1/2)
        >>> mp.dps = 30 # doctest: +SKIP
        >>> x = sympify(identify(x.evalf(30))) # doctest: +SKIP
        >>> x # doctest: +SKIP
        1/2 + 5**(1/2)/2

    (In fact, this functionality is available directly in SymPy as the
    function :func:`~mpmath.nsimplify`, which is essentially a wrapper for
    :func:`~mpmath.identify`.)

    **Miscellaneous issues and limitations**

    The input `x` must be a real number. All base constants must be
    positive real numbers and must not be rationals or rational linear
    combinations of each other.

    The worst-case computation time grows quickly with the number of
    base constants. Already with 3 or 4 base constants,
    :func:`~mpmath.identify` may require several seconds to finish. To search
    for relations among a large number of constants, you should
    consider using :func:`~mpmath.pslq` directly.

    The extended transformations are applied to x, not the constants
    separately. As a result, ``identify`` will for example be able to
    recognize ``exp(2*pi+3)`` with ``pi`` given as a base constant, but
    not ``2*exp(pi)+3``. It will be able to recognize the latter if
    ``exp(pi)`` is given explicitly as a base constant.

    c                 óD   •— ‰rt        d| «       ‰j                  | «       y )NzFound: )r-   r]   )rH   Ú	solutionsr<   s    €€r   Úaddsolutionzidentify.<locals>.addsolutionë  s   ø€ Ù”E˜) QÔ'Ø×Ñ˜Õr   r$   rm   Nz-(%s)gffffffæ?c              3   ó:   •K  — | ]  }|t        ‰|«      f–— Œ y ­wr   )Úgetattr)r   Únamer8   s     €r   r   zidentify.<locals>.<genexpr>  s   øè ø€ ÒL¸4˜d¤G¨C°Ó$5Ô6ÑLùs   ƒr   rg   r   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r   ©r   Úuws     r   r   zidentify.<locals>.<genexpr>  s   è ø€ Ò$9°¤S¨§WÑ$9ùr    r#   z/$cz$yrh   z$cú.)r   r#   r   é   c           	   3   ó–   •K  — | ]@  }t        ‰j                  ‰j                  ‰«      ‰j                  |«      z  d «      «      –— ŒB y­w)r   N)Úboolra   r¨   )r   rF   r`   r8   s     €€r   r   zidentify.<locals>.<genexpr>8  s6   øè ø€ ÒPÀQ”t˜CŸL™L¨¯©°«°3·6±6¸!³9Ñ)<¸QÓ?×@ÑPùs   ƒAA	c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr   r   r¸   s     r   r   zidentify.<locals>.<genexpr><  s   è ø€ Ò 5¨R¤ R§Ñ 5ùr    )Úkey)r/   ÚidentifyÚepsrn   ÚdictÚsortedÚitemsÚdirÚevalÚ
transformsr   r[   r,   rv   Úoner*   rƒ   Úreplacer-   Úsumr]   r¨   ro   r{   ) r8   r	   rr   r9   r:   Úfullr<   r³   ÚsolrH   ÚMr¶   r'   Ú	namespacerc   ÚvalueÚftÚftnÚredr€   ÚcnrJ   r`   rq   rd   ÚaaÚbbÚccÚilogsÚlogsrF   r²   s    `     `               `        @r   rÀ   rÀ     sí  û€ ðj €Iõð 	�‰�‹
€Að 	ˆA‚vÙ˜˜�ØØˆ1‚uØ�l‰l˜A˜2˜y¨#¨x¸¸wÓGˆØˆ;ØˆJÙØ'*Ö+ !�G˜A“IÒ+Ð+à˜S‘=Ð á
Ø�g‰g�c‹l‰à�g‰g�s‰lˆØ€AáÜ�i¤Ô&Ü=CÀIÇOÁOÓDUÓ=V×W±	°°q˜#Ÿ'™' !›* dÒ+ÐWˆIÒWäÓLÄ3ÀsÃ8ÔLÓLˆIØ:CÖD°Qœ$˜q )Ó,¨aÒ0ÐDˆIÑDàˆ	ð 	¨I×6™=˜D %’Ó6Ñ6Ø—g‘g˜a“j #Ð&Ð'¨)Ñ3ˆ	ô #ó ‰ˆˆC�Øó 	‰EˆAˆrÙ�r˜S’yØÙ�3�q˜“ˆAä�1‹v˜˜1™Š}¤ A£¨¢Øà—‘˜!˜¨iÖ8¨  !£Ò8Ñ8¸#¸qÓAˆAØˆAØˆ}¤Ñ$9°qÔ$9Ó!9¸QÒ!>À1ÀQÂ4Ü˜q )Ó,‘ð —H‘H˜cŸg™g q¨!¨Q©$Ð/°°aÓ8�Ø�=¤S¨£V¨q¢[°Q°q²TØ!"‘J�B˜˜BÜœ3˜r›7¤3 r£7¬3¨r«7Ó3°qÒ8Ü+¨C°°"°R¸Ó;˜ÙØ˜’9 %¨3¡,ØŸ™ D¨!Ó,×4Ñ4°U¸BÓ?‘AàŸ™ D¨!Ó,×4Ñ4°T¸2Ó>�AÙ˜A”Ù I¨a¡LÔ0ãÜ�c–
ò9	ðð@ 	ˆA‚vâˆàˆØò 	,‰DˆAˆqÜÔPÈ%ÔPÕPØ—‘˜SŸV™V A›Y¨˜NÕ+ð	,ð -2Ö2 q�—‘˜“œ3˜q›6Ò"Ò2°TÑ9ˆØ�H‰H�c—f‘f˜Q“i�[°$Ö#7¨Q A a£DÒ#7Ñ7¸¸aÓ@ˆØˆ=œSÑ 5°1Ô 5Ó5¸Ò:¸qÀºtÙœ
 1 dÓ+Ô,Ù 	¨!¡Ð,áÜ�i¤SÔ)Ð)àùòS ,ùó Xùò Eùó
 7ùò  9ùò> 3ùÚ#7s*   ÁOÂ6OÃ8OÄOÆOÌ#O!Í&O&
Ú__main__)Nr   r!   F)r   )Ú__doc__Úlibmp.backendr   Úlibmpr   r   r   Úobjectr   r[   ra   re   rv   r{   rƒ   rÇ   rÀ   r   ÚdoctestÚtestmodr   r   r   ú<module>rà      s¯  ðñõ
 "ß (ò1ô	˜Fô 	ódóL	sòj	òò0"ò.ñ" ˜ Ð#Ù˜ Ð#Ù˜ Ð#Ù˜]¨AÐ.Ù˜]¨AÐ.Ù˜]¨AÐ.ÙÐ.°Ð2ÙÐ.°Ð2ÙÐ.°Ð2Ù  *¨aÐ0Ù  *¨aÐ0Ù  *¨aÐ0Ù  -°Ð3Ù  -°Ð3Ù  -°Ð3Ù ¨qÐ1Ù ¨qÐ1Ù ¨qÐ1Ù ¨qÐ1Ù ¨qÐ1Ù ¨qÐ1Ù ¨aÐ0Ù ¨aÐ0Ù ¨aÐ0Ù ¨aÐ0Ù ¨aÐ0Ù ¨aÐ0ð7€
ð<  " t°dÀØóoðb	 "Ð Ô Ø!)Ð Ô Ø!)Ð Ô ð ˆzÒÛØ€G‡O�OÕð r   