Force function evaluation on declaration

扶醉桌前 提交于 2020-01-11 11:17:09

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5318942/force-function-evaluation-on-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!