       RE: Re: Slow iteration in a functional program

• To: mathgroup at smc.vnet.net
• Subject: [mg35677] RE: [mg35642] Re: Slow iteration in a functional program
• From: "DrBob" <majort at cox-internet.com>
• Date: Thu, 25 Jul 2002 04:46:18 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Jens' solution will run into problems with \$IterationLimit, unless the
user also calculates values "bottom-up" before asking for values like
Phi:

Phi/@Range;Phi

Where Jens said: "will save the function values for P[n]" he meant G[n]
and Phi[n], not P[n], since he didn't include a modification of the
definition for P[n].

Phi is the only function that needs this treatment, actually; G and P
are not recursive, so saving their values will be a waste of space
(contrary to the solution I sent earlier).  That determination isn't
entirely trivial, so be careful in general.

If "xsec" or anything else might be left symbolic in the calculations,
you'll need to add Simplify to Phi's definition, and good luck even
then!

Also, there are typos in Matthew's original definition of Phi, so that
has to be resolved (we're only guessing where the brackets belong).

Finally, ListIntegrate requires a second argument, missing in Matthew's
post.

Bobby

-----Original Message-----
From: Jens-Peer Kuska [mailto:kuska at informatik.uni-leipzig.de]
To: mathgroup at smc.vnet.net
Subject: [mg35677] [mg35642] Re: Slow iteration in a functional program

Hi,

that is called dynamic programming and

Phi = Flux;
Phi{n_] :=Phi[n]= Phi[n-1] Exp[-(1-P[n-1])*xsec
G[n_] := G[n]=ListIntegrate[xsec Phi[n]]

will save the function values for P[n] from previous
computations.

Regards
Jens

Matthew Rosen wrote:
>
> Everyone,
>   I've been trying to recast an iterative calculation I do as a
> procedural program in C as an elegant functional program in
> Mathematica 4.1. The Mathematica code is much more transparent, but
> the resultant execution time is more than two orders of magnitude
> longer. Any suggestions would be greatly appreciated.The following is
> a schematic of the problem.
>
> There are three equations in the iteration variable, n:
>
>    G[n_] := ListIntegrate[xsec Phi[n]]      Both xsec and Phi[n] are
> 400 points long.
>
>    P[n_] := G[n]/(G[n]+(a constant)+D[n])   D[n] is a simple algebraic
> function of n.
>
>    Phi = Flux;                             Flux is 400 points long.
>    Phi{n_] := Phi[n-1] Exp[-(1-P[n-1])*xsec
>
> The goal is to evaluate P[n_] for an n around 1000. After running, I
> need to know all the values of P[n] and Phi[n] at each n from 1 to
> nmax. Note, P[n] is a number and Phi[n] is 400 points long.
>
> Currently,
>
> Timing[P] = 0.1 s
> Timing[P] = 0.2 s
> Timing[P] = 8.4 s.
>
> I dont dare try to evaluate P as I need to do. Every time I
> evaluate these functions they recalculate from scratch. I think I
> need to somehow tell Mathematica to save the intermediate values.
> Curious is that the calculation time is going up like n^2, not like n
> as I would have thought. The equivalent procedural c-code runs in
> less than 1 second to evaluate P.
>
> Thanks in advance for any guidance!
>
> -Matt Rosen
> --
> Matthew Rosen
> Harvard-Smithsonian Center for Astrophysics
> Mail Stop 59
> 60 Garden Street
> Cambridge, MA 02138
>
> e: mrosen at cfa.harvard.edu
> o: (617) 496-7614

```

• Prev by Date: Re: RE: Re: Q: Simplify with "much less"assumptions
• Next by Date: Re: Substitution!!!
• Previous by thread: Re: Slow iteration in a functional program
• Next by thread: Re: Slow iteration in a functional program