SymPy understands that Sum
and +
commute.
from sympy import symbols, Idx, IndexedBase, Sum
i, n = symbols("i n", cls=Idx)
x = IndexedBase("x", shape=(n,))
s = Sum(x[i] + 1, (i, 1, n))
t = Sum(x[i], (i, 1, n)) + Sum(1, (i, 1, n))
assert s.equals(t) # OK
But for complex expressions that does not work.
# d, e, f = 3 rather large expressions
s = Sum(d + e + f, (i, 1, n))
t = Sum(d, (i, 1, n)) + Sum(e, (i, 1, n)) + Sum(f, (i, 1, n))
assert s.equals(t) # Fail!
How to explain to SymPy that this transformation is actually OK?