Re: a mathlink question
- To: mathgroup at smc.vnet.net
- Subject: [mg89633] Re: [mg89620] a mathlink question
- From: "J. McKenzie Alexander" <jalex at lse.ac.uk>
- Date: Mon, 16 Jun 2008 06:37:28 -0400 (EDT)
- References: <200806151013.GAA12268@smc.vnet.net>
Does it have to be a C-program? If not, you can do this via J/Link in
the following way. First, compile the following class:
public class MemorySum {
double total;
public MemorySum() {
total=0.0;
}
public double AddToPreviousSum(double x) {
total += x;
return total;
}
}
Once you've compiled that and saved the class inside a jar file,
create an instance of the class in Mathematica as follows:
Needs["JLink`"];
InstallJava[];
AddToClassPath["Path/To/JarFile/filename.jar"];
object=JavaNew["MemorySum"]
You then call the function as follows:
In[29]:= object@AddToPreviousSum[10]
object@AddToPreviousSum[10]
object@AddToPreviousSum[10]
object@AddToPreviousSum[10]
object@AddToPreviousSum[10]
Out[29]= 10.
Out[30]= 20.
Out[31]= 30.
Out[32]= 40.
Out[33]= 50.
The J/Link documentation has more details regarding persistence of
Java objects.
Jason
--
Dr J. McKenzie Alexander
Department of Philosophy, Logic and Scientific Method
London School of Economics and Political Science
Houghton Street, London WC2A 2AE
On 15 Jun 2008, at 11:13, GS wrote:
> I would like to call a C-program from Mathematica. All the examples in
> the MathLink tutorials (like AddTwo[i,j]) work like this: they are
> called from Mathematica, are executed, send the result to Mathematica
> and exit.
>
> I would like a C-program to not exit after the execution, and keep the
> memory of the previous inputs. Say, I want an external C-program, to
> be called as AddToPreviousSum[x]. When it is first called, it returns
> x. When it is called the second time AddToPreviousSum[y], it returns x
> +y. When it is called the third time AddToPreviousSum[z], it returns x
> +y+z, and so on.
>
> How do I implement this in C using MathLink?
>
> Thanks.
> GS.
>
Please access the attached hyperlink for an important electronic communications disclaimer: http://www.lse.ac.uk/collections/secretariat/legal/disclaimer.htm
- References:
- a mathlink question
- From: GS <vokaputs@gmail.com>
- a mathlink question