Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compiled function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg93909] Re: Compiled function
  • From: Michael Weyrauch <michael.weyrauch at gmx.de>
  • Date: Fri, 28 Nov 2008 05:08:53 -0500 (EST)
  • References: <gggqfq$320$1@smc.vnet.net> <ggj7fo$j5d$1@smc.vnet.net> <gglsoj$8c4$1@smc.vnet.net>
  • Reply-to: michael.weyrauch at gmx.de

Hello,

   just look at your variable "front" using InputForm

front//InputForm

and you will see the result of the compilation, which contains
lots of

{54, Function[{r, dt, \[Theta]}, derData[x$, dphi$]],....}

elements, which indicates (to me) an external call.

For me a well compiled code only contains numbers, True or False.
In my experience, if that is the case, compiled code often runs 
significantly faster than the uncompiled counterpart. If there are
external calls, compiled code may even be slower...

(But this is from my personal cook book, supported by some limited 
experience; unfortunately "official" rules are not published.
So, take it with caution...)

Michael




SigmundV schrieb:
> Hello,
> 
> Thank you for the answer, Michael. How can you conclude that the code
> does not compile fully in either case? I used the With construct so
> that I would not have to change the Module each time I wanted a new
> function fa. I also agree that Compile is scarcely documented. Perhaps
> someone can tell us the reason for this.
> 
> /Sigmund
> 
> On Nov 26, 11:13 am, Michael Weyrauch <michael.weyra... at gmx.de> wrote:
>> Hello,
>>
>>    if you look at the compiled code, you will see, that it does not
>> fully compile in both cases. It's only somewhat worse in case of
>> fa with Piecewice[], since expressions with the Head Piecewise cannot be
>> compiled anyway, since compiled code only supports real, complex,
>> integer and a few other basic data.
>>
>> I suggest to rewrite the code using IF[] or other Mathematica
>> constructs, which can be compiled easily.
>>
>> (Writing code that compiles properly in Mathematica is more an art than
>> a science since Compile[ ] is really poorly documented. I hope this will
>> change in the (distant?) future...)
>>
>> Michael
>>
>> SigmundV schrieb:
>>
>>> Dear group,
>>> Consider the following:
>>> derData[data_, h_] := (Drop[data, 1] - Drop[data, -1])/h;
>>> front = With[
>>>    {fa =
>>>      Function[{x, y}, Piecewise[{{0, 2 <= x <= 3 && 2 <= y =
> <= 3}}, 1],
>>> Listable],
>>>     fc = Function[{x, y}, (x y + 10)^0]},
>>>    Compile[{{r, _Real, 2}, {dt, _Real}, {\[Theta], _Real}},
>>>     Module[{x, y, a, b, c, C = Cos[\[Theta]], S = Sin[\[Theta]]=
> , da,
>>>       db, dc, dx, dy, cxy, xy, cxyxy, xnew, ynew,
>>>       dphi = 2 \[Pi]/1000},
>>>      x = r[[All, 1]]; y = r[[All, 2]];
>>>      dx = derData[x, dphi]; dx = Join[dx, {dx[[1]]}];
>>>      dy = derData[y, dphi]; dy = Join[dy, {dy[[1]]}];
>>>      a = fa[x, y];
>>>      b = 2 a; c = fc[x, y];
>>>      da = derData[a, dphi]; da = Join[da, {da[[1]]}]; db = =
> 2 da;
>>>      dc = derData[c, dphi]; dc = Join[dc, {dc[[1]]}];
>>>      cxy = dc dt + S dx + C dy; xy = C dx - S dy;
>>>      cxyxy = a^2 cxy^2 + b^2 xy^2;
>>>      xnew =
>>>       x + dt c S + dt^2 (-a^2 b db (dc dt S + dx))/cxyxy +
>>>        dt (a^2 C cxy - b^2 S xy) Sqrt[cxyxy - a^2 db^2 dt^2]/cx=
> yxy;
>>>      ynew =
>>>       y + dt c C + dt^2 (-a^2 b db (dc dt C + dy))/cxyxy -
>>>        dt (a^2 S cxy + b^2 C xy) Sqrt[cxyxy - a^2 db^2 dt^2]/cx=
> yxy;
>>>      Transpose[{xnew, ynew}]
>>>      ]
>>>     ]
>>>    ];
>>> As you see, fa is a piecewise function, but the module won't compile.
>>> However, when fa is a continuous function, like Exp[-(x^2 + y^2)], the
>>> module compiles without problems. Can anyone shed some light on this?
>>> Why does the compilation work in the latter case, but not in the
>>> first?
>>> King regards,
>>> Sigmund Vestergaard
> 
> 


  • Prev by Date: Re: CUDA support in 7.0?
  • Next by Date: Re: v.7.0 issues
  • Previous by thread: Re: Compiled function
  • Next by thread: Solve and parametric nonlinear equations