§
    OŠtjÉ[  ã                   óò  — d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddl m!Z!  e!d¦  «        Z"d„ Z# G d„ de$¦  «        Z%ed„ ¦   «         Z&d„ Z'd„ Z(ee	e"d„ ¦   «         ¦   «         ¦   «         Zee"e	d„ ¦   «         ¦   «         ¦   «         Z)d„ Z*d„ Z+ee"e	d„ ¦   «         ¦   «         ¦   «         Z,d„ Z-ee"d„ ¦   «         ¦   «         Z.dd„Z/dS ) aS  
Limits
======

Implemented according to the PhD thesis
https://www.cybertester.com/data/gruntz.pdf, which contains very thorough
descriptions of the algorithm including many examples.  We summarize here
the gist of it.

All functions are sorted according to how rapidly varying they are at
infinity using the following rules. Any two functions f and g can be
compared using the properties of L:

L=lim  log|f(x)| / log|g(x)|           (for x -> oo)

We define >, < ~ according to::

    1. f > g .... L=+-oo

        we say that:
        - f is greater than any power of g
        - f is more rapidly varying than g
        - f goes to infinity/zero faster than g

    2. f < g .... L=0

        we say that:
        - f is lower than any power of g

    3. f ~ g .... L!=0, +-oo

        we say that:
        - both f and g are bounded from above and below by suitable integral
          powers of the other

Examples
========
::
    2 < x < exp(x) < exp(x**2) < exp(exp(x))
    2 ~ 3 ~ -5
    x ~ x**2 ~ x**3 ~ 1/x ~ x**m ~ -x
    exp(x) ~ exp(-x) ~ exp(2x) ~ exp(x)**2 ~ exp(x+exp(-x))
    f ~ 1/f

So we can divide all the functions into comparability classes (x and x^2
belong to one class, exp(x) and exp(-x) belong to some other class). In
principle, we could compare any two functions, but in our algorithm, we
do not compare anything below the class 2~3~-5 (for example log(x) is
below this), so we set 2~3~-5 as the lowest comparability class.

Given the function f, we find the list of most rapidly varying (mrv set)
subexpressions of it. This list belongs to the same comparability class.
Let's say it is {exp(x), exp(2x)}. Using the rule f ~ 1/f we find an
element "w" (either from the list or a new one) from the same
comparability class which goes to zero at infinity. In our example we
set w=exp(-x) (but we could also set w=exp(-2x) or w=exp(-3x) ...). We
rewrite the mrv set using w, in our case {1/w, 1/w^2}, and substitute it
into f. Then we expand f into a series in w::

    f = c0*w^e0 + c1*w^e1 + ... + O(w^en),       where e0<e1<...<en, c0!=0

but for x->oo, lim f = lim c0*w^e0, because all the other terms go to zero,
because w goes to zero faster than the ci and ei. So::

    for e0>0, lim f = 0
    for e0<0, lim f = +-oo   (the sign depends on the sign of c0)
    for e0=0, lim f = lim c0

We need to recursively compute limits at several places of the algorithm, but
as is shown in the PhD thesis, it always finishes.

Important functions from the implementation:

compare(a, b, x) compares "a" and "b" by computing the limit L.
mrv(e, x) returns list of most rapidly varying (mrv) subexpressions of "e"
rewrite(e, Omega, x, wsym) rewrites "e" in terms of w
leadterm(f, x) returns the lowest power term in the series of f
mrv_leadterm(e, x) returns the lead term (c0, e0) for e
limitinf(e, x) computes lim e  (for x->oo)
limit(e, z, z0) computes any limit by converting it to the case x->oo

All the functions are really simple and straightforward except
rewrite(), which is the most difficult/complex part of the algorithm.
When the algorithm fails, the bugs are usually in the series expansion
(i.e. in SymPy) or in rewrite.

This code is almost exact rewrite of the Maple code inside the Gruntz
thesis.

Debugging
---------

Because the gruntz algorithm is highly recursive, it's difficult to
figure out what went wrong inside a debugger. Instead, turn on nice
debug prints by defining the environment variable SYMPY_DEBUG. For
example:

[user@localhost]: SYMPY_DEBUG=True ./bin/isympy

In [1]: limit(sin(x)/x, x, 0)
limitinf(_x*sin(1/_x), _x) = 1
+-mrv_leadterm(_x*sin(1/_x), _x) = (1, 0)
| +-mrv(_x*sin(1/_x), _x) = set([_x])
| | +-mrv(_x, _x) = set([_x])
| | +-mrv(sin(1/_x), _x) = set([_x])
| |   +-mrv(1/_x, _x) = set([_x])
| |     +-mrv(_x, _x) = set([_x])
| +-mrv_leadterm(exp(_x)*sin(exp(-_x)), _x, set([exp(_x)])) = (1, 0)
|   +-rewrite(exp(_x)*sin(exp(-_x)), set([exp(_x)]), _x, _w) = (1/_w*sin(_w), -_x)
|     +-sign(_x, _x) = 1
|     +-mrv_leadterm(1, _x) = (1, 0)
+-sign(0, _x) = 0
+-limitinf(1, _x) = 1

And check manually which line is wrong. Then go to the source code and
debug this function to figure out the exact problem.

é    )Úreduce)ÚBasicÚSÚMulÚ	PoleError)Úcacheit)ÚAppliedUndef)Úilcm)ÚIÚoo)ÚDummyÚWild)Ú	bottom_up)ÚlogÚexpÚsign)ÚOrder)Údebug_decorator)ÚtimethisÚgruntzc                 ó¾  — t          | ¦  «        t          |¦  «        }}t          | t          ¦  «        r8t          | t          ¦  «        s| j        r| j        t          j        k    r| j        }t          |t          ¦  «        r8t          |t          ¦  «        s|j        r|j        t          j        k    r|j        }t          ||z  |¦  «        }|dk    rdS |j	        rdS dS )z/Returns "<" if a<b, "=" for a == b, ">" for a>br   ú<ú>ú=)
r   Ú
isinstancer   r   Úis_PowÚbaser   ÚExp1ÚlimitinfÚis_infinite)ÚaÚbÚxÚlaÚlbÚcs         úQ/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/series/gruntz.pyÚcomparer(   ‰   sÐ   € õ �‰VŒV•S˜‘V”Vˆ€BÝ�!•UÑÔð ¥¨A­sÑ!3Ô!3ð ¸¼ð ÀQÄVÍqÌvÒEUÐEUØŒUˆÝ�!•UÑÔð ¥¨A­sÑ!3Ô!3ð ¸¼ð ÀQÄVÍqÌvÒEUÐEUØŒUˆå��B‘˜ÑÔ€AØˆA‚v€vØˆsØ	
Œð Øˆsàˆsó    c                   óH   ‡ — e Zd ZdZd„ Zˆ fd„Zd„ Zd„ Zd„ Zd
d„Z	d	„ Z
ˆ xZS )ÚSubsSeta~  
    Stores (expr, dummy) pairs, and how to rewrite expr-s.

    Explanation
    ===========

    The gruntz algorithm needs to rewrite certain expressions in term of a new
    variable w. We cannot use subs, because it is just too smart for us. For
    example::

        > Omega=[exp(exp(_p - exp(-_p))/(1 - 1/_p)), exp(exp(_p))]
        > O2=[exp(-exp(_p) + exp(-exp(-_p))*exp(_p)/(1 - 1/_p))/_w, 1/_w]
        > e = exp(exp(_p - exp(-_p))/(1 - 1/_p)) - exp(exp(_p))
        > e.subs(Omega[0],O2[0]).subs(Omega[1],O2[1])
        -1/w + exp(exp(p)*exp(-exp(-p))/(1 - 1/p))

    is really not what we want!

    So we do it the hard way and keep track of all the things we potentially
    want to substitute by dummy variables. Consider the expression::

        exp(x - exp(-x)) + exp(x) + x.

    The mrv set is {exp(x), exp(-x), exp(x - exp(-x))}.
    We introduce corresponding dummy variables d1, d2, d3 and rewrite::

        d3 + d1 + x.

    This class first of all keeps track of the mapping expr->variable, i.e.
    will at this stage be a dictionary::

        {exp(x): d1, exp(-x): d2, exp(x - exp(-x)): d3}.

    [It turns out to be more convenient this way round.]
    But sometimes expressions in the mrv set have other expressions from the
    mrv set as subexpressions, and we need to keep track of that as well. In
    this case, d3 is really exp(x - d2), so rewrites at this stage is::

        {d3: exp(x-d2)}.

    The function rewrite uses all this information to correctly rewrite our
    expression in terms of w. In this case w can be chosen to be exp(-x),
    i.e. d2. The correct rewriting then is::

        exp(-w)/w + 1/w + x.
    c                 ó   — i | _         d S ©N)Úrewrites©Úselfs    r'   Ú__init__zSubsSet.__init__Ê   s   € ØˆŒˆˆr)   c                 ó~   •— t          ¦   «                              ¦   «         dz   | j                             ¦   «         z   S )Nz, )ÚsuperÚ__repr__r.   )r0   Ú	__class__s    €r'   r4   zSubsSet.__repr__Í   s1   ø€ Ý‰wŒw×ÒÑ!Ô! DÑ(¨4¬=×+AÒ+AÑ+CÔ+CÑCÐCr)   c                 ób   — || vrt          ¦   «         | |<   t                               | |¦  «        S r-   )r   ÚdictÚ__getitem__)r0   Úkeys     r'   r8   zSubsSet.__getitem__Ð   s.   € Ø�dˆ?ˆ?Ý™œˆD�‰IÝ×Ò  cÑ*Ô*Ð*r)   c                 óh   — |                       ¦   «         D ]\  }}|                     ||i¦  «        }Œ|S )z)Substitute the variables with expressions)ÚitemsÚxreplace)r0   ÚeÚexprÚvars       r'   Údo_subszSubsSet.do_subsÕ   s9   € àŸš™œð 	(ð 	(‰IˆD�#Ø—
’
˜C ˜;Ñ'Ô'ˆAˆAØˆr)   c                 óÈ   — t          |                      ¦   «         ¦  «                             t          |                     ¦   «         ¦  «        ¦  «        t          ¦   «         k    S )z;Tell whether or not self and s2 have non-empty intersection)ÚsetÚkeysÚintersectionÚlist)r0   Ús2s     r'   ÚmeetszSubsSet.meetsÛ   s=   € å�4—9’9‘;”;ÑÔ×,Ò,­T°"·'²'±)´)©_¬_Ñ=Ô=ÅÁÄÒFÐFr)   Nc                 óJ  — |                       ¦   «         }i }|                     ¦   «         D ]9\  }}|| v r+|r|                     |||         i¦  «        }||         ||<   Œ4|||<   Œ:|j                             ¦   «         D ]"\  }}|                     |¦  «        |j        |<   Œ#||fS )z0Compute the union of self and s2, adjusting exps)Úcopyr;   r<   r.   )r0   rF   ÚexpsÚresÚtrr>   r?   Úrewrs           r'   ÚunionzSubsSet.unionß   sº   € à�iŠi‰kŒkˆØˆØŸš™œð 	 ð 	 ‰IˆD�#Ø�tˆ|ˆ|Øð ;ØŸ=š=¨#¨s°4¬yÐ)9Ñ:Ô:�DØ˜dœ)��3‘�à��D‘	�	Øœ×*Ò*Ñ,Ô,ð 	2ð 	2‰IˆC�Ø $§¢¨bÑ 1Ô 1ˆCŒL˜ÑÐØ�DˆyÐr)   c                 óœ   — t          ¦   «         }| j                             ¦   «         |_        |                      ¦   «         D ]
\  }}|||<   Œ|S )z Create a shallow copy of SubsSet)r+   r.   rI   r;   )r0   Úrr>   r?   s       r'   rI   zSubsSet.copyî   sK   € å‰IŒIˆØ”]×'Ò'Ñ)Ô)ˆŒ
ØŸš™œð 	ð 	‰IˆD�#ØˆAˆd‰GˆGØˆr)   r-   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r4   r8   r@   rG   rN   rI   Ú__classcell__)r5   s   @r'   r+   r+   ›   sª   ø€ € € € € ð-ð -ð\ð ð ðDð Dð Dð Dð Dð+ð +ð +ð
ð ð ðGð Gð Gðð ð ð ðð ð ð ð ð ð r)   r+   c           	      ó¾  ‡‡— ddl m}  || dd¬¦  «        } t          | t          ¦  «        st	          d¦  «        ‚|                      ‰¦  «        st          ¦   «         | fS | ‰k    rt          ¦   «         }||‰         fS | j        s| j        r·|  	                    ‰¦  «        \  }}|j
        | j
        k    r+t          |‰¦  «        \  }}||  
                    ||¦  «        fS |                     ¦   «         \  }}t          |‰¦  «        \  }	}
