       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:=
fibrules = {f -> 1, f -> 1,
f[n_] :> (f[n] = f[n - 1] + f[n - 2] /. fibrules)};

A recursive rule!

In:= f /. fibrules
Out= 8

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

In:= Clear[f]

You may even localize with Block

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

In:= Information["f", LongForm -> False]
>From In:= "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=DownValues[f]
> > produces
> >
> > Out={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
> >
> > we have
> >
> > In:=
> > DownValues[f]
> >
> > Out=
> > {HoldPattern[f]\[RuleDelayed]1,HoldPattern[f]\[RuleDelayed]1,
> >   HoldPattern[f]\[RuleDelayed]2,HoldPattern[f]\[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
> > 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 = 1;
> f = 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
>
> ?f
>
>
>
> To go into your question for the pattern, look
>
> Remove[f];
> f = 1;
> f = 1;
> f[n_] := f[n] = f[n - 1] + f[n - 2]
> f
>
> ?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[]]
>   ]
>
> 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