
    \h                    V   S SK Jr  S SKJr  S SKJr  SSKJr  SSKJ	r	  SSK
Jr  S SKJr  S S	KJr  S S
KJr  S SKJrJr  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKJr  S SK J!r!J"r"J#r#J$r$  S SK%J&r&  S SK'J(r(J)r)  S SK*J+r+  S SK,J-r-J.r.  S SK/J0r0  S SK1J2r2J3r3  S SK4J5r5  S SK6J7r7  S SK8J9r9  S SK:J;r;  S SK<J=r=J>r>  S SK?J@r@  S SKAJBrBJCrC  S SKDJErEJFrFJGrG  S S KHJIrI  S S!KJJKrK  S S"KLJMrM  S S#KNJOrO  S S$KPJQrQ  S S%KRJSrSJTrT  S S&KUJVrV  S S'KWrW " S( S)\	\5      rXS* rYS+ rZS, r[S- r\S. r]S/ r^S0 r_S1 r`S2 raS3 rbS4 rcg')5    )annotations)is_decreasing)AccumulationBounds   )ExprWithIntLimits)AddWithLimits)
gosper_sum)Expr)Add)Tuple)
Derivativeexpand)Mul)Float_illegal)Eq)S)ordered)DummyWildSymbolsymbols)	factorial)	bernoulliharmonic)re)explog)	Piecewise)cotcsc)hyper)KroneckerDelta)zeta)Integral)AndNot)apart)PolynomialErrorPolificationFailed)parallel_poly_from_exprPolyfactor)together)	limit_seq)O)residue)Contains)	FiniteSetInterval)siftNc                      \ rS rSr% SrSrS\S'   S rS rS r	S	 r
S
 rS rS rS rS rS rS rS rS rSS jrS rS rSrg)Sum*   a  
Represents unevaluated summation.

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

``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, sympy.concrete.products.product

References
==========

.. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
       Volume 28 Issue 2, April 1981, Pages 305-350
       https://dl.acm.org/doi/10.1145/322248.322255