t          |‰¦  «        \  }}t          |	||  
                    ||
|¦  «        ‰¦  «        S | j        r´| j        t           j        k    rŸt           j        }
| j        r| j        }|
| j        z  }
|} | j        °|dk    rt          ¦   «         |fS |
                     ‰¦  «        r-t          t'          |
t)          |¦  «        z  ¦  «        ‰¦  «        S t          |‰¦  «        \  }}|||
z  fS t          | t(          ¦  «        r/t          | j        d         ‰¦  «        \  }}|t)          |¦  «        fS t          | t&          ¦  «        s| j        �r;| j        t           j        k    �r%t          | j        t(          ¦  «        r t          | j        j        d         ‰¦  «        S t-          | j        ‰¦  «        }t/          d„ t1          j        |¦  «        D ¦   «         ¦  «        r‚t          ¦   «         }	|	|          }
t          | j        ‰¦  «        \  }}|	                     |¦  «        d         }t'          |¦  «        |j        |
<   t9          |	|
|t'          |¦  «        ||
‰¦  «        S t          | j        ‰¦  «        \  }}|t'          |¦  «        fS t          | t:          ¦  «        rt=          d	¦  «        ‚| j        rrˆfd
„| j        D ¦   «         }d„ |D ¦   «         }tA          |¦  «        dk    rtC          d¦  «        ‚|d         t          ¦   «         c}Šˆfd„|D ¦   «         }| | j
        |Ž fS | j"        rtC          d¦  «        ‚tC          d| z  ¦  «        ‚)zoReturns a SubsSet of most rapidly varying (mrv) subexpressions of 'e',
       and e rewritten in terms of theser   ©ÚpowsimpTr   ©ÚdeepÚcombineú e should be an instance of Basicé   c              3   ó$   K  — | ]}|j         V — Œd S r-   )r    )Ú.0Ú_s     r'   ú	<genexpr>zmrv.<locals>.<genexpr>&  s$   è è € Ð8Ð8 ˆqŒ}Ð8Ð8Ð8Ð8Ð8Ð8r)   z8MRV set computation for UndefinedFunction is not allowedc                 ó0   •— g | ]}t          |‰¦  «        ‘ŒS © )Úmrv)r_   r!   r#   s     €r'   ú
