Mathematica 中最佳的字符分配方法

发布于 2024-12-04 14:56:50 字数 419 浏览 3 评论 0原文

遇到以下示例代码的问题,我希望有解决方法。

假设我已经定义了一个函数:

f[x_,y_,z_] = x + y + z + x Log[x] + y Log[y] +z Log[z]

并且我要进行赋值

f[x_,y_,z_] = x + y + z + x Log[x] + y Log[y] +z Log[z]//.x->1//.y->1//.z->0

,但是我不想让 Mathematica 将 z 替换为 0,我只是希望忽略 z 来给出结果 f[x_,y_] = 2,而不必定义一个新函数。将上述代码输入 Mathematica 会产生明显的不确定解决方案

非常感谢帮助这位新手。

Running into a problem with the following example code for which I hope there is a way around.

Say I have defined a function:

f[x_,y_,z_] = x + y + z + x Log[x] + y Log[y] +z Log[z]

and I was to assign

f[x_,y_,z_] = x + y + z + x Log[x] + y Log[y] +z Log[z]//.x->1//.y->1//.z->0

But rather than have Mathematica replace z with 0 I just want z to be ignored to give the result f[x_,y_] = 2 without having to define a new function. Entering the above code into Mathematica results in an obvious Indeterminate solution

Helping this novice out is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-12-11 14:56:50

假设您希望对 z 描述的处理也适用于 xy,您可以这样做:

f[x_, y_, z_] := g[x] + g[y] + g[z]

g[0] = 0;
g[x_] := x + x Log[x]

辅助函数 g 显式处理零情况。这些定义产生如下结果:

f[1, E, E^2]
(* 1 + 2*E + 3*E^2 *)

f[1, 1, 1]
(* 3 *)

f[1, 1, 0]
(* 2 *)

f[0, 0, E]
(* 2*E *)

Assuming that you want the treatment you describe for z to apply to x and y as well, you could do this:

f[x_, y_, z_] := g[x] + g[y] + g[z]

g[0] = 0;
g[x_] := x + x Log[x]

The helper function g handles the zero case explicitly. These definitions yield results like these:

f[1, E, E^2]
(* 1 + 2*E + 3*E^2 *)

f[1, 1, 1]
(* 3 *)

f[1, 1, 0]
(* 2 *)

f[0, 0, E]
(* 2*E *)
峩卟喜欢 2024-12-11 14:56:50

首先,函数应用是通过调用函数来实现的:

f[1,1,1]

其次,为什么不使用 limit 引入一个新函数呢?

f[x_,y_,z_] := x + y + z + x*Log[x] + y*Log[y] +z*Log[z]
g[x_,y_]:=Limit[f[x,y,z],z->0]
g[1,1]

这应该给你2,虽然我现在不在mathematica前面所以我还没有检查

First, function application occurs by calling the function:

f[1,1,1]

Second, why not introduce a new function using limit?

f[x_,y_,z_] := x + y + z + x*Log[x] + y*Log[y] +z*Log[z]
g[x_,y_]:=Limit[f[x,y,z],z->0]
g[1,1]

That should give you the 2, though I'm not in front of mathematica now so i havent checked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文