如何替换mathematica中的函数 [英] How to do substitution of a function in mathematica

查看:68
本文介绍了如何替换mathematica中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表达式 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