       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:= g[x_ + y_] := gp[x, y] ; g[x_ y_] := gm[x, y]

This shows the default ordering used for the definitions.

In:= DownValues[g]
Out= {HoldPattern[g[x_ + y_]] :> gp[x, y], HoldPattern[g[x_ y_]] :> gm[x,
y]}

This reverses the order of the definitions for g.

In:= DownValues[g] = Reverse[DownValues[g]]
Out= {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:= f[x] = u
Out= 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:= f[x] + f[y]
Out= u + f[y]

This defines a value for f with any expression as an "argument".
In:= f[x_] = x^2
Out= \!\(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:= f[x] + f[y]
Out= \!\(u + y\^2\)

----------------------------

If I duplicate this definition set, I can query the rules:

In:= ?f
Global`f
f[x] = u,
f[x_] = x\^2\

In:= DownValues[f]
Out= \!\({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:= DownValues[f] = Reverse[DownValues[f]]
Out= \!\({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:= DownValues[f]
Out= \!\({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

```

• Prev by Date: Inversion using Cholesky Decomposition
• Next by Date: RE: How to set y always greater than or equal to x for a function?
• Previous by thread: Re: Inversion using Cholesky Decomposition
• Next by thread: Re: Reordering Downvalues?