# Source code for sympy.series.limitseq

"""Limits of sequences"""

from __future__ import print_function, division

from sympy.core.sympify import sympify
from sympy.core.singleton import S
[docs]def dominant(expr, n): """Finds the dominant term in a sum, that is a term that dominates every other term. 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 """ terms = Add.make_args(expr.expand(func=True)) term0 = terms[-1] comp = [term0] # comparable terms for t in terms[:-1]: e = (term0 / t).gammasimp() l = limit_seq(e, n) if l is S.Zero: term0 = t comp = [term0] elif l is None: return None elif l not in [S.Infinity, -S.Infinity]: comp.append(t) if len(comp) > 1: return None return term0
[docs]def limit_seq(expr, n=None, trials=5): """Finds limits of terms having sequences at infinity. Parameters ========== expr : Expr SymPy expression for the n-th term of the sequence n : Symbol The index of the sequence, an integer that tends to positive infinity. 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 """ if n is None: free = expr.free_symbols if len(free) == 1: n = free.pop() elif not free: return expr else: raise ValueError("expr %s has more than one variables. Please" "specify a variable." % (expr)) elif n not in expr.free_symbols: return expr n_ = Dummy("n", integer=True, positive=True) # If there is a negative term raised to a power involving n, consider # even and odd n separately. powers = (p.as_base_exp() for p in expr.atoms(Pow)) if any(b.is_negative and e.has(n) for b, e in powers): L1 = _limit_seq(expr.xreplace({n: 2*n_}), n_, trials) if L1 is not None: L2 = _limit_seq(expr.xreplace({n: 2*n_ + 1}), n_, trials) if L1 == L2: return L1 else: return _limit_seq(expr.xreplace({n: n_}), n_, trials)