Re: dynamic programming inside a function
- To: mathgroup at smc.vnet.net
- Subject: [mg40482] Re: dynamic programming inside a function
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Mon, 7 Apr 2003 04:56:04 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <b6j9it$63n$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
test[f_, vars_List] := Module[{v, vPat}, Clear[temp];
vPat = Sequence @@ (Pattern[#, Blank[]] & /@ vars);
v = Sequence @@ vars;
temp[vPat] := Evaluate[Blub[temp[v], f[v]]];
DownValues[temp] = DownValues[temp] /. Blub -> Set
]
Regards
Jens
Ken Sale wrote:
>
> Hi MathGroup,
>
> I'm trying to figure out how to create a function that remembers its
> values inside a function definition (I want the temporary function to
> stay hidden in a package eventually. I've tried the following
>
> test[f_, vars_List] :=
> Module[{v, vPat},
> Clear[temp];
> vPat = Sequence @@ (Pattern[#, Blank[]] & /@ vars);
> v = Sequence @@ vars;
> temp[vPat] := temp[v] = f[v];
> ]
>
> When I evaluate
> test[Sin, {x}]
>
> I find that the definition of temp isn't what I want
>
> ?temp
>
> temp[x_] := temp[v$97] = Sin[v$97]
>
> What I'd like to get instead is
>
> temp[x_] := temp[x] = Sin[x]
>
> I've tried putting ReleaseHold around various parts of the assignment
> but so far haven't found a way to do what I want.
>
> Any help will be appreciated.
>
> Thanks,
> Ken
> --
> Ken Sale
> Group Leader / Physicist
> Radiation Technology Group
> Lawrence Livermore National Laboratory
> (925) 423-0686