RE: Re: A faster alternative to ListIntegrate?

*To*: mathgroup at smc.vnet.net*Subject*: [mg35743] RE: [mg35721] Re: A faster alternative to ListIntegrate?*From*: "DrBob" <majort at cox-internet.com>*Date*: Sun, 28 Jul 2002 03:32:28 -0400 (EDT)*Reply-to*: <drbob at bigfoot.com>*Sender*: owner-wri-mathgroup at wolfram.com

We can do even better, by using a Dot product to replace whatever order of Interpolation we choose, as follows. We could compute the weight vector from theory, but I'll just discover it by differentiation -- particularly useful if the step-size isn't constant. For 3rd-order, the weight vector is 'wts2' below. If we want an even higher (or lower) order fit, replace ListIntegrate with Integrate and Interpolation as before. e[i_, n_] := Array[{0, Boole[i + 1 == #]} &, n] d[i_, data_] := ListIntegrate[data + e[i, Length[ data]]] - ListIntegrate[data] wts2 = d[#, data] & /@ Range[0, n]; Bobby -----Original Message----- From: DrBob [mailto:majort at cox-internet.com] To: mathgroup at smc.vnet.net Subject: [mg35743] RE: [mg35721] Re: A faster alternative to ListIntegrate? The trapezoidal rule is equivalent to an appropriate Dot product. Here are four methods compared: ListIntegrate, Integrate[Interpolation] with interpolation order descending from 3 to 1, and a Dot product. The weight vector for the Dot product can be pre-computed, so this will save a LOT of time. For the random data below there's no significant difference in accuracy, but high-order interpolation may be important for other data. I'd compare answers for the Dot product and third-order interpolation, and then decide if the difference is worth the extra time. (I doubt it.) << NumericalMath`ListIntegrate` n = 100000; h = 1/n; data = Transpose[{Range[0, n]/n, Random[] & /@ Range[0, n]}]; ListIntegrate[data] // Timing Integrate[Interpolation[data, InterpolationOrder -> 3][ x], {x, 0, 1}] // Timing Integrate[Interpolation[data, InterpolationOrder -> 2][x], {x, 0, 1}] // \ Timing Integrate[Interpolation[data, InterpolationOrder -> 1][x], {x, 0, 1}] // \ Timing wts = {h/2}~Join~(h & /@ Range[n - 1])~Join~{h/2}; wts.data[[All, 2]] // Timing {2.6559999999999997*Second, 0.49906638684786364} {2.6719999999999997*Second, 0.49906638684786364} {2.187000000000001*Second, 0.49887608638890346} {1.8130000000000006*Second, 0.4990676925038021} {0.10999999999999943*Second, 0.4990724913628793} Bobby Treat -----Original Message----- From: Allan Hayes [mailto:hay at haystack.demon.co.uk] To: mathgroup at smc.vnet.net Subject: [mg35743] [mg35721] Re: A faster alternative to ListIntegrate? Mathew, Some possibilities <<NumericalMath`ListIntegrate` ListIntegrate[data]//Timing {6.59 Second,13.7681} The following is suggested in the Help Browser entry for the package Integrate[ Interpolation[data, InterpolationOrder\[Rule]1][x], {x,0,100}]//Timing {4.56 Second,13.768} Trapezium rule with equal steps: #[[1]]+#[[-1]]+ 2 Tr[Take[#,{2,-2}]]&[data[[All,2]]] 0.01/2//Timing {0.22 Second,13.768} Trapezium rule with possibly unequal steps (Drop[#1,1] - Drop[#1,-1]).(Drop[#2,-1] + Drop[#2,1])&[ data[[All,1]], data[[All,2]]]/2//Timing {0.83 Second,13.768} -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Matthew Rosen" <mrosen at cfa.harvard.edu> wrote in message news:ahr122$l2v$1 at smc.vnet.net... > Hi Everyone; > I've tracked down the slow operation of my Mathematica simulation code to > lie in the ListIntegrate command: > > G[n_] := ListIntegrate[xsec Phi[n], 1] > > where both xsec and Phi[n] are 400 values long. > > Is there a way to speed up ListIntegrate via Compile or a similar technique? > > Thanks in advance and best regards, > > Matt > --- > Matthew Rosen > Harvard-Smithsonian Center for Astrophysics > Mail Stop 59 > 60 Garden Street > Cambridge, MA 02138 > > e: mrosen at cfa.harvard.edu > o: (617) 496-7614 >