2
$\begingroup$

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?

$\endgroup$
9
  • 3
    $\begingroup$ 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. $\endgroup$ May 5, 2013 at 2:14
  • $\begingroup$ @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? $\endgroup$
    – matheorem
    May 5, 2013 at 2:49
  • $\begingroup$ 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]}. $\endgroup$ May 5, 2013 at 2:58
  • $\begingroup$ @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? $\endgroup$
    – matheorem
    May 5, 2013 at 3:17
  • $\begingroup$ Yes. Try your "two kinds of evaluate" with Unevaluated[1+1] as input vs Evaluate[Unevaluated[1+1]] as the input. $\endgroup$ May 5, 2013 at 3:23

1 Answer 1

4
$\begingroup$

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

$\endgroup$
8
  • $\begingroup$ yeah! I notice it from Trace. Mathematica just simply strip Unevaluated away in the Table in the first step. But why? $\endgroup$
    – matheorem
    May 5, 2013 at 6:48
  • $\begingroup$ @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}] $\endgroup$
    – Rojo
    May 5, 2013 at 6:54
  • $\begingroup$ Well, you mean the result of evaluating the head Unevaluated is just strip it, right? $\endgroup$
    – matheorem
    May 5, 2013 at 8:36
  • $\begingroup$ @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. $\endgroup$
    – Rojo
    May 5, 2013 at 8:41
  • 3
    $\begingroup$ @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 $\endgroup$
    – Rojo
    May 5, 2013 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.