MathGroup Archive 2003

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

Search the Archive

Re: Passing CompiledFunction to Java

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38756] Re: [mg38707] Passing CompiledFunction to Java
  • From: Todd Gayley <tgayley at wolfram.com>
  • Date: Wed, 8 Jan 2003 04:13:38 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

At 02:42 AM 1/6/03, Reinhard Oldenburg wrote:
>Is it possible to pass a CompiledFunction from Mathematica
>to Java via J/Link?
>My basic problem is that I use symbolic methods to build functions
>that have to be evaluated many times within the Java part
>of my program, thus speed is the main objective. Any other suggestions?
>
>Yours,
>Reinhard Oldenburg


Reinhard,

I presume that you are _not_ asking whether you can pass a CompiledFunction 
to Java and then execute it entirely within the Java runtime. That is not 
possible--to call the CompiledFunction with some arguments you must call 
back to Mathematica.

To pass a CompiledFunction to Java, use J/Link's Expr class, which can 
store any Mathematica expression. If you have a method that takes an Expr 
argument, then you can pass anything for that argument slot from 
Mathematica. Say you have a Java method like this:

     private Expr storedExpr;

     public void storeThisExpr(Expr e) {
         storedExpr = e;
     }

You can call this from Mathematica like this:

In[100]:= cf = Compile[....];

In[101]:= obj@storeThisExpr[cf]


If you are manually reading the CompiledFunction from a link, use the 
getExpr() method:

     Expr e = ml.getExpr();


No matter which way you get the CompiledFunction into Java as an Expr, to 
call back to Mathematica to execute it you have to use a relatively obscure 
sequence of MathLink calls. You cannot use putFunction() because the head 
is not a symbol--it's the expression CompiledFunction[...]. You must 
manually build the expression from a head and arguments. Here is the code:

     // Assume e is an Expr containing a CompiledFunction that
     // you want to call with the argument 42.
     ml.putNext(MathLink.MLTKFUNC);
     ml.putArgCount(1);
     // Next we put the head
     ml.put(e);
     // Then the arguments
     ml.put(42);
     ml.waitForAnswer();
     //  Now read the answer ...


Having said all this, do you really need to pass the CompiledFunction 
itself to Java? Why not just send its name to Java (i.e., assign the 
function to a symbol and then pass the symbol name to Java)? Let the 
CompiledFunction live only in Mathematica and just refer to it by name from 
your Java code.


Todd Gayley
Wolfram Research



  • Prev by Date: Re: FONT PROBLEM, SuSe Linux 7.3 Mathematica 4.0 FrontEnd
  • Next by Date: Re: webMathematica trouble
  • Previous by thread: Passing CompiledFunction to Java
  • Next by thread: Re: Passing CompiledFunction to Java