
\c           @  s  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
 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 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& d  d l' m( Z( d  d l) m* Z* d  d l+ m, Z, d  d l- m. Z. d  d l/ m0 Z0 d  d l1 m2 Z2 d  d l3 m4 Z4 d  d l5 m6 Z6 d  d l7 Z7 d e e
 f d     YZ8 d   Z9 d   Z: d   Z; d    Z< d!   Z= d"   Z> d#   Z? d$   Z@ d S(%   i(   t   print_functiont   division(   t   is_decreasing(   t   AccumulationBounds(   t   AddWithLimits(   t   ExprWithIntLimits(   t
   gosper_sum(   t   Add(   t   range(   t
   Derivative(   t   Mul(   t   Eq(   t   S(   t   Dummyt   Wildt   Symbol(   t   zeta(   t	   Piecewise(   t   And(   t   apartt   PolynomialErrort   together(   t	   limit_seq(   t   O(   t	   FiniteSet(   t   denom(   t   combsimp(   t   powsimp(   t   solve(   t   solvesetNt   Sumc           B  s   e  Z d  Z d g Z d   Z d   Z d   Z d   Z d   Z d   Z	 d e
 e e d	  Z d
   Z d   Z d   Z d d d e d  Z d   Z RS(   s  Represents unevaluated summation.

    ``Sum`` represents a finite or infinite series, with the first argument
    being the general form of terms in the series, and the second argument
    being ``(dummy_variable, start, end)``, with ``dummy_variable`` taking
    all integer values from ``start`` through ``end``. In accordance with
    long-standing mathematical convention, the end term is included in the
    summation.

    Finite sums
    ===========

    For finite sums (and sums with symbolic limits assumed to be finite) we
    follow the summation convention described by Karr [1], especially
    definition 3 of section 1.4. The sum:

    .. math::

        \sum_{m \leq i < n} f(i)

    has *the obvious meaning* for `m < n`, namely:

    .. math::

        \sum_{m \leq i < n} f(i) = f(m) + f(m+1) + \ldots + f(n-2) + f(n-1)

    with the upper limit value `f(n)` excluded. The sum over an empty set is
    zero if and only if `m = n`:

    .. math::

        \sum_{m \leq i < n} f(i) = 0  \quad \mathrm{for} \quad  m = n

    Finally, for all other sums over empty sets we assume the following
    definition:

    .. math::

        \sum_{m \leq i < n} f(i) = - \sum_{n \leq i < m} f(i)  \quad \mathrm{for} \quad  m > n

    It is important to note that Karr defines all sums with the upper
    limit being exclusive. This is in contrast to the usual mathematical notation,
    but does not affect the summation convention. Indeed we have:

    .. math::

        \sum_{m \leq i < n} f(i) = \sum_{i = m}^{n - 1} f(i)

    where the difference in notation is intentional to emphasize the meaning,
    with limits typeset on the top being inclusive.

    Examples
    ========

    >>> from sympy.abc import i, k, m, n, x
    >>> from sympy import Sum, factorial, oo, IndexedBase, Function
    >>> Sum(k, (k, 1, m))
    Sum(k, (k, 1, m))
    >>> Sum(k, (k, 1, m)).doit()
    m**2/2 + m/2
    >>> Sum(k**2, (k, 1, m))
    Sum(k**2, (k, 1, m))
    >>> Sum(k**2, (k, 1, m)).doit()
    m**3/3 + m**2/2 + m/6
    >>> Sum(x**k, (k, 0, oo))
    Sum(x**k, (k, 0, oo))
    >>> Sum(x**k, (k, 0, oo)).doit()
    Piecewise((1/(1 - x), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True))
    >>> Sum(x**k/factorial(k), (k, 0, oo)).doit()
    exp(x)

    Here are examples to do summation with symbolic indices.  You
    can use either Function of IndexedBase classes:

    >>> f = Function('f')
    >>> Sum(f(n), (n, 0, 3)).doit()
    f(0) + f(1) + f(2) + f(3)
    >>> Sum(f(n), (n, 0, oo)).doit()
    Sum(f(n), (n, 0, oo))
    >>> f = IndexedBase('f')
    >>> Sum(f[n]**2, (n, 0, 3)).doit()
    f[0]**2 + f[1]**2 + f[2]**2 + f[3]**2

    An example showing that the symbolic result of a summation is still
    valid for seemingly nonsensical values of the limits. Then the Karr
    convention allows us to give a perfectly valid interpretation to
    those sums by interchanging the limits according to the above rules:

    >>> S = Sum(i, (i, 1, n)).doit()
    >>> S
    n**2/2 + n/2
    >>> S.subs(n, -4)
    6
    >>> Sum(i, (i, 1, -4)).doit()
    6
    >>> Sum(-i, (i, -3, 0)).doit()
    6

    An explicit example of the Karr summation convention:

    >>> S1 = Sum(i**2, (i, m, m+n-1)).doit()
    >>> S1
    m**2*n + m*n**2 - m*n + n**3/3 - n**2/2 + n/6
    >>> S2 = Sum(i**2, (i, m+n, m-1)).doit()
    >>> S2
    -m**2*n - m*n**2 + m*n - n**3/3 + n**2/2 - n/6
    >>> S1 + S2
    0
    >>> S3 = Sum(i, (i, m, m-1)).doit()
    >>> S3
    0

    See Also
    ========

    summation
    Product, product

    References
    ==========

    .. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
           Volume 28 Issue 2, April 1981, Pages 305-350
           http://dl.acm.org/citation.cfm?doid=322248.322255
    .. [2] https://en.wikipedia.org/wiki/Summation#Capital-sigma_notation
    .. [3] https://en.wikipedia.org/wiki/Empty_sum
    t   is_commutativec         O  sW   t  j |  | | |  } t | d  s+ | St d   | j D  rS t d   n  | S(   Nt   limitsc         s  s-   |  ]# } t  |  d  k p$ d | k Vq d S(   i   N(   t   lent   None(   t   .0t   l(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pys	   <genexpr>   s    s/   Sum requires values for lower and upper bounds.(   R   t   __new__t   hasattrt   anyR    t
   ValueError(   t   clst   functiont   symbolst   assumptionst   obj(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR%      s    c         C  s   |  j  j r t Sd  S(   N(   R*   t   is_zerot   True(   t   self(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   _eval_is_zero   s    c         K  sv  | j  d t  r' |  j j |   } n	 |  j } |  j j rL |  j   j   Sx t |  j  D] \ } } | \ } } } | | } | j r | d k  t k r | d | d } } | } n  t	 | | | | f  }	 |	 d  k r7| |  j k r|  j | | | | f  }
 |
 d  k	 r|
 S|  S|  j | |  j |  Sn  |	 } q\ W| j  d t  rrt | t  sr| j |   Sn  | S(   Nt   deepi    i   (   t   getR/   R*   t   doitt	   is_Matrixt   expandt	   enumerateR    t
   is_integert   eval_sumR"   t   eval_zeta_functiont   funct
   isinstanceR   (   R0   t   hintst   ft   nt   limitt   it   at   bt   dift   newft   zeta_function(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR4      s0    	


c         C  s   | \ } } } t  d d | g t  d d | g t  d d | g } } } | j | | | |  }	 |	 d k	 r | t j k r d |	 | |	 | }
 |	 | } |	 | |	 | | } t |
 t | |  t | d k | d k  f |  t f  Sd S(   s   
        Check whether the function matches with the zeta function.
        If it matches, then return a `Piecewise` expression because
        zeta function does not converge unless `s > 1` and `q > 0`
        t   wt   excludet   yt   zi   i    N(	   R   t   matchR"   R   t   InfinityR   R   R   R/   (   R0   R>   R    RA   RB   RC   RG   RI   RJ   t   resultt   coefft   st   q(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR:      s    A
c   
      C  s   t  | t  r% | |  j k r% t j S|  j t |  j  } } | j d  } | rh |  j	 | |  } n  t
 |  d k r | \ } } } | | j k s | | j k r d St | | d t } |  j	 | |  }	 |	 St d  Sd S(   sX  
        Differentiate wrt x as long as x is not in the free symbols of any of
        the upper or lower limits.

        Sum(a*b*x, (x, 1, a)) can be differentiated wrt x or b but not `a`
        since the value of the sum is discontinuous in `a`. In a case
        involving a limit variable, the unevaluated derivative is returned.
        ii   t   evaluates   Lower and upper bound expected.N(   R<   R   t   free_symbolsR   t   ZeroR*   t   listR    t   popR;   R!   R"   R	   R/   t   NotImplementedError(
   R0   t   xR>   R    R@   t   _RB   RC   t   dft   rv(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   _eval_derivative   s    c         C  s   |  j  d \ } } } | j | | |  } t |  j   d k rQ |  j  d } n |  j |  j  d    } t | | | d | f  j   S(   Nii   i    i   (   t   argst   subsR!   R;   R   R4   (   R0   R?   t   stept   kRX   t   uppert	   new_upperR>   (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   _eval_difference_delta  s    g333333?c         C  s#  d d l  m } m } d d l m } d d l m } t j | |  j	   }	 g  }
 g  } x |	 D] } | j
 t  r | j | |   } g  } x@ | D]8 } t | t  r | j | j    q | j |  q W|
 j | |    qa | j |  qa Wt | |
  |  } | | d |  j S(   Ni(   t
   factor_sumt   sum_combine(   R6   (   R
   R    (   t   sympy.simplify.simplifyRc   Rd   t   sympy.core.functionR6   t   sympy.core.mulR
   R   t	   make_argsR*   t   hasR   R<   t   appendt   _eval_simplifyR    (   R0   t   ratiot   measuret   rationalt   inverseRc   Rd   R6   R
   t   termst   s_tt   o_tt   termt   subtermst	   out_termst   subtermRM   (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyRk     s$    c         C  s   d  S(   N(   R"   (   R0   R>   RW   (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   _eval_summation6  s    c   *        s  d d l  m } m } m } m } m } | d d t \ } } } |  j d d  |  j d d  |  j d d  |  j }	 t	 |	 j
  d k r t d   n   j r  j r t j S t j k rJ t j k rt |	  d t j f  j   ot |	  t j d f  j   S| |	 j i   6  }	   t j  n  t  j d	 t d
 t }
 |	 j i |
  6 }	 |
  |      |	 j r x] |	 j D]R \ } } | t k s| j   j t j k rt |    f  } | j   SqWt j Sy5 t |	   } | d k	 r4| j t k r4t j SWn t k
 rHn Xy; t t  |	    } | d k	 r| j t k rt j SWn t k
 rn Xt! |	  t j f  } | j" j#  |  } | d k	 r| | d k  rt j S| | d k rt j Sn  | j" j# d  |  } | d k	 rZ| | d k r@t j S| | d k rZt j Sn  | j" j# d  | |   | | |    |  } | d k	 r| | d k s| | d k r| | d k s| | | | k od k n r| | d k rt j St j Sy? t  |	   } | d k	 rR| j$ rR| d k rRt j SWn t k
 rfn X|	 j i  d  6 } t% t& | |	   } y] t |   } | d k	 r| j$ rt  |  d k rt j St  |  d k  rt j Sn  Wn t k
 rn Xy_ t t  |	  d    } | d k	 rf| j$ rf| d k  rPt j S| d k rft j Sn  Wn t k
 rzn X|	 j# d  | |  } | | j'   rt( | |    rt j Sd } t) |	 j*       } | s  } n3 t+ | t,  r'| j j$ r'| | j   j  } n  | d k	 rt( |	 |  sRt( |	 |  r| |	    f  } y& | j-   } | j$ rt | j  SWqt k
 rqXn  | j" j. r| j" j } t/ |  } t d d	 t      f d   }     f d   }! x t0 d t	 |   D] }" x t1 j2 | |"  D]{ }# | t/ |#  }$ t3 |#   }% t3 |$   }& t( |%    r|  |&  }' |' d k	 r|' Sn  |! |% |&  }( |( d k	 r6|( Sq6WqWn  |  j d d }) |	 j i |)  6 }	 t d |	   d S(   s	  Checks for the convergence of a Sum.

        We divide the study of convergence of infinite sums and products in
        two parts.

        First Part:
        One part is the question whether all the terms are well defined, i.e.,
        they are finite in a sum and also non-zero in a product. Zero
        is the analogy of (minus) infinity in products as
        :math:`e^{-\infty} = 0`.

        Second Part:
        The second part is the question of convergence after infinities,
        and zeros in products, have been omitted assuming that their number
        is finite. This means that we only consider the tail of the sum or
        product, starting from some point after which all terms are well
        defined.

        For example, in a sum of the form:

        .. math::

            \sum_{1 \leq i < \infty} \frac{1}{n^2 + an + b}

        where a and b are numbers. The routine will return true, even if there
        are infinities in the term sequence (at most two). An analogous
        product would be:

        .. math::

            \prod_{1 \leq i < \infty} e^{\frac{1}{n^2 + an + b}}

        This is how convergence is interpreted. It is concerned with what
        happens at the limit. Finding the bad terms is another independent
        matter.

        Note: It is responsibility of user to see that the sum or product
        is well defined.

        There are various tests employed to check the convergence like
        divergence test, root test, integral test, alternating series test,
        comparison tests, Dirichlet tests. It returns true if Sum is convergent
        and false if divergent and NotImplementedError if it can not be checked.

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Convergence_tests

        Examples
        ========

        >>> from sympy import factorial, S, Sum, Symbol, oo
        >>> n = Symbol('n', integer=True)
        >>> Sum(n/(n - 1), (n, 4, 7)).is_convergent()
        True
        >>> Sum(n/(2*n + 1), (n, 1, oo)).is_convergent()
        False
        >>> Sum(factorial(n)/5**n, (n, 1, oo)).is_convergent()
        False
        >>> Sum(1/n**(S(6)/5), (n, 1, oo)).is_convergent()
        True

        See Also
        ========

        Sum.is_absolutely_convergent()
        Product.is_convergent()
        i(   t   Intervalt   Integralt   logR+   t   simplifys   p q rR)   i    i   i   sN   convergence checking for more than one symbol containing series is not handledt   integert   positivet   mc           sb   yJ t  t |     j  f  j     } | d  k	 rI | j rI t j SWn t k
 r] n Xd  S(   N(	   R   R   t   infR4   R"   t	   is_finiteR   t   trueRV   (   t   g_nt   ing_val(   t   intervalR~   t   sym(    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   _dirichlet_test&  s    *c           s   yr t  |    } | d  k	 rq | j sI t | t  rq | j | j j rq t |     f  j   rq t	 j
 Sn  Wn t k
 r n Xd  S(   N(   R   R"   R   R<   R   t   maxt   minR   t   is_absolutely_convergentR   R   RV   (   t   g1_nt   g2_nt   lim_val(   t   lower_limitR   t   upper_limit(    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   _bounded_convergent_test/  s    sF   The algorithm to find the Sum convergence of %s is not yet implementedN(4   t   sympyRx   Ry   Rz   R+   R{   R   R    R*   R!   RR   RV   R   R   R   t   NegativeInfinityRL   R   t   is_convergentt   xreplaceR   t   nameR/   t   is_PiecewiseR\   t   as_sett   supR   R"   R.   t   Falset   falset   absR   t   exprRK   t	   is_numberR   R   Ri   R   R   t   diffR<   R   R4   t   is_Mult   setR   t	   itertoolst   combinationsR
   (*   R0   Rx   Ry   Rz   R+   R{   t   pRP   t   rt   sequence_termt   sym_R;   t   condRO   R   t   lim_val_abst   ordert   p1_series_testt   p2_series_testt
   n_log_testt   lim_compt   next_sequence_termRl   t	   lim_ratiot   lim_evaluatedt   dict_valt   check_intervalt   maximat   integral_valt   integral_val_evaluatedR\   t   argsetR   R   R?   t   a_tuplet   b_sett   a_nt   b_nt   diricht   bc_testt   _sym(    (   R   R   R~   R   R   s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR   9  s    F(	!	$

< 4!'			c         C  s   t  t |  j  |  j  j   S(   s  
        Checks for the absolute convergence of an infinite series.

        Same as checking convergence of absolute value of sequence_term of
        an infinite series.

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Absolute_convergence

        Examples
        ========

        >>> from sympy import Sum, Symbol, sin, oo
        >>> n = Symbol('n', integer=True)
        >>> Sum((-1)**n, (n, 1, oo)).is_absolutely_convergent()
        False
        >>> Sum((-1)**n/n**2, (n, 1, oo)).is_absolutely_convergent()
        True

        See Also
        ========

        Sum.is_convergent()
        (   R   R   R*   R    R   (   R0   (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR   N  s    i    c           s  d d l  m } m } d d l m } t |  } t |  } |  j } t |  j  d k rk t	 d   n  |  j d \        k t
 k r    d k r t j t j f S d   d    | } n  t j }	 | rk j r  j rt |    d  } n  | s#| j   rWxt |  D]  }
 |	 | j    |
  7}	 q0Wn | j     } | rt | j d   | k  } | t
 k r|	 t |  f S| t k s| t j f Sn  |	 | 7}	 xn t d |  D]] }
 | j    |
  } t | j d   | k  r/| d k r/|	 t |  f S|	 | 7}	 qW   d | k r^|	 t j f S  | 7  n  t d  } | | j  |  |    f  } | r| j   } n  |	 | 7}	     f d	   } | |  \ } } | | d
 } | j   } x t d | d
  D] }
 | |  \ } } | d
 |
  | d
 |
  | | } | rw| rwt | j d   | k  s|
 | k rPn  |	 | 7}	 | j  d
 d t } qW|	 | t |  f S(   s  
        Return an Euler-Maclaurin approximation of self, where m is the
        number of leading terms to sum directly and n is the number of
        terms in the tail.

        With m = n = 0, this is simply the corresponding integral
        plus a first-order endpoint correction.

        Returns (s, e) where s is the Euler-Maclaurin approximation
        and e is the estimated error (taken to be the magnitude of
        the first omitted term in the tail):

            >>> from sympy.abc import k, a, b
            >>> from sympy import Sum
            >>> Sum(1/k, (k, 2, 5)).doit().evalf()
            1.28333333333333
            >>> s, e = Sum(1/k, (k, 2, 5)).euler_maclaurin()
            >>> s
            -log(2) + 7/20 + log(5)
            >>> from sympy import sstr
            >>> print(sstr((s.evalf(), e.evalf()), full_prec=True))
            (1.26629073187415, 0.0175000000000000)

        The endpoints may be symbolic:

            >>> s, e = Sum(1/k, (k, a, b)).euler_maclaurin()
            >>> s
            -log(a) + log(b) + 1/(2*b) + 1/(2*a)
            >>> e
            Abs(1/(12*b**2) - 1/(12*a**2))

        If the function is a polynomial of degree at most 2n+1, the
        Euler-Maclaurin formula becomes exact (and e = 0 is returned):

            >>> Sum(k, (k, 2, b)).euler_maclaurin()
            (b**2/2 + b/2 - 1, 0)
            >>> Sum(k, (k, 2, b)).doit()
            b**2/2 + b/2 - 1

        With a nonzero eps specified, the summation is ended
        as soon as the remainder term is less than the epsilon.
        i(   t	   bernoullit	   factorial(   Ry   i   s   More than 1 limiti    i   RW   c           sG    t  j k r% |  j     d f S|  j     |  j    f S(   Ni    (   R   RL   R]   (   R   (   RB   RC   RA   (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   fpoint  s    i   R{   (   t   sympy.functionsR   R   t   sympy.integralsRy   t   intR*   R!   R    R(   R/   R   RS   t
   is_IntegerR   t   is_polynomialR   R]   R   t   evalfR   R   R4   R   (   R0   R~   R?   t   epst   eval_integralR   R   Ry   R>   RO   R_   Rs   t   testRW   t   IR   t   fat   fbt   itermt   gt   gat   gb(    (   RB   RC   RA   s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   euler_maclaurink  sj    +	
	!
'$
&3
c   	      G  s   t  |  } x? t |  D]1 \ } } t | t  s |  j |  | | <q q Wd } g  } xi t |  j  D]X \ } } | } | | k r | } | d | d d | d d f } n  | j |  qj Wt | |  j |  S(   s  
        Reverse the order of a limit in a Sum.

        Usage
        =====

        ``reverse_order(self, *indices)`` reverses some limits in the expression
        ``self`` which can be either a ``Sum`` or a ``Product``. The selectors in
        the argument ``indices`` specify some indices whose limits get reversed.
        These selectors are either variable names or numerical indices counted
        starting from the inner-most limit tuple.

        Examples
        ========

        >>> from sympy import Sum
        >>> from sympy.abc import x, y, a, b, c, d

        >>> Sum(x, (x, 0, 3)).reverse_order(x)
        Sum(-x, (x, 4, -1))
        >>> Sum(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(x, y)
        Sum(x*y, (x, 6, 0), (y, 7, -1))
        >>> Sum(x, (x, a, b)).reverse_order(x)
        Sum(-x, (x, b + 1, a - 1))
        >>> Sum(x, (x, a, b)).reverse_order(0)
        Sum(-x, (x, b + 1, a - 1))

        While one should prefer variable names when specifying which limits
        to reverse, the index counting notation comes in handy in case there
        are several symbols with the same name.

        >>> S = Sum(x**2, (x, a, b), (x, c, d))
        >>> S
        Sum(x**2, (x, a, b), (x, c, d))
        >>> S0 = S.reverse_order(0)
        >>> S0
        Sum(-x**2, (x, b + 1, a - 1), (x, c, d))
        >>> S1 = S0.reverse_order(1)
        >>> S1
        Sum(x**2, (x, b + 1, a - 1), (x, d + 1, c - 1))

        Of course we can mix both notations:

        >>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1)
        Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
        >>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x)
        Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))

        See Also
        ========

        index, reorder_limit, reorder

        References
        ==========

        .. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
               Volume 28 Issue 2, April 1981, Pages 305-350
               http://dl.acm.org/citation.cfm?doid=322248.322255
        i   i    i   (	   RT   R7   R<   R   t   indexR    Rj   R   R*   (	   R0   t   indicest	   l_indicesRA   t   indxt   eR    R@   R$   (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   reverse_order  s    =&(   t   __name__t
   __module__t   __doc__t	   __slots__R%   R1   R4   R:   R[   Rb   R"   R   Rk   Rw   R   R   R/   R   R   (    (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR      s   					$		#	$		 	ic         O  s   t  |  | |  j d t  S(   s  
    Compute the summation of f with respect to symbols.

    The notation for symbols is similar to the notation used in Integral.
    summation(f, (i, a, b)) computes the sum of f with respect to i from a to b,
    i.e.,

    ::

                                    b
                                  ____
                                  \   `
        summation(f, (i, a, b)) =  )    f
                                  /___,
                                  i = a

    If it cannot compute the sum, it returns an unevaluated Sum object.
    Repeated sums can be computed by introducing additional symbols tuples::

    >>> from sympy import summation, oo, symbols, log
    >>> i, n, m = symbols('i n m', integer=True)

    >>> summation(2*i - 1, (i, 1, n))
    n**2
    >>> summation(1/2**i, (i, 0, oo))
    2
    >>> summation(1/log(n)**n, (n, 2, oo))
    Sum(log(n)**(-n), (n, 2, oo))
    >>> summation(i, (i, 0, n), (n, 0, m))
    m**3/6 + m**2/2 + m/3

    >>> from sympy.abc import x
    >>> from sympy import factorial
    >>> summation(x**n/factorial(n), (n, 0, oo))
    exp(x)

    See Also
    ========

    Sum
    Product, product

    R2   (   R   R4   R   (   R>   R+   t   kwargs(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt	   summation#  s    ,c   	      C  s^   | \ } } } d } xB t  |  D]4 } | |  j | | |  | j | | |  7} q" W| S(   s  Returns the direct summation of the terms of a telescopic sum

    L is the term with lower index
    R is the term with higher index
    n difference between the indexes of L and R

    For example:

    >>> from sympy.concrete.summations import telescopic_direct
    >>> from sympy.abc import k, a, b
    >>> telescopic_direct(1/k, -1/(k+2), 2, (k, a, b))
    -1/(b + 2) - 1/(b + 1) + 1/(a + 1) + 1/a

    i    (   R   R]   (	   t   Lt   RR?   R    RA   RB   RC   RO   R~   (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   telescopic_directR  s
    2c         C  s  | \ } } } |  j  s! | j  r% d St d  } | j |  j | | |   } d } | r | | k r | | } | j o |  j | | |  | k s d } q n  | d k rgt d  }	 y- t |  j | | |	  | |	  p g  } Wn t k
 r d SXg  | D]8 }
 |
 j r|  j | | |
  | j	   j
 r|
 ^ q} t |  d k rZd S| d } n  | d k  rt | |  t |  | | | f  S| d k rt |  | | | | | f  Sd S(   sb   Tries to perform the summation using the telescopic property

    return None if not possible
    R_   R~   i   i    N(   t   is_AddR"   R   RK   R]   R   R   R   RV   R6   R.   R!   R   R   (   R   R   R    RA   RB   RC   R_   t   solRO   R~   t   si(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt
   telescopich  s2     
&-/"c           s  d d l  m } m } d d l m } | \   } } |  t j k rK t j S  |  j k rj |  | | d S| | k r |  j   |  St	 |  t
  rt   f d   |  j D  sg  } xI |  j D]> } t | j |  }	 |	 d  k r d  S| j |	 | j f  q W|  j |   Sn  |  j |  rE| |  | d  rE| |  |  S| | }
 |
 j } | r|
 d k  rt |    | | f  St	 |  t
  rd  St |  j     | | f  } | d  k	 r| S| rt |    | | f  Sd  S(   Ni(   t   deltasummationt   _has_simple_delta(   t   KroneckerDeltai   c         3  s%   |  ] }   | j  d  j k Vq d S(   i   N(   R\   RR   (   R#   t   arg(   RA   (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pys	   <genexpr>  s    i    id   (   t   sympy.concrete.deltaR   R   R   R   R   RS   RR   R]   R<   R   R'   R\   R9   R   R"   Rj   R   R;   Ri   R   t   eval_sum_directt   eval_sum_symbolicR6   (   R>   R    R   R   R   RB   RC   t   newargsR   t   newexprRD   t   definitet   value(    (   RA   s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR9     s>    "
	c         C  s`   d d l  m } | \ } } } | | } | g  t | d  D] } |  j | | |  ^ q=   S(   Ni(   R   i   (   t
   sympy.coreR   R   R]   (   R   R    R   RA   RB   RC   RD   t   j(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR     s    
c   &      C  s  d d l  m } m } |  } | \ } } } |  j |  sJ |  | | d S|  j r |  j   \ } }	 | j |  s t |	 | | | f  }
 |
 r | |
 Sn  |	 j |  s t | | | | f  } | r |	 | Sn  y t |  |  }  Wq t k
 r q Xn  |  j	 r|  j   \ } }	 t
 | |	 | | | f  } | r?| St | | | | f  } t |	 | | | f  } d  | | f k r| | } | t j k	 r| Sqn  t d  } |  j | |  } | d  k	 r| | } | j r| d k re| t j k r| t j k	 s*| t j k r1| t j k	 r1t j S| | d | d  | | d |  | d j   S| j r| d k r| d k r| |  | | d  S| | t |   | | d t |   Sqqn  | j t j t j  p| j t j t j  st d d | g } t d d | g } t d	 d | g } t d
  } |  j   j | |  } | d  k	 r| j |  j   j | | |  } | d  k	 r| j |  qn  | d  k	 rA| | j |  } | | j |  } | | | | | d d | } | | | d } t | t | t j  f | t f  St |  | | | f  } t | t t  f  rd d l! m" } m# } | j$ | | d   j$ } t% t& |   } | | j$ @} g  }  x | |  D] }! ys t' | |!  }" |" rt |! |" d  n t j( }# |# t) k rG|  j* t+ | j |# j,   |  j-   |# f  n  PWqt. k
 r_qqXqW|  j* | t f  t |    S| d  t j f k r| Sn  t/ | | | | f  }$ |$ d  k	 r|$ S| j0   }% |% | k rt |% | | | f  Sd  S(   Ni(   t   harmonicR   i   R?   i    t   c1RH   t   c2t   c3t   wexp(   t   orderedt   Tuple(1   R   R   R   Ri   R   t   as_two_termsR   R   R   R   R   R"   R   t   NaNR   RK   R   RL   R   R6   R   R   RU   t   updateR]   R   R   t   OneR/   R   R<   R
   R   R   R   R   RR   R   R   R   R   R   Rj   R   R\   R4   RV   t   eval_sum_hypert   factor(&   R>   R    R   R   t   f_origRA   RB   RC   R   R   t   sRt   sLt   lrsumt   lsumt   rsumR   R?   RM   R   R   R   R   R   t   e_expR   RP   R$   R   R   t	   non_limitt   dent   den_symR\   t   vRO   R~   t   ht   factored(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR     s    		


	43&"%"1
c       	   C  s  d d l  m } d d l m } m } m } m } d d l m } m	 }	 d d l
 m }
 | d k r t |  j | | |  | d  S|  j | d  d k r | |  j | t d d t d	 t   d k r t d  t f St |  j | | d
  | d  S| |  |  } | d k rd St | |
  rId d l m } | |  } n  | |	 |   \ } } | j |  \ } } | j |  \ } } | | g } | | g } g  g  g } x t d  D] } x | | D] } d
 } | j r| j } | j } | j sd Sn  | | |  } | j   d
 k r)d S| j   \ } } | | c | | 9<| | c | | g | 7<qWqW| d d
 g } | d
 } | d | d
 } | | | |  } t |   }  |  j | d  | |  | j f S(   s)    Returns (res, cond). Sums from a to oo. i(   t   hyper(   t   hyperexpandt	   hypersimpt   fractionR{   (   t   PolyR   (   t   Floati    RA   R|   R}   i   (   t	   nsimplifyi   N(   R   R  t   sympy.simplifyR  R  R  R{   t   sympy.polys.polytoolsR  R   t   sympy.core.numbersR  t   _eval_sum_hyperR]   R   R/   R   R"   R<   Re   R  t   as_coeff_mulR   t   is_Powt   expt   baseR   t   degreet
   all_coeffsR   t   convergence_statement(    R>   RA   RB   R  R  R  R  R{   R  R   R  t   hsR  t   numerR   t   topt   toplt   bott   botlt   abt   factorst   paramsR_   t   fact   mulR   R~   R?   t   apt   bqRW   R	  (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR  @  sT    " 0 				#
c         C  s  d d l  m } | \ } } } | | j r0 d  St |  | | | f  } | t j k rD| t j k r t |  j	 | |  | |  } | d  k	 rAt
 | | t f  SqDt |  | |  } t |  | | d  }	 | d  k s |	 d  k r d  S| |	 \ } }
 \ }	 } | |
 |  } | t k r$d  St
 | |	 | f | t f  Sn  | t j k r	t |  j	 | |  | d  } t |  | d  }	 | d  k s|	 d  k rd  S| \ } }
 |	 \ }	 } | |
 |  } | t k s| j   t j k rd  St
 | |	 | f | t f  St |  | |  } | d  k	 r| \ } } | t k r| j r|  j	 | t d d t d t |  }  |  j s|  j rt j S|  j rt j Sn  d  St
 | | t f  Sd  S(   Ni(   R   i   i    RA   R|   R}   (   t   sympy.logic.boolalgR   R   R"   R   R   RL   R   R  R]   R   R/   R   R   t   EmptySetR   R   t   is_positiveR.   t   is_negative(   R>   t   i_a_bR   RA   RB   RC   t   old_sumt   rest   res1t   res2t   cond1t   cond2R   R   t   c(    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyR   v  sT      !	(	
(A   t
   __future__R    R   t   sympy.calculus.singularitiesR   t   sympy.calculus.utilR   t   sympy.concrete.expr_with_limitsR   t"   sympy.concrete.expr_with_intlimitsR   t   sympy.concrete.gosperR   t   sympy.core.addR   t   sympy.core.compatibilityR   Rf   R	   Rg   R
   t   sympy.core.relationalR   t   sympy.core.singletonR   t   sympy.core.symbolR   R   R   t&   sympy.functions.special.zeta_functionsR   t$   sympy.functions.elementary.piecewiseR   R*  R   t   sympy.polysR   R   R   t   sympy.series.limitseqR   t   sympy.series.orderR   t   sympy.sets.setsR   R  R   t   sympy.simplify.combsimpR   t   sympy.simplify.powsimpR   t   sympy.solversR   t   sympy.solvers.solvesetR   R   R   R   R   R   R9   R   R   R  R   (    (    (    s8   lib/python2.7/site-packages/sympy/concrete/summations.pyt   <module>   sJ      		/		+	-		x	6