I'm trying to write a simulation using Mathematica 8.0. Since I will most likely be doing the same operation over and over again, I'm trying to Compile whatever I can. However, I've been having problems avoiding MainEvaluate calls when I define CompiledFunction objects which refer to global variables. A simplified sample of what I'm trying to use is:
G = 4.49*^3;
M = 1.;
S = 1.;
\[Epsilon] = 2.;
SAcceleration = Compile[{{SPosition, _Real, 1}}, (-G (M + S))/(SPosition.SPosition + \[Epsilon]*\[Epsilon])^(3/2) SPosition];
I have also tried wrapping the whole thing inside a Module, to no avail:
SAcceleration2 = Compile[{{SPosition, _Real, 1}}, Module[{GG = G, MM = M, SS = S, \[Epsilon]\[Epsilon] = \[Epsilon]^2}, (-GG (MM + SS))/(SPosition.SPosition + \[Epsilon]\Epsilon])^(3/2) SPosition]];
They seem to run just fine. However, when I take a look at what the CompiledFunction is trying to do internally using CompilePrint
<< CompiledFunctionTools`
CompilePrint[SAcceleration]
CompilePrint[SAcceleration2]
I get, respectively
1 R1 = MainEvaluate[ Function[{SPosition}, G][ T(R1)0]]
...
3 R1 = MainEvaluate[ Function[{SPosition}, M][ T(R1)0]]
4 R4 = MainEvaluate[ Function[{SPosition}, S][ T(R1)0]]
...
7 R6 = MainEvaluate[ Function[{SPosition}, [Epsilon]][ T(R1)0]]
8 R7 = MainEvaluate[ Function[{SPosition}, [Epsilon]][ T(R1)0]]
...
and
1 R1 = MainEvaluate[ Function[{SPosition}, G][ T(R1)0]]
2 R3 = MainEvaluate[ Function[{SPosition}, M][ T(R1)0]]
3 R4 = MainEvaluate[ Function[{SPosition}, S][ T(R1)0]]
4 R6 = MainEvaluate[ Function[{SPosition}, [Epsilon]][ T(R1)0]]
...
Since I will most likely be toying with different values for these variables in different simulation runs, but they will be held constant within a given run, can I avoid both MainEvaluate and feeding these variables to my function as additional arguments? Thanks in advance.
