我们可以替换来自sympy.Function变量的区别的sympy中的“衍生”术语吗?

当运行以下代码时,将显示Derivative(Ksi(uix,uiy),uix))和Derivative(Ksi(uix,uiy),uiy))术语:

In [4]: dgN
Out[4]:
Matrix([
[-(x1x - x2x)*(-x1y + x2y)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)*Derivative(Ksi(uix, uiy), uix) + (-x1y + x2y)*(-(-x1x + x2x)*Derivative(Ksi(uix, uiy), uix) + 1)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)],
[-(-x1x + x2x)*(-x1y + x2y)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)*Derivative(Ksi(uix, uiy), uiy) + (x1x - x2x)*(-(-x1y + x2y)*Derivative(Ksi(uix, uiy), uiy) + 1)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)]])

我想用例如我知道的函数的导数的符号表达式来代替此导数项,我想设置Derivative(Ksi(uix,uiy),uix)= 2 * uix.
是否有一种巧妙的方法来进行此替换并获得将Derivative(Ksi(uix,uiy),uix)设置为2 * uix的dgN的符号表达式?这是我的代码:

import sympy as sp 

sp.var("kPenN, Xix, Xiy, uix, uiy, Alpha, x1x, x1y, x2x, x2y, x3x, x3y ", real = True) 
Ksi = sp.Function('Ksi')(uix,uiy)
Xi          = sp.Matrix([Xix, Xiy])
ui          = sp.Matrix([uix, uiy])
xix         = Xix + uix
xiy         = Xiy + uiy   
xi          = sp.Matrix([xix, xiy])
x1 = sp.Matrix([x1x, x1y])
x2 = sp.Matrix([x2x, x2y])

N           = sp.Matrix([x2 - x1, sp.zeros(1)]).cross(sp.Matrix([sp.zeros(2,1) , sp.ones(1)]))
N = sp.Matrix(2,1, sp.flatten(N[0:2]))
N = N / (N.dot(N))**(0.5)

xp = x1 + (x2 - x1)*Ksi
# make it scalar (in agreement with 9.231)
gN = (xi - xp).dot(N)
dgN = sp.Matrix([gN.diff(uix), gN.diff(uiy)])

最佳答案

您想要的替代品可以通过以下方式实现

dgN_subbed = dgN.subs(sp.Derivative(Ksi, uix), 2*uix)

这里的Ksi没有参数(uix,uiy),因为这些参数是在创建Ksi时已经声明的.

如果将Ksi定义为Ksi = sp.Function(‘Ksi’),则语法会更直观一些,而无论参数如何,都可以稍后提供.然后sp.Derivative(Ksi(uix,uiy),uix)将成为引用导数的方法.