1
$\begingroup$

I was learning that set delay doesn't evaluate things until its calling time. Hence, I thought that something like the followng:

dfdt := D[f[t],t]

would not evaluate the derivative until dfdt was called. Let me show you what I tried:

enter image description here

as you can tell, the Head is not the derivative, as I would ave thought. Why is this? Is there a misconception that I have about how mathematica works?


It still doesn't work (display the Derivative in the tree even if I say "unevaluate"):

enter image description here

$\endgroup$
9
  • 1
    $\begingroup$ You are right that := delays evaluation, but when you submit the expression subsequently (as in In[65]), Mathematica "evaluates" it. Try this: ClearAll[f, d, x]; d := D[f, x]; then evaluate d. Now define f = Sin[x] then evaluate d again. Because of the delayed definition you can redefine f as many times as you would like and d will always resolve it. $\endgroup$
    – mfvonh
    Aug 9 '15 at 20:49
  • 1
    $\begingroup$ You might also want to look at Hold, etc. See here. $\endgroup$
    – mfvonh
    Aug 9 '15 at 20:52
  • 2
    $\begingroup$ Evaluation in Mathematica is complex. I strongly recommend you study Chapter 7 of Power Programming with Mathematica by David B. Wagner, which has a highly accessible discussion of evaluation. You can find out how to download it free by reading this question $\endgroup$
    – m_goldberg
    Aug 9 '15 at 21:21
  • $\begingroup$ @mfvonh so that means that the derivative is not actually done until I "call" the variable, right? $\endgroup$ Aug 10 '15 at 15:59
  • $\begingroup$ Yes. The definition dfdt keeps f as is in the RHS, then computes the derivative (and in doing so, resolves f at that exact moment) each time you "call" it (clearer to say "evaluate" in Mathematica) $\endgroup$
    – mfvonh
    Aug 10 '15 at 16:04
4
$\begingroup$

Assuming that you have defined

f[a_, cs_List, ts_List] := cs.Exp[-(ts - a)^2]

setting

dfd2 := D[f[a, {c1, c2, c3}, {t1, t2, t3}], t1]

defines dfd2 without taking into consideration the current value of f. You can inspect the definition of dfd2 by evaluating ?dfd2, which returns exactly what you typed above. Then, whenever dfd2 is evaluated, it looks for any definition associated with the symbol f at that moment in order to manipulate the expression. This means that dfd2 returns

2 c1 Exp[-(a - t1)^2] (a - t1)

You can compare this behavior with that of

immediate = D[f[a, {c1, c2, c3}, {t1, t2, t3}], t1]

Check the expression returned by ?immediate. You'll see that, while in the definition of dfd2 the expression f[___] is kept in an unevaluated form, in immediate the right hand side is simplified at definition time.

Among other things, this means that if you redefine f, as with Clear[f]; f[___] := 0, dfd2 will evaluate to 0, reflecting the change in f, while immediate will remain unmodified. This, again, is because f does not appear in the definition of immediate, which you can check through ?immediate.

$\endgroup$

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