<listcomp>zmrv.<locals>.<listcomp>3  s!   ø€ Ð'Ð'Ð'˜1�S��A‰YŒYÐ'Ð'Ð'r)   c                 ó<   — g | ]\  }}|t          ¦   «         k    ¯|‘ŒS rc   )r+   )r_   Úsr`   s      r'   re   zmrv.<locals>.<listcomp>4  s%   € Ð2Ð2Ð2‘F�Q˜ 1­©	¬	¢> >ˆa > > >r)   zGMRV set computation for functions in several variables not implemented.c                 óF   •— g | ]}‰                      |d          ¦  «        ‘ŒS )r]   )r@   )r_   r#   Ússs     €r'   re   zmrv.<locals>.<listcomp>:  s)   ø€ Ð,Ð,Ð, Q�—
’
˜1˜Qœ4Ñ Ô Ð,Ð,Ð,r)   z8MRV set computation for derivatives not implemented yet.z+Don't know how to calculate the mrv of '%s')#Úsympy.simplify.powsimprX   r   r   Ú	TypeErrorÚhasr+   Úis_MulÚis_AddÚas_independentÚfuncrd   Úas_two_termsÚmrv_max1r   r   r   r   ÚOner   r   Úargsr   Úanyr   Ú	make_argsrN   r.   Úmrv_max3r	   Ú
ValueErrorÚis_FunctionÚlenÚNotImplementedErrorÚis_Derivative)r=   r#   rX   rg   ÚiÚdr>   r!   r"   Ús1Úe1rF   Úe2Úb1ÚliÚsuÚlÚl2rt   ri   s    `                 @r'   rd   rd   ÷   sU  øø€ ð /Ð.Ð.Ð.Ð.Ð.Øˆ�˜ eÐ,Ñ,Ô,€AÝ�a�ÑÔð <ÝÐ:Ñ;Ô;Ð;Ø�5Š5�‰8Œ8ð ?;Ý‰yŒy˜!ˆ|ÐØ	
ˆaŠˆÝ‰IŒIˆØ�!�A”$ˆwˆØ	
Œð :;�Q”Xð :;Ø×Ò Ñ"Ô"‰ˆˆ1ØŒ6�Q”VÒÐÝ˜!˜Q‘i”i‰GˆAˆtØ�a—f’f˜Q ‘o”oÐ%Ð%Ø�~Š~ÑÔ‰ˆˆ1Ý�Q˜‘”‰ˆˆBÝ�Q˜‘”‰ˆˆBÝ˜˜B §¢ q¨"¨bÑ 1Ô 1°1Ñ5Ô5Ð5Ø	
Œð 1;�a”f¥¤Ò&Ð&ÝŒUˆØŒhð 	Ø”ˆBØ�!”%‰KˆBØˆAð Œhð 	ð �Š7ˆ7Ý‘9”9˜b�=Ð Ø�6Š6�!‰9Œ9ð 	Ý•s˜2�c "™gœg™:‘”¨Ñ*Ô*Ð*å˜"˜a‘j”j‰GˆAˆtØ�d˜B‘h�;ÐÝ	�A•sÑ	Ô	ð $;Ý�a”f˜Q”i Ñ#Ô#‰ˆˆ4Ø•#�d‘)”)ˆ|ÐÝ	�A•sÑ	Ô	ð !; ¤ñ !;¨Q¬Vµq´vÒ-=Ñ-=õ �a”e�SÑ!Ô!ð 	)Ý�q”u”z !”} aÑ(Ô(Ð(õ �a”e˜QÑÔˆÝÐ8Ð8¥c¤m°BÑ&7Ô&7Ð8Ñ8Ô8Ñ8Ô8ð 		 Ý‘”ˆBØ�A”ˆBÝ˜œ ‘]”]‰FˆB�Ø—’˜"‘”˜a”ˆBÝ! "™gœgˆBŒK˜‰OÝ˜B  B­¨B©¬°°R¸Ñ;Ô;Ð;å˜!œ% ‘m”m‰GˆAˆtØ•c˜$‘i”i�<ÐÝ	�A•|Ñ	$Ô	$ð ;ÝÐSÑTÔTÐTØ	
Œð ;Ø'Ð'Ð'Ð' ¤Ð'Ñ'Ô'ˆØ2Ð2˜aÐ2Ñ2Ô2ˆÝˆr‰7Œ7�aŠ<ˆ<å%ð 'Lñ Mô Mð Mà�1”•w‘y”yˆˆˆ2Ø,Ð,Ð,Ð,¨!Ð,Ñ,Ô,ˆØ�&�!”&˜$�-ÐÐØ	
Œð ;Ý!ð #:ñ ;ô ;ð 	;å
Ø5¸Ñ9ñ;ô ;ð ;r)   c                 ó,  — t          | t          ¦  «        st          d¦  «        ‚t          |t          ¦  «        st          d¦  «        ‚| t          ¦   «         k    r||fS |t          ¦   «         k    r| |fS |                      |¦  «        r||fS t	          t          |                      ¦   «         ¦  «        d         t          |                     ¦   «         ¦  «        d         |¦  «        }|dk    r| |fS |dk    r||fS |dk    rt          d¦  «        ‚||fS )a)  
    Computes the maximum of two sets of expressions f and g, which
    are in the same comparability class, i.e. max() compares (two elements of)
    f and g and returns either (f, expsf) [if f is larger], (g, expsg)
    [if g is larger] or (union, expsboth) [if f, g are of the same class].
    z"f should be an instance of SubsSetz"g should be an instance of SubsSetr   r   r   r   zc should be =)r   r+   rk   rG   r(   rE   rC   rx   )ÚfÚexpsfÚgÚexpsgrN   Úexpsbothr#   r&   s           r'   rw   rw   C  s
  € õ �a�Ñ!Ô!ð >ÝÐ<Ñ=Ô=Ð=Ý�a�Ñ!Ô!ð >ÝÐ<Ñ=Ô=Ð=Ø�G‰IŒI‚~€~Ø�%ˆxˆØ	
