
    MZd2                         d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZmZmZmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lmZ ddl m Z  ddZ!d Z" G d de      Z#y)    )AccumBounds)SSymbolAddsympifyExpr	PoleErrorMul)factor_terms)Float_illegal)	factorial)Abssignargre)explog)gamma)PolynomialErrorfactor)Order   )gruntzc                 >    t        | |||      j                  d      S )aQ  Computes the limit of ``e(z)`` at the point ``z0``.

    Parameters
    ==========

    e : expression, the limit of which is to be taken

    z : symbol representing the variable in the limit.
        Other symbols are treated as constants. Multivariate limits
        are not supported.

    z0 : the value toward which ``z`` tends. Can be any expression,
        including ``oo`` and ``-oo``.

    dir : string, optional (default: "+")
        The limit is bi-directional if ``dir="+-"``, from the right
        (z->z0+) if ``dir="+"``, and from the left (z->z0-) if
        ``dir="-"``. For infinite ``z0`` (``oo`` or ``-oo``), the ``dir``
        argument is determined from the direction of the infinity
        (i.e., ``dir="-"`` for ``oo``).

    Examples
    ========

    >>> from sympy import limit, sin, oo
    >>> from sympy.abc import x
    >>> limit(sin(x)/x, x, 0)
    1
    >>> limit(1/x, x, 0) # default dir='+'
    oo
    >>> limit(1/x, x, 0, dir="-")
    -oo
    >>> limit(1/x, x, 0, dir='+-')
    zoo
    >>> limit(1/x, x, oo)
    0

    Notes
    =====

    First we try some heuristics for easy and frequent cases like "x", "1/x",
    "x**2" and similar, so that it's fast. For all other cases, we use the
    Gruntz algorithm (see the gruntz() function).

    See Also
    ========

     limit_seq : returns the limit of a sequence.
    F)deep)Limitdoit)ezz0dirs       5/usr/lib/python3/dist-packages/sympy/series/limits.pylimitr$      s$    f Ar3$$%$00    c                 b   d}|t         j                  u rBt        | j                  |d|z        |t         j                  d      }t        |t              ry|S | j                  s%| j                  s| j                  s| j                  rg }ddlm} | j                  D ]  }t        ||||      }|j                  t         j                        r~|j                  rt        | t               r`t#        |       }	t        |	t$              s ||	      }	t        |	t$              st'        |       }	t        |	t$              rt)        |	|||      c S  y yt        |t              r y|t         j*                  u r y|j-                  |        |r | j.                  | }|t         j*                  u r| j                  rt1        d |D              rg }
g }t3        |      D ]E  \  }}t        |t4              r|
j-                  |       (|j-                  | j                  |          G t7        |      dkD  r/t%        | j9                         }t        ||||      }|t%        |
 z  }|t         j*                  u r5	 ddlm}  ||       }|t         j*                  u s|| k(  ryt        ||||      S |S # t>        $ r Y yw xY w)a+  Computes the limit of an expression term-wise.
    Parameters are the same as for the ``limit`` function.
    Works with the arguments of expression ``e`` one by one, computing
    the limit of each and then combining the results. This approach
    works only for simple limits, but it is fast.
    Nr   +r   )togetherc              3   <   K   | ]  }t        |t                y wN)
isinstancer   ).0rrs     r#   	<genexpr>zheuristics.<locals>.<genexpr>h   s     /XPR
2{0K/Xs   )ratsimp) r   Infinityr$   subsZeror+   r   is_Mulis_Addis_Powis_Functionsympy.simplify.simplifyr(   argshas	is_finiter   r   r
   r   
heuristicsNaNappendfuncany	enumerater   lensimplifysympy.simplify.ratsimpr/   r   )r   r    r!   r"   rvrr(   almr2e2iirvale3r/   rat_es                    r#   r;   r;   C   sA    
B	QZZ166!QqS>1affc2b% ` I_ 
QXXQ]]4 	AaB$AuuQZZ Q[[%8a%$QA%a-$QK%a-"1I!!S))!QC88Au%aee%	& BQUU{qxxC/XVW/X,X )! .HB!$4		$		!&&*-	. r7Q;b**,Bb!R-AS"XBQUU{>#AJE AEE>UaZUAr3//I ' s   ,J" "	J.-J.c                   4    e Zd ZdZddZed        Zd Zd Zy)r   a  Represents an unevaluated limit.

    Examples
    ========

    >>> from sympy import Limit, sin
    >>> from sympy.abc import x
    >>> Limit(sin(x)/x, x, 0)
    Limit(sin(x)/x, x, 0, dir='+')
    >>> Limit(1/x, x, 0, dir="-")
    Limit(1/x, x, 0, dir='-')

    c                 `   t        |      }t        |      }t        |      }|t        j                  t        j                  t        j                  z  fv rd}n5|t        j                  t        j                  t        j                  z  fv rd}|j                  |      rt        d|d|d      t        |t              rt        |      }n't        |t              st        dt        |      z        t        |      dvrt        d|z        t        j                  |       }||||f|_        |S )	N-r'   z7Limits approaching a variable point are not supported (z -> )z6direction must be of type basestring or Symbol, not %s)r'   rQ   +-z1direction must be one of '+', '-' or '+-', not %s)r   r   r0   ImaginaryUnitNegativeInfinityr9   NotImplementedErrorr+   strr   	TypeErrortype
ValueErrorr   __new___args)clsr   r    r!   r"   objs         r#   r[   zLimit.__new__   s   AJAJR[!**aooajj899CA&&8J8J(JKKC66!9%34b': ; ;c3+CC( %'+Cy1 2 2s8++ &(+, - - ll32sO	
r%   c                     | j                   d   }|j                  }|j                  | j                   d   j                         |j                  | j                   d   j                         |S )Nr   r      )r8   free_symbolsdifference_updateupdate)selfr   isymss      r#   ra   zLimit.free_symbols   sS    IIaL		! 9 9:TYYq\../r%   c                    | j                   \  }}}}|j                  |j                  }}|j                  |      s$t	        |t        |      z  ||      }t        |      S t	        |||      }t	        |||      }	|	t        j                  u r@|t        j                  t        j                  fv rt	        ||dz
  z  ||      }t        |      S |	t        j                  u r#|t        j                  u rt        j                  S y y )Nr   )r8   baser   r9   r$   r   r   Oner0   rU   ComplexInfinity)
rd   r   _r    r!   b1e1resex_limbase_lims
             r#   pow_heuristicszLimit.pow_heuristics   s    ii1b!Bvvay3r7
Ar*Cs8Or1b!Q#quu!**a&8&899BQKB/3xq)))f

.B$$$ /C)r%   c           	         | j                   \  }t              dk(  rt        |d      }t        |d      }t        |t              r1t        |t              r!|j                   d   |j                   d   k(  r| S ||k(  r|S |j
                  r|j
                  rt        j                  S t        d|d|      t        j                  u rt        d      j
                  r@t              }|t        |      z  }|j                  |z        }dt        j                  |j                  d	d
      r6 |j                  di |} j                  di | j                  di ||k(  rS |j!                        s|S t        j"                  u rt        j"                  S  |j                   t$         r| S |j&                  r.t)        t        |j*                        g|j                   dd  S d}t              dk(  rd}nt              dk(  rd}fd|j!                  t,              rddlm}  ||      } |      }|j3                        rӉt        j                  u r|j                  dz        }| }n|j                  z         }	 |j5                  |      \  }}	|	dkD  rt        j6                  S |	dk(  r|S |dk(  st9        |	      dz  st        j                  t        |      z  S |dk(  rt        j:                  t        |      z  S t        j                  S t        j                  u r0|j<                  rt?        |      }|j                  dz        }| }n|j                  z         }	 |j5                  |      \  }}	t        |t@              r|	t        j6                  k(  r|S |j!                  t        j                  t        j:                  t        j                  t        j"                        r| S |j!                        s|	jB                  rt        j6                  S |	dk(  r|S |	jD                  rw|dk(  rt        j                  t        |      z  S |dk(  rAt        j:                  t        |      z  t        jF                  t        jH                  |	z   z  z  S t        j                  S t        d|	z        j\                  r|j_                  t`        tb              }d}	 tY        |      }|t        j"                  u s|t        j"                  u r
