Re: Compile a Module
- To: mathgroup at smc.vnet.net
- Subject: [mg83191] Re: [mg83148] Compile a Module
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Wed, 14 Nov 2007 04:50:30 -0500 (EST)
- References: <11952552.1194997018464.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
The following may be of help:
Clear[brute, better]
brute[{tip_, end_, calc_}] := Module[{mid, r, theta, along, away},
mid = (tip + end)/2;
r = Sqrt[Dot[calc - mid, calc - mid]];
theta = VectorAngle[calc - mid, end - mid];
along = r Cos[theta];
away = r Sin[theta];
{along, away}]
brute[{tip,end,calc}]/.VectorAngle[a_,b_]:>ArcCos[a.b/Sqrt[a.a
b.b]]//Simplify;
%/.{-(tip/2)+calc-end/2->u,end->2v+tip}//Simplify//PowerExpand
{u.v/Sqrt[v.v],Sqrt[u.u] Sqrt[1-(u.v)^2/(u.u v.v)]}
better[{tip_,end_,calc_}]:=Module[{u=calc-(tip+end)/2,v=(end-tip)/2,w},w=u.v/Sqrt[v.v];{w, =
Sqrt[u.u-w^2]}]
It's not compiled, but it does cut computations to a minimum, or nearly so.
Bobby
On Tue, 13 Nov 2007 02:35:52 -0600, JOHN ERB <john_c_erb at prodigy.net>
wrote:
> Can the following module be compiled?
> It needs to be used 10^4 times in my program.
> It is used to calculate the distance parallel and perpendicular
> to the midpoint of 2D line segment with reference to a point of
> interest (calc).
> AlongAway[{tip_,end_,calc_}]:=Module[{mid,r,theta,along,away},
> mid=(tip+end)/2;
> r=Sqrt[Dot[calc-mid,calc-mid]];
> theta=VectorAngle[calc-mid,end-mid];
> along=r Cos[theta];
> away=r Sin[theta];
> {along,away}]
> AlongAway[{{-1, 0}, {1, 0}, {2, 1}}]
> (* answer is {2,1} *)
> (* the code below does not seem to work *)
> cAlongAway=Compile[{{tip,_Real},{end,_Real},{calc,_Real}},AlongAway]
> John C. Erb
>
>
--
DrMajorBob at bigfoot.com