Re: Advice on Compile

*To*: mathgroup at smc.vnet.net*Subject*: [mg112465] Re: Advice on Compile*From*: "Carl K. Woll" <carlw at wolfram.com>*Date*: Thu, 16 Sep 2010 06:00:59 -0400 (EDT)

On 9/15/2010 7:02 PM, Niels R. Walet wrote: > I am trying to write the following code (simplified, Mathematica 7.0.2) > setZu[subs_] := > Block[{Zmint}, Zmint = Evaluate[EFRp[q] /. subs]; Clear[Zmn]; > Zmn = Compile[{{k, _Real}}, > NIntegrate[Evaluate[Zmint], {q, 0, k}]];] > I call > setZu[EFRp -> Function[{q}, q^2/(1 + q^2)^2]] > and than > Zmn[1.] > gives an error (because Zmint has not been expanded). > Of course I can move the assigment on Zmint within the compile, but it > leads to ugly code, and I am not sure how efficient the compile is (does > it do the substitution every time, or is it "compiled out"?). In real > life the integrals I calculate are extremely expensive, and give me the > coefficients for an ODE I try to solve, so extreme efficiency would be > appreciated: I want to compile the expression with the substitution > done, not the substitution process itself. > Any light on this issue would be appreciated. > Niels > > You need to use something like With to get Zmint evaluated: setZu[subs_] := With[{Zmint = EFRp[q] /. subs}, Zmn = Compile[{{k, _Real}}, NIntegrate[Evaluate[Zmint], {q, 0, k}]];] However, for this particular integral, it would be worth considering doing something like: nint = f /. First@NDSolve[{f'[q] == q^2/(1 + q^2)^2, f[0] == 0}, f, {q, 0, 10}] nint[2.3]//Timing Zmn[2.3]//Timing Also, care to share what the real integrals you are trying to do numerically? Carl Woll Wolfram Research