Re: compile a numerical integral
- To: mathgroup at smc.vnet.net
- Subject: [mg124581] Re: compile a numerical integral
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 25 Jan 2012 07:07:21 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 1/24/12 at 5:07 AM, ruth.lazkoz at ehu.es (Ruth Lazkoz S=C3=A1ez) wrote: >I am working to make a code faster using compile and failed at the >very beginning. Is there a way to make a version that works? >f=Compile[{u},NIntegrate[x*u,{x,0.,#}]&/@{1,2,3}] Try, f = Compile[{{u, _Real}, {a, _Real}}, NIntegrate[u x, {x, 0, a}]] But, while this compiles, it would be very surprising if there were any measurable improvement in execution speed. Compile cannot optimize built-in functions. Calls to built-in functions already call compiled code. What Compile can do is Compile an Mathematica expression. That is: u*x could be compiled by Compile. But, it is very unlikely you will see any significant improvement for such a simple expression. You would almost certainly get better performance by doing In[2]:= int = Integrate[u x, {x, 0, a}] Out[2]= (a^2*u)/2 and then supplying numeric values for u,a. or by doing: g = Compile[{{u, _Real}, {a, _Real}}, Evaluate[Integrate[u x, {x, 0, a}]]] That is telling Mathematica to evaluate the integral symbolically then compile the result.