'Contracting with kronecker delta in sympy
I'm trying to do some tensor calculations in sympy, but I can't seem to get it to simplify any contractions of tensors against the kronecker delta, i.e. with the minimal example:
from sympy import *
n = Idx('n')
i = Idx('i',(1,n))
j = Idx('j',(1,n))
x = IndexedBase('x')
print(Sum(KroneckerDelta(i,j)*x[j],(j,1,n)))
here, n is the dimension of the space, and i,j, are indices that run from 1 to n. You would expect the sum to evaluate to x[i], except sympy doesn't do any simplification whatsoever, despite hitting it with the simplify command
Solution 1:[1]
I think I found the solution to your question while I posted a similar question
What you want is the sympy.concrete.delta.deltasummation in place of Sum
print(sympy.concrete.delta.deltasummation(KroneckerDelta(i,j)*x[j],(j,1,n)))
>>> Piecewise((x[i], (n >= i) & (i >= 1)), (0, True))
This simplifies further if the KronckerDelta function specifies bounds
print(sympy.concrete.delta.deltasummation(KroneckerDelta(i,j,(1,n))*x[j],(j,1,n)))
>>> x[i]
Unfortunately I don't know how to automatically replace the Sum with the deltasummation, but at least this is a starting point for you.
There are a few other simplifications to be found in sympy.concrete and sympy.concrete.delta
edit: link to my somewhat related question Sympy simplify sum of Kronecker delta
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael V |
