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: [mg63247] Re: Speeding up simple Mathematica expressions?
  • From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
  • Date: Tue, 20 Dec 2005 23:35:32 -0500 (EST)
  • Organization: The Open University, Milton Keynes, U.K.
  • References: <do8ioc$rvd$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"AES" <siegman at stanford.edu> a écrit dans le message de news: 
do8ioc$rvd$1 at smc.vnet.net...
| 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.
|
Hi,

As a starter, you should use *NSum* rather than *Sum* (I have got an 
increase by a factor three just by doing that):

In[1]:= f[a_, x_] := Sum[Exp[-(Pi a)^2 n^2 - ((x - n Sqrt[1 - (Pi^2 a^4)])/ 
a)^2], {n, -Infinity, Infinity}]
In[3]:= Plot[f[0.1, x], {x, 0, 3}] // Timing
Out[3]= {642.359 Second, \[SkeletonIndicator]Graphics\[SkeletonIndicator]}

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

In[5]:= Plot[g[0.1, x], {x, 0, 3}] // Timing
Out[5]= {181.031 Second, \[SkeletonIndicator Graphics\[SkeletonIndicator]}

Best regards,

/J.M.



  • Prev by Date: Re: Packages with Cyclic Dependencies
  • Next by Date: Re: Speeding up simple Mathematica expressions?
  • Previous by thread: Re: Speeding up simple Mathematica expressions?
  • Next by thread: Returning an empty sequence