sympy - 有没有办法区分抽象变量? [英] sympy - is there a way to differentiate over an abstract variable?

查看:25
本文介绍了sympy - 有没有办法区分抽象变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习 SymPy,我想知道是否有一种方法可以通过一般形式的一个变量来区分函数.

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.

考虑这个例子:

有一个向量,让我们把它的分量写成 x_1、x_2、x_3.这种向量的长度 r 将是 r = sqrt(x_1^2 + x_2^2 + x_3^2).

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).

我想在 x_i 上区分这个向量,我没有指定 i,它应该给出类似 x_i/r^3 的东西.

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

可以在 SymPy 中做到这一点吗?

Is it possible to do this in SymPy?

抱歉缺少方程渲染..

推荐答案

您可以使用 IndexedBase 做到这一点.结果出现在 Kronecker delta 函数中,该函数在替换后进行了简化:

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]  

这篇关于sympy - 有没有办法区分抽象变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