gpt4 book ai didi

wolfram-mathematica - 了解跟踪*

转载 作者:行者123 更新时间:2023-12-04 18:51:30 30 4
gpt4 key购买 nike

再会,

当试图通过使用标准 Trace 来理解 Mathematica 的评估序列时和 TraceScan最近 thread 中开发的命令及其漂亮的视觉表示,我在他们的行为中遇到了一些模棱两可的地方。

首先,当我评估

In[1]:= Trace[a+1,TraceOriginal->True]



我得到

Out[1]= {a+1,{Plus},{a},{1},a+1,1+a,{Plus},{1},{a},1+a}



所有子列表都对应于子评估(如文档中所述)。最后一个表达式 1+a尽管文档中没有明确说明,但可能与评估结果相对应。但究竟是什么意思表达 a+11+a在列表中间? the standard evaluation sequence的评估步骤他们对应?

第二个奇怪的是 TraceScan .考虑以下:

In[1]:= list={}; TraceScan[AppendTo[list,StyleForm[#,"Input"]]&,(a+1),_,AppendTo[list,#]&]; list

Out[1]= {a+1, Plus, Plus, a, a, 1, 1, 1+a, Plus, Plus, 1, 1, a, a, 1+a, a+1}



可以看到列表中的最后两个表达式是 1+aa+1 .两者都是(子)评估的结果。但真正的输出是 1+a所以我不明白为什么 a+1是在评估链的末端吗?为什么没有 a+1在评估链的中间,就像在 Trace 的情况下一样?这是一个错误吗?

附言这些结果用 Mathematica 7.0.1 和 5.2 重现。

最佳答案

问题的第一部分很简单。
表达式 a+11+a列表中间是 Orderless Plus 的属性火灾和条款按默认顺序排列。
这是 standard evaluation sequence 中的第 8 点啧啧。
TraceScan中的“怪异”也出现在第 8 版中。
因为,这是一个罕见的命令,这里是 TraceScan 的文档

TraceScan[f, expr, form, fp] applies f before evaluation and fp after evaluation to expressions used in the evaluation of expr.



请注意,如果将其应用于表达式 a + 1 + b你得到
In[32]:= TraceScan[Print["f \t",#]&, a+1+b, _, Print["fp\t",#]&]
During evaluation of In[32]:= f a+1+b
During evaluation of In[32]:= f Plus
During evaluation of In[32]:= fp Plus
During evaluation of In[32]:= f a
During evaluation of In[32]:= fp a
During evaluation of In[32]:= f 1
During evaluation of In[32]:= fp 1
During evaluation of In[32]:= f b
During evaluation of In[32]:= fp b
During evaluation of In[32]:= f 1+a+b
During evaluation of In[32]:= fp 1+a+b
During evaluation of In[32]:= fp a+1+b
Out[32]= 1+a+b

从这里,很清楚发生了什么。 fp评估后适用 - 所以最终 fp其实对应第一个 f .它直到最后才打印,因为需要首先评估子表达式。

关于wolfram-mathematica - 了解跟踪*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5562126/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com