
    Ed0                         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 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"dS )    )AccumBounds)SSymbolAddsympifyExpr	PoleErrorMul)factor_terms)Float_illegal)	factorial)Abssignarg)explog)gamma)PolynomialErrorfactor)Order   )gruntz+c                 N    t          | |||                              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       3lib/python3.11/site-packages/sympy/series/limits.pylimitr$      s*    f Ar3$$%$000    c                    d}t          |          t          j        u r\t          |                     |d|z            |t          j        |t          j        u rdnd          }t          |t                    rdS n| j        s| j	        s| j
        s| j        rfg }ddlm} | j        D ]}t          ||||          }|                    t          j                  r|j        t          | t"                    rt%          |           }	t          |	t&                    s ||	          }	t          |	t&                    st)          |           }	t          |	t&                    rt+          |	|||          c S  dS  dS t          |t                    r dS |t          j        u r dS |                    |           |rB | j        | }|t          j        u r| j        rt3          d |D                       rg }
g }t5          |          D ]P\  }}t          |t6                    r|
                    |           0|                    | j        |                    Qt9          |          dk    r9t'          |                                 }t          ||||          }|t'          |
 z  }|t          j        u rL	 ddlm}  ||           }n# t@          $ r Y dS w xY w|t          j        u s|| k    rdS t          ||||          S |S )	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   -r   )togetherc              3   @   K   | ]}t          |t                    V  d S N)
isinstancer   ).0rrs     r#   	<genexpr>zheuristics.<locals>.<genexpr>h   s-      /X/XPR
2{0K0K/X/X/X/X/X/Xr%   )ratsimp)!absr   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   s@    
B
2ww!* 20166!QqS>>1afR1:5E.Ncc3OOb%   	F		
 .0QX .0 .0Q] .0444444 	 	AaB$$AuuQZ   Q[ a%% $QA%a-- ($HQKK%a-- &"1II!!S)) 9)!QC88888FFAu%% ae  	0BQU{ &qx &C/X/XVW/X/X/X,X,X & )! . .HB!$44 .		$		!&*----r77Q; &b**,,Bb!R--AS"XBQU{ 0>>>>>>#GAJJEE&   FFAE> UaZ FUAr3///Is   2K 
KKc                   <    e Zd ZdZddZed             Zd Zd ZdS )	r   zRepresents 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)
    >>> Limit(1/x, x, 0, dir="-")
    Limit(1/x, x, 0, dir='-')

    r   c                    t          |          }t          |          }t          |          }|t          j        t          j        t          j        z  fv rd}n)|t          j        t          j        t          j        z  fv rd}|                    |          rt          d|d|d          t          |t                    rt          |          }n4t          |t                    st          dt          |          z            t          |          dvrt          d|z            t          j        |           }||||f|_        |S )	Nr'   r   z7Limits approaching a variable point are not supported (z -> )z6direction must be of type basestring or Symbol, not %s)r   r'   +-z1direction must be one of '+', '-' or '+-', not %s)r   r   r1   ImaginaryUnitNegativeInfinityr:   NotImplementedErrorr+   strr   	TypeErrortype
ValueErrorr   __new___args)clsr   r    r!   r"   objs         r#   r[   zLimit.__new__   sR   AJJAJJR[[!*aoaj899 	CCA&8J(JKK 	C66!99 	;%%3411bbb': ; ; ;c3 	2++CCC(( 	2 %'+Cyy1 2 2 2s88++ 	- &(+, - - - l32sO	
r%   c                     | j         d         }|j        }|                    | j         d         j                   |                    | j         d         j                   |S )Nr   r      )r9   free_symbolsdifference_updateupdate)selfr   isymss      r#   ra   zLimit.free_symbols   sQ    IaL	! 9:::TYq\.///r%   c                    | j         \  }}}}|j        |j        }}|                    |          s0t	          |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 d S d S )Nr   )r9   baser   r:   r$   r   r   Oner1   rU   ComplexInfinity)
rd   r   _r    r!   b1e1resex_limbase_lims
             r#   pow_heuristicszLimit.pow_heuristics   s    i1b!Bvvayy 	3r77
