Nasser
Nasser

Reputation: 13131

Mathematica: question on evaluation of expression

There was a question on mathgroup, and while I was looking at it, I noticed this thing, and I can't understand why, I thought some expert here would know.

When doing Dt [ x[1] ]

it gives zero, because during evaluation of x[1], the last value left is 1, as can be seen from the TracePrint below. And hence '1' is what is seen by Dt, and so Dt[1] is 0.

Hence Dt[ x[1] ] is zero

In[86]:= TracePrint[ Dt[x[1] ]]

During evaluation of In[86]:=  Dt[x[1]]
During evaluation of In[86]:=   Dt
During evaluation of In[86]:=   x[1]
During evaluation of In[86]:=    x
During evaluation of In[86]:=    1
During evaluation of In[86]:=  0

Out[86]= 0

That made sense to me, until I typed x[1], and got back x[1]

In[84]:= x[1] Out[84]= x[1]

But x[1] returning x[1] also made sense to me, since x[1] has no value, so it should return unevaluated.

So, my question, is why it seems that x[1] was evaluated all the way down to '1' during the call above, but at the top level notebook interface, it did not evaluate to 1?

In[87]:= Evaluate[ x[1] ]
Out[87]= x[1]

Thanks

Upvotes: 3

Views: 414

Answers (1)

Leonid Shifrin
Leonid Shifrin

Reputation: 22579

The expression

x[1]

does not evaluate to 1 - it is an indexed variable with undefined value. The problem is that when you use the form of Dt with 1 argument, then x is considered a function, and 1 - its argument, and you get 0. This becomes clearer when you consider

In[1]:= Dt[x[y]]

Out[1]= Dt[y] Derivative[1][x][y]

If you now use

In[2]:= Dt[x[1],x[1]]

Out[2]= 1

you get 1, since now you differentiate over x[1] considered as a variable. Or,

In[3]:= Dt[x[1]^2, x[1]]

Out[3]= 2 x[1]

You were confused by evaluation printout since indeed, when evaluating an expression, all parts are normally evaluated - but (in the absense of any rules for x), x[1] evaluates back to itself also inside Dt, to be sure. What you observed is related to how Dt with one argument interprets that argument.

Upvotes: 5

Related Questions