This is documentation for Mathematica 7, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Compile

Compile[{x1, x2, ...}, expr]
creates a compiled function which evaluates expr assuming numerical values of the xi.
Compile[{{x1, t1}, ...}, expr]
assumes that xi is of a type which matches ti.
Compile[{{x1, t1, n1}, ...}, expr]
assumes that xi is a rank ni array of objects each of a type which matches ti.
Compile[vars, expr, {{p1, pt1}, ...}]
assumes that subexpressions in expr which match pi are of types which match pti.
_Integermachine-size integer
_Realmachine-precision approximate real number (default)
_Complexmachine-precision approximate complex number
True | Falselogical variable
  • Nested lists given as input to a compiled function must be full arrays of numbers.
  • Compile handles numerical functions, matrix operations, procedural programming constructs, list manipulation functions, functional programming constructs, etc.
  • Compiled code does not handle numerical precision and local variables in the same way as ordinary Mathematica code.
  • If a compiled function cannot be evaluated with particular arguments using compiled code, ordinary Mathematica code is used instead.
  • Ordinary Mathematica code can be called from within compiled code. Results obtained from the Mathematica code are assumed to be approximate real numbers, unless specified otherwise by the third argument of Compile.
  • The number of times and the order in which objects are evaluated by Compile may be different from ordinary Mathematica code.
  • Compile has attribute HoldAll, and does not by default do any evaluation before compilation.
  • You can use Compile[..., Evaluate[expr]] to specify that expr should be evaluated symbolically before compilation.
Compile the function Sin[x]+x^2-1/(1-x) for machine real x:
The CompiledFunction evaluates with machine numbers:
Plot the compiled function:
Compile the function Sin[x]+x^2-1/(1-x) for machine real x:
In[1]:=
Click for copyable input
Out[1]=
The CompiledFunction evaluates with machine numbers:
In[2]:=
Click for copyable input
Out[2]=
Plot the compiled function:
In[3]:=
Click for copyable input
Out[3]=
Compile function to take Newton iterations for z^3-1 and identify nearest root:
Plot the basins of attraction for the three roots:
New in 2 | Last modified in 3