English 中文(简体)
数学 模块相对 采用或封条——准则,习惯使用的规则?
原标题:Mathematica Module versus With or Block - Guideline, rule of thumb for usage?

Leonid在他的著作的第四章中写道:“......模块、区和带”。 这些建筑在数学图书和数学帮助中作了详细解释,因此,我在这里只讲几句话。 ......

从我读到的(能够找到)来看,我仍然处在黑暗之中。 关于一揽子职能,I(简单)使用模块,因为模块是行之有效的,我知道该建筑。 这不是最佳选择。 在什么时候、在什么地方或为什么使用(或区)时,我并不完全清楚(从文件上)。

Question. Is there a rule of thumb / guideline on when to use Module, With or Block ( for functions in packages )? Are there limitations compared to Module? The docs say that With is faster. I want to be able to defend my =choice= for Module ( or another construct ).

最佳回答

可在以下网址看出<代码>Block和/Module之间的更为实际的区别:

Module[{x}, x]
Block[{x}, x]
(*
-> x$1979
   x
*)

因此,如果您希望重新编号x,你可使用<代码>Block。 例如,

Plot[D[Sin[x], x], {x, 0, 10}]

工作不可行;

Plot[Block[{x}, D[Sin[x], x]], {x, 0, 10}]

(当然这并非理想,只是一个例子)。

另一种用途是:Block[{RecursionLimit=1000},...],其中临时改动<代码>$ Recurmit/code> (Module <>/code>本不会因为改名为而有效。 RecursionLimit

One can also use Block to block evaluation of something, eg

Block[{Sin}, Sin[.5]] // Trace
(*
-> {Block[{Sin},Sin[0.5]],Sin[0.5],0.479426}
*)

ie, it returns Sin[0.5] which is only evaluated after the Block has finished executing. This is because Sin inside the Block is just a symbol, rather than the sine function. You could even do something like

Block[{Sin = Cos[#/4] &}, Sin[Pi]]
(*
-> 1/Sqrt[2]
*)

(使用<代码>Trace,看其如何运作。) 因此,你也可以使用<条码>Block,在当地重新界定内在功能:

Block[{Plus = Times}, 3 + 2]
(*
-> 6
*)
问题回答

As you mentioned there are many things to consider and a detailed discussion is possible. But here are some rules of thumb that I apply the majority of the time:

Module[{x}, ...] is the safest and may be needed if either

  1. There are existing definitions for x that you want to avoid breaking during the evaluation of the Module, or

  2. There is existing code that relies on x being undefined (for example code like Integrate[..., x]).

Module is also the only choice for creating and returning a new symbol. In particular, Module is sometimes needed in advanced Dynamic programming for this reason.

如果您相信,对于X或依赖X的任何代码,有一些重要的现有定义没有界定,那么<代码>Block[{x}, ......]往往更快。 (请注意,在完全由你规范的项目中,相信这些条件是一种合理的“资本化”标准,你可能希望执行任何途径,因此,在这些情况下,企业常常是一个合理的选择。)

With[{x = ...}, expr] is the only scoping construct that injects the value of x inside Hold[...]. This is useful and important. With can be either faster or slower than Block depending on expr and the particular evaluation path that is taken. With is less flexible, however, since you can t change the definition of x inside expr.

Andrew has already provided a very comprehensive answer. I would just summarize by noting that Module is for defining local variables that can be redefined within the scope of a function definition, while With is for defining local constants, which can t be. You also can t define a local constant based on the definition of another local constant you have set up in the same With statement, or have multiple symbols on the LHS of a definition. That is, the following does not work.

With[{{a,b}= OptionValue /@ {opt1,opt2} }, ...]

I tend to set up complicated function definitions with Module enclosing a With. I set up all the local constants I can first inside the With, e.g. the Length of the data passed to the function, if I need that, then other local variables as needed. The reason is that With is a little faster of you genuinely do have constants not variables.

I d like to mention the official documentation on the difference between Block and Module is available at http://reference.wolfram.com/mathematica/tutorial/BlocksComparedWithModules.html.





相关问题
Solving vector equations in Mathematica

I m trying to figure out how to use Mathematica to solve systems of equations where some of the variables and coefficients are vectors. A simple example would be something like where I know A, V, and ...

Introspection of messages generated in Mathematica

Is there any way to get at the actual messages generated during the evaluation of an expression in Mathematica? Say I m numerically solving an ODE and it blows up, like so In[1] := sol = NDSolve[{x [...

Connecting points in Mathematica

I have a collection of points displayed in a graphic: alt text http://img69.imageshack.us/img69/874/plc1k1lrqynuyshgrdegvfy.jpg I d like to know if there is any command that will connect them ...

Mathematica, PDF Curves and Shading

I need to plot a normal distribution and then shade some specific region of it. Right now I m doing this by creating a plot of the distribution and overlaying it with a RegionPlot. This is pretty ...

Targeted Simplify in Mathematica

I generate very long and complex analytic expressions of the general form: (...something not so complex...)(...ditto...)(...ditto...)...lots... When I try to use Simplify, Mathematica grinds to a ...

热门标签