声明时强制功能评估 [英] Force function evaluation on declaration

查看:31
本文介绍了声明时强制功能评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 f[x_,y_,z_]:=Limit[g[x+eps,y,z],eps->0]; 并且我绘制了 f[x,y,z] 下一步.之前,我曾经在 f 的定义中计算限制并复制表达式.我试图一步完成这一切.但是,只有当我尝试绘制 f 时,才会对限制进行评估.结果,每次我改变变量并重新绘制时,都会重新评估限制(评估大约需要一分钟,所以很烦人).我尝试先评估限制,然后执行 f[x_,y_,z_]:=%.但这也行不通.如何获得在声明时评估限制的函数?

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?

推荐答案

Mr Wizard 的一个替代方案是,您还可以将 Evaluate 放在函数的定义中:

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}]

您可以通过 Timing Plot 将两个版本与没有 Evaluate 的版本进行比较.

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

这篇关于声明时强制功能评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