I'm using the Compile function in Mathematica to compute say the following :
f=Compile[{x,_Real},Cos[x]]
Then I can compute f[x] for x taking any real value. Ok, let's say now that I have an expression like tmp=Cos[x].
My question is the following: Is there a way that I can compile the expression assigned to tmp without explicitly typing "Cos[x]" in the Compile function ? I.e. I want to do something like :
f=Compile[{x,_Real},tmp]
But that won't work it seems. In principle I'd like the value assigned to tmp to be a somewhat huge and complicated symbolic function of x that I would like to compile for numerical efficiency.
Answer
compiler[fun_] := Compile[{{x, _Real}}, fun]
compiler[Sin[x] + x^2 - 1/(1 + x)]

cf = compiler[Cos[x]];
cf /@ Range[-3., 3., 6/12.]
Comments
Post a Comment