Ar**Cs88Or1b!!Q##qu 	 !*a&899  BQKB//3xxq)) 	%f
.B 	%$$	% 	% 	% 	%r%   c                    | j         \  }t          j        u rt          d          |                    dd          r' |j        di |} j        di | j        di ||k    rS |                              s|S t          j        u rt          j        S  |j        t           r| S |j	        r.t          t          |j                  g|j         dd         R  S d}t                    dk    rd}nt                    dk    rd	}fd
|                    t                    rddlm}  ||          } |          }|                              rt%                    t          j        u r|                    dz            }| }n|                    z             }	 |                    |          \  }}|dk    rt          j        S |dk    r|S |dk    st/          |          dz  st          j        t1          |          z  S |d	k    rt          j        t1          |          z  S t          j        S # t4          $ r Y nw xY wt%                    t          j        u r3|j        rt9          |          }|                    dz            }| }n|                    z             }	 |                    |          \  }}t;          |t<                    r|t          j        k    r|S |                    t          j        t          j        t          j        t          j                  r| S |                              s|j        rt          j        S |dk    r|S |j         r|j!        rW|dk    s|j"        rt          j        t1          |          z  S |d	k    rt          j        t1          |          z  S t          j        S |dk    rt          j        t1          |          z  S |d	k    r,t          j        t1          |          z  t          j#        |z  z  S t          j        S nP# t4          t          tH          f$ r6 ddl%m&}  ||          }|j'        r| (                    |          }	|	|	cY S Y nw xY wj)        r |*                    tV          tX                    }d}
	 t                    dk    r@t[          |d          }	t[          |d          }
|
|	k    rt5          d|
d|	          nt[          |          }	|	t          j        u s|
t          j        u rtI                      n2# tH          t4          f$ r |
 t]          |          }	|	| cY S Y nw xY w|	S )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.
        z.Limits at complex infinity are not implementedr   Tr   Nr   r   r'   c                 r   | j         s| S t          fd| j         D                       }|| j         k    r
 | j        | } t          | t                    }t          | t
                    }t          | t                    }|s|s|rt          | j         d         	          }|j        r t          d| j         d         z  	          }|j	        rg|dk     dk    r*|r| j         d          n|rt          j        nt          j        S |dk    dk    r)|r| j         d         n|rt          j        nt          j        S | S )Nc              3   .   K   | ]} |          V  d S r*    )r,   r   	set_signss     r#   r.   z0Limit.doit.<locals>.set_signs.<locals>.<genexpr>   s+      @@sIIcNN@@@@@@r%   r   r   T)r9   tupler?   r+   r   r   r   r$   is_zerois_extended_realr   NegativeOnePirh   r3   )
exprnewargsabs_flagarg_flag	sign_flagsigr"   rv   r    r!   s
         r#   rv   zLimit.doit.<locals>.set_signs   sa   9 @@@@di@@@@@G$)# + ty'*!$,,H!$,,H"4..I 
@9 
@ 
@DIaL!R55; <$)A,2s;;C' @aD( @19 !E11: DF'd* @08 !?	!)2 >@Kr%   )	nsimplify)cdir)powsimprS   z1The limit does not exist since left hand limit = z and right hand limit = ru   )/r9   r   ri   rV   getr   r:   r=   r   is_Orderr   r$   r|   rW   r   r8   r   is_meromorphicr0   r1   r2   leadtermr3   intr   rU   rZ   r4   r   r+   r   is_positiveis_negative
is_integeris_evenrz   r	   sympy.simplify.powsimpr   r6   rp   is_extended_positiverewriter   r   r   r<   )rd   hintsr   r   r   newecoeffexr   rF   rH   r"   rv   r    r!   s              @@@@r#   r   z
Limit.doit   sM    	1b#"" 	D% 'C D D D 99VT"" 	"AA!!5!!B6 	IuuQxx 	H; 	5L15( 	K: 	<qvq"--;qrr
;;;;s88s? 	DDXX_ 	D	 	 	 	 	 	 	 	, 55<< 	
 :99999	!AIaLL Ar"" 	-2ww!*$ )vva1~~uvvaR((- MM!$M77	r 6 !6M1W ! L19 -CGGaK -:d5kk11RZ --d5kk99,,     r77aj  	%x $ OO66!QqS>>D5DD66!QV$$D"	5ad33IE2 %-- ", yyQ%79JAERR 99Q<< 5> 56M1W 5 L^ 5} 519 5
 5#$:d5kk#99!RZ 5#$#5d5kk#AA#$#4419 5#$:d5kk#99!RZ 5#$:d5kk#9!-:K#KK#$#44A /; 	 	 	666666

Ax ''** HHH	L " 	,		)U++A	3xx4 *1aS))1aS))6 &$* qq!!&% & & &&
 1aS))AEz "Q!%Z "kk!":& 	 	 	 1aS))A  		 s8   =I 
I I Q ARR>BU *U=<U=Nr   )	__name__
__module____qualname____doc__r[   propertyra   rp   r   ru   r%   r#   r   r      sp            6   X% % %$m m m m m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   &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      sp   9 9 9 9 9 9 D D D D D D D D D D D D D D D D D D - - - - - - . . . . . . . . > > > > > > A A A A A A A A A A = = = = = = = = 9 9 9 9 9 9 / / / / / / / / $ $ $ $ $ $      31 31 31 31l< < <~r r r r rD r r r r rr%   