�g‰iŒiŠˆØ�%ˆxˆØ	
�Š�‰Œð Ø�hˆÐå•�Q—V’V‘X”X‘”˜qÔ!¥4¨¯ª©¬¡>¤>°!Ô#4°aÑ8Ô8€AØˆC‚x€xØ�%ˆxˆØ	
ˆcŠˆØ�%ˆxˆà�Š8ˆ8Ý˜_Ñ-Ô-Ð-Ø�hˆÐr)   c           	      óª   — |                       ||¦  «        \  }}t          | |                     |¦  «        ||                      |¦  «        |||¦  «        S )ag  Computes the maximum of two sets of expressions f and g, which
    are in the same comparability class, i.e. mrv_max1() compares (two elements of)
    f and g and returns the set, which is in the higher comparability class
    of the union of both, if they have the same order of variation.
    Also returns exps, with the appropriate substitutions made.
    )rN   rw   r@   )rˆ   rŠ   rJ   r#   Úur"   s         r'   rr   rr   `  sP   € ð �7Š7�1�dÑÔ�D€A€qÝ�A�q—y’y ‘”¨¨1¯9ª9°T©?¬?Ø�q˜!ñô ð r)   c                 ó&  — t          | t          ¦  «        st          d¦  «        ‚| j        rdS | j        rdS | j        rdS |                      |¦  «        s ddlm}  || ¦  «        } t          | ¦  «        S | |k    rdS | j
        r>|                      ¦   «         \  }}t          ||¦  «        }|sdS |t          ||¦  «        z  S t          | t          ¦  «        rdS | j        rK| j        t           j        k    rdS t          | j        |¦  «        }|dk    rdS | j        j        r
