1from sympy import (
2    Abs, And, binomial, Catalan, combsimp, cos, Derivative, E, Eq, exp, EulerGamma,
3    factorial, Function, harmonic, I, Integral, KroneckerDelta, log,
4    nan, oo, pi, Piecewise, Product, product, Rational, S, simplify, Identity,
5    sin, sqrt, Sum, summation, Symbol, symbols, sympify, zeta, gamma,
6    Indexed, Idx, IndexedBase, prod, Dummy, lowergamma, Range, floor,
7    rf, MatrixSymbol, tanh, sinh)
8from sympy.abc import a, b, c, d, k, m, x, y, z
9from sympy.concrete.summations import (
10    telescopic, _dummy_with_inherited_properties_concrete, eval_sum_residue)
11from sympy.concrete.expr_with_intlimits import ReorderError
12from sympy.core.facts import InconsistentAssumptions
13from sympy.testing.pytest import XFAIL, raises, slow
14from sympy.matrices import \
15    Matrix, SparseMatrix, ImmutableDenseMatrix, ImmutableSparseMatrix
16from sympy.core.mod import Mod
17
18n = Symbol('n', integer=True)
19
20def test_karr_convention():
21    # Test the Karr summation convention that we want to hold.
22    # See his paper "Summation in Finite Terms" for a detailed
23    # reasoning why we really want exactly this definition.
24    # The convention is described on page 309 and essentially
25    # in section 1.4, definition 3:
26    #
27    # \sum_{m <= i < n} f(i) 'has the obvious meaning'   for m < n
28    # \sum_{m <= i < n} f(i) = 0                         for m = n
29    # \sum_{m <= i < n} f(i) = - \sum_{n <= i < m} f(i)  for m > n
30    #
31    # It is important to note that he defines all sums with
32    # the upper limit being *exclusive*.
33    # In contrast, sympy and the usual mathematical notation has:
34    #
35    # sum_{i = a}^b f(i) = f(a) + f(a+1) + ... + f(b-1) + f(b)
36    #
37    # with the upper limit *inclusive*. So translating between
38    # the two we find that:
39    #
40    # \sum_{m <= i < n} f(i) = \sum_{i = m}^{n-1} f(i)
41    #
42    # where we intentionally used two different ways to typeset the
43    # sum and its limits.
44
45    i = Symbol("i", integer=True)
46    k = Symbol("k", integer=True)
47    j = Symbol("j", integer=True)
48
49    # A simple example with a concrete summand and symbolic limits.
50
51    # The normal sum: m = k and n = k + j and therefore m < n:
52    m = k
53    n = k + j
54
55    a = m
56    b = n - 1
57    S1 = Sum(i**2, (i, a, b)).doit()
58
59    # The reversed sum: m = k + j and n = k and therefore m > n:
60    m = k + j
61    n = k
62
63    a = m
64    b = n - 1
65    S2 = Sum(i**2, (i, a, b)).doit()
66
67    assert simplify(S1 + S2) == 0
68
69    # Test the empty sum: m = k and n = k and therefore m = n:
70    m = k
71    n = k
72
73    a = m
74    b = n - 1
75    Sz = Sum(i**2, (i, a, b)).doit()
76
77    assert Sz == 0
78
79    # Another example this time with an unspecified summand and
80    # numeric limits. (We can not do both tests in the same example.)
81    f = Function("f")
82
83    # The normal sum with m < n:
84    m = 2
85    n = 11
86
87    a = m
88    b = n - 1
89    S1 = Sum(f(i), (i, a, b)).doit()
90
91    # The reversed sum with m > n:
92    m = 11
93    n = 2
94
95    a = m
96    b = n - 1
97    S2 = Sum(f(i), (i, a, b)).doit()
98
99    assert simplify(S1 + S2) == 0
100
101    # Test the empty sum with m = n:
102    m = 5
103    n = 5
104
105    a = m
106    b = n - 1
107    Sz = Sum(f(i), (i, a, b)).doit()
108
109    assert Sz == 0
110
111    e = Piecewise((exp(-i), Mod(i, 2) > 0), (0, True))
112    s = Sum(e, (i, 0, 11))
113    assert s.n(3) == s.doit().n(3)
114
115
116def test_karr_proposition_2a():
117    # Test Karr, page 309, proposition 2, part a
118    i = Symbol("i", integer=True)
119    u = Symbol("u", integer=True)
120    v = Symbol("v", integer=True)
121
122    def test_the_sum(m, n):
123        # g
124        g = i**3 + 2*i**2 - 3*i
125        # f = Delta g
126        f = simplify(g.subs(i, i+1) - g)
127        # The sum
128        a = m
129        b = n - 1
130        S = Sum(f, (i, a, b)).doit()
131        # Test if Sum_{m <= i < n} f(i) = g(n) - g(m)
132        assert simplify(S - (g.subs(i, n) - g.subs(i, m))) == 0
133
134    # m < n
135    test_the_sum(u,   u+v)
136    # m = n
137    test_the_sum(u,   u  )
138    # m > n
139    test_the_sum(u+v, u  )
140
141
142def test_karr_proposition_2b():
143    # Test Karr, page 309, proposition 2, part b
144    i = Symbol("i", integer=True)
145    u = Symbol("u", integer=True)
146    v = Symbol("v", integer=True)
147    w = Symbol("w", integer=True)
148
149    def test_the_sum(l, n, m):
150        # Summand
151        s = i**3
152        # First sum
153        a = l
154        b = n - 1
155        S1 = Sum(s, (i, a, b)).doit()
156        # Second sum
157        a = l
158        b = m - 1
159        S2 = Sum(s, (i, a, b)).doit()
160        # Third sum
161        a = m
162        b = n - 1
163        S3 = Sum(s, (i, a, b)).doit()
164        # Test if S1 = S2 + S3 as required
165        assert S1 - (S2 + S3) == 0
166
167    # l < m < n
168    test_the_sum(u,     u+v,   u+v+w)
169    # l < m = n
170    test_the_sum(u,     u+v,   u+v  )
171    # l < m > n
172    test_the_sum(u,     u+v+w, v    )
173    # l = m < n
174    test_the_sum(u,     u,     u+v  )
175    # l = m = n
176    test_the_sum(u,     u,     u    )
177    # l = m > n
178    test_the_sum(u+v,   u+v,   u    )
179    # l > m < n
180    test_the_sum(u+v,   u,     u+w  )
181    # l > m = n
182    test_the_sum(u+v,   u,     u    )
183    # l > m > n
184    test_the_sum(u+v+w, u+v,   u    )
185
186
187def test_arithmetic_sums():
188    assert summation(1, (n, a, b)) == b - a + 1
189    assert Sum(S.NaN, (n, a, b)) is S.NaN
190    assert Sum(x, (n, a, a)).doit() == x
191    assert Sum(x, (x, a, a)).doit() == a
192    assert Sum(x, (n, 1, a)).doit() == a*x
193    assert Sum(x, (x, Range(1, 11))).doit() == 55
194    assert Sum(x, (x, Range(1, 11, 2))).doit() == 25
195    assert Sum(x, (x, Range(1, 10, 2))) == Sum(x, (x, Range(9, 0, -2)))
196    lo, hi = 1, 2
197    s1 = Sum(n, (n, lo, hi))
198    s2 = Sum(n, (n, hi, lo))
199    assert s1 != s2
200    assert s1.doit() == 3 and s2.doit() == 0
201    lo, hi = x, x + 1
202    s1 = Sum(n, (n, lo, hi))
203    s2 = Sum(n, (n, hi, lo))
204    assert s1 != s2
205    assert s1.doit() == 2*x + 1 and s2.doit() == 0
206    assert Sum(Integral(x, (x, 1, y)) + x, (x, 1, 2)).doit() == \
207        y**2 + 2
208    assert summation(1, (n, 1, 10)) == 10
209    assert summation(2*n, (n, 0, 10**10)) == 100000000010000000000
210    assert summation(4*n*m, (n, a, 1), (m, 1, d)).expand() == \
211        2*d + 2*d**2 + a*d + a*d**2 - d*a**2 - a**2*d**2
212    assert summation(cos(n), (n, -2, 1)) == cos(-2) + cos(-1) + cos(0) + cos(1)
213    assert summation(cos(n), (n, x, x + 2)) == cos(x) + cos(x + 1) + cos(x + 2)
214    assert isinstance(summation(cos(n), (n, x, x + S.Half)), Sum)
215    assert summation(k, (k, 0, oo)) is oo
216    assert summation(k, (k, Range(1, 11))) == 55
217
218
219def test_polynomial_sums():
220    assert summation(n**2, (n, 3, 8)) == 199
221    assert summation(n, (n, a, b)) == \
222        ((a + b)*(b - a + 1)/2).expand()
223    assert summation(n**2, (n, 1, b)) == \
224        ((2*b**3 + 3*b**2 + b)/6).expand()
225    assert summation(n**3, (n, 1, b)) == \
226        ((b**4 + 2*b**3 + b**2)/4).expand()
227    assert summation(n**6, (n, 1, b)) == \
228        ((6*b**7 + 21*b**6 + 21*b**5 - 7*b**3 + b)/42).expand()
229
230
231def test_geometric_sums():
232    assert summation(pi**n, (n, 0, b)) == (1 - pi**(b + 1)) / (1 - pi)
233    assert summation(2 * 3**n, (n, 0, b)) == 3**(b + 1) - 1
234    assert summation(S.Half**n, (n, 1, oo)) == 1
235    assert summation(2**n, (n, 0, b)) == 2**(b + 1) - 1
236    assert summation(2**n, (n, 1, oo)) is oo
237    assert summation(2**(-n), (n, 1, oo)) == 1
238    assert summation(3**(-n), (n, 4, oo)) == Rational(1, 54)
239    assert summation(2**(-4*n + 3), (n, 1, oo)) == Rational(8, 15)
240    assert summation(2**(n + 1), (n, 1, b)).expand() == 4*(2**b - 1)
241
242    # issue 6664:
243    assert summation(x**n, (n, 0, oo)) == \
244        Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**n, (n, 0, oo)), True))
245
246    assert summation(-2**n, (n, 0, oo)) is -oo
247    assert summation(I**n, (n, 0, oo)) == Sum(I**n, (n, 0, oo))
248
249    # issue 6802:
250    assert summation((-1)**(2*x + 2), (x, 0, n)) == n + 1
251    assert summation((-2)**(2*x + 2), (x, 0, n)) == 4*4**(n + 1)/S(3) - Rational(4, 3)
252    assert summation((-1)**x, (x, 0, n)) == -(-1)**(n + 1)/S(2) + S.Half
253    assert summation(y**x, (x, a, b)) == \
254        Piecewise((-a + b + 1, Eq(y, 1)), ((y**a - y**(b + 1))/(-y + 1), True))
255    assert summation((-2)**(y*x + 2), (x, 0, n)) == \
256        4*Piecewise((n + 1, Eq((-2)**y, 1)),
257                    ((-(-2)**(y*(n + 1)) + 1)/(-(-2)**y + 1), True))
258
259    # issue 8251:
260    assert summation((1/(n + 1)**2)*n**2, (n, 0, oo)) is oo
261
262    #issue 9908:
263    assert Sum(1/(n**3 - 1), (n, -oo, -2)).doit() == summation(1/(n**3 - 1), (n, -oo, -2))
264
265    #issue 11642:
266    result = Sum(0.5**n, (n, 1, oo)).doit()
267    assert result == 1
268    assert result.is_Float
269
270    result = Sum(0.25**n, (n, 1, oo)).doit()
271    assert result == 1/3.
272    assert result.is_Float
273
274    result = Sum(0.99999**n, (n, 1, oo)).doit()
275    assert result == 99999
276    assert result.is_Float
277
278    result = Sum(S.Half**n, (n, 1, oo)).doit()
279    assert result == 1
280    assert not result.is_Float
281
282    result = Sum(Rational(3, 5)**n, (n, 1, oo)).doit()
283    assert result == Rational(3, 2)
284    assert not result.is_Float
285
286    assert Sum(1.0**n, (n, 1, oo)).doit() is oo
287    assert Sum(2.43**n, (n, 1, oo)).doit() is oo
288
289    # Issue 13979
290    i, k, q = symbols('i k q', integer=True)
291    result = summation(
292        exp(-2*I*pi*k*i/n) * exp(2*I*pi*q*i/n) / n, (i, 0, n - 1)
293    )
294    assert result.simplify() == Piecewise(
295            (1, Eq(exp(-2*I*pi*(k - q)/n), 1)), (0, True)
296    )
297
298
299def test_harmonic_sums():
300    assert summation(1/k, (k, 0, n)) == Sum(1/k, (k, 0, n))
301    assert summation(1/k, (k, 1, n)) == harmonic(n)
302    assert summation(n/k, (k, 1, n)) == n*harmonic(n)
303    assert summation(1/k, (k, 5, n)) == harmonic(n) - harmonic(4)
304
305
306def test_composite_sums():
307    f = S.Half*(7 - 6*n + Rational(1, 7)*n**3)
308    s = summation(f, (n, a, b))
309    assert not isinstance(s, Sum)
310    A = 0
311    for i in range(-3, 5):
312        A += f.subs(n, i)
313    B = s.subs(a, -3).subs(b, 4)
314    assert A == B
315
316
317def test_hypergeometric_sums():
318    assert summation(
319        binomial(2*k, k)/4**k, (k, 0, n)) == (1 + 2*n)*binomial(2*n, n)/4**n
320    assert summation(binomial(2*k, k)/5**k, (k, -oo, oo)) == sqrt(5)
321
322
323def test_other_sums():
324    f = m**2 + m*exp(m)
325    g = 3*exp(Rational(3, 2))/2 + exp(S.Half)/2 - exp(Rational(-1, 2))/2 - 3*exp(Rational(-3, 2))/2 + 5
326
327    assert summation(f, (m, Rational(-3, 2), Rational(3, 2))) == g
328    assert summation(f, (m, -1.5, 1.5)).evalf().epsilon_eq(g.evalf(), 1e-10)
329
330fac = factorial
331
332
333def NS(e, n=15, **options):
334    return str(sympify(e).evalf(n, **options))
335
336
337def test_evalf_fast_series():
338    # Euler transformed series for sqrt(1+x)
339    assert NS(Sum(
340        fac(2*n + 1)/fac(n)**2/2**(3*n + 1), (n, 0, oo)), 100) == NS(sqrt(2), 100)
341
342    # Some series for exp(1)
343    estr = NS(E, 100)
344    assert NS(Sum(1/fac(n), (n, 0, oo)), 100) == estr
345    assert NS(1/Sum((1 - 2*n)/fac(2*n), (n, 0, oo)), 100) == estr
346    assert NS(Sum((2*n + 1)/fac(2*n), (n, 0, oo)), 100) == estr
347    assert NS(Sum((4*n + 3)/2**(2*n + 1)/fac(2*n + 1), (n, 0, oo))**2, 100) == estr
348
349    pistr = NS(pi, 100)
350    # Ramanujan series for pi
351    assert NS(9801/sqrt(8)/Sum(fac(
352        4*n)*(1103 + 26390*n)/fac(n)**4/396**(4*n), (n, 0, oo)), 100) == pistr
353    assert NS(1/Sum(
354        binomial(2*n, n)**3 * (42*n + 5)/2**(12*n + 4), (n, 0, oo)), 100) == pistr
355    # Machin's formula for pi
356    assert NS(16*Sum((-1)**n/(2*n + 1)/5**(2*n + 1), (n, 0, oo)) -
357        4*Sum((-1)**n/(2*n + 1)/239**(2*n + 1), (n, 0, oo)), 100) == pistr
358
359    # Apery's constant
360    astr = NS(zeta(3), 100)
361    P = 126392*n**5 + 412708*n**4 + 531578*n**3 + 336367*n**2 + 104000* \
362        n + 12463
363    assert NS(Sum((-1)**n * P / 24 * (fac(2*n + 1)*fac(2*n)*fac(
364        n))**3 / fac(3*n + 2) / fac(4*n + 3)**3, (n, 0, oo)), 100) == astr
365    assert NS(Sum((-1)**n * (205*n**2 + 250*n + 77)/64 * fac(n)**10 /
366              fac(2*n + 1)**5, (n, 0, oo)), 100) == astr
367
368
369def test_evalf_fast_series_issue_4021():
370    # Catalan's constant
371    assert NS(Sum((-1)**(n - 1)*2**(8*n)*(40*n**2 - 24*n + 3)*fac(2*n)**3*
372        fac(n)**2/n**3/(2*n - 1)/fac(4*n)**2, (n, 1, oo))/64, 100) == \
373        NS(Catalan, 100)
374    astr = NS(zeta(3), 100)
375    assert NS(5*Sum(
376        (-1)**(n - 1)*fac(n)**2 / n**3 / fac(2*n), (n, 1, oo))/2, 100) == astr
377    assert NS(Sum((-1)**(n - 1)*(56*n**2 - 32*n + 5) / (2*n - 1)**2 * fac(n - 1)
378              **3 / fac(3*n), (n, 1, oo))/4, 100) == astr
379
380
381def test_evalf_slow_series():
382    assert NS(Sum((-1)**n / n, (n, 1, oo)), 15) == NS(-log(2), 15)
383    assert NS(Sum((-1)**n / n, (n, 1, oo)), 50) == NS(-log(2), 50)
384    assert NS(Sum(1/n**2, (n, 1, oo)), 15) == NS(pi**2/6, 15)
385    assert NS(Sum(1/n**2, (n, 1, oo)), 100) == NS(pi**2/6, 100)
386    assert NS(Sum(1/n**2, (n, 1, oo)), 500) == NS(pi**2/6, 500)
387    assert NS(Sum((-1)**n / (2*n + 1)**3, (n, 0, oo)), 15) == NS(pi**3/32, 15)
388    assert NS(Sum((-1)**n / (2*n + 1)**3, (n, 0, oo)), 50) == NS(pi**3/32, 50)
389
390
391def test_euler_maclaurin():
392    # Exact polynomial sums with E-M
393    def check_exact(f, a, b, m, n):
394        A = Sum(f, (k, a, b))
395        s, e = A.euler_maclaurin(m, n)
396        assert (e == 0) and (s.expand() == A.doit())
397    check_exact(k**4, a, b, 0, 2)
398    check_exact(k**4 + 2*k, a, b, 1, 2)
399    check_exact(k**4 + k**2, a, b, 1, 5)
400    check_exact(k**5, 2, 6, 1, 2)
401    check_exact(k**5, 2, 6, 1, 3)
402    assert Sum(x-1, (x, 0, 2)).euler_maclaurin(m=30, n=30, eps=2**-15) == (0, 0)
403    # Not exact
404    assert Sum(k**6, (k, a, b)).euler_maclaurin(0, 2)[1] != 0
405    # Numerical test
406    for mi, ni in [(2, 4), (2, 20), (10, 20), (18, 20)]:
407        A = Sum(1/k**3, (k, 1, oo))
408        s, e = A.euler_maclaurin(mi, ni)
409        assert abs((s - zeta(3)).evalf()) < e.evalf()
410
411    raises(ValueError, lambda: Sum(1, (x, 0, 1), (k, 0, 1)).euler_maclaurin())
412
413
414@slow
415def test_evalf_euler_maclaurin():
416    assert NS(Sum(1/k**k, (k, 1, oo)), 15) == '1.29128599706266'
417    assert NS(Sum(1/k**k, (k, 1, oo)),
418              50) == '1.2912859970626635404072825905956005414986193682745'
419    assert NS(Sum(1/k - log(1 + 1/k), (k, 1, oo)), 15) == NS(EulerGamma, 15)
420    assert NS(Sum(1/k - log(1 + 1/k), (k, 1, oo)), 50) == NS(EulerGamma, 50)
421    assert NS(Sum(log(k)/k**2, (k, 1, oo)), 15) == '0.937548254315844'
422    assert NS(Sum(log(k)/k**2, (k, 1, oo)),
423              50) == '0.93754825431584375370257409456786497789786028861483'
424    assert NS(Sum(1/k, (k, 1000000, 2000000)), 15) == '0.693147930560008'
425    assert NS(Sum(1/k, (k, 1000000, 2000000)),
426              50) == '0.69314793056000780941723211364567656807940638436025'
427
428
429def test_evalf_symbolic():
430    f, g = symbols('f g', cls=Function)
431    # issue 6328
432    expr = Sum(f(x), (x, 1, 3)) + Sum(g(x), (x, 1, 3))
433    assert expr.evalf() == expr
434
435
436def test_evalf_issue_3273():
437    assert Sum(0, (k, 1, oo)).evalf() == 0
438
439
440def test_simple_products():
441    assert Product(S.NaN, (x, 1, 3)) is S.NaN
442    assert product(S.NaN, (x, 1, 3)) is S.NaN
443    assert Product(x, (n, a, a)).doit() == x
444    assert Product(x, (x, a, a)).doit() == a
445    assert Product(x, (y, 1, a)).doit() == x**a
446
447    lo, hi = 1, 2
448    s1 = Product(n, (n, lo, hi))
449    s2 = Product(n, (n, hi, lo))
450    assert s1 != s2
451    # This IS correct according to Karr product convention
452    assert s1.doit() == 2
453    assert s2.doit() == 1
454
455    lo, hi = x, x + 1
456    s1 = Product(n, (n, lo, hi))
457    s2 = Product(n, (n, hi, lo))
458    s3 = 1 / Product(n, (n, hi + 1, lo - 1))
459    assert s1 != s2
460    # This IS correct according to Karr product convention
461    assert s1.doit() == x*(x + 1)
462    assert s2.doit() == 1
463    assert s3.doit() == x*(x + 1)
464
465    assert Product(Integral(2*x, (x, 1, y)) + 2*x, (x, 1, 2)).doit() == \
466        (y**2 + 1)*(y**2 + 3)
467    assert product(2, (n, a, b)) == 2**(b - a + 1)
468    assert product(n, (n, 1, b)) == factorial(b)
469    assert product(n**3, (n, 1, b)) == factorial(b)**3
470    assert product(3**(2 + n), (n, a, b)) \
471        == 3**(2*(1 - a + b) + b/2 + (b**2)/2 + a/2 - (a**2)/2)
472    assert product(cos(n), (n, 3, 5)) == cos(3)*cos(4)*cos(5)
473    assert product(cos(n), (n, x, x + 2)) == cos(x)*cos(x + 1)*cos(x + 2)
474    assert isinstance(product(cos(n), (n, x, x + S.Half)), Product)
475    # If Product managed to evaluate this one, it most likely got it wrong!
476    assert isinstance(Product(n**n, (n, 1, b)), Product)
477
478
479def test_rational_products():
480    assert combsimp(product(1 + 1/n, (n, a, b))) == (1 + b)/a
481    assert combsimp(product(n + 1, (n, a, b))) == gamma(2 + b)/gamma(1 + a)
482    assert combsimp(product((n + 1)/(n - 1), (n, a, b))) == b*(1 + b)/(a*(a - 1))
483    assert combsimp(product(n/(n + 1)/(n + 2), (n, a, b))) == \
484        a*gamma(a + 2)/(b + 1)/gamma(b + 3)
485    assert combsimp(product(n*(n + 1)/(n - 1)/(n - 2), (n, a, b))) == \
486        b**2*(b - 1)*(1 + b)/(a - 1)**2/(a*(a - 2))
487
488
489def test_wallis_product():
490    # Wallis product, given in two different forms to ensure that Product
491    # can factor simple rational expressions
492    A = Product(4*n**2 / (4*n**2 - 1), (n, 1, b))
493    B = Product((2*n)*(2*n)/(2*n - 1)/(2*n + 1), (n, 1, b))
494    R = pi*gamma(b + 1)**2/(2*gamma(b + S.Half)*gamma(b + Rational(3, 2)))
495    assert simplify(A.doit()) == R
496    assert simplify(B.doit()) == R
497    # This one should eventually also be doable (Euler's product formula for sin)
498    # assert Product(1+x/n**2, (n, 1, b)) == ...
499
500
501def test_telescopic_sums():
502    #checks also input 2 of comment 1 issue 4127
503    assert Sum(1/k - 1/(k + 1), (k, 1, n)).doit() == 1 - 1/(1 + n)
504    f = Function("f")
505    assert Sum(
506        f(k) - f(k + 2), (k, m, n)).doit() == -f(1 + n) - f(2 + n) + f(m) + f(1 + m)
507    assert Sum(cos(k) - cos(k + 3), (k, 1, n)).doit() == -cos(1 + n) - \
508        cos(2 + n) - cos(3 + n) + cos(1) + cos(2) + cos(3)
509
510    # dummy variable shouldn't matter
511    assert telescopic(1/m, -m/(1 + m), (m, n - 1, n)) == \
512        telescopic(1/k, -k/(1 + k), (k, n - 1, n))
513
514    assert Sum(1/x/(x - 1), (x, a, b)).doit() == -((a - b - 1)/(b*(a - 1)))
515
516
517def test_sum_reconstruct():
518    s = Sum(n**2, (n, -1, 1))
519    assert s == Sum(*s.args)
520    raises(ValueError, lambda: Sum(x, x))
521    raises(ValueError, lambda: Sum(x, (x, 1)))
522
523
524def test_limit_subs():
525    for F in (Sum, Product, Integral):
526        assert F(a*exp(a), (a, -2, 2)) == F(a*exp(a), (a, -b, b)).subs(b, 2)
527        assert F(a, (a, F(b, (b, 1, 2)), 4)).subs(F(b, (b, 1, 2)), c) == \
528            F(a, (a, c, 4))
529        assert F(x, (x, 1, x + y)).subs(x, 1) == F(x, (x, 1, y + 1))
530
531
532def test_function_subs():
533    f = Function("f")
534    S = Sum(x*f(y),(x,0,oo),(y,0,oo))
535    assert S.subs(f(y),y) == Sum(x*y,(x,0,oo),(y,0,oo))
536    assert S.subs(f(x),x) == S
537    raises(ValueError, lambda: S.subs(f(y),x+y) )
538    S = Sum(x*log(y),(x,0,oo),(y,0,oo))
539    assert S.subs(log(y),y) == S
540    S = Sum(x*f(y),(x,0,oo),(y,0,oo))
541    assert S.subs(f(y),y) == Sum(x*y,(x,0,oo),(y,0,oo))
542
543
544def test_equality():
545    # if this fails remove special handling below
546    raises(ValueError, lambda: Sum(x, x))
547    r = symbols('x', real=True)
548    for F in (Sum, Product, Integral):
549        try:
550            assert F(x, x) != F(y, y)
551            assert F(x, (x, 1, 2)) != F(x, x)
552            assert F(x, (x, x)) != F(x, x)  # or else they print the same
553            assert F(1, x) != F(1, y)
554        except ValueError:
555            pass
556        assert F(a, (x, 1, 2)) != F(a, (x, 1, 3))  # diff limit
557        assert F(a, (x, 1, x)) != F(a, (y, 1, y))
558        assert F(a, (x, 1, 2)) != F(b, (x, 1, 2))  # diff expression
559        assert F(x, (x, 1, 2)) != F(r, (r, 1, 2))  # diff assumptions
560        assert F(1, (x, 1, x)) != F(1, (y, 1, x))  # only dummy is diff
561        assert F(1, (x, 1, x)).dummy_eq(F(1, (y, 1, x)))
562
563    # issue 5265
564    assert Sum(x, (x, 1, x)).subs(x, a) == Sum(x, (x, 1, a))
565
566
567def test_Sum_doit():
568    f = Function('f')
569    assert Sum(n*Integral(a**2), (n, 0, 2)).doit() == a**3
570    assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \
571        3*Integral(a**2)
572    assert summation(n*Integral(a**2), (n, 0, 2)) == 3*Integral(a**2)
573
574    # test nested sum evaluation
575    s = Sum( Sum( Sum(2,(z,1,n+1)), (y,x+1,n)), (x,1,n))
576    assert 0 == (s.doit() - n*(n+1)*(n-1)).factor()
577
578    # Integer assumes finite
579    assert Sum(KroneckerDelta(x, y), (x, -oo, oo)).doit() == Piecewise((1, And(-oo <= y, y < oo)), (0, True))
580    assert Sum(KroneckerDelta(m, n), (m, -oo, oo)).doit() == 1
581    assert Sum(m*KroneckerDelta(x, y), (x, -oo, oo)).doit() == Piecewise((m, And(-oo <= y, y < oo)), (0, True))
582    assert Sum(x*KroneckerDelta(m, n), (m, -oo, oo)).doit() == x
583    assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3
584    assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \
585           3 * Piecewise((1, And(1 <= k, k <= 3)), (0, True))
586    assert Sum(f(n) * Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \
587           f(1) + f(2) + f(3)
588    assert Sum(f(n) * Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \
589           Sum(f(n), (n, 1, oo))
590
591    # issue 2597
592    nmax = symbols('N', integer=True, positive=True)
593    pw = Piecewise((1, And(1 <= n, n <= nmax)), (0, True))
594    assert Sum(pw, (n, 1, nmax)).doit() == Sum(Piecewise((1, nmax >= n),
595                    (0, True)), (n, 1, nmax))
596
597    q, s = symbols('q, s')
598    assert summation(1/n**(2*s), (n, 1, oo)) == Piecewise((zeta(2*s), 2*s > 1),
599        (Sum(n**(-2*s), (n, 1, oo)), True))
600    assert summation(1/(n+1)**s, (n, 0, oo)) == Piecewise((zeta(s), s > 1),
601        (Sum((n + 1)**(-s), (n, 0, oo)), True))
602    assert summation(1/(n+q)**s, (n, 0, oo)) == Piecewise(
603        (zeta(s, q), And(q > 0, s > 1)),
604        (Sum((n + q)**(-s), (n, 0, oo)), True))
605    assert summation(1/(n+q)**s, (n, q, oo)) == Piecewise(
606        (zeta(s, 2*q), And(2*q > 0, s > 1)),
607        (Sum((n + q)**(-s), (n, q, oo)), True))
608    assert summation(1/n**2, (n, 1, oo)) == zeta(2)
609    assert summation(1/n**s, (n, 0, oo)) == Sum(n**(-s), (n, 0, oo))
610
611
612def test_Product_doit():
613    assert Product(n*Integral(a**2), (n, 1, 3)).doit() == 2 * a**9 / 9
614    assert Product(n*Integral(a**2), (n, 1, 3)).doit(deep=False) == \
615        6*Integral(a**2)**3
616    assert product(n*Integral(a**2), (n, 1, 3)) == 6*Integral(a**2)**3
617
618
619def test_Sum_interface():
620    assert isinstance(Sum(0, (n, 0, 2)), Sum)
621    assert Sum(nan, (n, 0, 2)) is nan
622    assert Sum(nan, (n, 0, oo)) is nan
623    assert Sum(0, (n, 0, 2)).doit() == 0
624    assert isinstance(Sum(0, (n, 0, oo)), Sum)
625    assert Sum(0, (n, 0, oo)).doit() == 0
626    raises(ValueError, lambda: Sum(1))
627    raises(ValueError, lambda: summation(1))
628
629
630def test_diff():
631    assert Sum(x, (x, 1, 2)).diff(x) == 0
632    assert Sum(x*y, (x, 1, 2)).diff(x) == 0
633    assert Sum(x*y, (y, 1, 2)).diff(x) == Sum(y, (y, 1, 2))
634    e = Sum(x*y, (x, 1, a))
635    assert e.diff(a) == Derivative(e, a)
636    assert Sum(x*y, (x, 1, 3), (a, 2, 5)).diff(y).doit() == \
637        Sum(x*y, (x, 1, 3), (a, 2, 5)).doit().diff(y) == 24
638    assert Sum(x, (x, 1, 2)).diff(y) == 0
639
640
641def test_hypersum():
642    from sympy import sin
643    assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x)
644    assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x)
645    assert simplify(summation((-1)**n*x**(2*n + 1) /
646        factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120
647
648    assert summation(1/(n + 2)**3, (n, 1, oo)) == Rational(-9, 8) + zeta(3)
649    assert summation(1/n**4, (n, 1, oo)) == pi**4/90
650
651    s = summation(x**n*n, (n, -oo, 0))
652    assert s.is_Piecewise
653    assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2)
654    assert s.args[0].args[1] == (abs(1/x) < 1)
655
656    m = Symbol('n', integer=True, positive=True)
657    assert summation(binomial(m, k), (k, 0, m)) == 2**m
658
659
660def test_issue_4170():
661    assert summation(1/factorial(k), (k, 0, oo)) == E
662
663
664def test_is_commutative():
665    from sympy.physics.secondquant import NO, F, Fd
666    m = Symbol('m', commutative=False)
667    for f in (Sum, Product, Integral):
668        assert f(z, (z, 1, 1)).is_commutative is True
669        assert f(z*y, (z, 1, 6)).is_commutative is True
670        assert f(m*x, (x, 1, 2)).is_commutative is False
671
672        assert f(NO(Fd(x)*F(y))*z, (z, 1, 2)).is_commutative is False
673
674
675def test_is_zero():
676    for func in [Sum, Product]:
677        assert func(0, (x, 1, 1)).is_zero is True
678        assert func(x, (x, 1, 1)).is_zero is None
679
680    assert Sum(0, (x, 1, 0)).is_zero is True
681    assert Product(0, (x, 1, 0)).is_zero is False
682
683
684def test_is_number():
685    # is number should not rely on evaluation or assumptions,
686    # it should be equivalent to `not foo.free_symbols`
687    assert Sum(1, (x, 1, 1)).is_number is True
688    assert Sum(1, (x, 1, x)).is_number is False
689    assert Sum(0, (x, y, z)).is_number is False
690    assert Sum(x, (y, 1, 2)).is_number is False
691    assert Sum(x, (y, 1, 1)).is_number is False
692    assert Sum(x, (x, 1, 2)).is_number is True
693    assert Sum(x*y, (x, 1, 2), (y, 1, 3)).is_number is True
694
695    assert Product(2, (x, 1, 1)).is_number is True
696    assert Product(2, (x, 1, y)).is_number is False
697    assert Product(0, (x, y, z)).is_number is False
698    assert Product(1, (x, y, z)).is_number is False
699    assert Product(x, (y, 1, x)).is_number is False
700    assert Product(x, (y, 1, 2)).is_number is False
701    assert Product(x, (y, 1, 1)).is_number is False
702    assert Product(x, (x, 1, 2)).is_number is True
703
704
705def test_free_symbols():
706    for func in [Sum, Product]:
707        assert func(1, (x, 1, 2)).free_symbols == set()
708        assert func(0, (x, 1, y)).free_symbols == {y}
709        assert func(2, (x, 1, y)).free_symbols == {y}
710        assert func(x, (x, 1, 2)).free_symbols == set()
711        assert func(x, (x, 1, y)).free_symbols == {y}
712        assert func(x, (y, 1, y)).free_symbols == {x, y}
713        assert func(x, (y, 1, 2)).free_symbols == {x}
714        assert func(x, (y, 1, 1)).free_symbols == {x}
715        assert func(x, (y, 1, z)).free_symbols == {x, z}
716        assert func(x, (x, 1, y), (y, 1, 2)).free_symbols == set()
717        assert func(x, (x, 1, y), (y, 1, z)).free_symbols == {z}
718        assert func(x, (x, 1, y), (y, 1, y)).free_symbols == {y}
719        assert func(x, (y, 1, y), (y, 1, z)).free_symbols == {x, z}
720    assert Sum(1, (x, 1, y)).free_symbols == {y}
721    # free_symbols answers whether the object *as written* has free symbols,
722    # not whether the evaluated expression has free symbols
723    assert Product(1, (x, 1, y)).free_symbols == {y}
724
725
726def test_conjugate_transpose():
727    A, B = symbols("A B", commutative=False)
728    p = Sum(A*B**n, (n, 1, 3))
729    assert p.adjoint().doit() == p.doit().adjoint()
730    assert p.conjugate().doit() == p.doit().conjugate()
731    assert p.transpose().doit() == p.doit().transpose()
732
733    p = Sum(B**n*A, (n, 1, 3))
734    assert p.adjoint().doit() == p.doit().adjoint()
735    assert p.conjugate().doit() == p.doit().conjugate()
736    assert p.transpose().doit() == p.doit().transpose()
737
738
739def test_noncommutativity_honoured():
740    A, B = symbols("A B", commutative=False)
741    M = symbols('M', integer=True, positive=True)
742    p = Sum(A*B**n, (n, 1, M))
743    assert p.doit() == A*Piecewise((M, Eq(B, 1)),
744                                   ((B - B**(M + 1))*(1 - B)**(-1), True))
745
746    p = Sum(B**n*A, (n, 1, M))
747    assert p.doit() == Piecewise((M, Eq(B, 1)),
748                                 ((B - B**(M + 1))*(1 - B)**(-1), True))*A
749
750    p = Sum(B**n*A*B**n, (n, 1, M))
751    assert p.doit() == p
752
753
754def test_issue_4171():
755    assert summation(factorial(2*k + 1)/factorial(2*k), (k, 0, oo)) is oo
756    assert summation(2*k + 1, (k, 0, oo)) is oo
757
758
759def test_issue_6273():
760    assert Sum(x, (x, 1, n)).n(2, subs={n: 1}) == 1
761
762
763def test_issue_6274():
764    assert Sum(x, (x, 1, 0)).doit() == 0
765    assert NS(Sum(x, (x, 1, 0))) == '0'
766    assert Sum(n, (n, 10, 5)).doit() == -30
767    assert NS(Sum(n, (n, 10, 5))) == '-30.0000000000000'
768
769
770def test_simplify_sum():
771    y, t, v = symbols('y, t, v')
772
773    _simplify = lambda e: simplify(e, doit=False)
774    assert _simplify(Sum(x*y, (x, n, m), (y, a, k)) + \
775        Sum(y, (x, n, m), (y, a, k))) == Sum(y * (x + 1), (x, n, m), (y, a, k))
776    assert _simplify(Sum(x, (x, n, m)) + Sum(x, (x, m + 1, a))) == \
777        Sum(x, (x, n, a))
778    assert _simplify(Sum(x, (x, k + 1, a)) + Sum(x, (x, n, k))) == \
779        Sum(x, (x, n, a))
780    assert _simplify(Sum(x, (x, k + 1, a)) + Sum(x + 1, (x, n, k))) == \
781        Sum(x, (x, n, a)) + Sum(1, (x, n, k))
782    assert _simplify(Sum(x, (x, 0, 3)) * 3 + 3 * Sum(x, (x, 4, 6)) + \
783        4 * Sum(z, (z, 0, 1))) == 4*Sum(z, (z, 0, 1)) + 3*Sum(x, (x, 0, 6))
784    assert _simplify(3*Sum(x**2, (x, a, b)) + Sum(x, (x, a, b))) == \
785        Sum(x*(3*x + 1), (x, a, b))
786    assert _simplify(Sum(x**3, (x, n, k)) * 3 + 3 * Sum(x, (x, n, k)) + \
787        4 * y * Sum(z, (z, n, k))) + 1 == \
788            4*y*Sum(z, (z, n, k)) + 3*Sum(x**3 + x, (x, n, k)) + 1
789    assert _simplify(Sum(x, (x, a, b)) + 1 + Sum(x, (x, b + 1, c))) == \
790        1 + Sum(x, (x, a, c))
791    assert _simplify(Sum(x, (t, a, b)) + Sum(y, (t, a, b)) + \
792        Sum(x, (t, b+1, c))) == x * Sum(1, (t, a, c)) + y * Sum(1, (t, a, b))
793    assert _simplify(Sum(x, (t, a, b)) + Sum(x, (t, b+1, c)) + \
794        Sum(y, (t, a, b))) == x * Sum(1, (t, a, c)) + y * Sum(1, (t, a, b))
795    assert _simplify(Sum(x, (t, a, b)) + 2 * Sum(x, (t, b+1, c))) == \
796        _simplify(Sum(x, (t, a, b)) + Sum(x, (t, b+1, c)) + Sum(x, (t, b+1, c)))
797    assert _simplify(Sum(x, (x, a, b))*Sum(x**2, (x, a, b))) == \
798        Sum(x, (x, a, b)) * Sum(x**2, (x, a, b))
799    assert _simplify(Sum(x, (t, a, b)) + Sum(y, (t, a, b)) + Sum(z, (t, a, b))) \
800        == (x + y + z) * Sum(1, (t, a, b))          # issue 8596
801    assert _simplify(Sum(x, (t, a, b)) + Sum(y, (t, a, b)) + Sum(z, (t, a, b)) + \
802        Sum(v, (t, a, b))) == (x + y + z + v) * Sum(1, (t, a, b))  # issue 8596
803    assert _simplify(Sum(x * y, (x, a, b)) / (3 * y)) == \
804        (Sum(x, (x, a, b)) / 3)
805    assert _simplify(Sum(Function('f')(x) * y * z, (x, a, b)) / (y * z)) \
806        == Sum(Function('f')(x), (x, a, b))
807    assert _simplify(Sum(c * x, (x, a, b)) - c * Sum(x, (x, a, b))) == 0
808    assert _simplify(c * (Sum(x, (x, a, b))  + y)) == c * (y + Sum(x, (x, a, b)))
809    assert _simplify(c * (Sum(x, (x, a, b)) + y * Sum(x, (x, a, b)))) == \
810        c * (y + 1) * Sum(x, (x, a, b))
811    assert _simplify(Sum(Sum(c * x, (x, a, b)), (y, a, b))) == \
812                c * Sum(x, (x, a, b), (y, a, b))
813    assert _simplify(Sum((3 + y) * Sum(c * x, (x, a, b)), (y, a, b))) == \
814                c * Sum((3 + y), (y, a, b)) * Sum(x, (x, a, b))
815    assert _simplify(Sum((3 + t) * Sum(c * t, (x, a, b)), (y, a, b))) == \
816                c*t*(t + 3)*Sum(1, (x, a, b))*Sum(1, (y, a, b))
817    assert _simplify(Sum(Sum(d * t, (x, a, b - 1)) + \
818                Sum(d * t, (x, b, c)), (t, a, b))) == \
819                    d * Sum(1, (x, a, c)) * Sum(t, (t, a, b))
820
821
822def test_change_index():
823    b, v, w = symbols('b, v, w', integer = True)
824
825    assert Sum(x, (x, a, b)).change_index(x, x + 1, y) == \
826        Sum(y - 1, (y, a + 1, b + 1))
827    assert Sum(x**2, (x, a, b)).change_index( x, x - 1) == \
828        Sum((x+1)**2, (x, a - 1, b - 1))
829    assert Sum(x**2, (x, a, b)).change_index( x, -x, y) == \
830        Sum((-y)**2, (y, -b, -a))
831    assert Sum(x, (x, a, b)).change_index( x, -x - 1) == \
832        Sum(-x - 1, (x, -b - 1, -a - 1))
833    assert Sum(x*y, (x, a, b), (y, c, d)).change_index( x, x - 1, z) == \
834        Sum((z + 1)*y, (z, a - 1, b - 1), (y, c, d))
835    assert Sum(x, (x, a, b)).change_index( x, x + v) == \
836        Sum(-v + x, (x, a + v, b + v))
837    assert Sum(x, (x, a, b)).change_index( x, -x - v) == \
838        Sum(-v - x, (x, -b - v, -a - v))
839    assert Sum(x, (x, a, b)).change_index(x, w*x, v) == \
840        Sum(v/w, (v, b*w, a*w))
841    raises(ValueError, lambda: Sum(x, (x, a, b)).change_index(x, 2*x))
842
843
844def test_reorder():
845    b, y, c, d, z = symbols('b, y, c, d, z', integer = True)
846
847    assert Sum(x*y, (x, a, b), (y, c, d)).reorder((0, 1)) == \
848        Sum(x*y, (y, c, d), (x, a, b))
849    assert Sum(x, (x, a, b), (x, c, d)).reorder((0, 1)) == \
850        Sum(x, (x, c, d), (x, a, b))
851    assert Sum(x*y + z, (x, a, b), (z, m, n), (y, c, d)).reorder(\
852        (2, 0), (0, 1)) == Sum(x*y + z, (z, m, n), (y, c, d), (x, a, b))
853    assert Sum(x*y*z, (x, a, b), (y, c, d), (z, m, n)).reorder(\
854        (0, 1), (1, 2), (0, 2)) == Sum(x*y*z, (x, a, b), (z, m, n), (y, c, d))
855    assert Sum(x*y*z, (x, a, b), (y, c, d), (z, m, n)).reorder(\
856        (x, y), (y, z), (x, z)) == Sum(x*y*z, (x, a, b), (z, m, n), (y, c, d))
857    assert Sum(x*y, (x, a, b), (y, c, d)).reorder((x, 1)) == \
858        Sum(x*y, (y, c, d), (x, a, b))
859    assert Sum(x*y, (x, a, b), (y, c, d)).reorder((y, x)) == \
860        Sum(x*y, (y, c, d), (x, a, b))
861
862
863def test_reverse_order():
864    assert Sum(x, (x, 0, 3)).reverse_order(0) == Sum(-x, (x, 4, -1))
865    assert Sum(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(0, 1) == \
866           Sum(x*y, (x, 6, 0), (y, 7, -1))
867    assert Sum(x, (x, 1, 2)).reverse_order(0) == Sum(-x, (x, 3, 0))
868    assert Sum(x, (x, 1, 3)).reverse_order(0) == Sum(-x, (x, 4, 0))
869    assert Sum(x, (x, 1, a)).reverse_order(0) == Sum(-x, (x, a + 1, 0))
870    assert Sum(x, (x, a, 5)).reverse_order(0) == Sum(-x, (x, 6, a - 1))
871    assert Sum(x, (x, a + 1, a + 5)).reverse_order(0) == \
872                         Sum(-x, (x, a + 6, a))
873    assert Sum(x, (x, a + 1, a + 2)).reverse_order(0) == \
874           Sum(-x, (x, a + 3, a))
875    assert Sum(x, (x, a + 1, a + 1)).reverse_order(0) == \
876           Sum(-x, (x, a + 2, a))
877    assert Sum(x, (x, a, b)).reverse_order(0) == Sum(-x, (x, b + 1, a - 1))
878    assert Sum(x, (x, a, b)).reverse_order(x) == Sum(-x, (x, b + 1, a - 1))
879    assert Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1) == \
880        Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
881    assert Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x) == \
882        Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
883
884
885def test_issue_7097():
886    assert sum(x**n/n for n in range(1, 401)) == summation(x**n/n, (n, 1, 400))
887
888
889def test_factor_expand_subs():
890    # test factoring
891    assert Sum(4 * x, (x, 1, y)).factor() == 4 * Sum(x, (x, 1, y))
892    assert Sum(x * a, (x, 1, y)).factor() == a * Sum(x, (x, 1, y))
893    assert Sum(4 * x * a, (x, 1, y)).factor() == 4 * a * Sum(x, (x, 1, y))
894    assert Sum(4 * x * y, (x, 1, y)).factor() == 4 * y * Sum(x, (x, 1, y))
895
896    # test expand
897    assert Sum(x+1,(x,1,y)).expand() == Sum(x,(x,1,y)) + Sum(1,(x,1,y))
898    assert Sum(x+a*x**2,(x,1,y)).expand() == Sum(x,(x,1,y)) + Sum(a*x**2,(x,1,y))
899    assert Sum(x**(n + 1)*(n + 1), (n, -1, oo)).expand() \
900        == Sum(x*x**n, (n, -1, oo)) + Sum(n*x*x**n, (n, -1, oo))
901    assert Sum(x**(n + 1)*(n + 1), (n, -1, oo)).expand(power_exp=False) \
902        == Sum(n*x**(n+1), (n, -1, oo)) + Sum(x**(n+1), (n, -1, oo))
903    assert Sum(a*n+a*n**2,(n,0,4)).expand() \
904        == Sum(a*n,(n,0,4)) + Sum(a*n**2,(n,0,4))
905    assert Sum(x**a*x**n,(x,0,3)) \
906        == Sum(x**(a+n),(x,0,3)).expand(power_exp=True)
907    assert Sum(x**(a+n),(x,0,3)) \
908        == Sum(x**(a+n),(x,0,3)).expand(power_exp=False)
909
910    # test subs
911    assert Sum(1/(1+a*x**2),(x,0,3)).subs([(a,3)]) == Sum(1/(1+3*x**2),(x,0,3))
912    assert Sum(x*y,(x,0,y),(y,0,x)).subs([(x,3)]) == Sum(x*y,(x,0,y),(y,0,3))
913    assert Sum(x,(x,1,10)).subs([(x,y-2)]) == Sum(x,(x,1,10))
914    assert Sum(1/x,(x,1,10)).subs([(x,(3+n)**3)]) == Sum(1/x,(x,1,10))
915    assert Sum(1/x,(x,1,10)).subs([(x,3*x-2)]) == Sum(1/x,(x,1,10))
916
917
918def test_distribution_over_equality():
919    f = Function('f')
920    assert Product(Eq(x*2, f(x)), (x, 1, 3)).doit() == Eq(48, f(1)*f(2)*f(3))
921    assert Sum(Eq(f(x), x**2), (x, 0, y)) == \
922        Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
923
924
925def test_issue_2787():
926    n, k = symbols('n k', positive=True, integer=True)
927    p = symbols('p', positive=True)
928    binomial_dist = binomial(n, k)*p**k*(1 - p)**(n - k)
929    s = Sum(binomial_dist*k, (k, 0, n))
930    res = s.doit().simplify()
931    assert res == Piecewise(
932        (n*p, p/Abs(p - 1) <= 1),
933        ((-p + 1)**n*Sum(k*p**k*(-p + 1)**(-k)*binomial(n, k), (k, 0, n)),
934        True))
935    # Issue #17165: make sure that another simplify does not change/increase
936    # the result
937    assert res == res.simplify()
938
939
940def test_issue_4668():
941    assert summation(1/n, (n, 2, oo)) is oo
942
943
944def test_matrix_sum():
945    A = Matrix([[0, 1], [n, 0]])
946
947    result = Sum(A, (n, 0, 3)).doit()
948    assert result == Matrix([[0, 4], [6, 0]])
949    assert result.__class__ == ImmutableDenseMatrix
950
951    A = SparseMatrix([[0, 1], [n, 0]])
952
953    result = Sum(A, (n, 0, 3)).doit()
954    assert result.__class__ == ImmutableSparseMatrix
955
956
957def test_failing_matrix_sum():
958    n = Symbol('n')
959    # TODO Implement matrix geometric series summation.
960    A = Matrix([[0, 1, 0], [-1, 0, 0], [0, 0, 0]])
961    assert Sum(A ** n, (n, 1, 4)).doit() == \
962        Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
963    # issue sympy/sympy#16989
964    assert summation(A**n, (n, 1, 1)) == A
965
966
967def test_indexed_idx_sum():
968    i = symbols('i', cls=Idx)
969    r = Indexed('r', i)
970    assert Sum(r, (i, 0, 3)).doit() == sum([r.xreplace({i: j}) for j in range(4)])
971    assert Product(r, (i, 0, 3)).doit() == prod([r.xreplace({i: j}) for j in range(4)])
972
973    j = symbols('j', integer=True)
974    assert Sum(r, (i, j, j+2)).doit() == sum([r.xreplace({i: j+k}) for k in range(3)])
975    assert Product(r, (i, j, j+2)).doit() == prod([r.xreplace({i: j+k}) for k in range(3)])
976
977    k = Idx('k', range=(1, 3))
978    A = IndexedBase('A')
979    assert Sum(A[k], k).doit() == sum([A[Idx(j, (1, 3))] for j in range(1, 4)])
980    assert Product(A[k], k).doit() == prod([A[Idx(j, (1, 3))] for j in range(1, 4)])
981
982    raises(ValueError, lambda: Sum(A[k], (k, 1, 4)))
983    raises(ValueError, lambda: Sum(A[k], (k, 0, 3)))
984    raises(ValueError, lambda: Sum(A[k], (k, 2, oo)))
985
986    raises(ValueError, lambda: Product(A[k], (k, 1, 4)))
987    raises(ValueError, lambda: Product(A[k], (k, 0, 3)))
988    raises(ValueError, lambda: Product(A[k], (k, 2, oo)))
989
990
991@slow
992def test_is_convergent():
993    # divergence tests --
994    assert Sum(n/(2*n + 1), (n, 1, oo)).is_convergent() is S.false
995    assert Sum(factorial(n)/5**n, (n, 1, oo)).is_convergent() is S.false
996    assert Sum(3**(-2*n - 1)*n**n, (n, 1, oo)).is_convergent() is S.false
997    assert Sum((-1)**n*n, (n, 3, oo)).is_convergent() is S.false
998    assert Sum((-1)**n, (n, 1, oo)).is_convergent() is S.false
999    assert Sum(log(1/n), (n, 2, oo)).is_convergent() is S.false
1000
1001    # Raabe's test --
1002    assert Sum(Product((3*m),(m,1,n))/Product((3*m+4),(m,1,n)),(n,1,oo)).is_convergent() is S.true
1003
1004    # root test --
1005    assert Sum((-12)**n/n, (n, 1, oo)).is_convergent() is S.false
1006
1007    # integral test --
1008
1009    # p-series test --
1010    assert Sum(1/(n**2 + 1), (n, 1, oo)).is_convergent() is S.true
1011    assert Sum(1/n**Rational(6, 5), (n, 1, oo)).is_convergent() is S.true
1012    assert Sum(2/(n*sqrt(n - 1)), (n, 2, oo)).is_convergent() is S.true
1013    assert Sum(1/(sqrt(n)*sqrt(n)), (n, 2, oo)).is_convergent() is S.false
1014    assert Sum(factorial(n) / factorial(n+2), (n, 1, oo)).is_convergent() is S.true
1015    assert Sum(rf(5,n)/rf(7,n),(n,1,oo)).is_convergent() is S.true
1016    assert Sum((rf(1, n)*rf(2, n))/(rf(3, n)*factorial(n)),(n,1,oo)).is_convergent() is S.false
1017
1018    # comparison test --
1019    assert Sum(1/(n + log(n)), (n, 1, oo)).is_convergent() is S.false
1020    assert Sum(1/(n**2*log(n)), (n, 2, oo)).is_convergent() is S.true
1021    assert Sum(1/(n*log(n)), (n, 2, oo)).is_convergent() is S.false
1022    assert Sum(2/(n*log(n)*log(log(n))**2), (n, 5, oo)).is_convergent() is S.true
1023    assert Sum(2/(n*log(n)**2), (n, 2, oo)).is_convergent() is S.true
1024    assert Sum((n - 1)/(n**2*log(n)**3), (n, 2, oo)).is_convergent() is S.true
1025    assert Sum(1/(n*log(n)*log(log(n))), (n, 5, oo)).is_convergent() is S.false
1026    assert Sum((n - 1)/(n*log(n)**3), (n, 3, oo)).is_convergent() is S.false
1027    assert Sum(2/(n**2*log(n)), (n, 2, oo)).is_convergent() is S.true
1028    assert Sum(1/(n*sqrt(log(n))*log(log(n))), (n, 100, oo)).is_convergent() is S.false
1029    assert Sum(log(log(n))/(n*log(n)**2), (n, 100, oo)).is_convergent() is S.true
1030    assert Sum(log(n)/n**2, (n, 5, oo)).is_convergent() is S.true
1031
1032    # alternating series tests --
1033    assert Sum((-1)**(n - 1)/(n**2 - 1), (n, 3, oo)).is_convergent() is S.true
1034
1035    # with -negativeInfinite Limits
1036    assert Sum(1/(n**2 + 1), (n, -oo, 1)).is_convergent() is S.true
1037    assert Sum(1/(n - 1), (n, -oo, -1)).is_convergent() is S.false
1038    assert Sum(1/(n**2 - 1), (n, -oo, -5)).is_convergent() is S.true
1039    assert Sum(1/(n**2 - 1), (n, -oo, 2)).is_convergent() is S.true
1040    assert Sum(1/(n**2 - 1), (n, -oo, oo)).is_convergent() is S.true
1041
1042    # piecewise functions
1043    f = Piecewise((n**(-2), n <= 1), (n**2, n > 1))
1044    assert Sum(f, (n, 1, oo)).is_convergent() is S.false
1045    assert Sum(f, (n, -oo, oo)).is_convergent() is S.false
1046    assert Sum(f, (n, 1, 100)).is_convergent() is S.true
1047    #assert Sum(f, (n, -oo, 1)).is_convergent() is S.true
1048
1049    # integral test
1050
1051    assert Sum(log(n)/n**3, (n, 1, oo)).is_convergent() is S.true
1052    assert Sum(-log(n)/n**3, (n, 1, oo)).is_convergent() is S.true
1053    # the following function has maxima located at (x, y) =
1054    # (1.2, 0.43), (3.0, -0.25) and (6.8, 0.050)
1055    eq = (x - 2)*(x**2 - 6*x + 4)*exp(-x)
1056    assert Sum(eq, (x, 1, oo)).is_convergent() is S.true
1057    assert Sum(eq, (x, 1, 2)).is_convergent() is S.true
1058    assert Sum(1/(x**3), (x, 1, oo)).is_convergent() is S.true
1059    assert Sum(1/(x**S.Half), (x, 1, oo)).is_convergent() is S.false
1060
1061    # issue 19545
1062    assert Sum(1/n - 3/(3*n +2), (n, 1, oo)).is_convergent() is S.true
1063
1064    # issue 19836
1065    assert Sum(4/(n + 2) - 5/(n + 1) + 1/n,(n, 7, oo)).is_convergent() is S.true
1066
1067
1068def test_is_absolutely_convergent():
1069    assert Sum((-1)**n, (n, 1, oo)).is_absolutely_convergent() is S.false
1070    assert Sum((-1)**n/n**2, (n, 1, oo)).is_absolutely_convergent() is S.true
1071
1072
1073@XFAIL
1074def test_convergent_failing():
1075    # dirichlet tests
1076    assert Sum(sin(n)/n, (n, 1, oo)).is_convergent() is S.true
1077    assert Sum(sin(2*n)/n, (n, 1, oo)).is_convergent() is S.true
1078
1079
1080def test_issue_6966():
1081    i, k, m = symbols('i k m', integer=True)
1082    z_i, q_i = symbols('z_i q_i')
1083    a_k = Sum(-q_i*z_i/k,(i,1,m))
1084    b_k = a_k.diff(z_i)
1085    assert isinstance(b_k, Sum)
1086    assert b_k == Sum(-q_i/k,(i,1,m))
1087
1088
1089def test_issue_10156():
1090    cx = Sum(2*y**2*x, (x, 1,3))
1091    e = 2*y*Sum(2*cx*x**2, (x, 1, 9))
1092    assert e.factor() == \
1093        8*y**3*Sum(x, (x, 1, 3))*Sum(x**2, (x, 1, 9))
1094
1095
1096def test_issue_10973():
1097    assert Sum((-n + (n**3 + 1)**(S(1)/3))/log(n), (n, 1, oo)).is_convergent() is S.true
1098
1099
1100def test_issue_14129():
1101    assert Sum( k*x**k, (k, 0, n-1)).doit() == \
1102        Piecewise((n**2/2 - n/2, Eq(x, 1)), ((n*x*x**n -
1103            n*x**n - x*x**n + x)/(x - 1)**2, True))
1104    assert Sum( x**k, (k, 0, n-1)).doit() == \
1105        Piecewise((n, Eq(x, 1)), ((-x**n + 1)/(-x + 1), True))
1106    assert Sum( k*(x/y+x)**k, (k, 0, n-1)).doit() == \
1107        Piecewise((n*(n - 1)/2, Eq(x, y/(y + 1))),
1108        (x*(y + 1)*(n*x*y*(x + x/y)**n/(x + x/y)
1109        + n*x*(x + x/y)**n/(x + x/y) - n*y*(x
1110        + x/y)**n/(x + x/y) - x*y*(x + x/y)**n/(x
1111        + x/y) - x*(x + x/y)**n/(x + x/y) + y)/(x*y
1112        + x - y)**2, True))
1113
1114
1115def test_issue_14112():
1116    assert Sum((-1)**n/sqrt(n), (n, 1, oo)).is_absolutely_convergent() is S.false
1117    assert Sum((-1)**(2*n)/n, (n, 1, oo)).is_convergent() is S.false
1118    assert Sum((-2)**n + (-3)**n, (n, 1, oo)).is_convergent() is S.false
1119
1120
1121def test_sin_times_absolutely_convergent():
1122    assert Sum(sin(n) / n**3, (n, 1, oo)).is_convergent() is S.true
1123    assert Sum(sin(n) * log(n) / n**3, (n, 1, oo)).is_convergent() is S.true
1124
1125
1126def test_issue_14111():
1127    assert Sum(1/log(log(n)), (n, 22, oo)).is_convergent() is S.false
1128
1129
1130def test_issue_14484():
1131    assert Sum(sin(n)/log(log(n)), (n, 22, oo)).is_convergent() is S.false
1132
1133
1134def test_issue_14640():
1135    i, n = symbols("i n", integer=True)
1136    a, b, c = symbols("a b c")
1137
1138    assert Sum(a**-i/(a - b), (i, 0, n)).doit() == Sum(
1139        1/(a*a**i - a**i*b), (i, 0, n)).doit() == Piecewise(
1140            (n + 1, Eq(1/a, 1)),
1141            ((-a**(-n - 1) + 1)/(1 - 1/a), True))/(a - b)
1142
1143    assert Sum((b*a**i - c*a**i)**-2, (i, 0, n)).doit() == Piecewise(
1144        (n + 1, Eq(a**(-2), 1)),
1145        ((-a**(-2*n - 2) + 1)/(1 - 1/a**2), True))/(b - c)**2
1146
1147    s = Sum(i*(a**(n - i) - b**(n - i))/(a - b), (i, 0, n)).doit()
1148    assert not s.has(Sum)
1149    assert s.subs({a: 2, b: 3, n: 5}) == 122
1150
1151
1152def test_issue_15943():
1153    s = Sum(binomial(n, k)*factorial(n - k), (k, 0, n)).doit().rewrite(gamma)
1154    assert s == -E*(n + 1)*gamma(n + 1)*lowergamma(n + 1, 1)/gamma(n + 2
1155        ) + E*gamma(n + 1)
1156    assert s.simplify() == E*(factorial(n) - lowergamma(n + 1, 1))
1157
1158
1159def test_Sum_dummy_eq():
1160    assert not Sum(x, (x, a, b)).dummy_eq(1)
1161    assert not Sum(x, (x, a, b)).dummy_eq(Sum(x, (x, a, b), (a, 1, 2)))
1162    assert not Sum(x, (x, a, b)).dummy_eq(Sum(x, (x, a, c)))
1163    assert Sum(x, (x, a, b)).dummy_eq(Sum(x, (x, a, b)))
1164    d = Dummy()
1165    assert Sum(x, (x, a, d)).dummy_eq(Sum(x, (x, a, c)), c)
1166    assert not Sum(x, (x, a, d)).dummy_eq(Sum(x, (x, a, c)))
1167    assert Sum(x, (x, a, c)).dummy_eq(Sum(y, (y, a, c)))
1168    assert Sum(x, (x, a, d)).dummy_eq(Sum(y, (y, a, c)), c)
1169    assert not Sum(x, (x, a, d)).dummy_eq(Sum(y, (y, a, c)))
1170
1171
1172def test_issue_15852():
1173    assert summation(x**y*y, (y, -oo, oo)).doit() == Sum(x**y*y, (y, -oo, oo))
1174
1175
1176def test_exceptions():
1177    S = Sum(x, (x, a, b))
1178    raises(ValueError, lambda: S.change_index(x, x**2, y))
1179    S = Sum(x, (x, a, b), (x, 1, 4))
1180    raises(ValueError, lambda: S.index(x))
1181    S = Sum(x, (x, a, b), (y, 1, 4))
1182    raises(ValueError, lambda: S.reorder([x]))
1183    S = Sum(x, (x, y, b), (y, 1, 4))
1184    raises(ReorderError, lambda: S.reorder_limit(0, 1))
1185    S = Sum(x*y, (x, a, b), (y, 1, 4))
1186    raises(NotImplementedError, lambda: S.is_convergent())
1187
1188
1189def test_sumproducts_assumptions():
1190    M = Symbol('M', integer=True, positive=True)
1191
1192    m = Symbol('m', integer=True)
1193    for func in [Sum, Product]:
1194        assert func(m, (m, -M, M)).is_positive is None
1195        assert func(m, (m, -M, M)).is_nonpositive is None
1196        assert func(m, (m, -M, M)).is_negative is None
1197        assert func(m, (m, -M, M)).is_nonnegative is None
1198        assert func(m, (m, -M, M)).is_finite is True
1199
1200    m = Symbol('m', integer=True, nonnegative=True)
1201    for func in [Sum, Product]:
1202        assert func(m, (m, 0, M)).is_positive is None
1203        assert func(m, (m, 0, M)).is_nonpositive is None
1204        assert func(m, (m, 0, M)).is_negative is False
1205        assert func(m, (m, 0, M)).is_nonnegative is True
1206        assert func(m, (m, 0, M)).is_finite is True
1207
1208    m = Symbol('m', integer=True, positive=True)
1209    for func in [Sum, Product]:
1210        assert func(m, (m, 1, M)).is_positive is True
1211        assert func(m, (m, 1, M)).is_nonpositive is False
1212        assert func(m, (m, 1, M)).is_negative is False
1213        assert func(m, (m, 1, M)).is_nonnegative is True
1214        assert func(m, (m, 1, M)).is_finite is True
1215
1216    m = Symbol('m', integer=True, negative=True)
1217    assert Sum(m, (m, -M, -1)).is_positive is False
1218    assert Sum(m, (m, -M, -1)).is_nonpositive is True
1219    assert Sum(m, (m, -M, -1)).is_negative is True
1220    assert Sum(m, (m, -M, -1)).is_nonnegative is False
1221    assert Sum(m, (m, -M, -1)).is_finite is True
1222    assert Product(m, (m, -M, -1)).is_positive is None
1223    assert Product(m, (m, -M, -1)).is_nonpositive is None
1224    assert Product(m, (m, -M, -1)).is_negative is None
1225    assert Product(m, (m, -M, -1)).is_nonnegative is None
1226    assert Product(m, (m, -M, -1)).is_finite is True
1227
1228    m = Symbol('m', integer=True, nonpositive=True)
1229    assert Sum(m, (m, -M, 0)).is_positive is False
1230    assert Sum(m, (m, -M, 0)).is_nonpositive is True
1231    assert Sum(m, (m, -M, 0)).is_negative is None
1232    assert Sum(m, (m, -M, 0)).is_nonnegative is None
1233    assert Sum(m, (m, -M, 0)).is_finite is True
1234    assert Product(m, (m, -M, 0)).is_positive is None
1235    assert Product(m, (m, -M, 0)).is_nonpositive is None
1236    assert Product(m, (m, -M, 0)).is_negative is None
1237    assert Product(m, (m, -M, 0)).is_nonnegative is None
1238    assert Product(m, (m, -M, 0)).is_finite is True
1239
1240    m = Symbol('m', integer=True)
1241    assert Sum(2, (m, 0, oo)).is_positive is None
1242    assert Sum(2, (m, 0, oo)).is_nonpositive is None
1243    assert Sum(2, (m, 0, oo)).is_negative is None
1244    assert Sum(2, (m, 0, oo)).is_nonnegative is None
1245    assert Sum(2, (m, 0, oo)).is_finite is None
1246
1247    assert Product(2, (m, 0, oo)).is_positive is None
1248    assert Product(2, (m, 0, oo)).is_nonpositive is None
1249    assert Product(2, (m, 0, oo)).is_negative is False
1250    assert Product(2, (m, 0, oo)).is_nonnegative is None
1251    assert Product(2, (m, 0, oo)).is_finite is None
1252
1253    assert Product(0, (x, M, M-1)).is_positive is True
1254    assert Product(0, (x, M, M-1)).is_finite is True
1255
1256
1257def test_expand_with_assumptions():
1258    M = Symbol('M', integer=True, positive=True)
1259    x = Symbol('x', positive=True)
1260    m = Symbol('m', nonnegative=True)
1261    assert log(Product(x**m, (m, 0, M))).expand() == Sum(m*log(x), (m, 0, M))
1262    assert log(Product(exp(x**m), (m, 0, M))).expand() == Sum(x**m, (m, 0, M))
1263    assert log(Product(x**m, (m, 0, M))).rewrite(Sum).expand() == Sum(m*log(x), (m, 0, M))
1264    assert log(Product(exp(x**m), (m, 0, M))).rewrite(Sum).expand() == Sum(x**m, (m, 0, M))
1265
1266    n = Symbol('n', nonnegative=True)
1267    i, j = symbols('i,j', positive=True, integer=True)
1268    x, y = symbols('x,y', positive=True)
1269    assert log(Product(x**i*y**j, (i, 1, n), (j, 1, m))).expand() \
1270        == Sum(i*log(x) + j*log(y), (i, 1, n), (j, 1, m))
1271
1272
1273def test_has_finite_limits():
1274    x = Symbol('x')
1275    assert Sum(1, (x, 1, 9)).has_finite_limits is True
1276    assert Sum(1, (x, 1, oo)).has_finite_limits is False
1277    M = Symbol('M')
1278    assert Sum(1, (x, 1, M)).has_finite_limits is None
1279    M = Symbol('M', positive=True)
1280    assert Sum(1, (x, 1, M)).has_finite_limits is True
1281    x = Symbol('x', positive=True)
1282    M = Symbol('M')
1283    assert Sum(1, (x, 1, M)).has_finite_limits is True
1284
1285    assert Sum(1, (x, 1, M), (y, -oo, oo)).has_finite_limits is False
1286
1287def test_has_reversed_limits():
1288    assert Sum(1, (x, 1, 1)).has_reversed_limits is False
1289    assert Sum(1, (x, 1, 9)).has_reversed_limits is False
1290    assert Sum(1, (x, 1, -9)).has_reversed_limits is True
1291    assert Sum(1, (x, 1, 0)).has_reversed_limits is True
1292    assert Sum(1, (x, 1, oo)).has_reversed_limits is False
1293    M = Symbol('M')
1294    assert Sum(1, (x, 1, M)).has_reversed_limits is None
1295    M = Symbol('M', positive=True, integer=True)
1296    assert Sum(1, (x, 1, M)).has_reversed_limits is False
1297    assert Sum(1, (x, 1, M), (y, -oo, oo)).has_reversed_limits is False
1298    M = Symbol('M', negative=True)
1299    assert Sum(1, (x, 1, M)).has_reversed_limits is True
1300
1301    assert Sum(1, (x, 1, M), (y, -oo, oo)).has_reversed_limits is True
1302    assert Sum(1, (x, oo, oo)).has_reversed_limits is None
1303
1304
1305def test_has_empty_sequence():
1306    assert Sum(1, (x, 1, 1)).has_empty_sequence is False
1307    assert Sum(1, (x, 1, 9)).has_empty_sequence is False
1308    assert Sum(1, (x, 1, -9)).has_empty_sequence is False
1309    assert Sum(1, (x, 1, 0)).has_empty_sequence is True
1310    assert Sum(1, (x, y, y - 1)).has_empty_sequence is True
1311    assert Sum(1, (x, 3, 2), (y, -oo, oo)).has_empty_sequence is True
1312    assert Sum(1, (y, -oo, oo), (x, 3, 2)).has_empty_sequence is True
1313    assert Sum(1, (x, oo, oo)).has_empty_sequence is False
1314
1315
1316def test_empty_sequence():
1317    assert Product(x*y, (x, -oo, oo), (y, 1, 0)).doit() == 1
1318    assert Product(x*y, (y, 1, 0), (x, -oo, oo)).doit() == 1
1319    assert Sum(x, (x, -oo, oo), (y, 1, 0)).doit() == 0
1320    assert Sum(x, (y, 1, 0), (x, -oo, oo)).doit() == 0
1321
1322
1323def test_issue_8016():
1324    k = Symbol('k', integer=True)
1325    n, m = symbols('n, m', integer=True, positive=True)
1326    s = Sum(binomial(m, k)*binomial(m, n - k)*(-1)**k, (k, 0, n))
1327    assert s.doit().simplify() == \
1328        cos(pi*n/2)*gamma(m + 1)/gamma(n/2 + 1)/gamma(m - n/2 + 1)
1329
1330
1331def test_issue_14313():
1332    assert Sum(S.Half**floor(n/2), (n, 1, oo)).is_convergent()
1333
1334
1335def test_issue_14563():
1336    # The assertion was failing due to no assumptions methods in Sums and Product
1337    assert 1 % Sum(1, (x, 0, 1)) == 1
1338
1339
1340def test_issue_16735():
1341    assert Sum(5**n/gamma(n+1), (n, 1, oo)).is_convergent() is S.true
1342
1343
1344def test_issue_14871():
1345    assert Sum((Rational(1, 10))**n*rf(0, n)/factorial(n), (n, 0, oo)).rewrite(factorial).doit() == 1
1346
1347
1348def test_issue_17165():
1349    n = symbols("n", integer=True)
1350    x = symbols('x')
1351    s = (x*Sum(x**n, (n, -1, oo)))
1352    ssimp = s.doit().simplify()
1353
1354    assert ssimp == Piecewise((-1/(x - 1), Abs(x) < 1),
1355                              (x*Sum(x**n, (n, -1, oo)), True))
1356    assert ssimp == ssimp.simplify()
1357
1358
1359def test_issue_19379():
1360    assert Sum(factorial(n)/factorial(n + 2), (n, 1, oo)).is_convergent() is S.true
1361
1362
1363def test_issue_20777():
1364    assert Sum(exp(x*sin(n/m)), (n, 1, m)).doit() == Sum(exp(x*sin(n/m)), (n, 1, m))
1365
1366
1367def test__dummy_with_inherited_properties_concrete():
1368    x = Symbol('x')
1369
1370    from sympy import Tuple
1371    d = _dummy_with_inherited_properties_concrete(Tuple(x, 0, 5))
1372    assert d.is_real
1373    assert d.is_integer
1374    assert d.is_nonnegative
1375    assert d.is_extended_nonnegative
1376
1377    d = _dummy_with_inherited_properties_concrete(Tuple(x, 1, 9))
1378    assert d.is_real
1379    assert d.is_integer
1380    assert d.is_positive
1381    assert d.is_odd is None
1382
1383    d = _dummy_with_inherited_properties_concrete(Tuple(x, -5, 5))
1384    assert d.is_real
1385    assert d.is_integer
1386    assert d.is_positive is None
1387    assert d.is_extended_nonnegative is None
1388    assert d.is_odd is None
1389
1390    d = _dummy_with_inherited_properties_concrete(Tuple(x, -1.5, 1.5))
1391    assert d.is_real
1392    assert d.is_integer is None
1393    assert d.is_positive is None
1394    assert d.is_extended_nonnegative is None
1395
1396    N = Symbol('N', integer=True, positive=True)
1397    d = _dummy_with_inherited_properties_concrete(Tuple(x, 2, N))
1398    assert d.is_real
1399    assert d.is_positive
1400    assert d.is_integer
1401
1402    # Return None if no assumptions are added
1403    N = Symbol('N', integer=True, positive=True)
1404    d = _dummy_with_inherited_properties_concrete(Tuple(N, 2, 4))
1405    assert d is None
1406
1407    x = Symbol('x', negative=True)
1408    raises(InconsistentAssumptions,
1409           lambda: _dummy_with_inherited_properties_concrete(Tuple(x, 1, 5)))
1410
1411
1412def test_matrixsymbol_summation_numerical_limits():
1413    A = MatrixSymbol('A', 3, 3)
1414    n = Symbol('n', integer=True)
1415
1416    assert Sum(A**n, (n, 0, 2)).doit() == Identity(3) + A + A**2
1417    assert Sum(A, (n, 0, 2)).doit() == 3*A
1418    assert Sum(n*A, (n, 0, 2)).doit() == 3*A
1419
1420    B = Matrix([[0, n, 0], [-1, 0, 0], [0, 0, 2]])
1421    ans = Matrix([[0, 6, 0], [-4, 0, 0], [0, 0, 8]]) + 4*A
1422    assert Sum(A+B, (n, 0, 3)).doit() == ans
1423    ans = A*Matrix([[0, 6, 0], [-4, 0, 0], [0, 0, 8]])
1424    assert Sum(A*B, (n, 0, 3)).doit() == ans
1425
1426    ans = (A**2*Matrix([[-2, 0, 0], [0,-2, 0], [0, 0, 4]]) +
1427           A**3*Matrix([[0, -9, 0], [3, 0, 0], [0, 0, 8]]) +
1428           A*Matrix([[0, 1, 0], [-1, 0, 0], [0, 0, 2]]))
1429    assert Sum(A**n*B**n, (n, 1, 3)).doit() == ans
1430
1431
1432def test_issue_21651():
1433    from sympy import floor, Sum, Symbol
1434    i = Symbol('i')
1435    a = Sum(floor(2*2**(-i)), (i, S.One, 2))
1436    assert a.doit() == S.One
1437
1438
1439@XFAIL
1440def test_matrixsymbol_summation_symbolic_limits():
1441    N = Symbol('N', integer=True, positive=True)
1442
1443    A = MatrixSymbol('A', 3, 3)
1444    n = Symbol('n', integer=True)
1445    assert Sum(A, (n, 0, N)).doit() == (N+1)*A
1446    assert Sum(n*A, (n, 0, N)).doit() == (N**2/2+N/2)*A
1447
1448
1449def test_summation_by_residues():
1450    x = Symbol('x')
1451
1452    # Examples from Nakhle H. Asmar, Loukas Grafakos,
1453    # Complex Analysis with Applications
1454    assert eval_sum_residue(1 / (x**2 + 1), (x, -oo, oo)) == pi/tanh(pi)
1455    assert eval_sum_residue(1 / x**6, (x, S(1), oo)) == pi**6/945
1456    assert eval_sum_residue(1 / (x**2 + 9), (x, -oo, oo)) == pi/(3*tanh(3*pi))
1457    assert eval_sum_residue(1 / (x**2 + 1)**2, (x, -oo, oo)).cancel() == \
1458        (-pi**2*tanh(pi)**2 + pi*tanh(pi) + pi**2)/(2*tanh(pi)**2)
1459    assert eval_sum_residue(x**2 / (x**2 + 1)**2, (x, -oo, oo)).cancel() == \
1460        (-pi**2 + pi*tanh(pi) + pi**2*tanh(pi)**2)/(2*tanh(pi)**2)
1461    assert eval_sum_residue(1 / (4*x**2 - 1), (x, -oo, oo)) == 0
1462    assert eval_sum_residue(x**2 / (x**2 - S(1)/4)**2, (x, -oo, oo)) == pi**2/2
1463    assert eval_sum_residue(1 / (4*x**2 - 1)**2, (x, -oo, oo)) == pi**2/8
1464    assert eval_sum_residue(1 / ((x - S(1)/2)**2 + 1), (x, -oo, oo)) == pi*tanh(pi)
1465    assert eval_sum_residue(1 / x**2, (x, S(1), oo)) == pi**2/6
1466    assert eval_sum_residue(1 / x**4, (x, S(1), oo)) == pi**4/90
1467    assert eval_sum_residue(1 / x**2 / (x**2 + 4), (x, S(1), oo)) == \
1468        -pi*(-pi/12 - 1/(16*pi) + 1/(8*tanh(2*pi)))/2
1469
1470    # Some examples made from 1 / (x**2 + 1)
1471    assert eval_sum_residue(1 / (x**2 + 1), (x, S(0), oo)) == \
1472        S(1)/2 + pi/(2*tanh(pi))
1473    assert eval_sum_residue(1 / (x**2 + 1), (x, S(1), oo)) == \
1474        -S(1)/2 + pi/(2*tanh(pi))
1475    assert eval_sum_residue(1 / (x**2 + 1), (x, S(-1), oo)) == \
1476        1 + pi/(2*tanh(pi))
1477    assert eval_sum_residue((-1)**x / (x**2 + 1), (x, -oo, oo)) == \
1478        pi/sinh(pi)
1479    assert eval_sum_residue((-1)**x / (x**2 + 1), (x, S(0), oo)) == \
1480        pi/(2*sinh(pi)) + S(1)/2
1481    assert eval_sum_residue((-1)**x / (x**2 + 1), (x, S(1), oo)) == \
1482        -S(1)/2 + pi/(2*sinh(pi))
1483    assert eval_sum_residue((-1)**x / (x**2 + 1), (x, S(-1), oo)) == \
1484        pi/(2*sinh(pi))
1485
1486    # Some examples made from shifting of 1 / (x**2 + 1)
1487    assert eval_sum_residue(1 / (x**2 + 2*x + 2), (x, S(-1), oo)) == S(1)/2 + pi/(2*tanh(pi))
1488    assert eval_sum_residue(1 / (x**2 + 4*x + 5), (x, S(-2), oo)) == S(1)/2 + pi/(2*tanh(pi))
1489    assert eval_sum_residue(1 / (x**2 - 2*x + 2), (x, S(1), oo)) == S(1)/2 + pi/(2*tanh(pi))
1490    assert eval_sum_residue(1 / (x**2 - 4*x + 5), (x, S(2), oo)) == S(1)/2 + pi/(2*tanh(pi))
1491    assert eval_sum_residue((-1)**x * -1 / (x**2 + 2*x + 2), (x, S(-1), oo)) ==  S(1)/2 + pi/(2*sinh(pi))
1492    assert eval_sum_residue((-1)**x * -1 / (x**2 -2*x + 2), (x, S(1), oo)) == S(1)/2 + pi/(2*sinh(pi))
1493
1494    # Some examples made from 1 / x**2
1495    assert eval_sum_residue(1 / x**2, (x, S(2), oo)) == -1 + pi**2/6
1496    assert eval_sum_residue(1 / x**2, (x, S(3), oo)) == -S(5)/4 + pi**2/6
1497    assert eval_sum_residue((-1)**x / x**2, (x, S(1), oo)) == -pi**2/12
1498    assert eval_sum_residue((-1)**x / x**2, (x, S(2), oo)) == 1 - pi**2/12
1499
1500
1501@slow
1502def test_summation_by_residues_failing():
1503    x = Symbol('x')
1504
1505    # Failing because of the bug in residue computation
1506    assert eval_sum_residue(x**2 / (x**4 + 1), (x, S(1), oo))
1507    assert eval_sum_residue(1 / ((x - 1)*(x - 2) + 1), (x, -oo, oo)) != 0
1508