o
    Th3                     @   s   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 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dZ%dd Z&G dd deZ'dS )    )AccumBounds)SSymbolAddsympifyExpr	PoleErrorMul)factor_terms)Float_illegal)AppliedUndef)Dummy)	factorial)Abssignargre)explog)gamma)PolynomialErrorfactor)Order   )gruntz+c                 C   s   t | |||jd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dir r$   T/home/air/segue/gemini/back/venv/lib/python3.10/site-packages/sympy/series/limits.pylimit   s   3r&   c                 C   s6  d}|t ju rt| |d| |t jd}t|trdS |S | js1| js1| j	s1| j
rt| tsg }ddlm} | jD ]X}t||||}|t jr|jdu rt| tr|t| }	t|	tsb||	}	t|	tskt| }	t|	tryt|	|||  S  dS  dS t|tr dS |t ju r dS || q<|r| j| }|t ju r| jrtdd |D rg }
g }t|D ]\}}t|tr|
| q|| j|  qt|dkrt|  }t||||}|t|
  }|t ju rzddlm} || }W n t y   Y dS 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                 s   s    | ]}t |tV  qd S N)
isinstancer   ).0rrr$   r$   r%   	<genexpr>j   s    zheuristics.<locals>.<genexpr>)ratsimp)!r   Infinityr&   subsZeror)   r   is_Mulis_Addis_Powis_Functionr   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_er$   r$   r%   r9   E   sf   

0&







"
r9   c                   @   s6   e Zd ZdZdddZedd Zdd Zd	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                 C   s   t |}t |}t |}|tjtjtj fv rd}n|tjtjtj fv r'd}||r4td||f t|tr>t	|}nt|t	sKt
dt| t|dvrWtd| t| }||||f|_|S )N-r   z@Limits approaching a variable point are not supported (%s -> %s)z6direction must be of type basestring or Symbol, not %s)r   rM   +-z1direction must be one of '+', '-' or '+-', not %s)r   r   r.   ImaginaryUnitNegativeInfinityr7   NotImplementedErrorr)   strr   	TypeErrortype
ValueErrorr   __new___args)clsr    r!   r"   r#   objr$   r$   r%   rV      s0   




zLimit.__new__c                 C   s8   | j d }|j}|| j d j || j d j |S )Nr   r      )r6   free_symbolsdifference_updateupdate)selfr    isymsr$   r$   r%   r[      s
   
zLimit.free_symbolsc           
      C   s   | j \}}}}|j|j}}||s!t|t| ||}t|S t|||}t|||}	|	tju rH|tjtj	fv rHt||d  ||}t|S |	tj	u rU|tju rWtj
S d S d S )Nr   )r6   baser   r7   r&   r   r   Oner.   rP   ComplexInfinity)
r^   r    _r!   r"   b1e1resex_limbase_limr$   r$   r%   pow_heuristics   s   

zLimit.pow_heuristicsc                    s<  | j \} t dkrJt|dd}t|dd}t|tr3t|tr3|j d |j d kr3| S ||kr9|S |jrB|jrBtjS td||f tju rSt	djrmt
}|t| }|| }d tj|dd	r|jdi |}jdi |jdi ||krS |s|S tju rtjS |jt r| S |jrtt|jg|j d
d R  S tj}t dkrtj}n	t dkrtj} fdd|trddlm} ||}|}|rVtju r|d
 }| }n| }z|j|d\}}	W n
 ty$   Y n2w |	dkr-tjS |	dkr4|S |d
ks@t|	d
@ sGtjt
| S |dkrStjt
| S tjS tju r}|j rdt!|}t"dj#j$j%d}
|d
|
 }| }|
}n
| }}z|j||d\}}	W nd tt	t&fy   ddl'm(} ||}|j)r| *|}|dur| Y S z-|j+||d}||kr|t,s|tj-rt.||dt/|j$rdndW  Y S W n tt	t&fy   Y nw Y n_w t|t0r|	tjkr|S |tjtjtjtjr| S ||sU|	j#r!tjS |	dkr(|S |	j$rO|d
kr8tjt
| S |dkrLtjt
| tjtj|	   S tjS t	d|	 j1r_|2t3t4}d}zt.| }|tju su|tju rxt& W |S  t&tfy   |dur t5| }|du r|  Y S Y |S 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.
        rN   r   )r#   rM   r   zMThe limit does not exist since left hand limit = %s and right hand limit = %sz.Limits at complex infinity are not implementedr   Tr   Nc                    s  | j s| S tfdd| j D }|| j kr| j| } t| t}t| t}t| t}|s0|s0|rzt| j d  }|jrJtd| j d   }W n t	yV   |  Y S w |j
r|dk dkrp|rh| j d  S |rmtjS tjS |dkdkr|r}| j d S |rtjS tjS | S )Nc                 3   s    | ]} |V  qd S r(   r$   )r*   r   )	set_signsr$   r%   r,     s    z0Limit.doit.<locals>.set_signs.<locals>.<genexpr>r   r   T)r6   tupler<   r)   r   r   r   r&   is_zerorQ   is_extended_realr   NegativeOnePira   r0   )exprnewargsabs_flagarg_flag	sign_flagsigr#   rj   r!   r"   r$   r%   rj     s>   




zLimit.doit.<locals>.set_signs)	nsimplify)cdirr!   )positivenegativereal)powsimpzNot sure of sign of %sr$   )6r6   rR   r&   r)   r   is_infiniter   rb   rU   rQ   r   absr/   r.   getr   r7   r:   r   is_Orderr   rp   r0   ra   rn   r   r5   rw   is_meromorphicleadtermintrP   r1   r
   r   is_positiveis_negativeis_realr   sympy.simplify.powsimpr}   r3   ri   as_leading_termr   Exp1r   r   r   is_extended_nonnegativerewriter   r   r9   )r^   hintsr    rC   rE   rx   rw   newecoeffexdummynewzr}   r$   rv   r%   r      s   



$





$$


	

z
Limit.doitNr   )	__name__
__module____qualname____doc__rV   propertyr[   ri   r   r$   r$   r$   r%   r      s    

r   Nr   )(!sympy.calculus.accumulationboundsr   
sympy.corer   r   r   r   r   r   r	   sympy.core.exprtoolsr
   sympy.core.numbersr   r   sympy.core.functionr   sympy.core.symbolr   (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&   r9   r   r$   r$   r$   r%   <module>   s     $
6?