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: [mg63250] Re: [mg63232] Speeding up simple Mathematica expressions?
  • From: "Carl K. Woll" <carlw at wolfram.com>
  • Date: Tue, 20 Dec 2005 23:35:35 -0500 (EST)
  • References: <200512200919.EAA28491@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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}];
> 

Have you considered using NSum instead of Sum?

f[a_?NumericQ,x_?NumericQ]:=NSum[ etc. ]

works considerably faster.

Carl Woll
Wolfram Research

> (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.


  • Prev by Date: Re: Speeding up simple Mathematica expressions?
  • Next by Date: Re: Gray's Differential Geometry error?
  • Previous by thread: Speeding up simple Mathematica expressions?
  • Next by thread: Re: Speeding up simple Mathematica expressions?