MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: RE: memoizing function again

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32583] RE: [mg32510] RE: [mg32495] memoizing function again
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 30 Jan 2002 03:18:59 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Erich,

got another, unconventional idea, 
perhaps you can use it:

In[15]:=
fibrules = {f[0] -> 1, f[1] -> 1, 
      f[n_] :> (f[n] = f[n - 1] + f[n - 2] /. fibrules)};


A recursive rule!

In[16]:= f[5] /. fibrules
Out[16]= 8

In[17]:= Information["f", LongForm -> False]
>From In[17]:= "Global`f"
>From In[17]:=
f[2] = 2
f[3] = 3
f[4] = 5
f[5] = 8

In[18]:= Clear[f]

You may even localize with Block

In[19]:= Block[{f}, f[10] /. fibrules]
Out[19]= 89

In[20]:= Information["f", LongForm -> False]
>From In[20]:= "Global`f"

This seems to be near your original intend
"throw away ... [definitions] with values between 
calculations"
--
Hartmut Wolf

> -----Original Message-----
> From: Wolf, Hartmut [mailto:Hartmut.Wolf at t-systems.com]
To: mathgroup at smc.vnet.net
> Sent: Thursday, January 24, 2002 11:21 AM
> Subject: [mg32583] [mg32510] RE: [mg32495] memoizing function again
> 
> 
> 
> > -----Original Message-----
> > From: Erich Neuwirth [mailto:erich.neuwirth at univie.ac.at]
To: mathgroup at smc.vnet.net
> > Sent: Wednesday, January 23, 2002 7:00 AM
> > Subject: [mg32583] [mg32510] [mg32495] memoizing function again
> > 
> > 
> > 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
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > --
> > Erich Neuwirth, Computer Supported Didactics Working Group
> > Visit our SunSITE at http://sunsite.univie.ac.at
> > Phone: +43-1-4277-38624 Fax: +43-1-4277-9386
> > 
> 
> Erich,
> 
> why not just Remove[f] and execute its definition again? 
> However you are free to do funny things:
> 
>  
> c1 = 0; c2 = 0;
> Remove[f];
> f[0] = 1; 
> f[1] = 1; 
> f[n_] := (++c1; 
>    Unevaluated[f[n] = Unevaluated[++c2; rhs]] /. 
>        rhs :> RuleCondition[f[n - 1] + f[n - 2]])
> 
> ?f
>  
> (c1 = 0; c2 = 0; {f[#], c1, c2}) & /@ Range[10, 1, -1]
> 
> ?f
> 
> Scan[Unset, Take[First /@ DownValues[f], {3, -2}]]
> 
> ?f
> 
> (c1 = 0; c2 = 0; {f[#], c1, c2}) & /@ Range[10]
> 
> ?f
> 
> 
> 
> To go into your question for the pattern, look
>  
> Remove[f];
> f[0] = 1; 
> f[1] = 1; 
> f[n_] := f[n] = f[n - 1] + f[n - 2]
> f[10]
> 
> ?f
> 
> Now you may reset the definitions for f: 
> 
> DownValues[f] = 
>   DeleteCases[DownValues[f], 
>     rule_ /; IntegerQ[rule[[1, 1, 1]]] 
>              && (rule[[1, 1, 1]] > 1) 
>              && IntegerQ[rule[[2]]]
>   ]
> 
> You must be careful as not to execute the recursive 
> definition for f when doing the test. This is achieved 
> here by the non-strict evaluation of And.
> 
> You also must be careful if you try to use names for parts
> of the rule:
>  
> DownValues[f] = 
> Select[DownValues[f], 
>   Apply[Function[{arg, rhs}, 
>           ! (IntegerQ[arg] && (arg > 1) && IntegerQ[rhs]), 
> {HoldAll}], 
>       Extract[#, {{1, 1, 1}, {2}}, Unevaluated]] &]
> 
> HoldAll and Unevaluated prevent the evaluation of rhs before 
> the test IntegerQ[arg] has been made. If you have a recursive 
> definition for an integer argument (not a pattern), which is 
> possible in principle -- e.g. in my first example, if I had
> not used RuleCondition -- then more work is needed. 
> 
> See for example
> 
> Map[
>   (Function[{rhs},
>            Replace[Unevaluated[rhs], 
>                 {_Integer -> False, _ -> True}],
>            {HoldAll}] @@ Extract[#, {2}, Hold] &)
>   , DownValues[f]]
> 
> or else
> 
> Function[{rhs}, 
>     Replace[Unevaluated[rhs], {_Integer -> False, _ -> True}], 
>     {HoldAll}] /@ 
> (Extract[#, {2}, Unevaluated] &) /@
>   DownValues[f]
> 
> 
> {False, False, False, False, False, False, False, False, 
>  False, False, False, True}
> 
> --
> Hartmut Wolf
> 
> 



  • Prev by Date: RE: Arbitrary selection of a particular cube root of a negative number
  • Next by Date: redefine Power[A_?MatrixQ,-1]
  • Previous by thread: Re: memoizing function again
  • Next by thread: Re: memoizing function again