Derivative of summations
<p>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, i.e. n1 to nmax and performing a summation on it. Then I want to be able to take the derivative:</p>

<p>So far I tried the following:</p>

<pre><code>numSpecies = 10
n = IndexedBase('n')
i = symbols("i",cls=Idx)
nges = summation(n[i],[i,1,numSpecies])
</code></pre>

<p>However, if i try to take the derivative with respect to one variable, this fails:</p>

<pre><code>diff(nges,n[5])
</code></pre>

<p>I also tried to avoid working with <a href="http://docs.sympy.org/latest/modules/tensor/indexed.html" rel="nofollow"><code>IndexedBase</code></a>.</p>

<pre><code>numSpecies = 10
n = symbols('n0:%d'%numSpecies)
k = symbols('k',integer=True)
ntot = summation(n[k],[k,0,numSpecies])
</code></pre>

<p>However, here already the summation fails because mixing python tuples and the sympy summation.</p>

<p>How I can perform indexedbase derivatives or some kind of workaround?</p>

<p>I don't know why the <code>IndexedBase</code> approach does not work (I would be interested to know also). You can, however, do the following:</p>

<pre><code>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])
</code></pre>

python sympy derivative