Re: Speeding up simple Mathematica expressions?
- To: mathgroup at smc.vnet.net
- Subject: [mg63254] Re: Speeding up simple Mathematica expressions?
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Tue, 20 Dec 2005 23:35:38 -0500 (EST)
- Organization: The University of Western Australia
- References: <do8ioc$rvd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <do8ioc$rvd$1 at smc.vnet.net>, AES <siegman at stanford.edu> 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}]; Essentially, this question appeared in the In and Out column of The Mathematica Journal 9(4) under the heading "Sum of Gaussians". After completing the square of the exponent, such sums can be expressed in closed form in terms of EllipticTheta functions. See http://functions.wolfram.com/09.03.06.0001.01 For this particular example, f[a_, x_] = a Sqrt[Pi] E^(-(Pi a x)^2) * EllipticTheta[3, -Pi Sqrt[1 - a^4 Pi^2] x, E^(-(Pi a)^2)] > (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? Using the EllipticTheta form, evaluation is immediate. > 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? Indeed, this will also work fine for such parameter values. Cheers, Paul _______________________________________________________________________ Paul Abbott Phone: 61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul