o
    jgH                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddl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mZ ddlmZmZ ddlmZ dddZ dd Z!dd Z"dd Z#dddZ$dS )zLimits of sequences    )AccumulationBounds)Add)	PoleError)Pow)S)Dummy)sympify)	fibonacci)	factorialsubfactorial)gamma)Abs)MaxMin)cossin)LimitN   c                 C   s   t | } |du r%| j}t|dkr| }nt|dkrtjS td|  t |}|jdu s3|jdu r7tdt	| drF| 
||}|rF|S | ||| |  S )a  Difference Operator.

    Explanation
    ===========

    Discrete analog of differential operator. Given a sequence x[n],
    returns the sequence x[n + step] - x[n].

    Examples
    ========

    >>> from sympy import difference_delta as dd
    >>> from sympy.abc import n
    >>> dd(n*(n + 1), n)
    2*n + 2
    >>> dd(n*(n + 1), n, 2)
    4*n + 6

    References
    ==========

    .. [1] https://reference.wolfram.com/language/ref/DifferenceDelta.html
    Nr   r   zqSince there is more than one variable in the expression, a variable must be supplied to take the difference of %sFzStep should be a finite number._eval_difference_delta)r   free_symbolslenpopr   Zero
ValueError	is_number	is_finitehasattrr   subs)exprnstepfresult r#   M/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/series/limitseq.pydifference_delta   s$   

r%   c           	      C   s   t | jdd}|d }|g}|dd D ]4}|| }| }||kr(| }t||}|du r4 dS |jr=|}|g}q|tjtj	fvrJ|
| qt|dkrSdS |S )a  Finds the dominant term in a sum, that is a term that dominates
    every other term.

    Explanation
    ===========

    If limit(a/b, n, oo) is oo then a dominates b.
    If limit(a/b, n, oo) is 0 then b dominates a.
    Otherwise, a and b are comparable.

    If there is no unique dominant term, then returns ``None``.

    Examples
    ========

    >>> from sympy import Sum
    >>> from sympy.series.limitseq import dominant
    >>> from sympy.abc import n, k
    >>> dominant(5*n**3 + 4*n**2 + n + 1, n)
    5*n**3
    >>> dominant(2**n + Sum(k, (k, 0, n)), n)
    2**n

    See Also
    ========

    sympy.series.limitseq.dominant
    T)funcNr   )r   	make_argsexpand	gammasimpfactor	limit_seqis_zeror   InfinityNegativeInfinityappendr   )	r   r   termsterm0comptrelr#   r#   r$   dominantC   s(   

r8   c              	   C   s2   zt | |tjjddW S  ttfy   Y d S w )NF)deep)r   r   r.   doitNotImplementedErrorr   )r   r   r#   r#   r$   
_limit_infu   s
   r<   c                    s  ddl m} t|D ]}| |st|  }|d ur|  S |  \}}| r.| s@t|   }|d ur=|  S  d S  fdd||fD \}}||  } | |set|  }|d ure|  S |  \}}t| }|d u rw d S t| }|d u r d S ||  } q
d S )Nr   Sumc                 3   s    | ]
}t |  V  qd S N)r%   r)   ).0r4   r   r#   r$   	<genexpr>   s    z_limit_seq.<locals>.<genexpr>)	sympy.concrete.summationsr>   rangehasr<   as_numer_denomr:   r*   r8   )r   r   trialsr>   ir"   numdenr#   rA   r$   
_limit_seq|   s6   





rK      c                    s  ddl m}  du r | j}t|dkr|  n|s| S td | jvr'| S | ttj	} | t
tt} tdddd}tdddd	}tdddd
}dd | tD }t fdd|D se| ttrt|  |i|}	|	durt|  |i|}
|	|
kr|	jr|
jrtt|	|
t|	|
S dS nt|  |i|}	|	dur|	S | jrŇ fdd| jD }tdd |D rdS t| S | |stt|  |i|}|dur|jrtjS dS dS dS )a  Finds the limit of a sequence as index ``n`` tends to infinity.

    Parameters
    ==========

    expr : Expr
        SymPy expression for the ``n-th`` term of the sequence
    n : Symbol, optional
        The index of the sequence, an integer that tends to positive
        infinity. If None, inferred from the expression unless it has
        multiple symbols.
    trials: int, optional
        The algorithm is highly recursive. ``trials`` is a safeguard from
        infinite recursion in case the limit is not easily computed by the
        algorithm. Try increasing ``trials`` if the algorithm returns ``None``.

    Admissible Terms
    ================

    The algorithm is designed for sequences built from rational functions,
    indefinite sums, and indefinite products over an indeterminate n. Terms of
    alternating sign are also allowed, but more complex oscillatory behavior is
    not supported.

    Examples
    ========

    >>> from sympy import limit_seq, Sum, binomial
    >>> from sympy.abc import n, k, m
    >>> limit_seq((5*n**3 + 3*n**2 + 4) / (3*n**3 + 4*n - 5), n)
    5/3
    >>> limit_seq(binomial(2*n, n) / Sum(binomial(2*k, k), (k, 1, n)), n)
    3/4
    >>> limit_seq(Sum(k**2 * Sum(2**m/m, (m, 1, k)), (k, 1, n)) / (2**n*n), n)
    4

    See Also
    ========

    sympy.series.limitseq.dominant

    References
    ==========

    .. [1] Computing Limits of Sequences - Manuel Kauers
    r   r=   Nr   zAExpression has more than one variable. Please specify a variable.r   T)integerpositive)oddrN   )evenrN   c                 s   s    | ]}|  V  qd S r?   )as_base_exp)r@   pr#   r#   r$   rB          zlimit_seq.<locals>.<genexpr>c                 3   s$    | ]\}}|j o| V  qd S r?   )is_negativerE   )r@   br6   rA   r#   r$   rB      s   " c                    s   g | ]}t | qS r#   )r,   )r@   termr   rG   r#   r$   
<listcomp>   s    zlimit_seq.<locals>.<listcomp>c                 s   s    | ]}|d u V  qd S r?   r#   )r@   r"   r#   r#   r$   rB      rS   ) rC   r>   r   r   r   r   rewriter	   r   GoldenRatior
   r   r   r   atomsr   anyrE   r   r   rK   xreplaceis_comparabler   r   r   is_Addargsr   r   r-   r   )r   r   rG   r>   freen_n1n2powersL1L2limitslimr#   rW   r$   r,      sR   0



r,   )Nr   )NrL   )%__doc__!sympy.calculus.accumulationboundsr   sympy.core.addr   sympy.core.functionr   sympy.core.powerr   sympy.core.singletonr   sympy.core.symbolr   sympy.core.sympifyr   %sympy.functions.combinatorial.numbersr	   (sympy.functions.combinatorial.factorialsr
   r   'sympy.functions.special.gamma_functionsr   $sympy.functions.elementary.complexesr   (sympy.functions.elementary.miscellaneousr   r   (sympy.functions.elementary.trigonometricr   r   sympy.series.limitsr   r%   r8   r<   rK   r,   r#   r#   r#   r$   <module>   s(    
02%