[Date Index]
[Thread Index]
[Author Index]
Re: Why does the order of down values come back?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg124898] Re: Why does the order of down values come back?
*From*: A Retey <awnl at gmx-topmail.de>
*Date*: Sat, 11 Feb 2012 06:37:15 -0500 (EST)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*References*: <jgo0e9$a2k$1@smc.vnet.net> <jgqpna$n12$1@smc.vnet.net>
Hi,
>> In[]:= f[0] := 0;f[1] := 1;f[n_] := f[n - 1] + f[n - 2]
>>
>> In[]:= DownValues[f]
>> Out[]:= {HoldPattern[f[0]] :> 0, HoldPattern[f[1]] :> 1,
>> HoldPattern[f[n_]] :> f[n - 1] + f[n - 2]}
>>
>> In[]:= DownValues[f] = Reverse[DownValues[f]]
>> Out[]:= {HoldPattern[f[n_]] :> f[n - 1] + f[n - 2], HoldPattern[f[1]] :>
>> 1, HoldPattern[f[0]] :> 0}
>>
>> In[]:= DownValues[f]
>> Out[]:= {HoldPattern[f[0]] :> 0, HoldPattern[f[1]] :> 1,
>> HoldPattern[f[n_]] :> f[n - 1] + f[n - 2]}
>>
>> ====================================================
>> My question is:
>>
>> Why does the order of down values comes back after reordering?
>>
>> Thanks.
>>
>
> Because DownValues sorts its output by default. The actual downvalues are
> not, however, sorted; the order is as you set them. To see their actual
> order, use the (undocumented) option:
>
> DownValues[f, Sort -> False]
>
I'm sure that you (Oleksandr) are aware of it, but I doubt the OP is:
there are actually two sorting activities involved:
1) whenever you add to or change the DownValues, they will be sorted so
that more specific patterns will be tried before more general ones. I
don't think there is any way to circumvent this, which probably is
because the pattern matcher does somehow rely on it.
2) when using DownValues to show what's there, the patterns are by
default not shown as stored, but are again sorted. This can be avoided
by using the Sort option. I guess this is supposed to make it easier to
compare lists of DownValues which would behave identically when used by
the pattern matcher. AFAIK this sorting will preserve the specific
before general rule.
In consequence, for your example
f[0] := 0;f[1] := 1;f[n_] := f[n - 1] + f[n - 2]
DownValues[f] = Reverse[DownValues[f]]
DownValues[f, Sort -> False]
will still list the most general pattern as last entry, despite the
Reverse. In cases where it's not possible to decide about which pattern
is more specific, it is the order in which you evaluate your definitions
that defines the order in which they are tried (and listed by
DownValues), compare e.g.:
ClearAll[g]
g[x_, 1] := 1
g[1, y_] := 2
g[1,1]
ClearAll[g]
g[1, y_] := 2
g[x_, 1] := 1
g[1,1]
hth,
albert
Prev by Date:
**Re: Baffled by FindMinimum**
Next by Date:
**Re: How to call 'Clear' from within a function?**
Previous by thread:
**Re: Why does the order of down values come back?**
Next by thread:
**Re: Why does the order of down values come back?**
| |