Re: Passing CompiledFunction to Java
- To: mathgroup at smc.vnet.net
- Subject: [mg38758] Re: Passing CompiledFunction to Java
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 9 Jan 2003 04:40:24 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <avgqmh$s2f$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi, it would be possible if there would be a reference of the byte code available. The old references on MathSource are complete out of date But I'm sure that a reference exist. Is there a way to make a actual reference of the CompiledFunction[] codes available ? If it is possible, one should bundle the calls to Mathematica, i.e., if one needs several evaluations it should be one call that return the list of the results. Regards Jens Todd Gayley wrote: > > 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