Re: Derivative
- To: MathGroup at yoda.physics.unc.edu
- Subject: Re: Derivative
- From: villegas
- Date: Thu, 30 Jul 92 18:30:46 CDT
Hi Trott,
Concerning the outcome of:
Derivative[n_][F]:=FU[n]
Derivative[n_][G][x_]:=GU[n][x]
{F''[y], G''[y] }
We can see what happens if we freeze the expressions F''[y] and G''[y]
immediately after Mathematica parses them but before it starts to evaluate them
in the normal manner. We define a general function to tell us how Mathematica
understands an expression we type in as input:
Attributes[ParsedForm] = HoldFirst
ParsedForm[expr_] := FullForm[expr] //HoldForm
FullForm is a display format for an expression; it causes the internal stored
form of an expression to be displayed in full. HoldForm prevents 'expr'
(whatever it happens to be) from being evaluated, but the FullForm, being simply
a display formatter, still takes effect inside HoldForm.
Here is an excerpt of a Mathematica session, with comments:
_________________________________________________________________________
In[1]:= Attributes[ParsedForm] = HoldFirst
Out[1]= HoldFirst
In[2]:= ParsedForm[expr_] := FullForm[expr] //HoldForm
In[3]:= Derivative[n_][F]:=FU[n]
In[4]:= Derivative[n_][G][x_]:=GU[n][x]
The following should tell us how any second derivative, written in the
"double-prime" form that we used, will be understood:
In[5]:= ParsedForm[ H''[x] ]
Out[5]= Derivative[1][Derivative[1][H]][x]
As expected, using F and G for H gives the same form:
In[6]:= ParsedForm[ F''[x] ]
Out[6]= Derivative[1][Derivative[1][F]][x]
In[7]:= ParsedForm[ G''[x] ]
Out[7]= Derivative[1][Derivative[1][G]][x]
So the special-input form H'' stands for the first derivative of the first
derivative of H, not the second derivative straight off. Now that we know what
expressions the Mathematica evaluator starts with (the parsing stage being
complete), we can use some general principles of evaluation of expressions:
first evaluate the head and then the elements in turn.
The head of the parsed form of F''[x] shown above is
Derivative[1][Derivative[1][F]],
which is itself an expression with head Derivative[1] and element
Derivative[1][F]. The element Derivative[1][F] can be evaluated because we told
the kernel what to do:
In[8]:= Derivative[1][F]
Out[8]= FU[1]
Then it applies the head Derivative[1] to this and gets:
In[9]:= Derivative[1][%]
Out[9]= (FU[1])'
This is applied to the symbol 'x' to give the final result of the evaluation.
The evaluation of the parsed form of G proceeds differently. It starts the same
as F's, with the head:
Derivative[1][Derivative[1][G]]
This time the element Derivative[1][G] can _not_ be evaluated, because we
provided no rule for the derivative operator acting on G unless there is an
argument present as well. That's where the different assignments for F and G
comes in, and we get:
In[10]:= Derivative[1][Derivative[1][G]]
Out[10]= G''
In[11]:= FullForm[%]
Out[11]//FullForm= Derivative[2][G]
When this acts on x, the rule for G comes into play:
In[12]:= %[x]
Out[12]= GU[2][x]
So the result is that we get:
In[13]:= {F''[x], G''[x]}
Out[13]= {(FU[1])'[x], GU[2][x]}
Robby Villegas