MathGroup Archive 2008

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

Search the Archive

Re: a mathlink question

  • To: mathgroup at
  • Subject: [mg89633] Re: [mg89620] a mathlink question
  • From: "J. McKenzie Alexander" <jalex at>
  • Date: Mon, 16 Jun 2008 06:37:28 -0400 (EDT)
  • References: <>

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() {

	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:


You then call the function as follows:

In[29]:= 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.


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:

  • Prev by Date: Re: export pdf font question
  • Next by Date: Workbench editing snag
  • Previous by thread: a mathlink question
  • Next by thread: Re: a mathlink question