我会时不时地使用渐变,但我不太擅长它。目前,我一直在定义一个索引变量列表,即从n1到nmax,然后对其进行求和。然后我想可以求导数:
到目前为止,我尝试了以下几种方法:
numSpecies = 10
n = IndexedBase('n')
i = symbols("i",cls=Idx)
nges = summation(n[i],[i,1,numSpecies])但是,如果我尝试对一个变量求导,这将失败:
diff(nges,n[5])我也尽量避免使用IndexedBase。
numSpecies = 10
n = symbols('n0:%d'%numSpecies)
k = symbols('k',integer=True)
ntot = summation(n[k],[k,0,numSpecies])然而,这里的求和已经失败了,因为混合了python元组和渐近求和。
如何执行indexedbase导数或某种变通方法?
发布于 2016-08-29 01:30:46
我不知道为什么IndexedBase方法不起作用(我也想知道)。但是,您可以执行以下操作:
import sympy as sp
numSpecies = 10
n = sp.symbols('n0:%d'%numSpecies) # note that n equals the tuple (n0, n1, ..., n9)
ntot = sum(n) # sum elements of n using the standard
# Python function for summing tuple elements
#ntot = sp.Add(*n) # same result using Sympy function
sp.diff(ntot, n[5])https://stackoverflow.com/questions/39193771
复制相似问题