From c95bb45a34ddb9cf3e59e86bd364b7ac92863ff4 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 2 Jun 2021 09:08:23 +0300 Subject: [PATCH] concrete: fix finite geometric sums Added powsimp().cancel() heuristics to catch some cases when the term is not in a canonical form. Added regression test. Closes sympy/sympy#21557 --- diofant/concrete/summations.py | 4 ++-- diofant/tests/concrete/test_sums_products.py | 9 +++++++++ docs/release/notes-0.13.rst | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/diofant/concrete/summations.py b/diofant/concrete/summations.py index 9351b0a1183..a8004b9b313 100644 --- a/diofant/concrete/summations.py +++ b/diofant/concrete/summations.py @@ -654,13 +654,13 @@ def eval_sum_symbolic(f, limits): else: return harmonic(b, abs(n)) - harmonic(a - 1, abs(n)) - if not (a.has(oo, -oo) or - b.has(oo, -oo)): + if not (a.has(oo, -oo) or b.has(oo, -oo)): # Geometric terms c1 = Wild('c1', exclude=[i]) c2 = Wild('c2', exclude=[i]) c3 = Wild('c3', exclude=[i]) + f = f.powsimp().cancel() e = f.match(c1**(c2*i + c3)) if e is not None: diff --git a/diofant/tests/concrete/test_sums_products.py b/diofant/tests/concrete/test_sums_products.py index a46005f6deb..775ca85be91 100644 --- a/diofant/tests/concrete/test_sums_products.py +++ b/diofant/tests/concrete/test_sums_products.py @@ -911,3 +911,12 @@ def test_sympyissue_10156(): def test_sympyissue_15943(): s = Sum(binomial(n, k)*factorial(n - k), (k, 0, n)) assert s.doit().simplify() == E*(gamma(n + 1) - lowergamma(n + 1, 1)) + + +def test_sympyissue_21557(): + f1 = 2**(-k)*exp(I*k) + assert summation(f1, (k, 1, oo)).simplify() == E**I/(2 - E**I) + f2 = 2**(k*(I - 1)) + assert summation(f2, (k, 1, oo)).simplify() == 2**I/(2 - 2**I) + assert (summation(f2, (k, 1, x)).simplify() == + 2**(-x + I)*(-2**x + 2**(I*x))/(-2 + 2**I)) diff --git a/docs/release/notes-0.13.rst b/docs/release/notes-0.13.rst index 5a8d502df02..fd67cd21670 100644 --- a/docs/release/notes-0.13.rst +++ b/docs/release/notes-0.13.rst @@ -88,3 +88,4 @@ These Sympy issues also were addressed: * :sympyissue:`21530`: Incorrect limit * :sympyissue:`21166`: Wrong integration result involving square root of absolute value * :sympyissue:`21549`: Bug: integrate(x*sqrt(abs(x)),(x,-1,0)) returns wrong result +* :sympyissue:`21557`: Summation of geometric series with non-real exponent does not evaluate