Repository: sympy/sympy (Python)

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:
My opinion is that explicit calls of `expand` for sum or limit or integrals should be left as formal operation on trees, rather than mathematically correct operation (I'm not sure convergence can be determined easily or cheaply.)
(But it can be carefully applied for `doit`)
For me, this was part of a larger expression. I called expand() (actually factor(), which called expand()) to try to simplify it, but it resulted in an expression that was mathematically wrong. Functions like expand() should never perform operations that are mathematically incorrect. 

There's no need to check convergence. expand() should not expand infinite sums, unless possibly force=True is provided.
For mathematical correctness of expansion, integral would already be doing wrong like
```
from sympy import *

x = Symbol('x')
Integral((exp(I/x) - 1) * sin(x), (x, 0, oo)).expand()
```

(Which is an example from https://doi.org/10.1145/2016567.2016569)
Hello masters, this is Priyanshu Panwar eagerly looking to participate in GSOC 2021 via SymPy org. Can I please work on this issue if you people can allow me and guide me a bit?
I'm fine with changing the behavior of summation, despite some counterexamples that are hard to answer, but I hope these sort of things can get organized for a better solution in future. So you can go on.
@priyanshu-panwar are you still working on this issue?

We can change `_eval_expand_basic` in the `AddWithLimits` class to prevent expansion of integrals and sums with infinite limits as follows:

``` python
--- 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))
```

I'm happy to create a PR for this.
Feel free to take over things like this with comments that are over a year old. 



Patch:
diff --git a/.mailmap b/.mailmap
index c769920d5dd6..4f0e7867f35a 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 f51a5c9848e4..5293a060b9cf 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 a6d00f60c0c8..e8dff42a6128 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)) \