This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / New in Version 3.0 / Programming  /
Compile

  • Compile[ , , ... , expr ] creates a compiled function which evaluates expr assuming numerical values of the .
  • Compile[ , , ... , expr ] assumes that is of a type which matches .
  • Compile[ , , , ... , expr ] assumes that is a rank array of objects each of a type which matches .
  • Compile[ vars , expr , , , ... ] assumes that subexpressions in expr which match are of types which match .
  • The types handled by Compile are:
  • 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.
  • Compile generates a CompiledFunction object.
  • 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.
  • See the Mathematica book: Section 1.11.4Section 2.5.14.
  • See also: Dispatch, Function, InterpolatingFunction, CForm.

    Further Examples

    This is an iteration of times starting with a value of .

    In[1]:=

    Out[1]=

    Compiling this procedure speeds it up considerably, especially for a large number of iterations.

    In[2]:=

    In[3]:=

    Out[3]=

    In[4]:=

    This compiles an expression that performs an operation on a matrix.

    In[5]:=

    Out[5]=

    The list operations are now carried out in a compiled way, and the result is an integer.

    In[6]:=

    Out[6]=