I have Mathematica 7.01 and Mathematica 5.2 installed on the same machine. I wish to be able to evaluate code in the v.5.2 kernel from within Mathematica 7.01 session. I mean that running Mathematica 7.0.1 standard session I wish to have a command like kernel5Evaluate
to evaluate some code in the 5.2 kernel and return the result into the 7.01 kernel and linked 7.01 FrontEnd notebook in such a way as this code would be executed in the 7.01 kernel.
For example (in the standard Mathematica v.7.01 session):
In[1]:= solutionFrom5 = kernel5Evaluate[NDSolve[{(y^\[Prime])[x]==y[x],y[1]==2},y,{x,0,3}]]
Out[1]= {{y -> InterpolatingFunction[{{0., 3.}}, <>]}}
In[2]:= kernel5Evaluate[Plot3D[Sin[x y],{x,-Pi,Pi},{y,-Pi,Pi}]]
During evaluation of In[2]:= GraphicsData["PostScript", "\<\............
Out[2]= -SurfaceGraphics-
In the both cases the result should be as if the v.5.2 kernel is set to be "Notebook's Kernel" in the v.7.01 FrontEnd. And of course solutionFrom5
variable should be set to the real solution returned by v.5.2 kernel.
Here is working implementation of what I wanted. I have added checking for a dead
MathLink
connection as suggested by Todd Gayleyhere
. Nowkernel5Evaluate
works reliable even if the slave kernel was terminated in unusual way. I also have much improved parsing ofMessage
s and added some diagnostic messages forkernel5Evaluate
. Here is the code:And here are some test expressions:
Here is an implementation based on Simon's code. It still requires improvement. The one unclear thing to me is how to handle Messages generated in the slave (v.5.2) kernel.
Here is my code:
Here are test expressions:
It seems to work as expected. However it could be better...
Here's my attempt at what you want,
First I define
linkEvaluate
that takes an activeLink
and passes it an expression. If there's things forLinkRead
still to read, then it reads them until there are no more. Then it writes the expression and waits for the results to come back. Then it reads the output until there's nothing left to read. Normally, it then returns the firstReturnExpressionPacket
unless you have set the final, optional argument,all
, toTrue
- in which case it returns everything it read.Then
kernel5Evaluate
first checks if the global$kern5
is defined as aLinkObject
, if not then it defines it. It then simply passes the work over tolinkEvaluate
. You will have to replace "math5" with the filename and path of your Mma 5.2 kernel.