Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8124 > unrolled thread

compile a numerical integral

Started byRuth Lazkoz Sáez <ruth.lazkoz@ehu.es>
First post2012-01-24 10:09 +0000
Last post2012-01-26 08:25 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  compile a numerical integral Ruth Lazkoz Sáez <ruth.lazkoz@ehu.es> - 2012-01-24 10:09 +0000
    Re: compile a numerical integral psycho_dad <s.nesseris@gmail.com> - 2012-01-25 12:01 +0000
    Re: compile a numerical integral danl@wolfram.com - 2012-01-25 12:03 +0000
    Re: compile a numerical integral "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-25 12:09 +0000
    Re: compile a numerical integral Gabriel Landi <gtlandi@gmail.com> - 2012-01-26 08:25 +0000

#8124 — compile a numerical integral

FromRuth Lazkoz Sáez <ruth.lazkoz@ehu.es>
Date2012-01-24 10:09 +0000
Subjectcompile a numerical integral
Message-ID<jflvvs$jr4$1@smc.vnet.net>
Hi wise people,

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}]

Best,

Ruth Lazkoz

[toc] | [next] | [standalone]


#8134

Frompsycho_dad <s.nesseris@gmail.com>
Date2012-01-25 12:01 +0000
Message-ID<jfoqu4$5o2$1@smc.vnet.net>
In reply to#8124
Hi Ruth,
According to the documentation, NIntegrate already uses compile, see
tutorial/CompilingMathematicaExpressions

Quoting:
"You should realize that built-in Mathematica functions quite often themselves use Compile. Thus, for example, NIntegrate by default automatically uses Compile on the expression you tell it to integrate."

Actually, you can turn it on by just using Compiled->True, eg (I modified one of the NIntegrate examples in help):

g[x_?NumericQ] := Nest[Sin[# + Sin[2 #]] &, x, 20]
NIntegrate[g[x], {x, 0, 1}, Compiled -> True] // AbsoluteTiming
NIntegrate[g[x], {x, 0, 1}, Compiled -> False] // AbsoluteTiming

I hope it helps.

Cheers,
Savvas

[toc] | [prev] | [next] | [standalone]


#8138

Fromdanl@wolfram.com
Date2012-01-25 12:03 +0000
Message-ID<jfor1v$5ps$1@smc.vnet.net>
In reply to#8124
This variant seems to work in version 8.

f2 = Compile[{u}, Table[NIntegrate[x*u, {x, 0., j}], {j,{1,2,3}}]]

Your version appears to run without a hitch in version 7 (see below).

In[5]:= f = Compile[{u}, NIntegrate[x*u, {x,0.,#}]& /@ {1,2,3}]

Out[5]= CompiledFunction[{u}, (NIntegrate[x u, {x, 0., #1}] & ) /@ {1, 2, 3}, 
 
>    -CompiledCode-]

In[6]:= f[1.1]

Out[6]= {0.55, 2.2, 4.95}

So this may indicate a bug in version 8.

[toc] | [prev] | [next] | [standalone]


#8153

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2012-01-25 12:09 +0000
Message-ID<jfordg$616$1@smc.vnet.net>
In reply to#8124
On Tue, 24 Jan 2012 10:09:00 -0000, Ruth Lazkoz S=E1ez <ruth.lazkoz@ehu.es>  
wrote:

> Hi wise people,
>
> 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}]
>
> Best,
>
> Ruth Lazkoz
>

Sorry to disappoint, but no. NIntegrate cannot be compiled and, even if it  
could, it wouldn't provide any meaningful performance improvement  
(although sometimes the integrand can be compiled; if this is likely to be  
beneficial it will be done automatically, but you can try it manually  
using the option Compiled->True). However, one can often benefit by  
changing the Method option appropriately (see  
tutorial/NIntegrateIntegrationRules), although this can be quite involved  
and is obviously not a panacea. It is worth persisting, however: in an  
example from my own work, optimizing the Method yielded a fourfold  
reduction in calculation time versus the default setting when evaluating
some complicated projection integrals, and I was only able to improve on
this slightly (by another 40% or so) by pre-calculating the integration 
bounds analytically for special cases of the integrand instead of letting  
NIntegrate find the bounds itself.

[toc] | [prev] | [next] | [standalone]


#8177

FromGabriel Landi <gtlandi@gmail.com>
Date2012-01-26 08:25 +0000
Message-ID<jfr2mi$nb4$1@smc.vnet.net>
In reply to#8124
You may profit considerably from "SymbolicProcessing"->0. For instance

In[106]:= tab1 = Table[
    NIntegrate[x^n Exp[x], {x, -1, 1}], {n, 2, 40, 2}]; // AbsoluteTiming

Out[106]= {0.282903, Null}

In[116]:= tab2 = Table[NIntegrate[x^n Exp[x], {x, -1, 1}, Method -> {"GlobalAdaptive"}     ], {n, 2, 40, 2}]; // AbsoluteTiming

Out[116]= {0.273898, Null}

In[117]:= tab3 = Table[
    NIntegrate[x^n Exp[x], {x, -1, 1},
     Method -> {"GlobalAdaptive", "SymbolicProcessing" -> 0}
     ], {n, 2, 40, 2}]; // AbsoluteTiming

Out[117]= {0.069791, Null}

And

In[114]:= tab1 - tab2
Out[114]= {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}

In[115]:= tab1 - tab3
Out[115]= {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}

Best regards,

Gabriel

On Jan 25, 2012, at 10:05 AM, Oleksandr Rasputinov wrote:

> On Tue, 24 Jan 2012 10:09:00 -0000, Ruth Lazkoz S=E1ez <ruth.lazkoz@ehu.es> 
> wrote:
>
>> Hi wise people,
>>
>> 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}]
>>
>> Best,
>>
>> Ruth Lazkoz
>>
>
> Sorry to disappoint, but no. NIntegrate cannot be compiled and, even if it 
> could, it wouldn't provide any meaningful performance improvement 
> (although sometimes the integrand can be compiled; if this is likely to be 
> beneficial it will be done automatically, but you can try it manually 
> using the option Compiled->True). However, one can often benefit by 
> changing the Method option appropriately (see 
> tutorial/NIntegrateIntegrationRules), although this can be quite involved 
> and is obviously not a panacea. It is worth persisting, however: in an 
> example from my own work, optimizing the Method yielded a fourfold 
> reduction in calculation time versus the default setting when evaluating
> some complicated projection integrals, and I was only able to improve on
> this slightly (by another 40% or so) by pre-calculating the integration
> bounds analytically for special cases of the integrand instead of letting 
> NIntegrate find the bounds itself.
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web