Re: Compiled function

*To*: mathgroup at smc.vnet.net*Subject*: [mg93802] Re: Compiled function*From*: Michael Weyrauch <michael.weyrauch at gmx.de>*Date*: Wed, 26 Nov 2008 05:13:17 -0500 (EST)*References*: <gggqfq$320$1@smc.vnet.net>*Reply-to*: michael.weyrauch at gmx.de

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]/cxyxy; > 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]/cxyxy; > > 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 >