MathGroup Archive 2005

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

Search the Archive

Speeding up simple Mathematica expressions?

  • To: mathgroup at
  • Subject: [mg63232] Speeding up simple Mathematica expressions?
  • From: AES <siegman at>
  • Date: Tue, 20 Dec 2005 04:19:29 -0500 (EST)
  • Organization: Stanford University
  • Sender: owner-wri-mathgroup at

I'd appreciate some practical advice on speeding up some simple function 

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.

  • Prev by Date: Re: Re: Cross results?
  • Next by Date: Re: Packages with Cyclic Dependencies
  • Previous by thread: Visualization site updates - string theory and photons
  • Next by thread: Re: Speeding up simple Mathematica expressions?