'sympy - taking derivative of sum of symbolic number of elements

I am trying to find the closed form solution of the derivative of a sum of symbolic number of elements. But the results obtained from my code is not correct.

from sympy import *
i, n = symbols('i n')
s, x = symbols('s x', cls=Function)
s = summation(x(i), (i, 1, n))
frac = x(i)/s
diff(frac,x(i))

It's easy to derive that the correct result should be:

equation1

but the above code actually gives me is:

equation2

(I apologize if the Latex code is not rendered correctly, thank you for eye-rendering it..) There is a similar post here but their problem is different: the derivative is taken over a parameter, not $x(i)$, one of the element.

Wonder why this occurred?



Solution 1:[1]

I find it very hard to parse your LaTeX but it seems like when I try this I get what you said you expected to see (maybe that's the wrong way round). By the way a better way to demonstrate this on SO is to use SymPy's pretty-printing feature (e.g. pprint or init_printing):

In [8]: frac
Out[8]: 
   x(i)   
??????????
  n       
 ___      
 ?        
  ?       
  ?   x(i)
 ?        
 ???      
i = 1     

In [9]: diff(frac, x(i))
Out[9]: 
          n                 
         ___                
         ?                  
          ?                 
   x(i)?  ?   1             
         ?                  
         ???                
        i = 1         1     
- ????????????? + ??????????
              2     n       
  ?  n       ?     ___      
  ? ___      ?     ?        
  ? ?        ?      ?       
  ?  ?       ?      ?   x(i)
  ?  ?   x(i)?     ?        
  ? ?        ?     ???      
  ? ???      ?    i = 1     
  ?i = 1     ?  

Firstly you need to understand that in SymPy if x is a function then x(i) is the function evaluated at i. If you want to use i to index different variables then you should declare x as IndexedBase rather than Function and then use square subscript brackets. Also there can be a confusion between free and bound symbols so it is better to use different symbols here for the (bound) summation variable and the variable that you differentiate wrt:

In [15]: x = IndexedBase('x')

In [16]: j = symbols('j')

In [17]: s = Sum(x[j], (j, 1, n))

In [18]: frac = x[i] / s

In [19]: frac
Out[19]: 
   x[i]   
??????????
  n       
 ___      
 ?        
  ?       
  ?   x[j]
 ?        
 ???      
j = 1     

In [20]: diff(frac, x[i])
Out[20]: 
         n                    
        ___                   
        ?                     
         ?   ?                
  x[i]?  ?    i,j             
        ?                     
        ???                   
       j = 1            1     
- ??????????????? + ??????????
               2      n       
   ?  n       ?      ___      
   ? ___      ?      ?        
   ? ?        ?       ?       
   ?  ?       ?       ?   x[j]
   ?  ?   x[j]?      ?        
   ? ?        ?      ???      
   ? ???      ?     j = 1     
   ?j = 1     ? 

That looks correct to me (note the upper sum has a 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 Oscar Benjamin