a
    kh3                     @   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$   A/var/www/auris/lib/python3.9/site-packages/sympy/series/limits.pylimit   s    3r&   c                 C   sJ  d}|t ju r<t| |d| |t jd}t|tr8dS n
| jsb| jsb| j	sb| j
rFt| tsFg }ddlm} | jD ]}t||||}|t jr|jdu rt| trt| }	t|	ts||	}	t|	tst| }	t|	trt|	|||  S  dS  dS t|tr dS |t ju r& dS || qx|rF| j| }|t ju r| jrtdd |D rg }
g }t|D ]2\}}t|tr|
| n|| j|  qzt|dkrt|  }t||||}|t|
  }|t ju rFzddlm} || }W n t y   Y dS 0 |t ju s4|| kr8d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       zheuristics.<locals>.<genexpr>)ratsimp)!r   Infinityr&   subsZeror)   r   is_MulZis_Addis_PowZis_Functionr   sympy.simplify.simplifyr'   argshas	is_finiter   r
   r	   r   
heuristicsNaNappendfuncany	enumerater   lenZsimplifyZsympy.simplify.ratsimpr.   r   )r    r!   r"   r#   rvrr'   almZr2e2iirvalZe3r.   Zrat_er$   r$   r%   r8   E   sb    

&





(r8   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 r4d}n|tjtjtj fv rNd}||rhtd||f t|tr|t	|}nt|t	st
dt| t|dvrt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   rG   +-z1direction must be one of '+', '-' or '+-', not %s)r   r   r/   ZImaginaryUnitNegativeInfinityr6   NotImplementedErrorr)   strr   	TypeErrortype
ValueErrorr   __new___args)clsr    r!   r"   r#   objr$   r$   r%   rO      s0    




zLimit.__new__c                 C   s8   | j d }|j}|| j d j || j d j |S )Nr   r      )r5   free_symbolsdifference_updateupdate)selfr    Zisymsr$   r$   r%   rT      s
    
zLimit.free_symbolsc           
      C   s   | j \}}}}|j|j }}||sBt|t| ||}t|S t|||}t|||}	|	tju r|tjtj	fv rt||d  ||}t|S |	tj	u r|tju rtj
S d S )Nr   )r5   baser   r6   r&   r   r   Oner/   rI   ComplexInfinity)
rW   r    _r!   r"   b1e1resZex_limZbase_limr$   r$   r%   pow_heuristics   s    

zLimit.pow_heuristicsc                    sP  | j \} t dkrt|dd}t|dd}t|trft|trf|j d |j d krf| S ||krr|S |jr|jrtjS td||f tju rt	djrt
}|t| }|| }d tj|dd	r|jf i |}jf i |jf i ||kr&S |s6|S tju rHtjS |jt rX| 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} ||}|}|rtju r |d
 }| }n| }z|j|d\}}	W n tyZ   Y nd0 |	dkrltjS |	dkrz|S |d
kst|	d
@ stjt
| S |dkrtjt
| S tjS tju r|j rt!|}t"dj#j$j%d}
|d
|
 }| }|
}n| }}z|j||d\}}	W n tt	t&fy   ddl'm(} ||}|j)r| *|}|dur| Y S zZ|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0 Y n0 t|t0r|	tjkr|S |tjtjtjtjr:| S ||s|	j#rTtjS |	dkrb|S |	j$r|d
krtjt
| S |dkrtjt
| tjtj|	   S tjS nt	d|	 j1r|2t3t4}d}z0t.| }|tju s|tju rt& W nD t&tfyJ   |dur& t5| }|du rF|  Y S Y n0 |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.
        rH   r   )r#   rG   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r6| j| } t| t}t| t}t| t}|sb|sb|rz6t| j d  }|jrtd| j d   }W n t	y   |  Y S 0 |j
r|dk dkr|r| j d  S |rtjS tjS |dkdkr|r| j d S |rtjS tjS | S )Nc                 3   s   | ]} |V  qd S r(   r$   )r*   r   )	set_signsr$   r%   r,     r-   z0Limit.doit.<locals>.set_signs.<locals>.<genexpr>r   r   T)r5   tupler;   r)   r   r   r   r&   is_zerorJ   Zis_extended_realr   NegativeOnePirY   r1   )exprZnewargsZabs_flagZarg_flagZ	sign_flagsigr#   r`   r!   r"   r$   r%   r`     s2    





zLimit.doit.<locals>.set_signs)	nsimplify)cdirr!   )Zpositivenegativereal)powsimpzNot sure of sign of %s)6r5   rK   r&   r)   r   is_infiniter   rZ   rN   rJ   r   absr0   r/   getr   r6   r9   r   Zis_Orderr   re   r1   rY   rc   r   r4   rh   Zis_meromorphicZleadtermintrI   r2   r
   r   Zis_positiveZis_negativeZis_realr   Zsympy.simplify.powsimprm   r3   r_   Zas_leading_termr   ZExp1r   r   r   Zis_extended_nonnegativeZrewriter   r   r8   )rW   hintsr    r@   rB   ri   rh   ZneweZcoeffexdummyZnewzrm   r$   rg   r%   r      s    

$




$(





z
Limit.doitN)r   )	__name__
__module____qualname____doc__rO   propertyrT   r_   r   r$   r$   r$   r%   r      s   

r   N)r   )(Z!sympy.calculus.accumulationboundsr   Z
sympy.corer   r   r   r   r   r   r	   Zsympy.core.exprtoolsr
   Zsympy.core.numbersr   r   Zsympy.core.functionr   Zsympy.core.symbolr   Z(sympy.functions.combinatorial.factorialsr   Z$sympy.functions.elementary.complexesr   r   r   r   Z&sympy.functions.elementary.exponentialr   r   Z'sympy.functions.special.gamma_functionsr   Zsympy.polysr   r   Zsympy.series.orderr   r   r&   r8   r   r$   r$   r$   r%   <module>   s   $
6?