|| j        z  S nEt          | t&          ¦  «        r0| j        d         j        rt          | j        d         dz
  |¦  «        S t+          | |¦  «        \  }}t          ||¦  «        S )a  
    Returns a sign of an expression e(x) for x->oo.

    ::

        e >  0 for x sufficiently large ...  1
        e == 0 for x sufficiently large ...  0
        e <  0 for x sufficiently large ... -1

    The result of this function is currently undefined if e changes sign
    arbitrarily often for arbitrarily large x (e.g. sin(x)).

    Note that this returns zero only if e is *constantly* zero
    for x sufficiently large. [If e is constant, of course, this is just
    the same thing as the sign of e.]
    r\   r]   éÿÿÿÿr   )Ú
logcombine)r   r   rk   Úis_positiveÚis_negativeÚis_zerorl   Úsympy.simplifyr‘   Ú_signrm   rq   r   r   r   r   r   r   Ú
is_Integerr   rt   Úmrv_leadterm)	r=   r#   r‘   r!   r"   Úsarg   Úc0Úe0s	            r'   r   r   l  s³  € õ( �a�ÑÔð <ÝÐ:Ñ;Ô;Ð;à„}ð &ØˆqØ	
Œð &ØˆrØ	
Œð &Øˆqà�UŠU�1‰XŒXð &Ø-Ð-Ð-Ð-Ð-Ð-ØˆJ�q‰MŒMˆÝ�Q‰xŒxˆØ	
ˆaŠˆØˆqØ	
Œð &Ø�~Š~ÑÔ‰ˆˆ1Ý�!�Q‰ZŒZˆØð 	Ø�1Ø•D˜˜A‘J”J‰ÐÝ	�A•sÑ	Ô	ð &ØˆqØ	
Œð 	&ØŒ6•Q”VÒÐØ�1Ý�”˜‰OŒOˆØ�Š6ˆ6Ø�1ØŒ5Ôð 	Ø�a”e‘8ˆOð	å	�A•sÑ	Ô	ð & ¤ q¤	Ô 5ð &Ý�A”F˜1”I ‘M 1Ñ%Ô%Ð%õ ˜!˜QÑÔ�F€BˆÝ��A‰;Œ;Ðr)   c                 ó(  — | }|                       |¦  «        s| S ddlm} ddlm} |                       t
          ¦  «        r&|                      ¦   «                              ¦   «         } |j        r|j	        r)t          dd¬¦  «        }|                      ||¦  «        } |}|                      dd|¬¦  «        }  || ¦  «        } t          | |¦  «        rLt          | j        |¦  «        t          | j        |¦  «        k    rt"          ‚t          | j        |¦  «        \  }}nt          | |¦  «        \  }}t%          ||¦  «        }|d	k    rt&          j        S |d
k    rk|                     t,          t/          dt,          g¬¦  «        z  ¦  «        r
|t0          z  S t%          ||¦  «        }	|	dk    rt3          d¦  «        ‚|	t0          z  S |dk    r*||k    r|                     ¦   «         }t7          ||¦  «        S t3          d                     |¦  «        ¦  «        ‚)zLimit e(x) for x-> oo.r   )Ú	powdenest©ÚAccumBoundsÚpT©ÚpositiveÚ	tractable)rZ   Úlimitvarr]   r�   r!   )ÚexcludezLeading term should not be 0z{} could not be evaluated)rl   rj   r�   Úsympy.calculus.utilrŸ   r   ÚexpandÚremoveOr’   Ú
is_integerr   ÚsubsÚrewriter   r˜   ÚminÚmaxr{   r   r   ÚZeroÚmatchr   r   r   rx   Úcancelr   Úformat)
r=   r#   Úoldr�   rŸ   r    rš   r›   Úsigrg   s
             r'   r   r   ¨  s  € ð €CØ�5Š5�‰8Œ8ð ØˆØ0Ð0Ð0Ð0Ð0Ð0Ø/Ð/Ð/Ð/Ð/Ð/Ø‡u‚u�U�|„|ð !Ø�HŠH‰JŒJ×ÒÑ Ô ˆØŒ=ð ˜AœLð õ �# Ð%Ñ%Ô%ˆØ�FŠF�1�a‰LŒLˆØˆØ	�	Š	�+ D°1ˆ	Ñ5Ô5€AØˆ	�!‰Œ€AÝ�!�[Ñ!Ô!ð $Ý˜œ˜qÑ!Ô!¥\°!´%¸Ñ%;Ô%;Ò;Ð;Ý%Ð%Ý˜aœe QÑ'Ô'‰ˆˆBˆBå˜a Ñ#Ô#‰ˆˆBÝ
