Integration over sum of indexed symbols

118 views
Skip to first unread message

Plamen Dimitrov

unread,
Jan 28, 2025, 4:40:22 AM1/28/25
to sympy
Hi, does anyone know how to properly integrate this indexed sum for an xi term that is also present in the sum indexed by j below?
```
i, j, C = sp.symbols('i, j, k, C', integer=True)
x = sp.IndexedBase('x')
dF_dxi = x[i] + sp.Sum(x[j], (j, 1, C))
Fxi = sp.integrate(dF_dxi, x[i])
# result: x[i]**2/2 + x[i]*Sum(x[j], (j, 1, C))
```
It seems the integration does not realize that one of the xj terms in the sum is an xi since it assumes the indices don't ever intersect even though these are both defined as integers.


Jatin Bhardwaj

unread,
Jan 31, 2025, 4:13:24 PM1/31/25
to sympy
You have to explicitly tell that indexed variables can overlap and intersect. Maybe this code fetches you the desired result,
Try to split the sum into parts

dF_dxi = x[i] + sp.Sum(x[j], (j, 1, i-1)) + sp.Sum(x[j], (j, i+1, C)) + x[i]
#outpur: (Sum(x[j], (j, 1, i - 1)) + Sum(x[j], (j, i + 1, C)))*x[i] + x[i]**2

Plamen Dimitrov

unread,
Feb 10, 2025, 10:28:48 PM2/10/25
to sympy
> You have to explicitly tell that indexed variables can overlap and intersect.

I see, I had similar thoughts for explicit splits but was hoping there might be some automation on the side of sympy for this way. Indeed I guess this could stretch too much what sympy should do for us and a split remains viable solution for simple enough cases. I believe there could be more complex equations out there where such explicit split might not be easily possible (in addition to slightly harmed readability from a shorter form of each equation) but have to reach this point first to confirm. In any case, thanks for the quick reply!

Plamen Dimitrov

unread,
Feb 10, 2025, 11:14:40 PM2/10/25
to sympy
One more important observation here: sympy can in fact handle different index symbols in the case of differentiation just fine
```
expr1 = x*y + sp.sin(x)**2
der1ij = sp.diff(expr1, x.subs(i, j))
# result: 2*sin(x[i])*cos(x[i])*KroneckerDelta(i, j) + KroneckerDelta(i, j)*y[i]
```
as I guess it will assume any "x" symbol can still depend on "x_i" even if we differentiate with respect to "x_j". In other words, it supports the possibility of i=j while integration does not. This means that there is lack of symmetry in expectation of supported functionality between integration and differentiation, I assume mostly because the Kroenecker delta approach is only possible for differentiation. I assume there is no way we can achieve something similar for integration here?
Message has been deleted

Plamen Dimitrov

unread,
Apr 27, 2025, 2:15:48 AM4/27/25
to sympy
Do you think this is something I could work on and help out with? Is it of interest to the maintainers? If so I could create a GH issue about it.

As an alternavice could anyone suggest a nice dynamical systems focused libraries based on sympy that could improve on this?
Reply all
Reply to author
Forward
0 new messages