Re: Compiled function with NIntegrate

*To*: mathgroup at smc.vnet.net*Subject*: [mg52579] Re: Compiled function with NIntegrate*From*: DrBob <drbob at bigfoot.com>*Date*: Fri, 3 Dec 2004 03:53:31 -0500 (EST)*References*: <200412020721.CAA05864@smc.vnet.net>*Reply-to*: drbob at bigfoot.com*Sender*: owner-wri-mathgroup at wolfram.com

The situation is a little different in version 5.1. f = Sin[x]; fc = Compile[{x}, f]; fc[1] gives these errors: CompiledFunction::"cfse" CompiledFunction::"cfex" Compiled expression `1` should be a `2`. External evaluation error at instruction `1`; proceeding with uncompiled \ evaluation. Yet NIntegrate works (despite throwing another error): NIntegrate[fc@x,{x,0,1}] CompiledFunction::"cfsa" 0.459698 Argument `1` at position `2` should be a `3`. Getting rid of the first two errors is easy enough: f=Sin[x]; fc=Compile[{x},Evaluate@f]; fc[1] 0.841471 But NIntegrate gives the same error again. (It still gets the right answer, too.) This gets rid of the final error, though: g[x_?NumericQ]:=fc@x NIntegrate[g@x,{x,0,1}] 0.459698 It was caused by a bad habit of some numerical procedures (like NIntegrate) to evaluate functions at symbolic arguments, perhaps (in this case) while attempting to Compile the integrand. NIntegrate really should suppress the error message, I think. Bobby On Thu, 2 Dec 2004 02:21:50 -0500 (EST), Alexei Akolzin <akolzine at uiuc.edu> wrote: > Hello, > > I wonder why NIntegrate gives the following error with compiled functions: > > In: f = Sin[x]; > In: fc = Compile[{x},f]; > > In: fc[1] > Out: 0.841471 > > In: NIntegrate[fc[x], {x, 0, 1}] > Out: CompiledFunction::"cfsa" : Argument x at position 1 should be a > machine-size real number. > or > Out: CompiledFunction::cfsa: Argument NIntegrate`Private`XX7 at position 1 > should be a machine-size real number. > > It is true that NIntegrate compiles functions by default. This was one of > the suggestions found in the archive if not to deal, but to get around the > problem. However, my particular function f takes around 10 minutes to > evaluate for a single argument value. I never had patience to wait long > enough (hours) for a result from NIntegrate to appear, with "Compiled" > option set or not. > > To speed things up I used optimization package "optimize.m": > > In: fc = Compile[{x},Optimize[f]]. > > This helped a lot, with fc[1] ("1", for example) being evaluated in a matter > of mere seconds. But the problem now is that NIntegrate does not want to > work with my function. > > So, the question is: is there any way to get my compiled function, as it is, > into NIntegrate? > > Thanks, > Alexei. > > > > -- DrBob at bigfoot.com www.eclecticdreams.net

**References**:**Compiled function with NIntegrate***From:*Alexei Akolzin <akolzine@uiuc.edu>