0

I'm a beginner to Mathematica. I'd read several document. But I still have a problem. For example, every expression in Mathematica are of the form Head[expr1,expr2,...] If I type Plus[3^3,6*9], what is the practical order Mathematica interpret?

  1. Interpret Plus first, and then interpret expr1, expr2,... In other words, from 'outside' to 'inside'
  2. Evaluate Power[3,3] first, then Times[6,9], after evaluating these, then interpret Plus

Or there may be other ways Mathematica use? What else should I notice? Thanks for your replying and forgiving my bad English grammar. :-)

Eric
  • 1,191
  • 7
  • 20
  • Order of evaluation can get complicated but in general it is inside out. Here #2 is correct. Take a look at Plus[3^3, 6*9] // Trace – mfvonh Jun 24 '14 at 05:32
  • http://reference.wolfram.com/mathematica/tutorial/TheStandardEvaluationProcedure.html – Szabolcs Jun 24 '14 at 05:33
  • Take a look at this Q&A: http://mathematica.stackexchange.com/q/29339/131. – Yves Klett Jun 24 '14 at 05:34
  • http://reference.wolfram.com/mathematica/tutorial/EvaluationOfExpressionsOverview.html – Szabolcs Jun 24 '14 at 05:34
  • (I'm Eric) Is there a exception? For example, If I type Hold[{a+b,3*6}], Mathematica should still calculate a+b,3*6 first? – Eric Jun 24 '14 at 05:37
  • The point of Hold is that what is inside it does not get evaluated :) But once you release the hold it would be evaluated in the standard order as you describe. – mfvonh Jun 24 '14 at 05:39
  • According to the tutorial Szablocs gave, the tutorial said "In the standard evaluation procedure, Mathematica first evaluates the head of an expression, and then evaluates each element of the expressions." I'm confusing. I feel this just said #1. – Eric Jun 24 '14 at 05:45
  • 2
    It evaluates the head by itself to decide what to do with the whole expression. Try this: p = Plus; p[3^3, 6*9] // Trace. Notice how it evalutes p, then it evalutes what's inside, then it evaluates the whole expression including the head and the interior elements. That's how, for example, it knows not to evaluate the things inside Hold. – mfvonh Jun 24 '14 at 06:07
  • Now that I reread #1 I see I may have understood it differently than you meant. It "interprets" the head first, yes, but then it evaluates the interior expressions before evaluating the head (as applied to the interior expressions). – mfvonh Jun 24 '14 at 06:17
  • Thanks! Your example and explanation is very clear. I think I truly understand how it works now. Thanks again! – Eric Jun 24 '14 at 07:02
  • I have a new question about interpreting. If I type Plus[3, b] // traceViewCompact, it displays that Mathematica used three steps. But I type Plus[3, 4] // traceViewCompact, then it displays 4 steps(the expression of '7' appear twice). This is weird. I can't imagine why Mathematica do these similar works in different way. http://ppt.cc/pmq~ You can see when evaluated Plus[3, 4], Mathematica seemed to evalute by the order: 3, then 4, then get 7, then get the result 7. – Eric Jun 24 '14 at 12:54
  • I strongly suggest you download David Wagner's book from this link and read Chapter 7. – m_goldberg Jun 24 '14 at 12:56
  • OK, I will! Thanks. – Eric Jun 24 '14 at 13:01
  • I have checked the book, but I still have trouble understanding this. ps: I found the explanation in p.27(sec2.2) to be a similar but not the same thing I asked. So I ask for further explanation for this again, thanks!

    My question is why: Plus[2,3] //traceViewCompact Plus[2,b] //traceViewCompact turned out to be different steps evaluating? Namely 2->3->5->5(weird trivial evaluation) and 2,b,2+b

    – Eric Jun 26 '14 at 15:54

1 Answers1

1

Maybe this type of visualization will help understand the evaluation order better. Things are printed in InputForm here, but please "think" FullForm when you look at expressions.

In[2]:= On[]
Plus[3^3,6*9]
Off[]
During evaluation of In[2]:= On::trace: On[] --> Null. >>
During evaluation of In[2]:= Power::trace: 3^3 --> 27. >>
During evaluation of In[2]:= Times::trace: 6 9 --> 54. >>
During evaluation of In[2]:= Plus::trace: 3^3+6 9 --> 27+54. >>
During evaluation of In[2]:= Plus::trace: 27+54 --> 81. >>
Out[3]= 81

