Reordering Downvalues?
- To: mathgroup at smc.vnet.net
- Subject: [mg46589] Reordering Downvalues?
- From: "Fred Klingener" <gigabitbucket at brockeng.com>
- Date: Tue, 24 Feb 2004 21:04:46 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Book Section 2.5.13 (Advanced Topic: Manipulating Value Lists) walks
through an example in which two more-or-less independent functions are
defined in association with g.
------------------------------
In[3]:= g[x_ + y_] := gp[x, y] ; g[x_ y_] := gm[x, y]
This shows the default ordering used for the definitions.
In[4]:= DownValues[g]
Out[4]= {HoldPattern[g[x_ + y_]] :> gp[x, y], HoldPattern[g[x_ y_]] :> gm[x,
y]}
This reverses the order of the definitions for g.
In[5]:= DownValues[g] = Reverse[DownValues[g]]
Out[5]= {HoldPattern[g[x_ y_]] :> gm[x, y], HoldPattern[g[x_ + y_]] :> gp[x,
y]}
-----------------------------------
I'm able to duplicate the observations here, and I think I understand the
issues..
In a slightly different case from section 2.5.6 (Making Definitions for
Functions) a specific function and a general function are defined.
---------------------------------
In[1]:= f[x] = u
Out[1]= u
When the specific expression f[x] appears, it is replaced by u. Other
expressions of the form f[argument] are, however, not modified.
In[2]:= f[x] + f[y]
Out[2]= u + f[y]
This defines a value for f with any expression as an "argument".
In[3]:= f[x_] = x^2
Out[3]= \!\(x\^2\)
The old definition for the specific expression f[x] is still used, but the
new general definition for f[x_] is now used to find a value for f[y].
In[4]:= f[x] + f[y]
Out[4]= \!\(u + y\^2\)
----------------------------
If I duplicate this definition set, I can query the rules:
In[213]:= ?f
Global`f
f[x] = u,
f[x_] = x\^2\
In[223]:= DownValues[f]
Out[223]= \!\({HoldPattern[f[x]] :> u, HoldPattern[f[x_]] :> x\^2}\)
But if I attempt to Reverse[] the order using the same approach as that used
in 2.5.13 the result is different:
In[225]:= DownValues[f] = Reverse[DownValues[f]]
Out[225]= \!\({HoldPattern[f[x_]] :> x\^2, HoldPattern[f[x]] :> u}\)
Here the Out seems to confirm the reversal, but a direct check of the order
gives:
In[226]:= DownValues[f]
Out[226]= \!\({HoldPattern[f[x]] :> u, HoldPattern[f[x_]] :> x\^2}\)
and a test of f[x]+f[y], which gives u + y^2, confirms that the order is
unchanged. What's going on? What am I doing wrong? What don't I
understand?
TIA,
Fred Klingener
Mathematica 5.0.0, Examples from Help Browser