[英]Derivative of summations
I am using sympy from time to time, but am not very good at it. 我不时使用同情,但不是很擅长。 At the moment I am stuck with defining a list of indexed variables, ie n1 to nmax and performing a summation on it.
目前我仍然坚持定义一个索引变量列表,即n1到nmax并对其进行求和。 Then I want to be able to take the derivative:
然后我希望能够采用衍生物:
So far I tried the following: 到目前为止我尝试了以下内容:
numSpecies = 10
n = IndexedBase('n')
i = symbols("i",cls=Idx)
nges = summation(n[i],[i,1,numSpecies])
However, if i try to take the derivative with respect to one variable, this fails: 但是,如果我尝试针对一个变量采用派生,则会失败:
diff(nges,n[5])
I also tried to avoid working with IndexedBase
. 我也试图避免使用
IndexedBase
。
numSpecies = 10
n = symbols('n0:%d'%numSpecies)
k = symbols('k',integer=True)
ntot = summation(n[k],[k,0,numSpecies])
However, here already the summation fails because mixing python tuples and the sympy summation. 但是,这里总和已经失败,因为混合了python元组和sympy总和。
How I can perform indexedbase derivatives or some kind of workaround? 我如何执行indexedbase衍生产品或某种变通方法?
With SymPy's development version, your example works. 使用SymPy的开发版本,您的示例可行。
To install SymPy's development version, just pull it down with git
: 要安装SymPy的开发版本,只需使用
git
将其下拉:
git clone git://github.com/sympy/sympy.git
cd sympy
Then run python from that path or set the PYTHONPATH
to include that directory before Python's default installation. 然后从该路径运行python或将
PYTHONPATH
设置为在Python的默认安装之前包含该目录。
Your example on the development version: 您在开发版本上的示例:
In [3]: numSpecies = 10
In [4]: n = IndexedBase('n')
In [5]: i = symbols("i",cls=Idx)
In [6]: nges = summation(n[i],[i,1,numSpecies])
In [7]: nges
Out[7]: n[10] + n[1] + n[2] + n[3] + n[4] + n[5] + n[6] + n[7] + n[8] + n[9]
In [8]: diff(nges,n[5])
Out[8]: 1
You could also use the contracted form of summation: 您还可以使用简约形式的总和:
In [9]: nges_uneval = Sum(n[i], [i,1,numSpecies])
In [10]: nges_uneval
Out[10]:
10
___
╲
╲ n[i]
╱
╱
‾‾‾
i = 1
In [11]: diff(nges_uneval, n[5])
Out[11]:
10
___
╲
╲ δ
╱ 5,i
╱
‾‾‾
i = 1
In [12]: diff(nges_uneval, n[5]).doit()
Out[12]: 1
Also notice that in the next SymPy version you will be able to derive symbols with symbolic indices: 另请注意,在下一个SymPy版本中,您将能够使用符号索引派生符号:
In [13]: j = symbols("j")
In [13]: diff(n[i], n[j])
Out[13]:
δ
j,i
Where you get the Kronecker delta . 你得到Kronecker三角洲的地方 。
If you don't feel like installing the SymPy development version, just wait for the next full version (probably coming out this autumn), it will support derivatives of IndexedBase
. 如果您不想安装SymPy开发版本,只需等待下一个完整版本(可能会在今年秋季推出),它将支持
IndexedBase
衍生IndexedBase
。
I don't know why the IndexedBase
approach does not work (I would be interested to know also). 我不知道为什么
IndexedBase
方法不起作用(我也有兴趣知道)。 You can, however, do the following: 但是,您可以执行以下操作:
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])
I'm not clear about what you want to do. 我不清楚你想做什么。 However, perhaps this will help.
但是,也许这会有所帮助。 Edited in response to two comments received.
编辑回应收到的两条评论。
from sympy import *
nspecies = 10
[var('n%s'%_) for _ in range(nspecies)]
expr = sympify('+'.join(['n%s'%_ for _ in range(nspecies)]))
expr
print ( diff(expr,n1) )
expr = sympify('n0**n1+n1**n2')
expr
print ( diff(expr,n1) )
Only the first expression responds to the original question. 只有第一个表达式响应原始问题。 This is the output.
这是输出。
1
n0**n1*log(n0) + n1**n2*n2/n1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.