tK               	 |S # t        $ r Y w xY w# t        t        tJ        f$ r ddl&m'}
  |
|      }|jP                  r| jS                  |      }||cY S 	 |jU                  |      }||k7  r<|j!                  tV              r'tY        |dt[        |      jD                  rdnd      cY S n# t        t        tJ        f$ r Y nw xY wY 0w xY w# tJ        t        f$ r | te        |      }|| cY S Y |S w xY w)aP  Evaluates the limit.

        Parameters
        ==========

        deep : bool, optional (default: True)
            Invoke the ``doit`` method of the expressions involved before
            taking the limit.

        hints : optional keyword arguments
            To be passed to ``doit`` methods; only used if deep is True.
        rS   r'   )r"   rQ   r   z1The limit does not exist since left hand limit = z and right hand limit = z.Limits at complex infinity are not implementedr   Tr   Nc                    | j                   s| S t        fd| j                   D              }|| j                   k7  r | j                  | } t        | t              }t        | t
              }t        | t              }|s|s|rt        | j                   d   	      }|j                  rt        d| j                   d   z  	      }|j                  rw|dk  dk(  r4|r| j                   d    S |rt        j                  S t        j                  S |dkD  dk(  r3|r| j                   d   S |rt        j                  S t        j                  S | S )Nc              3   .   K   | ]  } |        y wr*    )r,   r   	set_signss     r#   r.   z0Limit.doit.<locals>.set_signs.<locals>.<genexpr>  s     @sIcN@s   r   r   T)r8   tupler>   r+   r   r   r   r$   is_zerois_extended_realr   NegativeOnePirh   r2   )
