3

I'm learning SymPy now, and I wonder if there's a way to differentiate a function over one of its variables in the general form.

Consider this example:

There a vector, lets write its components as x_1, x_2, x_3. The length r of such vector will be r = sqrt(x_1^2 + x_2^2 + x_3^2).

I want to differentiate this vector over x_i, where I don't specify i, which should give something like x_i/r^3.

Is it possible to do this in SymPy?

Sorry for lack of equation rendering..

Bbllaaddee
  • 145
  • 1
  • 9

1 Answers1

4

You can do this with IndexedBase. The result comes out in Kronecker delta functions which than simplify after substitution:

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

In [4]: r = sqrt(x[1]**2 + x[2]**2 + x[3]**2)

In [5]: r
Out[5]: 
   _______________________
  ╱     2       2       2 
╲╱  x[1]  + x[2]  + x[3]  

In [6]: i = Symbol('i')

In [7]: r.diff(x[i])
Out[7]: 
δ   ⋅x[1] + δ   ⋅x[2] + δ   ⋅x[3]
 1,i         2,i         3,i     
─────────────────────────────────
       _______________________   
      ╱     2       2       2    
    ╲╱  x[1]  + x[2]  + x[3]     

In [8]: r.diff(x[i]).subs(i, 2)
Out[8]: 
           x[2]           
──────────────────────────
   _______________________
  ╱     2       2       2 
╲╱  x[1]  + x[2]  + x[3] 

You can also do this for a vector of symbolic dimension:

In [9]: j = Symbol('j')

In [9]: N = Symbol('N')

In [10]: r = sqrt(Sum(x[i]**2, (i, 1, N)))

In [10]: r
Out[10]: 
         _____________
        ╱   N         
       ╱   ___        
      ╱    ╲          
     ╱      ╲       2 
    ╱       ╱   x[i]  
   ╱       ╱          
  ╱        ‾‾‾        
╲╱        i = 1       

In [11]: r.diff(x[j])
Out[11]: 
     N                  
    ___                 
    ╲                   
     ╲   2⋅δ   ⋅x[i]    
     ╱      i,j         
    ╱                   
    ‾‾‾                 
   i = 1                
────────────────────────
           _____________
          ╱   N         
         ╱   ___        
        ╱    ╲          
       ╱      ╲       2 
2⋅    ╱       ╱   x[i]  
     ╱       ╱          
    ╱        ‾‾‾        
  ╲╱        i = 1       

In [12]: r.diff(x[j]).subs(N, 3).subs(j, 2)
Out[12]: 
     3                  
    ___                 
    ╲                   
     ╲   2⋅δ   ⋅x[i]    
     ╱      2,i         
    ╱                   
    ‾‾‾                 
   i = 1                
────────────────────────
           _____________
          ╱   3         
         ╱   ___        
        ╱    ╲          
       ╱      ╲       2 
2⋅    ╱       ╱   x[i]  
     ╱       ╱          
    ╱        ‾‾‾        
  ╲╱        i = 1       

In [13]: r.diff(x[j]).subs(N, 3).subs(j, 2).doit()
Out[13]: 
           x[2]           
──────────────────────────
   _______________________
  ╱     2       2       2 
╲╱  x[1]  + x[2]  + x[3]  
Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
  • Wow, thanks! I've read a bit about it now, and it turns out I can use symbolic indices too! One question: is there a way to somehow reduce this expression with deltas to only one delta? Such that instead of \delta_{1,2}... + \delta{1,3}... + \delta{2,3} there is only one? – Bbllaaddee Jun 28 '21 at 21:50
  • What I mean is, I need to differentiate it at least four times to get tensors matrix elements up to 4th rank. I want all deltas with non-repeated symbolic indices to dissapear. – Bbllaaddee Jun 28 '21 at 22:13
  • 1
    I don't understand what you mean. I suggest asking a new question. – Oscar Benjamin Jun 28 '21 at 23:20