开发者

Best character assignment method in Mathematica

开发者 https://www.devze.com 2023-04-04 15:10 出处:网络
Running into a problem with the following example code for which I hope there is a way around. Say I have defined a function:

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开发者_StackOverflow社区 define a new function. Entering the above code into Mathematica results in an obvious Indeterminate solution

Helping this novice out is greatly appreciated.


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 *)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号