Speeding up simple Mathematica expressions?
- To: mathgroup at smc.vnet.net
- Subject: [mg63232] Speeding up simple Mathematica expressions?
- From: AES <siegman at stanford.edu>
- Date: Tue, 20 Dec 2005 04:19:29 -0500 (EST)
- Organization: Stanford University
- Sender: owner-wri-mathgroup at wolfram.com
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.
- Follow-Ups:
- Re: Speeding up simple Mathematica expressions?
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Speeding up simple Mathematica expressions?
- From: "Carl K. Woll" <carlw@wolfram.com>
- Re: Speeding up simple Mathematica expressions?