MathGroup Archive 2005

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

Search the Archive

Preserving definitions in dynamic programming?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59667] Preserving definitions in dynamic programming?
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Tue, 16 Aug 2005 05:39:32 -0400 (EDT)
  • Organization: The University of Western Australia
  • Sender: owner-wri-mathgroup at wolfram.com

Here is a question from a former student of mine, Peter Falloon (author 
of the SpheroidalHarmonics package that will be incorporated into a 
future version of Mathematica). 

The approach he outlines below seems quite elegant -- except that 
defining functions becomes more complicated -- but maybe that is the 
price you have to pay to take full advantage of dynamic programming. 

Comments appreciated.

________________________________________________________________________
I want to use dynamic programming in some function definitions, but I'd 
like to be able to Clear the dynamic values easily, but not erase the 
principal definitions. One way is to use "standard" dynamic programming 
and directly modify the DownValues. This is potentially dangerous 
however, so I was trying to find a "safer" approach. I ended up with the 
following idea (as a simple example):

  SetAttributes[dynamicDefinitions, HoldAll]

  f[x_] := dynamicDefinitions[f[x]] /.
                  dynamicDefinitions[f[xp_]] :>
                     (Print["evaluating for the first time..."];
                      Pause[2];
                      dynamicDefinitions[f[xp]] = xp^2)

  f[1]
  evaluating for the first time...

  1

  f[1]

  1

  ?f
  Global`f

  f[x_] := dynamicDefinitions[f[x]] /. dynamicDefinitions[f[xp_]] :>
     (Print["evaluating for the first time..."]; Pause[2];
      dynamicDefinitions[f[xp]] = xp^2)

  ?dynamicDefinitions
  Global`dynamicDefinitions

  Attributes[dynamicDefinitions] = {HoldAll}
  dynamicDefinitions[f[1]] = 1

  Clear[dynamicDefinitions]

  f[1]
  evaluating for the first time...

  1

In this approach, dynamically defined values are stored as DownValues of 
a special function, dynamicDefinitions, which I can then simply Clear 
when I want to remove all dynamic values, without affecting the 
principal definitions of f. This seems like a reasonable approach. But I 
was just wondering if there is maybe another, "standard" way of doing 
this that I am not aware of, or if you see a major flaw with this method 
which I can't see?

Cheers,
Peter

-- 
Paul Abbott                                      Phone: +61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul
        http://InternationalMathematicaSymposium.org/IMS2005/


  • Prev by Date: Re: Avoiding parentheses in Formatted or box structures
  • Next by Date: Re: Re: Some bugs in Mathematica
  • Previous by thread: Re: About Simplify
  • Next by thread: N-dimensional rotations