MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: compiling problem

  • To: mathgroup at
  • Subject: [mg18956] RE: [mg18933] compiling problem
  • From: "Ersek, Ted R" <ErsekTR at>
  • Date: Fri, 30 Jul 1999 01:33:46 -0400
  • Sender: owner-wri-mathgroup at

Kiran Shrestha  wrote:
Can anybody teach me how to compile a function of several variables which
are values of other functions. For example, I have a function           

f[x_, y_]:= alpha[x] + alpha[y] - beta[x]/beta[y].

The alpha and beta function values are known. 


I think you need to include the calculation of alpha and beta in the
CompiledFunction.  For example suppose alpah and beta are defined as:

alpha[a_]:=x Exp[-x];
f[x_, y_]:= alpha[x] + alpha[y] - beta[x]/beta[y]

Then I would use Compiled as below.  

It's a good idea to make sure the Compiled function is translated into
"op-code" by evaluating  f[[-2]].  If f[[-2]] returns a list of lists where
each sublist is a list of integers all is well.  Any portions represented
otherwise probably couldn't be converted to op-code, and the compiled
function will probably not run very fast.

f=Compile[{x,y},x Exp[-x]+y Exp[y]-(2+Sin[x])/(2+Sin[y])];



Keep in mind that not all math functions in the Mathematica kernel can be
converted to op-code.  A list of functions that can is available at:

Factorial can't be converted to opcode, so the function below doesn't run
very fast.  Instead one should replace Factorial[n] with (Times@@Range[n]).


When you evaluate the line above you don't get a message indicating
Factorial[n] couldn't be converted to op-code.  In version 4 one can use the
following line to set a system option to cause posting of such messages (as
demonstrated below).


f2=Compile[{{x,_Real},{n,_Integer}},x^n/ Factorial[n]];

Compile::extscalar: n! can not be compiled and will be evaluated externally.
The result is assumed to be of type integer.

The Compile messages are listed in the Help Browser under
  Other Information > Warning Messages > Compile

I read over these messages, and came up with the 
following requirements for Compile to work:

- All variables must have numeric or True/False values.

- Compiled evaluation has no speed advantage if some 
  numeric values encountered are not machine numbers.

- The type of expression returned from each subexpression 
  in the compiled function must be known.

- If Apply is used the first argument must have the head 
  Plus, Times, or List.

- If Outer is used the first argument must have the head 
  Plus, Times, or List.

- (Tr) can be used with two arguments only when the second 
   argument is Plus, Times, or List.

- If an expression returns a tensor, the compiled function 
  interpreter needs to be able to determine the rank of the tensor.

- Empty tensors such as {},or {{},{},{}},or {{{}},{{}}} can't be 
  used in a compiled function.

- The second argument of Compile can't include Patterns.

- The only types compiled functions can work with are 

- The only list structures compiled functions can work with 
  are vectors, matrices, and (other tensors with rank<101).

- A variable in a compiled function has to keep the same type.

- Local variables used in compiled evaluation must have a 
  numeric value when they are used.

- Certain functions supported by Compile don't work in situations 
  as general as they do in uncompiled evaluation.
  e.g.  In compiled evaluation Floor can't take a complex argument.

Here is an example where Compile has trouble.  
A global variable can't be represented using op-code.  

The compiled function(f2) below isn't converted to op-code because the
global variable (w) issued in the function.  The best way around this known
to me, is to provide the global variable to the function as an argument.


w = 0.25;
f2 = Compile[{x}, (x^2 + Sqrt[Exp[x]]+w)];


Here is another example where Compile has trouble.
You can't use opcode to change the value of a function's argument.  The
CompiledFunction below doesn't convert to op-code because the function
changes the value of (x) (one of the function's arguments).



This next feature is only available in versions 3.0 and later. 
Using  Compile[{{x1,t1,n1}, ...},expr] (x1) is a rank (n1) tensor where each
element has the type (t1).  A vector is a rank 1 tensor, and a matrix is a
rank 2 tensor.

magnitude = 
  Compile[{{vector, _Real, 1}}, Sqrt[Plus @@ vector^2] ];



This next example uses the same feature as the last and 
is only available in versions 3.0 and later. 
Compile[{{x, _Real, 2}}, ...] takes an argument that is a matrix of real
numbers.  The matrix can have any dimensions.  As an example a
CompiledFunction (func) is defined below.  The function (func) takes two
matrices of real numbers named (a) and (p) and computes


A demonstration of (func) is given below.




Ted Ersek

  • Prev by Date: Mathematica Programmer
  • Next by Date: Incompatibility between CleanSlate and Export
  • Previous by thread: compiling problem
  • Next by thread: Re: $UserDirectory