MathGroup Archive 2002

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

Search the Archive

RE: memoizing function again

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32510] RE: [mg32495] memoizing function again
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Thu, 24 Jan 2002 05:21:04 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----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
> To: mathgroup at smc.vnet.net
> Subject: [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: path problem
  • Next by Date: Re: Magic Matrices
  • Previous by thread: Re: memoizing function again
  • Next by thread: Re: memoizing function again