Repository: sympy/sympy

Issue:
expand should not split infinite sums
```py
>>> expr = Sum(-log(1 + 3/(4*k)) + 3/(4*k), (k, 1, oo))
>>> expr.evalf()
0.348510627655664
>>> pprint(expr.expand(), order='none')
  ∞                     ∞
 ____                  ____
 ╲                     ╲
  ╲                     ╲
   ╲      ⎛     3 ⎞      ╲   3
   ╱  -log⎜1 + ───⎟ +    ╱  ───
  ╱       ⎝    4⋅k⎠     ╱   4⋅k
 ╱                     ╱
 ‾‾‾‾                  ‾‾‾‾
k = 1                 k = 1
```

The sum in question converges, but the split sums clearly do not (the second is a harmonic series).

Note you have to print this manually with `order='none'` otherwise the printer will hang trying to numerically evaluate it (a known issue, see for instance https://github.com/sympy/sympy/issues/19790 and https://github.com/sympy/sympy/issues/10800).

Discussion:

Patch:
diff --git a/.mailmap b/.mailmap
index c769920d5d..4f0e7867f3 100644
--- a/.mailmap
+++ b/.mailmap
@@ -884,6 +884,7 @@ Maxence Mayrand <35958639+maxencemayrand@users.noreply.github.com>
 Mayank Raj <mayank_1901cs35@iitp.ac.in> mayank <54908605+mayankray2020@users.noreply.github.com>
 Mayank Raj <mayank_1901cs35@iitp.ac.in> mayank raj <mayank_1901cs35@iitp.ac.in>
 Mayank Singh <mayank.singh081997@gmail.com>
+Megan Ly <megan.ly@learnosity.com>
 Megan Ly <megan@artcompiler.com> Megan Ly <meganly@MacBook-Pro.local>
 Meghana Madhyastha <meghana.madhyastha@gmail.com>
 Micah Fitch <micahscopes@gmail.com> <fitchmicah@gmail.com>
diff --git a/sympy/concrete/expr_with_limits.py b/sympy/concrete/expr_with_limits.py
index f51a5c9848..5293a060b9 100644
--- a/sympy/concrete/expr_with_limits.py
+++ b/sympy/concrete/expr_with_limits.py
@@ -592,7 +592,8 @@ def _eval_factor(self, **hints):
 
     def _eval_expand_basic(self, **hints):
         summand = self.function.expand(**hints)
-        if summand.is_Add and summand.is_commutative:
+        if (summand.is_Add and summand.is_commutative and
+                self.has_finite_limits is not False):
             return Add(*[self.func(i, *self.limits) for i in summand.args])
         elif isinstance(summand, MatrixBase):
             return summand.applyfunc(lambda x: self.func(x, *self.limits))


Test patch:
diff --git a/sympy/concrete/tests/test_sums_products.py b/sympy/concrete/tests/test_sums_products.py
index a6d00f60c0..e8dff42a61 100644
--- a/sympy/concrete/tests/test_sums_products.py
+++ b/sympy/concrete/tests/test_sums_products.py
@@ -944,9 +944,9 @@ def test_factor_expand_subs():
     assert Sum(x+1,(x,1,y)).expand() == Sum(x,(x,1,y)) + Sum(1,(x,1,y))
     assert Sum(x+a*x**2,(x,1,y)).expand() == Sum(x,(x,1,y)) + Sum(a*x**2,(x,1,y))
     assert Sum(x**(n + 1)*(n + 1), (n, -1, oo)).expand() \
-        == Sum(x*x**n, (n, -1, oo)) + Sum(n*x*x**n, (n, -1, oo))
+        == Sum(n*x*x**n + x*x**n, (n, -1, oo))
     assert Sum(x**(n + 1)*(n + 1), (n, -1, oo)).expand(power_exp=False) \
-        == Sum(n*x**(n+1), (n, -1, oo)) + Sum(x**(n+1), (n, -1, oo))
+        == Sum(n*x**(n + 1) + x**(n + 1), (n, -1, oo))
     assert Sum(a*n+a*n**2,(n,0,4)).expand() \
         == Sum(a*n,(n,0,4)) + Sum(a*n**2,(n,0,4))
     assert Sum(x**a*x**n,(x,0,3)) \