How to do substitution of a function in mathematica

夙愿已清 提交于 2019-12-03 12:29:06

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]

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.

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

Nothing new, just the way I usually think of it:

D[f[x, y], x] /. f -> Function[{x, y}, x y]

Out

y

You can also try Hold and Release or Defer etc.

Hold@D[f[x, y], x] /. {f[x, y] -> x*y}

D[x y, x]    


Hold@D[f[x, y], x] /. {f[x, y] -> x*y} // Release

y
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!