本文介绍了我们可以用sympy.Function变量的区别替换sympy中的“派生"项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时,出现Derivative(Ksi(uix, uiy), uix))Derivative(Ksi(uix, uiy), uiy))术语:

When the following code is run Derivative(Ksi(uix, uiy), uix)) and Derivative(Ksi(uix, uiy), uiy)) terms appear:

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项,我想设置Derivative(Ksi(uix,uiy), uix) = 2 * uix.是否有一种巧妙的方法来执行此替换并在Derivative(Ksi(uix,uiy), uix)设置为2 * uix的情况下获取dgN的符号表达式?这是我的代码:

I would like to replace this Derivative terms by, let's say, the symbolic expression of the derivative of a function that I know for example, I would like to set Derivative(Ksi(uix,uiy), uix) = 2 * uix.Is there a neat way to do this substitution and to get a symbolic expression for dgN with Derivative(Ksi(uix,uiy), uix) set to 2 * uix? Here is my code:

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时已经声明的.

Here Ksi is without arguments (uix,uiy) since those were already declared when Ksi was created.

如果将Ksi定义为Ksi = sp.Function('Ksi'),则语法会更直观一些,而无论参数如何,都可以在以后提供.那么sp.Derivative(Ksi(uix, uiy), uix)将是引用导数的方式.

The syntax would be a little more intuitive if you defined Ksi as Ksi = sp.Function('Ksi'), leaving the arguments -- whatever they may be -- to be supplied later. Then sp.Derivative(Ksi(uix, uiy), uix) would be the way to reference the derivative.

这篇关于我们可以用sympy.Function变量的区别替换sympy中的“派生"项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:55