ˆr�1‰+Œ+€CØ
ˆa‚x€xÝŒvˆØ	�ŠˆØ�8Š8•A•d˜3­¨Ð,Ñ,Ô,Ñ,Ñ-Ô-ð 	Ø•b‘5ˆLÝ��Q‰KŒKˆà�Š6ˆ6ÝÐ;Ñ<Ô<Ð<Ø•‰tˆØ	�ŠˆØ�Š9ˆ9Ø—’‘”ˆBÝ˜˜A‰ŒÐåÐ4×;Ò;¸CÑ@Ô@ÑAÔAÐAr)   c           	      óP  — t          ¦   «         }|                      ¦   «         D ],\  }}|||                     |t          |¦  «        i¦  «        <   Œ-| j                             ¦   «         D ]<\  }}| j        |                              |t          |¦  «        i¦  «        |j        |<   Œ=|S r-   )r+   r;   r<   r   r.   )rg   r#   rP   r>   r?   s        r'   Úmoveup2rµ   Ø  sœ   € Ý‰	Œ	€AØ—W’W‘Y”Yð ,ð ,‰	ˆˆcØ(+ˆˆ$�-Š-˜�C ™FœF˜Ñ
$Ô
$Ñ%Ð%Ø”Z×%Ò%Ñ'Ô'ð @ð @‰	ˆˆTØœ* Sœ/×2Ò2°Aµs¸1±v´v°;Ñ?Ô?ˆŒ
�3‰ˆØ€Hr)   c                 ó    ‡— ˆfd„| D ¦   «         S )Nc                 óX   •— g | ]&}|                      ‰t          ‰¦  «        i¦  «        ‘Œ'S rc   )r<   r   )r_   r=   r#   s     €r'   re   zmoveup.<locals>.<listcomp>â  s/   ø€ Ð/Ð/Ð/¨ˆA�JŠJ˜�3˜q™6œ6�{Ñ#Ô#Ð/Ð/Ð/r)   rc   )r…   r#   s    `r'   Úmoveupr¸   á  s   ø€ Ø/Ð/Ð/Ð/¨QÐ/Ñ/Ô/Ð/r)   c                 óè  ‡— t          ¦   «         }|                      ‰¦  «        s| t          j        fS |t          ¦   «         k    rt	          | ‰¦  «        \  }}|s|t          j        fS ‰|v r+t          |‰¦  «        }t          |g‰¦  «        d         }|}|}t          dd¬¦  «        }t          ||‰|¦  «        \  }}| 	                    ˆfd„d„ ¦  «        }	 | 
                    ||¬¦  «        }	�nR# t          t          t          f$ �r7 d}
t          d¦  «        }t          j        }|j        r'|                     ||
|z   |¬	¦  «        }|d
z  }|j        °'|                     ¦   «                              ¦   «         }	 | 
                    ||¬¦  «        }	nª# t          t          t          f$ r� |                     |¦  «        }	|	d                              |¦  «        r]|                     ¦   «         d                              |¦  «        }|                     ¦   «         d         }|d         |z  |d         |z  f}	Y nw xY wY nw xY w|	d                              t/          |¦  «        |¦  «        |	d         fS )zReturns (c0, e0) for e.r   ÚwTr¡   c                 ó<   •— | j         o|                      ‰¦  «        S r-   )r   rl   )rˆ   r#   s    €r'   ú<lambda>zmrv_leadterm.<locals>.<lambda>  s   ø€ ˜AœHÐ1¨¯ª¨q©¬€ r)   c                 óT   — t          t          | j        ¦  «        | j         z  ¦  «        S r-   )r   r   r   )rˆ   s    r'   r¼   zmrv_leadterm.<locals>.<lambda>  s   € ½SÅÀQÄVÁÄÈQÌUÑARÑ=SÔ=S€ r)   )Úlogxr]   )Únr¾   é   )r+   rl   r   r®   rd   rµ   r¸   r   r«   ÚreplaceÚleadtermr{   r   rx   r   rs   Úis_OrderÚ_eval_nseriesr§   r¨   Úas_coeff_exponentÚas_base_exprª   r   )r=   r#   ÚOmegarJ   ÚOmega_upÚexps_uprº   rˆ   ÚlogwÚltÚn0Ú_seriesÚincrÚseriesr   Úexs    `              r'   r˜   r˜   å  st  ø€ õ
 ‰IŒI€EØ�5Š5�‰8Œ8ð Ø•1”6ˆ{ÐØ•‘	”	ÒÐÝ˜!˜Q‘i”i‰ˆˆtØð à•Q”Vˆ|ÐØˆE€z€zå˜5 !Ñ$Ô$ˆÝ˜$˜ Ñ#Ô# AÔ&ˆàˆØˆõ 	ˆc˜DÐ!Ñ!Ô!€AÝ�d˜E 1 aÑ(Ô(�G€A€tð 	
�	Š	Ð1Ð1Ð1Ð1Ð3SÐ3SÑTÔT€Að/Ø�ZŠZ˜ ˆZÑ%Ô%ˆ‰øÝ¥­JÐ7ð /ñ /ð /ØˆÝ˜‘(”(ˆÝŒuˆØÔð 	Ø—o’o a¨2¨d©7¸�oÑ>Ô>ˆGØ�A‰IˆDð Ôð 	ð —’Ñ!Ô!×)Ò)Ñ+Ô+ˆð	/Ø—’ ¨�Ñ.Ô.ˆBˆBøÝ#¥Yµ
Ð;ð 	/ð 	/ð 	/Ø×$Ò$ QÑ'Ô'ˆBØ�!Œu�yŠy˜‰|Œ|ð /Ø—}’}‘” qÔ)×;Ò;¸AÑ>Ô>�Ø—]’]‘_”_ QÔ'�Ø˜1”g˜r‘k 4¨¤7¨2¡:Ð.�øøð	/øøøøøð/øøøð  ˆqŒE�JŠJ•s˜1‘v”v˜tÑ$Ô$ b¨¤eÐ,Ð,s8   ÃC1 Ã1B	I Å;FÆI ÆB$H:È7I È9H:È:I È?I c                 ó   —  G d„ d¦  «        }i }| D ]"\  }} |¦   «         }||_         ||_        |||<   Œ#| D ]V\  }}||v rM||         }||         }| D ]:\  }}	|                     |	¦  «        r |j                             ||	         ¦  «         Œ;ŒW|S )aÃ   Helper function for rewrite.

    We need to sort Omega (mrv set) so that we replace an expression before
    we replace any expression in terms of which it has to be rewritten::

        e1 ---> e2 ---> e3
                 \
                  -> e4

    Here we can do e1, e2, e3, e4 or e1, e2, e4, e3.
    To do this we assemble the nodes into a tree, and sort them by height.

    This function builds the tree, rewrites then sorts the nodes.
    c                   ó   — e Zd Zd„ Zd„ ZdS )ú#build_expression_tree.<locals>.Nodec                 ó0   — g | _         d | _        d | _        d S r-   )Úbeforer>   r?   r/   s    r'   r1   z,build_expression_tree.<locals>.Node.__init__,  s   € ØˆDŒKØˆDŒIØˆDŒHˆHˆHr)   c                 óD   — t          d„ d„ | j        D ¦   «         d¦  «        S )Nc                 ó   — | |z   S r-   rc   )r#   Úys     r'   r¼   z8build_expression_tree.<locals>.Node.ht.<locals>.<lambda>1  s
   €  q¨1¡u€ r)   c                 ó6   — g | ]}|                      ¦   «         ‘ŒS rc   ©Úht)r_   r#   s     r'   re   z:build_expression_tree.<locals>.Node.ht.<locals>.<listcomp>2  s    € Ð7Ð7Ð7 a˜1Ÿ4š4™6œ6Ð7Ð7Ð7r)   r]   )r   rÕ   r/   s    r'   rÛ   z&build_expression_tree.<locals>.Node.ht0  s/   € ÝÐ,Ð,Ø7Ð7¨4¬;Ð7Ñ7Ô7¸ñ<ô <ð <r)   N)rQ   rR   rS   r1   rÛ   rc   r)   r'   ÚNoderÓ   +  s2   € € € € € ð	ð 	ð 	ð	<ð 	<ð 	<ð 	<ð 	<r)   rÜ   )r?   r>   rl   rÕ   Úappend)
