[英]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, ie with the minimal example:我正在尝试在 sympy 中进行一些张量计算,但我似乎无法让它简化张量对 kronecker delta 的任何收缩,即最小的例子:
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.这里,n 是空间的维数,i,j 是从 1 到 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
你会期望总和计算为 x[i],除了 sympy 不做任何简化,尽管使用 simplify 命令点击它
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
你想要的是
sympy.concrete.delta.deltasummation
代替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如果 KronckerDelta function 指定边界,这将进一步简化
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.不幸的是,我不知道如何用 deltasummation 自动替换 Sum,但至少这是你的起点。
There are a few other simplifications to be found in sympy.concrete
and sympy.concrete.delta
在
sympy.concrete
和sympy.concrete.delta
中可以找到一些其他的简化
edit: link to my somewhat related question Sympy simplify sum of Kronecker delta编辑:链接到我有点相关的问题Sympy simplify sum of Kronecker delta
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.