问题描述
限时送ChatGPT账号..我有表达式 D[f[x, y], x]
,我想用 x* 替换
,我尝试了以下操作:f[x,y]
y
I have the expression D[f[x, y], x]
, and I want to substitute f[x,y]
with x*y
, I tried the following:
D[f[x, y], x]/.{f[x,y] ->x*y}
和D[f[x, y], x]/.{f ->x*y}
但都没有奏效.感谢您的帮助!谢谢.
But neither worked. Would appreciate your help! Thanks.
推荐答案
你的表达式中导数的 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
这篇关于如何替换mathematica中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论