exprnewargsabs_flagarg_flag	sign_flagsigr"   rv   r    r!   s
         r#   rv   zLimit.doit.<locals>.set_signs
  s+   99@dii@@G$))# tyy'*!$,H!$,H"4.I9DIIaL!R5;;$))A,2s;C''aD(191 F1:F@AF'd*08		! @)2@89@Kr%   )	nsimplify)cdirzNot sure of sign of %s)powsimpru   )3r8   rW   r$   r+   r   is_infiniter   ri   rZ   rV   r   absr1   r0   getr   r9   r<   r   is_Orderr   r|   r   r7   r   is_meromorphicleadtermr2   intrU   r3   r   r   is_positiveis_negativerz   rh   r	   sympy.simplify.powsimpr   r5   rp   as_leading_termr   r   r   is_extended_positiverewriter   r   r;   )rd   hintsr   rE   rG   r   r   newecoeffexr   r"   rv   r    r!   s              @@@@r#   r   z
Limit.doit   s    		1b#s8taBC(AaBC(A!U#
1e(<66!9q	)KAv}}((( !1& ' ' """% 'C D D >>8DD	>Dq$q&!ACB99VT"AA!5!B6IuuQxH;55L155(K::qvvq"-;qr
;;s8s?DX_D	, 55<
 :!AaL Ar"QZZvva1~uvvaR(- MM!$M7	r 666M1W L19CGaK::d5k11RZ--d5k99,,,xx O66!QqS>D5D66!QV$D"	Mad3IE2  %-",yyQ%7%79J9JAEER99Q<>>66M1W L^^qy zz$u+55 11$u+=ammaeeVXj>YYY 000-.F.KLL ""		)U+A		q!R%AAEEzQ!%%Zk! ( ]  0 /; 	6
Axx''*=H,,QT,:D=UYYs^!%Abh6J6JsPSTT 3Y? 	^ :& 	}1aS)Ay  	sb   (U U 	<X 	UUAXAW3/X2X3X
X	X

XX$X>=X>Nr'   )	__name__
__module____qualname____doc__r[   propertyra   rp   r   ru   r%   r#   r   r      s+    6  %$zr%   r   Nr   )$!sympy.calculus.accumulationboundsr   
sympy.corer   r   r   r   r   r	   r
   sympy.core.exprtoolsr   sympy.core.numbersr   r   (sympy.functions.combinatorial.factorialsr   $sympy.functions.elementary.complexesr   r   r   r   &sympy.functions.elementary.exponentialr   r   'sympy.functions.special.gamma_functionsr   sympy.polysr   r   sympy.series.orderr   r   r$   r;   r   ru   r%   r#   <module>r      sI    9 D D D - . > E E = 9 / $ 31l<~D r%   