In[5]:= p=Plus
Out[5]= Plus

In[6]:= On[]
p[3^3,6 9]
Off[]
During evaluation of In[6]:= On::trace: On[] --> Null. >>
During evaluation of In[6]:= p::trace: p --> Plus. >>
During evaluation of In[6]:= Power::trace: 3^3 --> 27. >>
During evaluation of In[6]:= Times::trace: 6 9 --> 54. >>
During evaluation of In[6]:= Plus::trace: p[3^3,6 9] --> 27+54. >>
During evaluation of In[6]:= Plus::trace: 27+54 --> 81. >>
Out[7]= 81

This shows the evaluation order clearly and precisely. In the second example,

  1. evaluate p -> Plus
  2. 3^3 -> 27
  3. 6*9 -> 54
  4. p[3^3,6 9] -> Plus[27, 54]
  5. 27+54 -> 81

TracePrint shows the same information, but perhaps it's not quite as clear:

In[9]:= TracePrint[p[3^3,6 9]]
During evaluation of In[9]:=  p[3^3,6 9]
During evaluation of In[9]:=   p
During evaluation of In[9]:=   Plus
During evaluation of In[9]:=   (3^3)
During evaluation of In[9]:=    Power
During evaluation of In[9]:=    3
During evaluation of In[9]:=    3
During evaluation of In[9]:=   27
During evaluation of In[9]:=   6 9
During evaluation of In[9]:=    Times
During evaluation of In[9]:=    6
During evaluation of In[9]:=    9
During evaluation of In[9]:=   54
During evaluation of In[9]:=  27+54
During evaluation of In[9]:=  81
Out[9]= 81

It also mentions subexpressions that evaluate to themselves (i.e. don't evaluate), e.g. Times -> Times or 6 -> 6.

The evaluation sequence is documented in detail here:

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Thanks! But in your last paragraph, I still can't see why does TracePrint[a + b] prints a->b->a+b then not again a+b?(not like TracePrint[3+2]). Is it because there's no probability that can make a+b turn into another expression? I think I have been familiar with the evaluation order now. But that point confuse me~ – Eric Jun 27 '14 at 14:36
  • TracePrint[a + b] prints Plus[a,b] first because that's where it starts. Then, it takes each element of this expression one by one. Note that the next three lines it prints are indented by a space, meaning that these are part of the expression printed before them. Now it prints Plus, meaning Plus evaluates to itself a, meaning a evaluates to itself, b, meaning b evaluates to itself. Done. Any expression that doesn't change anymore is printed only once. Thus Plus, a, b and Plus[a,b] are all printed only once. – Szabolcs Jun 27 '14 at 14:45
  • Is it different from 3+2? TracePrint[3 + 2] prints Plus[3,2] first. Then it prints Plus, meaning Plus evaluates to itself, 3 meaning 3 evaluates to itself, 2, meaning 2 evaluates to itself. Then done. – Eric Jun 27 '14 at 15:00
  • @Eric yes, it's different. After the evaluator made sure that it's done with all subparts of 3+2 (i.e. Plus, 3, 2), it's still not yet done with 3+2 itself: it is at this point that it carries out the addition. There's an extra line saying 5 at the end. – Szabolcs Jun 27 '14 at 15:06
  • Oh, I suddenly realize it. The reason why Plus[a,b] and Plus[3,2] take different steps is: Plus[a,b] evaluate Plus first, then a and b, however, at the very time, by the rule defined for Plus, it doesn't have a rule corresponding to a symbol plus a symbol, so it keeps its form being Plus[a,b]. – Eric Jun 27 '14 at 15:43
  • On the other hand, it is not the same of Plus[3,2]. Mathematica kernel had already defined the rule for a number plus number, hence Plus[2,3] changes into 5, and Mathematica again treat 5 as a new expression(but it suddenly find it to be an atomic expression), so evaluate 5 to 5 again. End. So the critical point is Mathematica can't tell what should Plus[symbol,symbol] be. – Eric Jun 27 '14 at 15:44