且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何替换mathematica中的函数

更新时间:2023-10-05 15:46:10

你的表达式中导数的 FullForm

The FullForm of the derivative in your expression is

In[145]:= D[f[x,y],x]//FullForm

Out[145]//FullForm= Derivative[1,0][f][x,y]

这应该可以解释为什么第一条规则失败了 - 您的表达式中不再有 f[x,y] 了.第二条规则失败了,因为 Derivative 认为 f 是一个函数,而你用一个表达式来代替它.你可以做的是:

This should explain why the first rule failed - there is no f[x,y] in your expression any more. The second rule failed because Derivative considers f to be a function, while you substitute it by an expression. What you can do is:

In[146]:= D[f[x,y],x]/.f->(#1*#2&)

Out[146]= y

请注意,围绕纯函数的括号是必不可少的,以避免与优先级相关的错误.

Note that the parentheses around a pure function are essential, to avoid precedence - related bugs.

或者,您可以通过模式定义您的 r.h.s:

Alternatively, you could define your r.h.s through patterns:

In[148]:= 
fn[x_,y_]:=x*y;
D[f[x,y],x]/.f->fn

Out[149]= y

HTH