1

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

Charlie Parker
  • 265
  • 3
  • 8
  • 1
    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. – mfvonh Aug 09 '15 at 20:49
  • 1
    You might also want to look at Hold, etc. See here. – mfvonh Aug 09 '15 at 20:52
  • 2
    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 – m_goldberg Aug 09 '15 at 21:21
  • @mfvonh so that means that the derivative is not actually done until I "call" the variable, right? – Charlie Parker Aug 10 '15 at 15:59
  • 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) – mfvonh Aug 10 '15 at 16:04
  • @m_goldberg you choose to hold this question. Please show me were I can easily find it in the documentation please. I'd find it super helpful, since I've been reading books and documentation like crazy for the past 3 days. Thanks in advance :)

    Btw, I also tried putting Unevaluated[dfd2] and it didn't quite work either. Not sure, if I still have a misconception. Is it just impossible to display D in the tree form of an expression?

    – Charlie Parker Aug 10 '15 at 16:54
  • I see. But even if I try to TreeForm[Unevaluated[d]] in your example, it doesn't show the derivative in the tree diagram :/ – Charlie Parker Aug 10 '15 at 16:55
  • Perhaps the behavior may not be intuitive, but reading the documentation for Unevaluated pays off. To quote, Unevaluated works only where it appears; it is not propagated:. On the other hand, Hold helps. Compare TreeForm[D[f,x]] and TreeForm[Hold[D[f,x]]]. Also, note the mention of Unevaluated suppressing evaluation when it's an argument to a function. Try also TreeForm[somehead[Unevaluated[D[f,x]]]] – LLlAMnYP Aug 10 '15 at 17:15
  • I am having a hard time understanding what you are trying to achieve. Please try TreeForm@Inactivate[D[f[a, {t1, t2, t3}, {c1, c2, c3}], c1], D] /. Inactive[D] -> "D". Is that what you want? – m_goldberg Aug 10 '15 at 17:35

1 Answers1

4

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.

Federico
  • 2,553
  • 16
  • 16