rÇ   r.   rÜ   Únodesr>   Úvr¿   r`   rP   Úv2s
             r'   Úbuild_expression_treerá     sÚ   € ð<ð <ð <ð <ð <ñ <ô <ð <ð €EØð ð ‰ˆˆaØˆD‰FŒFˆØˆŒØˆŒØˆˆa‰ˆØð /ð /‰ˆˆ1Ø�ˆ=ˆ=Ø�a”ˆAØ˜”ˆAØð /ð /‘��2Ø—5’5˜‘9”9ð /Ø”H—O’O E¨"¤IÑ.Ô.Ð.øøà€Lr)   c           	      óR  ‡— ddl m} t          |t          ¦  «        st	          d¦  «        ‚t          |¦  «        dk    rt          d¦  «        ‚|                     ¦   «         D ]&}t          |t          ¦  «        st          d¦  «        ‚Œ'|j	        }t          |                     ¦   «         ¦  «        }t          ||¦  «        Š|                     ˆfd„d¬¦  «         |D ]M\  }}t          |j        |¦  «        }	|	d	k    r-|	d
k    r'|	                     |¦  «        st!          d|	z  ¦  «        ‚ŒN|	d	k    rd	|z  }g }
g }|D ]À\  }}t#          |j        |j        z  |¦  «        }|j        r|                     |j        ¦  «         |j        }||v r=t          ||         t          ¦  «        st          d¦  «        ‚||         j        d         }|
                     |t          |||j        z  z
  ¦  «        ||z  z  f¦  «         ŒÁddlm}  || dd¬¦  «        }|
D ]\  }}|                     ||i¦  «        }Œ|D ]\  }}|                     |¦  «        rJ ‚Œ|j        }|	d	k    r| }t3          t4          |d	¦  «        }|                     |||z  i¦  «        }||z  }t9          |d„ ¦  «        }||fS )zÝe(x) ... the function
    Omega ... the mrv set
    wsym ... the symbol which is going to be used for w

    Returns the rewritten e in terms of w and log(w). See test_rewrite1()
    for examples and correct results.
    r   rž   z&Omega should be an instance of SubsSetzLength cannot be 0zValue should be expc                 óD   •— ‰| d                                        ¦   «         S )Nr]   rÚ   )r#   rÞ   s    €r'   r¼   zrewrite.<locals>.<lambda>\  s   ø€ ˜U 1 Q¤4œ[Ÿ^š^Ñ-Ô-€ r)   T)r9   Úreverser]   r�   z Result depends on the sign of %srW   r   rY   c                 ó<   ‡ —  t          ‰ dˆ fd„¦  «        ¦   «         S )NÚnormalc                  ó   •— ‰ S r-   rc   ©rº   s   €r'   r¼   z+rewrite.<locals>.<lambda>.<locals>.<lambda>�  s   ø€ ¸A€ r)   )Úgetattrrè   s   `r'   r¼   zrewrite.<locals>.<lambda>�  s"   ø€ Ð>�w q¨(°I°I°I°IÑ>Ô>Ñ@Ô@€ r)   )ÚsympyrŸ   r   r+   rk   rz   rx   rC   r   r.   rE   r;   rá   Úsortr   rl   r{   r   Úis_RationalrÝ   Úqrt   rj   rX   r<   r   r
   rª   r   )r=   rÇ   r#   ÚwsymrŸ   Útr.   rŠ   r`   r³   ÚO2Údenominatorsrˆ   r?   r&   ÚargrX   r!   r"   rÊ   ÚexponentrÞ   s                        @r'   r«   r«   D  s	  ø€ ð "Ð!Ð!Ð!Ð!Ð!Ý�e�WÑ%Ô%ð BÝÐ@ÑAÔAÐAÝ
