MathGroup Archive 2005

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

Search the Archive

Re: Speeding up simple Mathematica expressions?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63248] Re: [mg63232] Speeding up simple Mathematica expressions?
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Tue, 20 Dec 2005 23:35:33 -0500 (EST)
  • References: <200512200919.EAA28491@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 20 Dec 2005, at 18:19, AES wrote:

> I'd appreciate some practical advice on speeding up some simple  
> function
> evaluations.
>
> I'm evaluating a series of functions of which a typical example is
>
>   f[a_, x_] := Sum[
>                     Exp[-(Pi a)^2 n^2 -
>                              ((x - n Sqrt[1 -  (Pi^2 a^4)])/a)^2],
>                     {n, -Infinity, Infinity}];
>
> (The function is essentially a set of narrow gaussian peaks located  
> at x
> ? n Sqrt[1 - (Pi a^2)^2] ? n , with the peak amplitudes dropping off
> rapidly with increasing x.)
>
> Despite being a fairly simple function, this evaluates very slowly  
> on my
> iBook G4 -- takes a long time to make a plot of say  f[0.1, x] for   
> 0 <
> x < 3.  What can or should I do to speed this up?
>
> a)  If this were back in early FORTRAN days, I'd surely pull the  
> square
> root outside the sum -- do something like
>
>    f[a_, x_] := Module[{b},
>                      b=Sqrt[1 - (Pi a^2)^2];
>                      Sum[Exp[-(Pi a n)^2 -  ((x - n b)/a)^2];
>
> Is Mathematica smart enough to do that automatically, without the
> Module[] coding?  Is the added overhead of the Module[] small enough
> that it's worthwhile for me to do it?  Is there some other way to
> "compile" the function for a given value of a?
>
> b)  Since I mostly want just plots of the first two or three peaks,  
> and
> 1% accuracy should be fine, maybe I can cut the accuracy options in
> Plot[ ].  If so, how best to do this?  (I've not played with those
> somewhat confusing options before.)
>
> c)  Since the individual peaks have very little overlap for a < 0.2,
> maybe I can truncate the series to a small range of n?
>
> Obviously I can experiment with these and other approaches, but it's
> tedious.  If any gurus have suggestions on a good quick approach, I'll
> be glad to hear them.
>


There is one very obvious thing you could try. Use NSum instead of  
Sum.  Just compare these two timings:

f[a_, x_] := Sum[
                     Exp[-(Pi a)^2 n^2 -
                              ((x - n Sqrt[1 -  (Pi^2 a^4)])/a)^2],
                     {n, -Infinity, Infinity}];

g[a_, x_] := NSum[
                     Exp[-(Pi a)^2 n^2 -
                              ((x - n Sqrt[1 -  (Pi^2 a^4)])/a)^2],
                     {n, -Infinity, Infinity}];


f[0.2,0.3]//N//Timing


{0.396954 Second,0.105403}



g[0.2,0.3]//Timing


{0.009731 Second,0.105403}

Looking at the difference in timings and the accuracy of the results  
I do not see much need for any further ideas, do you?

Andrzej Kozlowski


  • Prev by Date: Re: Speeding up simple Mathematica expressions?
  • Next by Date: Re: Expression timing; a black art?
  • Previous by thread: Re: Speeding up simple Mathematica expressions?
  • Next by thread: Expression timing; a black art?