如何替换mathematica中的函数

编程入门 行业动态 更新时间:2024-10-25 14:22:58
本文介绍了如何替换mathematica中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-25 07:14:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   mathematica

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!