.. [2] https://en.wikipedia.org/wiki/Summation#Capital-sigma_notation
.. [3] https://en.wikipedia.org/wiki/Empty_sum
 z tuple[tuple[Symbol, Expr, Expr]]limitsc                    [         R                  " X/UQ70 UD6n[        US5      (       d  U$ [        S UR                   5       5      (       a  [        S5      eU$ )Nr:   c              3  V   #    U  H  n[        U5      S :g  =(       d    SU;   v   M!     g7f)   N)len).0ls     Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/concrete/summations.py	<genexpr>Sum.__new__.<locals>.<genexpr>   s$     <As1v{'dai's   ')z/Sum requires values for lower and upper bounds.)r   __new__hasattranyr:   
ValueError)clsfunctionr   assumptionsobjs        rA   rD   Sum.__new__   sT    ##CKGK{KsH%%J<<<<NOO
    c                ^    U R                   R                  (       d  U R                  (       a  gg NT)rI   is_zerohas_empty_sequenceselfs    rA   _eval_is_zeroSum._eval_is_zero   s!    
 ==  D$;$; %<rM   c                R    U R                   (       a  gU R                  R                  $ rO   )rQ   rI   is_extended_realrR   s    rA   _eval_is_extended_realSum._eval_is_extended_real   s    ""}}---rM   c                r    U R                   (       a&  U R                  SL a  U R                  R                  $ g g NF)has_finite_limitshas_reversed_limitsrI   is_positiverR   s    rA   _eval_is_positiveSum._eval_is_positive   1    !!d&>&>%&G==,,, 'H!rM   c                r    U R                   (       a&  U R                  SL a  U R                  R                  $ g g r[   )r\   r]   rI   is_negativerR   s    rA   _eval_is_negativeSum._eval_is_negative   ra   rM   c                `    U R                   (       a  U R                  R                  (       a  gg g rO   )r\   rI   	is_finiterR   s    rA   _eval_is_finiteSum._eval_is_finite   s"    !!dmm&=&= '>!rM   c                   UR                  SS5      (       a  U R                  R                  " S0 UD6nOU R                  n0 nU R                   H  n[	        U5      nU(       d  M  XSUS   '   M      U(       a  UR                  5        VVs0 s H  u  pgXv_M	     nnnU R                  U5      R                  " S0 UD6n	[        U	[        5      (       a,  [        U	 V
s/ s H  oR                  U5      PM     sn
5      n	U	$ U	b  U	R                  U5      n	U	$ U n	U	$ U R                  R                  (       a0  U R                  5       nX:w  a  UR                  5       $ [        U 5      $ [        U R                  5       H  u  pUu  pnX-
  nUS:X  a  [        R                  s  $ UR                  (       a  UR                   (       a  US-   US-
  pU* n[#        X*X45      nUcO  X R                  :X  a  U R%                  X*X45      nUb  Us  $ U s  $ U R&                  " U/U R                  US  Q76 s  $ UnM     UR                  SS5      (       a'  [        U[(        5      (       d  UR                  " S0 UD6$ U$ s  snnf s  sn
f )NdeepTr   r   r9   )getrI   doitr:   )_dummy_with_inherited_properties_concreteitemsxreplace
isinstancetuple	is_Matrixr   _eval_matrix_sum	enumerater   Zero
is_integerrc   eval_sumeval_zeta_functionfuncr   )rS   hintsfrepsxabdkvundodidiexpandednlimitabdifnewfzeta_functions                      rA   rn   Sum.doit   s3   99VT""""+U+AA ;;C9#>Aq SV  %)ZZ\2\TQAD\D2--%**3U3C#u%%s;s!ZZ-s;<
 J	 ll4( J J ==""{{}H}}&#D))!$++.HAGA!%Cbyvv~~#//1ua!e1BA1y)D|%$($;$;A1y$IM$0,,K99Q9QR99A' /* 99VT"" a++vv&[ 3 <s   I1I7c                n   Uu  p4nUR                   (       aL  UR                   (       a;  XE:  a6  U R                  XU[        R                  -   U[        R                  -
  45      $ U[        R                  La  g[        SU/S9[        SU/S9[        SU/S9pnUR                  Xc-  U-   U* -  5      =n	(       a~  SX   X   -  -  n
X   nX   X   -  U-   n[        U
[        X5      -  [        [        [        U* [        R                  5      5      [        U5      [        R                  :  5      4U S45      $ g)z
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`
Nwexcludeyzr   T)is_comparablerz   r   OneInfinityr   matchr   r$   r&   r'   r2   	Naturals0r   )rS   r}   r:   r   r   r   r   r   r   resultcoeffsqs                rA   rz   Sum.eval_zeta_function  s    a??q15**1!aee)QY.GHHAJJsQC($sQC*@$sUVTWBXaWWaeaiaR01161	VY..E	A	FI%)Aed1j0!#hr1;;&?"@"Q%!%%-PR"D\+ +	 2rM   c                   [        U[        5      (       a  XR                  ;  a  [        R                  $ U R
                  [        U R                  5      p2UR                  S5      nU(       a  U R                  " U/UQ76 nUu  pVnXR                  ;   d  XR                  ;   a  g[        X!SS9nU R                  X5      n	U	$ )aA  
Differentiate wrt x as long as x is not in the free symbols of any of
the upper or lower limits.

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

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.
rl   NT)evaluate)rr   r   free_symbolsr   rw   rI   listr:   popr{   r   )
rS   xr}   r:   r   _r   r   dfrvs
             rA   _eval_derivativeSum._eval_derivative'  s    " a  Q.?.?%?66M MM4#46

2		!%f%Aa!~~"5t,YYr!	rM   c                   U R                   S   u  p4nUR                  XU-   5      n[        U R                   5      S:X  a  U R                   S   nOU R                  " U R                   S S 6 n[	        XsUS-   U45      R                  5       $ )Nrl      r   r   )argssubsr>   r{   r7   rn   )rS   r   stepr   r   upper	new_upperr}   s           rA   _eval_difference_deltaSum._eval_difference_deltaJ  sy    iimeJJqd(+	tyy>Q		!A		499Sb>*A1%!)Y/05577rM   c           	        U R                   nUR                  SS5      (       a  UR                  " S0 UD6n[        R                  " [        U5      5      n/ n/ nU H  nUR                  [        5      (       a  [        R                  " [        U5      5      n/ nU HL  n	[        U	[        5      (       a#  UR                  U	R                  " S0 UD65        M;  UR                  U	5        MN     UR                  [        U6 5        M  UR                  U5        M     SSKJn
Jn  [        U" U5      /UQ76 nU
" XR                  S9$ )Nrk   Tr   )
factor_sumsum_combine)r:   r9   )rI   rm   simplifyr   	make_argsr   hasr7   r   rr   append_eval_simplifysympy.simplify.simplifyr   r   r:   )rS   kwargsrI   termss_to_ttermsubterms	out_termssubtermr   r   r   s                rA   r   Sum._eval_simplifyU  s   ==::fd##((262H fX./Dxx}} ==6	'G!'3//!(()?)?)I&)IJ "((1  ( 

3	?+

4 ' , 	D[%,,&55rM   c           
     P  ^)^*^+^,^- [        S[        S9u  pnU R                  S   S   m,U R                  S   S   m*U R                  S   S   m-U R                  R	                  5       n[        UR                  5      S:  a  [        S5      eT*R                  (       a!  T-R                  (       a  [        R                  $ T*[        R                  L a  T-[        R                  L a]  [        UT,S[        R                  45      R                  5       =(       a+    [        UT,[        R                  S45      R                  5       $ SSKJn  U" UR!                  T,T,* 05      5      nT-* m*[        R                  m-[#        T,R$                  SSS	9nUR!                  T,U05      nUm,['        T*T-5      m)UR(                  (       aw  UR*                   HW  u  pxUS:X  d-  UR-                  5       R.                  [        R                  L d  M8  [        UT,T*T-45      n	U	R                  5       s  $    [        R                  $  [1        UT,5      n
U
b  U
R2                  SL a  [        R4                  $  [1        [7        U5      T,5      nUb  UR2                  SL a  [        R4                  $ [9        UT,[        R                  45      nUR:                  R=                  T,U-  5      nUb0  X   S:  a  [        R                  $ X   S:  a  [        R4                  $ UR:                  R=                  ST,U-  [?        ST,-  5      U-  -  [?        [?        ST,-  5      * 5      U-  -  -  5      =(       dP    UR:                  R=                  ST,U-  [?        ST,-  5      * U-  -  [?        [?        ST,-  5      * 5      U-  -  -  5      nUbc  X   S:  d;  X   S:X  a  X   S:  d+  X   X   s=:X  a  S:X  a*  O  [        R4                  $ X   S:  a  [        R                  $ [        R4                  $  [1        T,U-  T,5      nUb'  UR@                  (       a  US:  a  [        R4                  $ UR!                  T,T,S-   05      nSSK!J"n  SSK#J$n  U" U" UU-  5      5      n [1        UT,5      nUbb  UR@                  (       aQ  U[        RJ                  La>  [7        U5      S:  a  [        R4                  $ [7        U5      S:  a  [        R                  $ US:X  a{  T,UURM                  T,T,S-   5      -  S-
  -  nURO                  5       n [1        UT,5      n
U
b=  U
R@                  (       a,  U
S:  a  [        R                  $ U
S:  a  [        R4                  $  [1        [7        U5      ST,-  -  T,5      nUb=  UR@                  (       a,  US:  a  [        R                  $ US:  a  [        R4                  $ UR=                  [        RP                  T,U-   -  U-  5      nUU   RS                  T,5      (       d$  [U        UU   T)5      (       a  [        R                  $ S
nSSK+J,n  U" UR[                  T,5      T,T)5      nU(       d  T)nOP[]        U[^        5      (       a;  UR.                  R@                  (       a   ['        UR.                  T)R.                  5      nUbj  [U        UU5      (       d  [U        U* U5      (       aG  [a        UT,T*T-45      n URc                  5       nUR@                  (       a  [        UR                  5      $  UR:                  Rd                  (       a  UR:                  R*                  n[g        U5      n[#        SSS9m+U)U+U,4S jnU*U,U-4S jn [i        S[        U5      5       Hs  n![j        Rl                  " UU!5       HU  n"U[g        U"5      -
  n#[o        U"6 n$[o        U#6 n%[U        U$T)5      (       a  U" U%5      n&U&b  U&s  s  $ U " U$U%5      n'U'c  MQ  U's  s  $    Mu     U R                  S   S   n(UR!                  T,U(05      n[        SU-  5      e! [         a     GNf = f! [         a     GNf = f! [         a     GNf = f! [         a    S
n GNf = f! [         a     GNf = f! [         a     GNf = f! [         a     GNf = f)a  
Checks for the convergence of a Sum.

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

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 cannot 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
sympy.concrete.products.Product.is_convergent
zp q r)rH   r   r   r   zNconvergence checking for more than one symbol containing series is not handledr   TintegerpositiveNFrl   combsimp)powsimp)solvesetm)r   c                   >  [        [        U TTR                  T45      R                  5       T5      nUb"  UR                  (       a  [
        R                  $ g g ! [         a     g f = fN)r/   r7   infrn   rg   r   trueNotImplementedError)g_ning_valintervalr   syms     rA   _dirichlet_test*Sum.is_convergent.<locals>._dirichlet_testx  sb    'C#x||Q1G(H(M(M(OQRSG*w/@/@ vv 0A** s   AA 
A'&A'c                L  >  [        U T5      nUb  UR                  (       d=  [        U[        5      (       a\  UR                  UR
                  -
  R                  (       a3  [        UTTT45      R                  5       (       a  [        R                  $ g g g g ! [         a     g f = fr   )r/   rg   rr   r   maxminr7   is_absolutely_convergentr   r   r   )g1_ng2_nlim_vallower_limitr   upper_limits      rA   _bounded_convergent_test3Sum.is_convergent.<locals>._bounded_convergent_test  s    'c2G*0A0A"7,>??$[[7;;6AA"4#{K)HIbbdd'(vv  e B @ +
 + s   BB 
B#"B#zFThe algorithm to find the Sum convergence of %s is not yet implemented)8r   r   r:   rI   r   r>   r   r   rg   r   r   NegativeInfinityr   r7   is_convergentr   rq   r   namer4   is_Piecewiser   as_setsupr/   rP   falseabsr0   exprr   r   	is_numbersympy.simplify.combsimpr   sympy.simplify.powsimpr   NaNr   	gammasimpNegativeOner   r   sympy.solvers.solvesetr   diffrr   r3   r%   rn   is_Mulsetrange	itertoolscombinationsr   ).rS   pr   rsequence_termr   sym_r{   condr   r   lim_val_absorderp_series_test
n_log_testlim_compnext_sequence_termr   r   ratio	lim_ratiotest_vallim_evaluateddict_valcheck_intervalr   maximaintegral_valintegral_val_evaluatedr   argsetr   r   r   a_tupleb_seta_nb_ndirichbc_test_symr   r   r   r   r   s.                                            @@@@@rA   r   Sum.is_convergent|  s   T 't,akk!nQkk!nQ'kk!nQ'..0}))*Q.% 'I J J   [%:%:66M !,,,ajj(=31::*>?MMO YMC1C1CQ+GHVVXY8$]%;%;S3$K%HIM&,K**KSXXtd;%..T{;K5 %%+00
4<4;;=#4#4

#BD3["ABA??,,	 1
 66M	s3G"w%'?ww	#C$6<K&;+>+>%+Gww -#qzz!23 

((a0$"$vv2%ww jj&&q#q&QsUQ*>sC#J;?OQR?R*R'ST Xjj&&q#q&3qu:+1A*A#s1S5zkBRTUBU*U'VW 	!!!#
(9*-41477N :D9J66M77N	 ]!2C8H#(:(:x!|ww
 +33S#'NC42!3M!ABC	!%-I$)<)<RSRWRWAWy>A%77Ny>A%66M
 >M&++Cq9:<=> ?H))+H#Hc2&7+<+<{ vv{ ww	%c-&81S5&A3GM(]-D-D 1$66M 1$77N
 !&&q}}sQw'?'AB{s##hqk8(L(L66M 3-,,S13A%N	**vzz/C/C%fjj(,,?N&=.99=..99'!Ck#B D-9->->-@*-77 !7!A!ABB 82 ::::??DYF c4(A	 1c&k*(55dA>G"S\1Ew-Cu+C$S(33!0!5!-#)M6sC@G*&  ? + {{1~a %..T{;! #;>K#M N 	NU # 		 # 		< # 		 # 	I	 '  # 		2 + s   2-`/ !6a  8a ?Aa" a" %5a5 a5 2Ab 7b #5b /
`=<`= 
aa
aa"a21a25
bb
bb
b%$b%c                p    [        [        U R                  5      U R                  5      R	                  5       $ )a  
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, 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
)r7   r   rI   r:   r   rR   s    rA   r   Sum.is_absolutely_convergent  s'    6 3t}}%t{{3AACCrM   c           
       ^^^ [        U5      n[        U5      nU R                  n[        U R                  5      S:w  a  [	        S5      eU R                  S   u  mmmTT:  S:X  a7  TT-
  S:X  a   [
        R                  [
        R                  4$ TS-   TS-
  smmU* n[
        R                  nU(       Gak  TR                  (       a#  TR                  (       a  [        UTT-
  S-   5      nU(       a  UR                  T5      (       a5  [        [        U5       Vs/ s H  ouR                  TTU-   5      PM     sn6 nOUR                  TT5      nU(       aH  [        UR                  S5      5      U:  n	U	S:X  a  U[        U5      4$ U	S:X  d  U[
        R                  4$ Un[        SU5       HO  nUR                  TTU-   5      n[        UR                  S5      5      U:  a  US:w  a  U[        U5      4s  $ Xh-  nMQ     TT-
  S-   U:X  a  U[
        R                  4$ TU-  m[        S5      n
[!        UR                  TU
5      U
TT45      nU(       a  UR#                  5       nXk-  nUUU4S jnU" U5      u  pX-   S	-  nUR%                  T5      n[        SUS	-   5       H  nU" U5      u  nn['        S	U-  5      [)        S	U-  5      -  UU-
  -  nXr:  a    O~U(       a^  U(       aW  UR                  S5      nU[
        R*                  L a"  [
        R*                  [
        R*                  4s  $ [        U5      U:  a    OXh-  nUR%                  TS	SS
9nM     Xo-   [        W5      4$ s  snf )a  
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.
r   zMore than 1 limitr   Tr=   Fr   c                   > T[         R                  L a  U R                  TT5      S4$ U R                  TT5      U R                  TT5      4$ )Nr   )r   r   r   )r   r   r   r   s    rA   fpoint#Sum.euler_maclaurin.<locals>.fpoint  sB    AJJyyA))99Q?DIIaO33rM   r   r   )intrI   r>   r:   rG   r   rw   
is_Integerr   is_polynomialr   r   r   r   evalfr   r%   rn   r   r   r   r   )rS   r   r   epseval_integralr}   r   r   r   testr   Ir  fafbitermggagb
term_evalfr   r   r   s                       @@@rA   euler_maclaurinSum.euler_maclaurin  s   V FFMMt{{q 011++a.1aEd?1uzvvqvv~%q5!a%DAqAFF||1q519%!//!,,E!H=Hq&&AE*H=>vva|tzz!}-3Dt| #d)|+"em#QVV|+q!A66!QU+D4::a=)C/DAI #d)|+IA	 %
 1uqyA~!&&y FA#JQVVAq\Aq!9-A		4 !FF1Iq!a%AAYFBQqS>)AaC.0"r':Dut!ZZ]
&55!%%<'z?S(IAq!e,A ! y#d)##[ >s   M#c                f   [        U5      n[        U5       H/  u  p4[        U[        5      (       a  M  U R	                  U5      X#'   M1     Sn/ n[        U R
                  5       H4  u  p7UnX2;   a  U* nUS   US   S-   US   S-
  4nUR                  U5        M6     [        XPR                  -  /UQ76 $ )a  
Reverse the order of a limit in a Sum.

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

``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
========

sympy.concrete.expr_with_intlimits.ExprWithIntLimits.index, reorder_limit,
sympy.concrete.expr_with_intlimits.ExprWithIntLimits.reorder

References
==========

.. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
       Volume 28 Issue 2, April 1981, Pages 305-350
       https://dl.acm.org/doi/10.1145/322248.322255
r   r   r   )	r   rv   rr   r  indexr:   r   r7   rI   )	rS   indices	l_indicesr   indxer:   r   r@   s	            rA   reverse_orderSum.reverse_order(  s    | M	 +GAdC((#zz$/	 , !$++.HAA~B1XuQx!|U1X\:MM! / 1}}$.v..rM   c                    SSK Jn  U R                  R                  (       a/  [	        U" [        U R                  5      /U R                  Q76 5      $ g )Nr   )Product)sympy.concrete.productsr8  rI   rW   r   r   r:   )rS   r   r   r8  s       rA   _eval_rewrite_as_ProductSum._eval_rewrite_as_Productw  s9    3==))ws4==1@DKK@AA *rM   N)r   r   r   T)__name__
__module____qualname____firstlineno____doc__	__slots____annotations__rD   rT   rX   r_   rd   rh   rn   rz   r   r   r   r   r   r-  r5  r:  __static_attributes__r9   rM   rA   r7   r7   *   sr    BH I,,.
--;z+*!F	8%6NbNH	D:h$VM/^BrM   r7   c                :    [        U /UQ70 UD6R                  SS9$ )aF  
Compute the summation of f with respect to symbols.

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

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::

Examples
========

>>> 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, sympy.concrete.products.product

F)rk   )r7   rn   )r}   r   r   s      rA   	summationrE  }  s(    d q%7%f%***66rM   c                    Uu  pEn[        [        U5       Vs/ s H+  opR                  XEU-   5      UR                  XFU-
  5      -   PM-     sn6 $ s  snf )a  
Returns the direct summation of the terms of a telescopic sum

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

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

Examples
========

>>> 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

)r   r   r   )LRr   r:   r   r   r   r   s           rA   telescopic_directrI    sK    ( IQ1eAhGhq5!AFF1!e$44hGHHGs   2Ac                   Uu  p4nU R                   (       d  UR                   (       a  g[        S5      nU* R                  U R                  X3U-   5      5      nSnU(       a7  Xg;   a2  Xv   nUR                  (       a  U R                  X3U-   5      U-   S:X  d  SnUc  [        S5      n	 SSKJn
  U
" U R                  X3U	-   5      U-   U	5      =(       d    / nU Vs/ s HM  oR                  (       d  M  U R                  X3U-   5      U-   R                  5       R                  (       d  MK  UPMO     nn[        U5      S:w  a  gUS   nUS:  a  [        X[        U5      X4U45      $ US:  a  [        XXXE45      $ g! [         a     gf = fs  snf )z]
Tries to perform the summation using the telescopic property.

Return None if not possible.
Nr   r   r   solver   )is_Addr   r   r   r  r   sympy.solvers.solversrL  r   r   rP   r>   rI  r   )rG  rH  r:   r   r   r   r   solr   r   rL  sis               rA   
telescopicrQ    si    IQ1xx188 	S	A2**QVVA1u%
&CA
qxFq5!1A!5!:A
 	y#J	3qa%(1,a06BC   9Cb==rqb&!A%--/77 C 9s8q=F1u s1vay99	
Q qa)44 
 # 		9s$   '-E6 F02F&F6
FFc                  ^ Uu  mp#U R                   (       a  [        R                  $ TU R                  ;  a
  XU-
  S-   -  $ X#:X  a  U R	                  TU5      $ UR
                  (       aG  UR
                  (       a6  X#:  a1  [        U TU[        R                  -   U[        R                  -
  45      $ [        U [        5      (       a  [        U4S jU R                   5       5      (       d[  / nU R                   H:  n[        UR                  U5      nUc    g UR                  XeR                  45        M<     U R                  " U6 $ U R!                  ["        5      (       a4  SSKJnJn  U R+                  S S 5      n U" XS   5      (       a  U" X5      $ X2-
  n	U	R,                  n
U
(       a  U	S:  a  [/        U TX#45      $ [        U [        5      (       a  g [1        U R3                  5       TX#45      nUb  U$ U
(       a  [/        U TX#45      $ g )Nr   c              3  \   >#    U  H!  nTUR                   S    R                  ;   v   M#     g7f)r   N)r   r   )r?   argr   s     rA   rB   eval_sum.<locals>.<genexpr>  s$     CFS1000Fs   ),)deltasummation_has_simple_deltac                "    [        U [        5      $ r   )rr   r7   r   s    rA   <lambda>eval_sum.<locals>.<lambda>  s    jC(rM   c                "    U R                  5       $ r   )r-   rY  s    rA   rZ  r[    s
    ahhjrM   r   d   )rP   r   rw   r   r   r   ry   r   rr   r   rF   r   r   r   r   r{   r   r#   deltarV  rW  replacer  eval_sum_directeval_sum_symbolicr   )r}   r:   r   r   newargsrT  newexprrV  rW  r   definitevaluer   s               @rA   ry   ry     s   IQyyvva%!)}vvva|1??quAq155y!aee)455!YCAFFCCC Gvv"388V4?23	 
 667##uu^<II( 
 Qq	**!!,,
%C~~HS3Yq1a),,!Y ahhj1a)4Eq1a),, rM   c           
        Uu  p#nXC-
  nU R                   (       a  U R                  U5      u  pgUS:w  a.  [        XrX445      nU(       a  Xh-  n	U	[        R                  La  U	$ OnU R                  5       u  pU
R                  U5      (       d  [        XX445      nU(       a  X-  $ UR                  U5      (       d  [        XX445      nU(       a  X-  $  [        X5      n U R                  (       a  U R                  U5      u  pgUS:w  a4  [        XrX445      nU(       a  XeS-   -  U-   n	U	[        R                  La  U	$ OLU R                  5       u  p[        XX445      n[        XX445      nSX4;  a  X-   n	U	[        R                  La  U	$ [        [        US-   5       Vs/ s H  nU R                  X#U-   5      PM     sn6 $ ! [         a     Nf = fs  snf )z
Evaluate expression directly, but perform some simple checks first
to possibly result in a smaller expression and faster execution.
r   r   N)r   as_independentr`  r   r   as_two_termsr   r(   r)   rM  r   r   r   )r   r:   r   r   r   r   	without_iwith_ir   r   rG  rH  sRsLlsumrsumjs                    rA   r`  r`  )  s   
 IQ1
%C{{ //2	>A	2AKAEE>H $$&DA5588$QA	24K5588$QA	24KT~ {{ //2	>A	2AQw'!+AEE>H $$&DA"1!i0D"1!i0DD<'KAEE>HeC!Gn=n1!e$n=>>/  . >s   F= G=
G
	G
c                   U nUu  p4nU R                  U5      (       d
  XU-
  S-   -  $ U R                  (       a  U R                  U5      u  pgUS:w  a.  [        XsXE45      nU(       a  Xh-  n	U	[        R
                  La  U	$ OnU R                  5       u  pU
R                  U5      (       d  [        XXE45      nU(       a  X-  $ UR                  U5      (       d  [        XXE45      nU(       a  X-  $  [        X5      n U R                  (       a  U R                  5       u  p[        XX4U45      nU(       a  U$ U R                  U5      u  pgUS:w  a7  [        XsXE45      nU(       a"  XeU-
  S-   -  U-   n	U	[        R
                  La  U	$ O<[        XXE45      n[        XXE45      nS UU4;  a  UU-   n	U	[        R
                  La  U	$ [        S5      nU R                  UU-  5      nUGb  UU   nUR                  (       a  US:  a  U[        R                  L a  U[        R                  Ld&  U[        R                  L a#  U[        R                  La  [        R                  $ [!        US-   US-   5      [!        US-   U5      -
  US-   -  R#                  5       $ UR                  (       aT  US:  aN  US:X  a  [%        U5      [%        US-
  5      -
  $ [%        U['        U5      5      [%        US-
  ['        U5      5      -
  $ UR                  [        R                  [        R                  5      (       Gds  UR                  [        R                  [        R                  5      (       Gd?  [        SU/S9n[        SU/S9n[        SU/S9n[        S	5      nU R)                  5       R                  UU-  5      nUb  UR+                  U5      R#                  5       R                  UU-  U-   5      nUb  UR-                  U5        UU-  R/                  U5      nUU-  R/                  U5      nUUU-  UUS-   -  -
  -  SU-
  -  n	UXT-
  S-   -  n[1        U[3        U[        R4                  5      4U	S
45      $ [7        XXE45      n	[9        U	[:        [<        45      (       a  SSKJ n  SSK!J"n  U	RF                  [I        USS  6 RF                  -
  nU" [K        U	5      5      nUURF                  -  n / n![M        U 5       H{  n" U" UU"5      nU(       a  [3        U"US   5      O[        RN                  n#U#S:w  aB  U!RQ                  [S        UR.                  " U#RT                  6 U5      RW                  5       U#45          O   U!RQ                  U	S
45        [1        U!6 $ U	S [        R
                  4;  a  U	$ [[        X#XE45      n$U$b  U$$ []        X#XE45      n	U	b  U	$ UR_                  5       n%U%U:w  a  [        U%X4U45      $ g ! [         a     GNf = f! [X         a     GM  f = f)Nr   r   r   rl   c1r   c2c3wexpT)denomrK  F)0r   r   rg  ra  r   r   rh  r(   r)   rM  rQ  r   r   r  r   r   r   r   r   r   r   r   updater   r   r   r   r	   rr   r   r   sympy.simplify.radsimpru  rN  rL  r   r   r.   r   r   r   r7   r   rn   r   eval_sum_hypereval_sum_residuer-   )&r}   r:   f_origr   r   r   ri  rj  r   r   rG  rH  rk  rl  lrsumrm  rn  r   r   rq  rr  rs  rt  r4  e_expr   r   r@   ru  rL  	non_limitdenden_symr   r   r   hfactoreds&                                         rA   ra  ra  i  se   FIQ15588a%!)} 	xx,,Q/	>!&a)4AKAEE>H >>#DA5588&qa)44K5588&qa)44K!K 	xx~~1!+L ,,Q/	>!&a)4A1uqy)A-AEE>H %QA	2D$QA	2DD$<'4KAEE>H 	S	AWWQT]F1I<<AvO1C1C(C+++0C::%"1q5!a%09QUA3FFQOWWYY!q&7#A;!a%88#As1v.!a%Q1HHHEE!**a0011EE!**a0011$$$$$$F|
 IIKbDj)=EE$K&&(..r!tby9E VMM!$VMM!$q!ta!a%j()1q51quqyM !R155\!2QI>>qa)$a#c##43qr
);)H)HHI$C#"2"22GDW%c1A'(1adaggAEzSaff)=v%F%K%K%Mq$QR & KKD	"d##T155M!Hv1y)A}Q+A}}}H6 A!955 S  n + s%   /W& /A7W7&
W43W47
XXc                \   US:w  a  [        U R                  XU-   5      US5      $ U R                  US5      S:X  a\  SSKJn  U" U R                  U[	        SSSS95      5      S:X  a  [
        R                  S4$ [        U R                  XS-   5      US5      $ SSKJn  U" X5      nUc  g[        U[        5      (       a  SS	KJ
n  U" U5      nSS
KJn  SSKJn  SSKJn	  U	" [#        U5      5      u  pU
R%                  U5      u  pUR%                  U5      u  pX/nX/n/ / /n['        S5       H  nUU    H  nSnUR(                  (       a,  UR*                  nUR,                  nUR.                  (       d      g[1        UU5      nUR3                  5       S:w  a      gUR5                  5       u  nnUU==   UU-  -  ss'   UU==   UU-  /U-  -  ss'   M     M     US   S/-   nUS   nUS   US   -  n[7        UUU5      nU" U 5      n U R                  US5      U" U5      -  UR8                  4$ )z(Returns (res, cond). Sums from a to oo. r   r   r   Tr   r   )	hypersimpN)	nsimplifyr   )hyperexpand)fractionr   )_eval_sum_hyperr   r   r   r   r   rw   r  rr   r   r  r   r   sympy.simplify.hyperexpandr  rw  r  r-   as_coeff_mulr   is_Powr   baser  r,   degree
all_coeffsr"   convergence_statement)r}   r   r   r   r  hsr  r   r  r  numerru  toptoplbotbotlabfactorsparamsr   facmulr   r   r   apbqr   r  s                                rA   r  r    s   AvqvvaQ/A66vva|q4AFF1eCEFG1L664<qvvaQ/A661	1B	z"e5r]06/F2J'LE""1%IC""1%IC
BlG"XF1X1:CCzzgghh~~S!AxxzQ<<>DAqqEQVOE1I!A#s"I  " 
aSB	B
1beAb"aAA66!Q<A&(?(???rM   c           	     |   Uu  p#nU R                  U5      SL a  g XC-
  R                  (       a  g [        XX445      nU[        R                  :w  a  U[        R
                  L a.  [        U R                  X"* 5      X$* 5      nUb  [        XeS45      $ OrS nU" U 5      n[        XU5      n	U	b  U" U	5      U:  a  g [        XUS-   5      n
U
b  U" U
5      U:  a  g Xsu  pu  p[        X5      nUS:X  a  g [        X-
  U4US45      $ U[        R
                  L a~  [        U R                  X"* 5      US5      n	[        XS5      n
U	b  U
c  g U	u  pU
u  p[        X5      nUS:X  d"  UR                  5       [        R                  :X  a  g [        X-   U4US45      $ [        XU5      nUb  Uu  pUS:X  a  UR                  (       aq  U R                  U[        SSSS9U-   5      n U R                  (       d  U R                  (       a  [        R                  $ U R                   (       a  [        R
                  $ g [        XeS45      $ g )NFTc                6   ^  [        U 4S j[         5       5      $ )Nc              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   )count)r?   r   r   s     rA   rB   3eval_sum_hyper.<locals>.<lambda>.<locals>.<genexpr>B  s     %C(Qaggajj(s   !)sumr   rY  s   `rA   rZ   eval_sum_hyper.<locals>.<lambda>B  s    #%C(%C"CrM   r   r   r   r   )is_hypergeometricr  r7   r   r   r   r  r   r   r&   r   EmptySetr   r   r^   rP   rc   )r}   i_a_br   r   r   old_sumres	n_illegalhadres1res2cond1cond2r   r   cs                   rA   rx  rx  0  s$   GA!1&	!YGAJJ"""!!&&B-B7C o66  DIA,C"1+D|y4"1Q/D|y4+/(MT=Du$Du}dk407D/BBAqvva}a3qQ'<4<5 5=DKKMQZZ7$+t,wo>> !
"C
:{{FF1eCEIJ==AII::%]]---o.. rM   c                	  ^^ Uu  mp#UR                   (       aG  UR                   (       a6  X#:  a1  [        U TU[        R                  -   U[        R                  -
  45      $ S nS nS nU4S jn[	        S5      mUU4S jnU R
                  T1-
  (       a  gUR                  (       d%  U[        R                  [        R                  4;   d  gUR                  (       d%  U[        R                  [        R                  4;   d  gU[        R                  :w  a  U[        R                  :w  a  gU" U T5      n	U	(       a  Sn
U	u  pO,U" U [        R                  T-  -  T5      n	U	(       a  S	n
U	u  pOgUR                  T5      UR                  T5      -
  S
:  a  gX#4[        R                  [        R                  4:X  a[  U" U5      nUc  gUu  pU(       a  gU" XU
5      nU Vs/ s H  n[        UTU5      PM     nn[        R                  * [        U5      -  $ UR                  (       a  U[        R                  L d  gU" X5      (       d  U" U5      nUR                  (       d  gUS:X  a  gUR                  U5      nUR                  U5      nU" X5      (       d  gU
(       aJ  [        R                  T-  [        R                  U-  UR!                  5       -  UR!                  5       -  -  n O!UR!                  5       UR!                  5       -  n [        U TUU-
  UU-
  45      $ U" U5      nUc  gUu  pU(       aW  U Vs/ s H  n[#        U5      PM     nn[%        U5      n['        U5      n[)        U5      UU-
  S-   :X  d  gU[%        U5      ::  a  gU" XU
5      nX-    Vs/ s H  n[        UTU5      PM     nn[        R                  * [        U5      -  nU(       d  UU R+                  TS05      -   S
-  n[-        [#        U5      S5       Vs/ s H  nU R+                  TU05      PM     nn[-        S[#        U5      5       Vs/ s H  nU R+                  TU05      PM     nnU[        U5      -   [        U5      -
  nU$ US
-  n[-        [%        U5      S-   [#        U5      5       Vs/ s H  nU R+                  TU05      PM     nnU[        U5      -
  nU$ s  snf s  snf s  snf s  snf s  snf s  snf )a   Compute the infinite summation with residues

Notes
=====

If $f(n), g(n)$ are polynomials with $\deg(g(n)) - \deg(f(n)) \ge 2$,
some infinite summations can be computed by the following residue
evaluations.

.. math::
    \sum_{n=-\infty, g(n) \ne 0}^{\infty} \frac{f(n)}{g(n)} =
    -\pi \sum_{\alpha|g(\alpha)=0}
    \text{Res}(\cot(\pi x) \frac{f(x)}{g(x)}, \alpha)

.. math::
    \sum_{n=-\infty, g(n) \ne 0}^{\infty} (-1)^n \frac{f(n)}{g(n)} =
    -\pi \sum_{\alpha|g(\alpha)=0}
    \text{Res}(\csc(\pi x) \frac{f(x)}{g(x)}, \alpha)

Examples
========

>>> from sympy import Sum, oo, Symbol
>>> x = Symbol('x')

Doubly infinite series of rational functions.

>>> Sum(1 / (x**2 + 1), (x, -oo, oo)).doit()
pi/tanh(pi)

Doubly infinite alternating series of rational functions.

>>> Sum((-1)**x / (x**2 + 1), (x, -oo, oo)).doit()
pi/sinh(pi)

Infinite series of even rational functions.

>>> Sum(1 / (x**2 + 1), (x, 0, oo)).doit()
1/2 + pi/(2*tanh(pi))

Infinite series of alternating even rational functions.

>>> Sum((-1)**x / (x**2 + 1), (x, 0, oo)).doit()
pi/(2*sinh(pi)) + 1/2

This also have heuristics to transform arbitrarily shifted summand or
arbitrarily shifted summation range to the canonical problem the
formula can handle.

>>> Sum(1 / (x**2 + 2*x + 2), (x, -1, oo)).doit()
1/2 + pi/(2*tanh(pi))
>>> Sum(1 / (x**2 + 4*x + 5), (x, -2, oo)).doit()
1/2 + pi/(2*tanh(pi))
>>> Sum(1 / (x**2 + 1), (x, 1, oo)).doit()
-1/2 + pi/(2*tanh(pi))
>>> Sum(1 / (x**2 + 1), (x, 2, oo)).doit()
-1 + pi/(2*tanh(pi))

References
==========

.. [#] http://www.supermath.info/InfiniteSeriesandtheResidueTheorem.pdf

.. [#] Asmar N.H., Grafakos L. (2018) Residue Theory.
       In: Complex Analysis with Applications.
       Undergraduate Texts in Mathematics. Springer, Cham.
       https://doi.org/10.1007/978-3-319-94063-2_5
c                <   [        S U R                  5        5       5      n[        S UR                  5        5       5      n[        S U R                  5        5       5      n[        S UR                  5        5       5      nU=(       a    U=(       d    U=(       a    U$ )z1Test if the rational function is an even functionc              3  4   #    U  H  u  oS -  S:H  v   M     g7fr   r   Nr9   r?   r   s     rA   rB   =eval_sum_residue.<locals>.is_even_function.<locals>.<genexpr>       ?Q!   c              3  4   #    U  H  u  oS -  S:H  v   M     g7fr  r9   r  s     rA   rB   r    r  r  c              3  4   #    U  H  u  oS -  S:H  v   M     g7fr   r   Nr9   r  s     rA   rB   r         >~tA
~r  c              3  4   #    U  H  u  oS -  S:H  v   M     g7fr  r9   r  s     rA   rB   r    r  r  )allmonoms)r  ru  
numer_even
denom_even	numer_odd	denom_odds         rA   is_even_function*eval_sum_residue.<locals>.is_even_function  so    ???
???
>u||~>>	>u||~>>	)zGy/FYGrM   c                ~    U R                  5       u  p# [        X#4U5      u  u  p#nX#4$ ! [        [        4 a     g f = fr   )as_numer_denomr+   r*   r)   )r}   r   r  ru  opts        rA   match_rational(eval_sum_residue.<locals>.match_rational  sO    '')	"95.!"LNUC | #O4 		s   ) <<c                ~    U R                  5       R                  5       n[        US 5      nS U;   a  g US   US   p2X#4$ )Nc                    U R                   $ r   )rx   rY  s    rA   rZ  5eval_sum_residue.<locals>.get_poles.<locals>.<lambda>  s    allrM   TF)sqf_part	all_rootsr5   )ru  roots	int_rootsnonint_rootss       rA   	get_poles#eval_sum_residue.<locals>.get_poles  sF     **,U235="'+uU|<&&rM   c                   > U R                  T5      nU R                  TU-  5      nU R                  TUS-
  -  5      nU* U-  U-  nU$ )Nr   )r  coeff_monomial)ru  r   r   r   shiftr   s        rA   	get_shift#eval_sum_residue.<locals>.get_shift  sP    LLO  A&  QqS*a!rM   r   c                   > U R                  5       UR                  5       -  R                  TT5      nU(       d!  U[        [        R                  T-  5      -  nU$ U[        [        R                  T-  5      -  nU$ r   )as_exprr   r    r   Pir!   )r  ru  alternatingresidue_factorr   r   s       rA   get_residue_factor,eval_sum_residue.<locals>.get_residue_factor  se    --/EMMO;AA!QGc!$$(m+N  c!$$(m+NrM   NFTr   r   r   )r   ry  r   r   r   r   r  r   r   r   r  r1   r  r  rg   r  r  r  r   r   r>   rq   r   )r}   r  r   r   r  r  r  r  r  r   r  r  ru  polesr  r  r  rootresiduesr  int_roots_maxint_roots_minfull_sumhalf_sumi0extraneous_negextraneous_posr   
extraneousr   r   s                                @@rA   ry  ry  l  sZ   J GAq 	1??quAq155y!aee)#<==H' 	c
A 	~~LLA!**a.@.@!AALLA!**a.@.@!AA 	A1

?1a Euq1==!#33Q7K LE5||Aa(1,	v!$$ajj11% ="'	+E+FAMNGNAt4Nus8}$$KKAOE)) % A:E"E"--q AMM5$85==?$JU]]_$\]A%--/1AAqw%#899eE}#I+4594SY9	5II 9~!>!BB I'kBN=F=UV=UT40=UHVus8}$H qzz1a&11Q6 9>c!fa8HI8H"!**aW-8HI8=aQ8HI8H"!**aW-8HIC//#n2EE !|H 16c)nq6H#a&0QR0Q"!**aW%0QJRJ'FMM OF 6 W JI Ss$   !S<SSS"S'$S,c                    U R                   nU R                   HV  nUu  p4nXT-
  nUR                  (       d  M  US:  S:X  a  US-   US-
  pTU* n[        XXE45      nUc  MF  UR	                  5       s  $    g )Nr   Tr   )rI   r:   r  r`  rn   )
expressionr}   r   r   r   r   r   r   s           rA   ru   ru   P  sx    A""ae>>>aD 1ua!e1B"1!i0Dyy{" #rM   c                  ^	 U u  pnX#/n/ SQn0 n0 nU HN  m	UR                   R                  T	S5      nU(       a  SUT	'   M-  [        U	4S jU 5       5      (       d  MI  SUT	'   MP     U(       a  UR                  U5        [	        S0 UD6$ g)z
Return a Dummy symbol that inherits as many assumptions as possible
from the provided symbol and limits.

If the symbol already has all True assumption shared by the limits
then return None.
)extended_nonnegativenonnegativeextended_nonpositivenonpositiveextended_positiver   extended_negativenegativer   rationalfinitezerorealextended_realNTc              3  B   >#    U  H  n[        US T-   5      v   M     g7f)is_N)getattr)r?   r   assums     rA   rB   <_dummy_with_inherited_properties_concrete.<locals>.<genexpr>w  s     6AqEEM**As   )r   )_assumptionsrm   r  rv  r   )
r:   r   r   r   r@   assumptions_to_considerassumptions_to_keepassumptions_to_add
assum_truer  s
            @rA   ro   ro   _  s     GA!	
A@ (^^''t4
)-&6A666(,u% ) ""#560/00 rM   )d
__future__r   sympy.calculus.singularitiesr   !sympy.calculus.accumulationboundsr   expr_with_intlimitsr   expr_with_limitsr   gosperr	   sympy.core.exprr
   sympy.core.addr   sympy.core.containersr   sympy.core.functionr   r   sympy.core.mulr   sympy.core.numbersr   r   sympy.core.relationalr   sympy.core.singletonr   sympy.core.sortingr   sympy.core.symbolr   r   r   r   (sympy.functions.combinatorial.factorialsr   %sympy.functions.combinatorial.numbersr   r   $sympy.functions.elementary.complexesr   &sympy.functions.elementary.exponentialr   r   $sympy.functions.elementary.piecewiser   (sympy.functions.elementary.trigonometricr    r!   sympy.functions.special.hyperr"   (sympy.functions.special.tensor_functionsr#   &sympy.functions.special.zeta_functionsr$   sympy.integrals.integralsr%   sympy.logic.boolalgr&   r'   sympy.polys.partfracr(   sympy.polys.polyerrorsr)   r*   sympy.polys.polytoolsr+   r,   r-   sympy.polys.rationaltoolsr.   sympy.series.limitseqr/   sympy.series.orderr0   sympy.series.residuesr1   sympy.sets.containsr2   sympy.sets.setsr3   r4   sympy.utilities.iterablesr5   r   r7   rE  rI  rQ  ry   r`  ra  r  rx  ry  ru   ro   r9   rM   rA   <module>r(     s    " 6 @ 2 +     ' 2  . $ " & : : > E 3 ; : = / C 7 . ( & F G G . +   ) ( / * PB-* PBf27jI0+5\.-b=?@N6b3@l9/xaH#1rM   