ˆ5�z„z�Q‚€ÝÐ-Ñ.Ô.Ð.à�ZŠZ‰\Œ\ð 4ð 4ˆÝ˜!�SÑ!Ô!ð 	4ÝÐ2Ñ3Ô3Ð3ð	4àŒ~€HÝ�—’‘”ÑÔ€Eå! %¨Ñ2Ô2€EØ	‡J‚JÐ-Ð-Ð-Ð-°t€JÑ<Ô<Ð<ð ð Pð P‰ˆˆ1Ý�1”5˜!‰nŒnˆØ�!Š8ˆ8˜˜rš	˜	¨#¯'ª'°+Ñ*>Ô*>˜	Ý%Ð&HÈ3Ñ&NÑOÔOÐOøØ
ˆa‚x€xØ�‰vˆà	€BØ€LØð 	7ð 	7‰ˆˆ3Ý�Q”U˜1œ5‘[ !Ñ$Ô$ˆØŒ=ð 	%Ø×Ò ¤Ñ$Ô$Ð$ØŒeˆØ�(ˆ?ˆ?Ý˜h sœm­SÑ1Ô1ð 8Ý Ð!6Ñ7Ô7Ð7Ø˜3”-Ô$ QÔ'ˆCØ
�	Š	�3�˜S 1 Q¤U¡7™]Ñ,Ô,¨T°1©WÑ4Ð5Ñ6Ô6Ð6Ð6ð /Ð.Ð.Ð.Ð.Ð.Øˆ�˜ eÐ,Ñ,Ô,€AØð ð ‰ˆˆ1Ø�JŠJ˜˜1�vÑÔˆˆàð ð ‰ˆˆ3Ø—5’5˜‘:”:ÐÐˆ~Ðð Œ5€DØ
ˆa‚x€xØˆuˆõ •d˜L¨!Ñ,Ô,€HØ	�Š��d˜H‘nÐ%Ñ&Ô&€AØˆHÑ€Dõ
 	�!Ð@Ð@ÑAÔA€Aàˆdˆ7€Nr)   ú+c                 óø  — |j         st          d¦  «        ‚d}|t          t          t          z  fv r| }n¡|t           t           t          z  fv r|                      || ¦  «        }not          |¦  «        dk    r|                      ||d|z  z
  ¦  «        }n?t          |¦  «        dk    r|                      ||d|z  z   ¦  «        }nt          d¦  «        ‚t          ||¦  «        }|                     dd¬	¦  «        S )
ac  
    Compute the limit of e(z) at the point z0 using the Gruntz algorithm.

    Explanation
    ===========

    ``z0`` can be any expression, including oo and -oo.

    For ``dir="+"`` (default) it calculates the limit from the right
    (z->z0+) and for ``dir="-"`` the limit from the left (z->z0-). For infinite z0
    (oo or -oo), the dir argument does not matter.

    This algorithm is fully described in the module docstring in the gruntz.py
    file. It relies heavily on the series expansion. Most frequently, gruntz()
    is only used if the faster limit() function (which uses heuristics) fails.
    z Second argument must be a SymbolNú-r]   rô   zdir must be '+' or '-'ÚintractableT)rZ   )Ú	is_symbolr{   r   r   rª   Ústrr   r«   )r=   ÚzÚz0ÚdirrP   r›   s         r'   r   r   •  s÷   € ð" Œ;ð FÝ!Ð"DÑEÔEÐEð 	€AØ	�b•!•B‘$ˆZÐÐØˆˆØ	•�•a�R�‘Uˆ|Ð	Ð	Ø�VŠV�A˜�r‰]Œ]ˆˆåˆs‰8Œ8�sŠ?ˆ?Ø—’˜˜2  !¡™8Ñ$Ô$ˆBˆBÝ�‰XŒX˜Š_ˆ_Ø—’˜˜2  !¡™8Ñ$Ô$ˆBˆBå%Ð&>Ñ?Ô?Ð?å��Q‰Œ€Að �9Š9�]¨ˆ9Ñ.Ô.Ð.r)   N)rô   )0rT   Ú	functoolsr   Ú
sympy.corer   r   r   r   Úsympy.core.cacher   Úsympy.core.functionr	   Úsympy.core.intfuncr
   Úsympy.core.numbersr   r   Úsympy.core.symbolr   r   Úsympy.core.traversalr   Úsympy.functionsr   r   r   r–   Úsympy.series.orderr   Úsympy.utilities.miscr   ÚdebugÚsympy.utilities.timeutilsr   Útimeitr(   r7   r+   rd   rw   rr   r   rµ   r¸   r˜   rá   r«   r   rc   r)   r'   ú<module>r     s¬  ððuð uðl Ð Ð Ð Ð Ð à /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø $Ð $Ð $Ð $Ð $Ð $Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø #Ð #Ð #Ð #Ð #Ð #Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ø )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ø *Ð *Ð *Ð *Ð *Ð *à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø $Ð $Ð $Ð $Ð $Ð $Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø .Ð .Ð .Ð .Ð .Ð .à	ˆ�(Ñ	Ô	€ðð ð ð$Yð Yð Yð Yð Yˆdñ Yô Yð Yðx ðH;ð H;ñ „ðH;ðVð ð ð:	ð 	ð 	ð ØØð6ð 6ñ „ñ 	„ñ „ð6ðr ØØð*Bð *Bñ 	„ñ „ñ „ð*BðZð ð ð0ð 0ð 0ð ØØð1-ð 1-ñ 	„ñ „ñ „ð1-ðh%ð %ð %ðP ØðLð Lñ „ñ „ðLð^(/ð (/ð (/ð (/ð (/ð (/r)   