了解跟踪* [英] Understanding Trace*

查看:26
本文介绍了了解跟踪*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

当试图通过使用标准的 TraceTraceScan 命令来理解 Mathematica 的计算序列时,以及最近开发的它们漂亮的视觉表示线程,我在他们的行为中遇到了一些歧义.>

首先,当我评估

<块引用>

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

我明白

<块引用>

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

所有子列表都对应于子评估(如文档中所述).虽然文档中没有明确说明,但最后一个表达式 1+a 可能对应于评估的结果.但是列表中间的表达式 a+11+a 到底是什么意思?它们对应于标准评估序列的哪些评估步骤?

第二个奇怪的地方是TraceScan.考虑以下几点:

<块引用>

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

出[1]={a+1Plus、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 的情况下一样?是bug吗?

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

解决方案

TraceScanfp 参数使用两个参数调用.第一个是原始的未计算表达式.二是评价结果.在您的示例中,第二个 AppendTo 使用第一个参数,因此您看到的是未计算的表达式.把#改成#2,你就会看到你想要的结果.

另请注意,第二个参数not被包裹在 HoldForm 中(尽管有文档),所以一般来说必须小心使用一个函数来保存它的参数fp 参数以避免生成虚假评估.

比较 Trace 和 TraceScan

Trace 的行为在 中有相当详细的描述Mathematica 8 文档.它指出,默认情况下,Trace 仅显示 头部和参数之后的表达式.因此,我们看到这样的序列:

In[28]:= SetAttributes[f, Orderless]跟踪[f[a, 1]]出[29]= {f[a,1],f[1,a]}

仅显示输入表达式及其结果.TraceOriginal 选项控制(引用)是否在计算头部和参数之前查看表达式".当此选项为 True 时,输出将补充有头部和参数表达式:

In[30]:= Trace[f[a,1], TraceOriginal->True]出[30]= {f[a,1],{f},{a},{1},f[a,1],f[1,a]}

新列表的第一个元素是原始表达式之前头部和参数被求值.然后我们看到正在评估的头部和参数.最后,在对 head 和 arguments 求值之后,我们再次看到顶级表达式.列表的最后两个元素与原始跟踪输出的两个元素匹配.

正如链接文档所述,Trace 对其返回的表达式非常有选择性.例如,它完全省略了琐碎的评估链.TraceScan 很全面,可以为每次评估调用提供的函数,无论是否微不足道.您可以使用以下 TraceScan 表达式查看完整的评估集:

TraceScan[Print, f[a,1], _, Print[{##}]&]

下表匹配了 Trace 产生的输出,有和没有 TraceOriginal,以及 TraceScan 表达式的输出:

Trace Trace TraceScan原来的f[a,1] f[a,1]F{f} {f,F}一种{a} {a,一种}1{1} {1,1}f[1,a]{f[1,a],f[1,a]}f[a,1] f[a,1] {f[a,1]f[1,a] f[1,a] ,f[1,a]}

鉴于无法访问Trace 的内部结构,此表中存在一定数量的关于哪个条目匹配哪个条目的推测.进一步的实验可能会提供调整对齐的信息.但是,关键在于 Trace 生成的所有信息都可以使用 TraceScan 获得——而 TraceScan 提供了更多信息.

Good day,

When trying to understand the Mathematica's evaluation sequence by using standard Trace and TraceScan commands and their nice visual representations developed in the recent thread, I faced some ambiguities in their behavior.

First of all, when I evaluate

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

I get

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

All sublists correspond to sub-evaluations (as it is stated in the Documentation). The last expression 1+a probably corresponds to the result of the evaluation although it is not clearly stated in the Documentation. But what exactly mean expressions a+1 and 1+a in the middle of the list? To which evaluation steps of the standard evaluation sequence they correspond?

The second oddity is with TraceScan. Consider the following:

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}

You can see that the last two expressions in the list are 1+a and a+1. Both are results of (sub)evaluations. But the real output is 1+a and so I do not understand why a+1 is at the end of the evaluation chain? And why is no there a+1 in the middle of the evaluation chain as it was in the case of Trace? Is it a bug?

P.S. These results are reproduced with Mathematica 7.0.1 and 5.2.

解决方案

The fp argument to TraceScan is called with two arguments. The first is the original unevaluated expression. The second is the result of the evaluation. In your example, the second AppendTo is using the first argument so you are seeing the unevaluated expression. Change # to #2 and then you will see the results you expect.

Also note that the second argument is not wrapped in HoldForm (documentation notwithstanding), so in general one must take care to use a function that holds its arguments for the fp argument to avoid generating spurious evaluations.

Comparing Trace and TraceScan

The behaviour of Trace is described in considerable detail in the Mathematica 8 documentation. It states that, by default, Trace only shows expressions after the head and arguments have been evaluated. Thus, we see a sequence like this:

In[28]:= SetAttributes[f, Orderless]
         Trace[f[a, 1]]
Out[29]= {f[a,1],f[1,a]}

Only the input expression and its result is shown. The TraceOriginal options controls (quote) "whether to look at expressions before their heads and arguments are evaluated". When this option is True then the output is supplemented with the head and argument expressions:

In[30]:= Trace[f[a,1], TraceOriginal->True]
Out[30]= {f[a,1],{f},{a},{1},f[a,1],f[1,a]}

The first element of the new list is the original expression before the head and arguments are evaluated. Then we see the head and arguments being evaluated. Finally, we see the top-level expressions again, after the head and arguments have been evaluated. The last two elements of the list match the two elements of the original trace output.

As the linked documentation states, Trace is very selective about what expressions it returns. For example, it omits trivial evaluation chains completely. TraceScan is comprehensive and calls the supplied functions for every evaluation, trivial or not. You can see the comprehensive set of evaluations using the following TraceScan expression:

TraceScan[Print, f[a,1], _, Print[{##}]&]

The following table matches the output produced by Trace with and without TraceOriginal, along with the output of the TraceScan expression:

Trace   Trace    TraceScan
        Original

        f[a,1]   f[a,1]
                 f
        {f}      {f
                 ,f}
                 a
        {a}      {a
                 ,a}
                 1
        {1}      {1
                 ,1}
                 f[1,a]
                 {f[1,a]
                 ,f[1,a]}
f[a,1]  f[a,1]   {f[a,1]
f[1,a]  f[1,a]   ,f[1,a]}

There is certain amount of speculation in this table about which entry matches against which, given that the internals of Trace are inaccessible. Further experiments might give information that adjusts the alignment. However, the key point is that all of the information generated by Trace is available using TraceScan -- and TraceScan provides more.

这篇关于了解跟踪*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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