MathGroup Archive 2004

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

Search the Archive

Re: Launching the Mathematica interface via mathlink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49738] Re: [mg49667] Launching the Mathematica interface via mathlink
  • From: Oleksandr Pavlyk <pavlyk at phys.psu.edu>
  • Date: Thu, 29 Jul 2004 07:45:53 -0400 (EDT)
  • Organization: Penn State University; Department of Physics
  • References: <200407271100.HAA11136@smc.vnet.net>
  • Reply-to: pavlyk at phys.psu.edu
  • Sender: owner-wri-mathgroup at wolfram.com

Hello Peter,

The following code in .NET does what you want.
It is very far from perfect, but it might give you
and idea. I would illustrate it with C# example



///////////////// Beginning of code
using System;
using Wolfram.NETLink;

namespace hi_there
{
	class my_first
	{
		[STAThread]
		static void Main(string[] args)
		{
			IKernelLink ml = MathLinkFactory.CreateKernelLink();
			ml.WaitAndDiscardAnswer();
			string input = "a = N[Pi^2]";
			ml.Evaluate(input);
			ml.WaitForAnswer();
			double answer = ml.GetDouble();
			Console.WriteLine(input + " evaluates to " + answer);
			Console.Write("\n");
			ml.Evaluate("ToString[l = LinkConnect[LinkMode -> Listen, 
LinkProtocol -> \"TCPIP\" ]]");
			ml.WaitForAnswer();
			Console.WriteLine(ml.GetString());
			ml.Evaluate("$ParentLink = l");
			try
			{
				ml.WaitAndDiscardAnswer();
			}
			catch
			{
			};
			ml.Close();
		}
	}
}
///// End of code

compile it and run. I get the following output

C:\test>hi_there.exe
a = N[Pi^2] evaluates to 9.86960440108936

LinkObject[3970@snappy,3971@snappy, 2, 2]

Then I open Mathematica Front End, click Kernel -> Kernel Configuration 
Options. In the little window that opens, click Add to create a new 
kernel configuration. Click "Advanced Options" radio box, and in the
textfield Arguments to MLOpen type

-LinkMode Connect -LinkName "3970@snappy" -LinkProtocol "TCPIP"

(LinkName is taken from the output of your program).

Click "OK", and then go to Kernel -> Notebook's Kernel and
select newly created kernel config. Then type evaluate
InputForm[a]

you will see a bunch of error messages, never mind them, I
do not know how to get rid of them, but evaluate that same
command again, and you would see the number 9.86960440108936 printed by
your program.

This is very unsatisfactory result, since you must manually set up the 
linkname, and because of the error. I also attempted to launch front end
with

Mathematica.exe -LinkMode Listen -LinkProtocol "TCPIP" -LinkName 
"3970@snappy"

where Mathematica front end actually connect to the kernel you ran your
calculations in, but I could not make front end to work with that 
connection.

I do hope this slightly messy note will help you. I am open to critics
and suggestions, and would appreciate if anybody would suggest any 
improvement.

Sincerely,
Sasha

Peter Saffrey wrote:
> Is this possible? I have some code that interacts with a Mathematica
> kernal via mathlink. When it has finished, I'd like to fire up the
> Mathematica interface to allow my user to "poke around" in the
> results, draw graphs and so on. I know I could just invoke a
> Mathematica session from the command line, but how could I then gain
> access to this session via Mathlink to set up the appropriate
> variables?
> 
> Any advice gratefully received,
> 
> Peter
> 


  • Prev by Date: Re: Re: Fibonacci[1,000,000,000] contains 208,987,640 decimal digits (was: Fibonachi[5,000,000] contains 1044938 decimal digits)
  • Next by Date: Re: Q: extract all k-tuple from a list of n elements
  • Previous by thread: Launching the Mathematica interface via mathlink
  • Next by thread: Combinations