Re: memoizing function again
- To: mathgroup at smc.vnet.net
- Subject: [mg32501] Re: [mg32495] memoizing function again
- From: BobHanlon at aol.com
- Date: Thu, 24 Jan 2002 05:20:54 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 1/23/02 5:22:08 AM, erich.neuwirth at univie.ac.at writes: >i have the following function > >f[x_] /; x <= 2 := f[x] = 1 >f[x_] /; x > 2 := f[x] = f[x - 1] + f[x - 2] > > >it remembers what it already calculated >i want to be able to throw away rules with values >between calculations > >In[3]=DownValues[f] >produces > >Out[3]={HoldPattern[f[x_]/;x\[LessEqual]2]\[RuleDelayed](f[x]=1), > HoldPattern[f[x_]/;x>2]\[RuleDelayed](f[x]=f[x-1]+f[x-2])} > > >after f[4] > >we have > >In[5]:= >DownValues[f] > >Out[5]= >{HoldPattern[f[1]]\[RuleDelayed]1,HoldPattern[f[2]]\[RuleDelayed]1, > HoldPattern[f[3]]\[RuleDelayed]2,HoldPattern[f[4]]\[RuleDelayed]3, > HoldPattern[f[x_]/;x\[LessEqual]2]\[RuleDelayed](f[x]=1), > HoldPattern[f[x_]/;x>2]\[RuleDelayed](f[x]=f[x-1]+f[x-2])} > > >\[RuleDelayed] is ascii for :>, i think > >so > >DownValues[f] = Take[DownValues[f], -2] > >removes all the rules giving calculated values >but i would like to throw away the rules following the pattern > >HoldPattern[f[x_Integer]:>y_Integer > >but i have not been able wo write an expression using Cases and a >pattern >which gets rid of the rules i want to get rid of > DownValues[f] = Select[DownValues[f], !FreeQ[#, x]&] or DownValues[f] = Cases[DownValues[f], _?(!FreeQ[#, x]&)] or DownValues[f] = DeleteCases[DownValues[f], _?(FreeQ[#, x]&)] Bob Hanlon Chantilly, VA USA