2

Why does

Map[Unevaluated, Table[PauliMatrix[i], {i, 1, 3}]

give

{Unevaluated[{{0, 1}, {1, 0}}], Unevaluated[{{0, -I}, {I, 0}}], Unevaluated[{{1, 0}, {0, -1}}]}

while

Table[Unevaluated[PauliMatrix[i]], {i, 1, 3}]

gives

{{{0, 1}, {1, 0}}, {{0, -I}, {I, 0}}, {{1, 0}, {0, -1}}}

I think they should give the same result! Why not?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
matheorem
  • 17,132
  • 8
  • 45
  • 115
  • 3
    It's just that Table evaluates it's arguments in a non-standard way. In particular, it Holds it's arguments, explicitly evaluates the second argument (the iterator), substitutes values obtained from the iterator into the first argument and then (importantly!) explicitly evaluates the first argument at those values. – Mark McClure May 05 '13 at 02:14
  • @MarkMcClure But according to the docs of Map. Map always effectively constructs a complete new expression and then evaluates it. And use Trace, I found in the last three steps, mathematica actually remove the Unevaluated, and finally bring back the Unevaluated head, why? – matheorem May 05 '13 at 02:49
  • I'm just saying that Table evaluates it's arguments in a non-standard way and (by implication) that Map does not. Thus, when the documentation says that Map "constructs a complete new expression and then evaluates it", it does so in the standard way. Thus, Map[Unevaluated,{1,2}] produces the same output as {Unevaluated[1],Unevaluated[2]}. – Mark McClure May 05 '13 at 02:58
  • @MarkMcClure You're telling me that the step "then evaluate it" in Map and the step 'substitutes values ....explicitly evaluates the first argument at those values' in Table is two kind of evaluate?!! I still don't understand, Now that Table has the attributes HoldAll, it should hold the Unevaluated. It seems that "expr, shift+Enter" and "Evaluate[expr]" is different ? And Map use the first one Table use the second? – matheorem May 05 '13 at 03:17
  • Yes. Try your "two kinds of evaluate" with Unevaluated[1+1] as input vs Evaluate[Unevaluated[1+1]] as the input. – Mark McClure May 05 '13 at 03:23
  • Now, I'm not claiming to fully understand the whole deal here (which is why I wrote a comment, rather than an answer). I'm simply stating that Table and Map have different evaluation procedures and that's what leads to this behavior. Also, functions like Unevaluated and attributes like HoldAll are intimately connected with these issues. – Mark McClure May 05 '13 at 03:25
  • If you interesting in how evaluation is done to extent of using Trace, you are ready for David Wagner's book. It will answer your question and many, many more. In particular, see Chapter 7. – m_goldberg May 05 '13 at 05:05
  • @MarkMcClure thank you very much – matheorem May 05 '13 at 06:15
  • @m_goldberg thank you for recommending the very good book. – matheorem May 05 '13 at 06:18

1 Answers1

4

The answer isn't so much related to Map or Table, but to Unevalauted and the evaluation sequence.

The first one

Map[Unevaluated, {1, 2}]

(* {Unevaluated[1], Unevaluated[2]} *)

All the heads and arguments are inert, and none has heads Evaluate or Unevaluated to worry about. Note that the symbol Unevaluated doesn't have head Unevaluated. Just apply the mapping downvalue to get {Unevalauted[1], Unevaluated[2]}

Now,

Table[Unevaluated@i, {i, 2}]

(* {1, 2} *)

Strip Unevaluated from the head of the argument Unevaluated@i. Now it's just like Table[i, {i,2}] giving {1, 2}.

Rojo
  • 42,601
  • 7
  • 96
  • 188
  • yeah! I notice it from Trace. Mathematica just simply strip Unevaluated away in the Table in the first step. But why? – matheorem May 05 '13 at 06:48
  • @matheorem that's equivalent to asking why MMA evaluates first the head. It's part of the evaluation sequence to strip the Unevaluateds. It's useful when you want to pass arguments to functions before they are evaluated. For example, Map[Hold, Unevaluated@{2+2, 3+7}] – Rojo May 05 '13 at 06:54
  • Well, you mean the result of evaluating the head Unevaluated is just strip it, right? – matheorem May 05 '13 at 08:36
  • @matheorem. No. Unevaluated[something] is inert. I'm mean it gets stripped. Unevaluated is very special, you couldn't replace it with, for example, Identity. – Rojo May 05 '13 at 08:41
  • What does inert mean? Are there any other head that are inert too? – matheorem May 05 '13 at 08:49
  • 3
    @matheorem with inert I just meant that it evaluates to itself, it doesn't change when evaluated. Just like {2, 3}, or Hold[5], Unevaluated[whatever] evaluates to itself. Identity, on the other hand, evaluates to its contents, or "strips Identity upon evaluation". Unevaluated isn't stripped when the Unevaluated[something] is evaluated. It is stripped when something that has it as argument is evaluated. That is very special behaviour, unique to Unevaluated – Rojo May 05 '13 at 08:56
  • I think I understand. Clear explanation, Thank you very much! – matheorem May 05 '13 at 09:06
  • I agree with this analysis. I guess this behavior of Unevaluated is exactly why we use it in a case like Length[Unevaluated[1+2+3]]. An even simpler example would be f[x_]=x^2; f[Unevaluated[2]]. – Mark McClure May 05 '13 at 11:35