Force function evaluation on declaration

2019-02-25 20:11发布

I have a function f[x_,y_,z_]:=Limit[g[x+eps,y,z],eps->0]; and I plot f[x,y,z] in the next step. Earlier, I used to evaluate the limit and copy the expression in the definition of f. I tried to make it all in one step. However, the evaluation of the Limit is done only when I try to plot f. As a result, every time I change around the variables and replot, the limit is evaluated all over again (it takes about a min to evaluate, so it becomes annoying). I tried evaluating the limit first, and then doing f[x_,y_,z_]:=%. But that doesn't work either. How do I get the function to evaluate the limit upon declaration?

2条回答
男人必须洒脱
2楼-- · 2019-02-25 20:39

An alternative to Mr Wizard's solution is that you can also put the Evaluate in the function's definition:

f[x_, y_, z_] := Evaluate[Limit[Multinomial[x, y, z], x->0]]

Plot3D[f[x, y, z], {y, 1, 5}, {z, 1, 5}]

You can compare the two versions with the one without an Evaluate by Timing the Plot.

查看更多
太酷不给撩
3楼-- · 2019-02-25 20:46

The function you need is logically called Evaluate and you can use it within the Plot command.

Here is a contrived example:

f[x_, y_, z_] := Limit[Multinomial[x, y, z], x -> 0]

Plot3D[ Evaluate[ f[x, y, z] ], {y, 1, 5}, {z, 1, 5}]

Addressing your follow-up question, perhaps all you seek is something like

ff = f[x, y, z]

Plot3D[ff, {y, 1, 5}, {z, 1, 5}]

or possibly merely

ClearAll[f, x, y, z]

f[x_, y_, z_] = Limit[Multinomial[x, y, z], x -> 0]

Plot3D[f[x, y, z], {y, 1, 5}, {z, 1, 5}]

It would be helpful if you would post a more complete version of your code.

查看更多
登录 后发表回答