
    g+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#dS )    )AccumBounds)SSymbolAddsympifyExpr	PoleErrorMul)factor_terms)Float_illegal)	factorial)Abssignargre)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       O/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/series/limits.pylimitr%      s*    f Ar3$$%$000    c                    d}|t           j        u rLt          |                     |d|z            |t           j        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        rt1          d |D                       rg }
g }t3          |          D ]P\  }}t          |t4                    r|
                    |           0|                    | j        |                    Qt7          |          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) 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   s    
B	QZ166!QqS>>1afc22b%   	F		
 .0QX .0 .0Q] .0444444 	 	AaB$$AuuQZ   Q[%8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{{>>>>>>#GAJJEE&   FFAE>>UaZZFUAr3///Is   J' '
J54J5c                   <    e Zd ZdZddZed             Zd Zd ZdS )	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='-')

    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 )	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__   sL   AJJAJJR[[!*aoaj8999CCA&8J(JK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      )r8   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   )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    i1b!Bvvayy 	3r77
Ar**Cs88Or1b!!Q##qu!*a&8999BQKB//3xxq)))f
.B.B$$ *).B.Br&   c           	         | j         \  }t                    dk    rt          |d          }t          |d          }t          |t                    r3t          |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        rHt                    }|t          |          z  }|                    |z            }dt          j        |                    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}  ||          } |          }|                              rt          j        u r|                    dz            }| }n|                    z             }	 |                    |          \  }}	|	dk    rt          j        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          $ r Y nw xY wt          j        u r3|j        rt?          |          }|                    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"        rm|dk    rt          j        t          |          z  S |dk    r9t          j        t          |          z  t          j#        t          j$        |	z   z  z  S t          j        S t          d|	z            n# t          t          tJ          f$ r ddl&m'}
  |
|          }|j(        r| )                    |          }||cY S 	 |*                    |          }||k    rc|                    tV                    s|                    t          j,                  r*t[          |dt]          |          j"        rdnd          cY S n# t          t          tJ          f$ r Y nw xY wY nw xY wj/        r |0                    tb          td                    }d}	 t[          |          }|t          j        u s|t          j        u rtK                      n2# tJ          t          f$ r | tg          |          }|| 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.
        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                 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)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
  sY   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)cdirzNot sure of sign of %s)powsimpru   )4r8   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   Exp1r   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#s88taBC(((AaBC(((A!U##  
1e(<(<  6!9q	))KAvv} ) )((* !11& ' ' ' """% 'C D D D > 	88DD		>Dq$q&!!ACB99VT"" 	"AA!!5!!B66IuuQxx 	H;;5L15( 	K: 	<qvq"--;qrr
;;;;s88s??DDXX__D	 	 	 	 	 	 	 	, 55<< 	
 :99999	!AIaLL Ar"" 	-QZvva1~~uvvaR((- MM!$M77	r 666M1WW L199CGGaK9:d5kk11RZZ-d5kk99,,     x $ OO66!QqS>>D5DD66!QV$$D"	Mad33IE2  %-- ",,yyQ%79JAERR 99Q<< M> M6M1WW L^ Mqyy z$u++55 1$u++=amaeVXj>YYY 00-.F.KLLLM' /; 	 	 	666666

Ax ''**=HHH,,QT,::D==eiinn=		!&8I8I=!%Abhh6J/SssPSTTTTT 3Y?   	L " 	,		)U++A		q!R%%AAEzzQ!%ZZkk! (:& 	 	 	}1aS))Ay y		 sb   7M 
MM8S2 2AW=A=V>:W=W>WWWWW<Y *Y76Y7Nr   )	__name__
__module____qualname____doc__r[   propertyra   rp   r   ru   r&   r$   r   r      sp            6   X% % %$z z z z 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      sv   9 9 9 9 9 9 D D D D D D D D D D D D D D D D D D - - - - - - . . . . . . . . > > > > > > E E E E E E E E E E E E = = = = = = = = 9 9 9 9 9 9 / / / / / / / / $ $ $ $ $ $      31 31 31 31l< < <~    D     r&   