MathLink.jl icon indicating copy to clipboard operation
MathLink.jl copied to clipboard

Julia -> Mathematica

Open thomasklau opened this issue 9 years ago • 11 comments

Hi All,

Really interesting repo. Could this package possibly be used to call Julia code from Mathematica? From what I understand, the MathLink library for C/C++ allows for this functionality.

http://edenwaith.com/development/tutorials/mathlink/ML_Tut.pdf

If not, what would need to be added to enable this functionality? Anywhere I should go looking? Would it be possible to statically compile a Julia script and run it inside Mathematica? In the same spirit as Mathematica.jl, I'm looking for a low-hassle way of calling a Julia function inside of Mathematica as if it were native Mathematica. I see that this is possible with MATLAB: https://github.com/rsmenon/MATLink

thomasklau avatar Dec 06 '16 10:12 thomasklau

In theory this should be fairly simple, just a case of registering a C function pointer (created from a Julia function) with MathLink, which can accept some Mathematica expression objects and return a new one.

Unfortunately I never actually figured out how to do this, if the API exists in MathLink I couldn't see it. MATLink may well have some clues – if anyone is able to dig around and figure something out then patches for this functionality are certainly welcome.

MikeInnes avatar Dec 07 '16 15:12 MikeInnes

I have next to zero experience with Julia, so I cannot hack on this, but if you need some help with MathLink feel free to ask me (I am one of the authors of MATLink, which uses MathLink to call MATLAB from Mathematica).

szhorvat avatar May 24 '19 20:05 szhorvat

Mathematica now has an External Languages interface: https://reference.wolfram.com/language/guide/ExternalInterpretedLanguageInterfaces.html which may be easier for this specific aim?

simonbyrne avatar Sep 04 '19 04:09 simonbyrne

@simonbyrne I would not recommend it. There are no documented tools there that would help, and I am very sceptical about a one-shoe-fits-all solution. Each language should have its own, tailored interface.

szhorvat avatar Sep 04 '19 07:09 szhorvat

What could potentially be an alternative to MathLink is to use the same communication method as these new interfaces, but not try to fit in the ExtenalEvaluate framework (there's no documented way to do that anyway).

They use ZeroMQ for interprocess communication, through ZeroMQLink` . Most of ZeroMQLink is now in documented system functions (see Socket*).

There still needs to be a way to encode/decode data. The new WXF format could be used for this. Its structure is fully documented, so it saves some of the guesswork that's typically needed when dealing with MathLink.

I mention all this as a potential alternative to MathLink. Do not take it as a recommendation.

szhorvat avatar Sep 04 '19 07:09 szhorvat

Thanks @szhorvat, that is very good to know.

simonbyrne avatar Sep 04 '19 17:09 simonbyrne

At the moment I am thinking about a similar problem of passing a Julia function or other object into mathematica (e.g. an arbitrary Julia function to NDSolve), which would require calling back into the Julia session.

One option is to open up a new link for each such function, which could then be polled in a background task in Julia (on the Mathematica side this would mean that calling the function would result in putting the arguments on the link, and getting the results back again).

The main problem with this approach would be knowing when to clean up the task on the Julia side. @szhorvat Do you know if LinkClose is triggered automatically when the link goes out of scope? Or is there another "finalizer" mechanism we can use here?

simonbyrne avatar Sep 04 '19 17:09 simonbyrne

Callback capability is not simple, and you may want to implement calling Julia from Mathematica before tackling the callback problem. But it's up to you.

Do you know if LinkClose is triggered automatically when the link goes out of scope?

No, it's not.

Or is there another "finalizer" mechanism we can use here?

Do check out Internal`WithLocalSettings and this thread. J/Link's JavaBlock is also based on WithLocalSettings. You could make something similar to JavaBlock, which essentially solves the same problem for J/Link. Unfortunately, it's not pretty.

szhorvat avatar Sep 04 '19 18:09 szhorvat

Calling Julia from Mathematica: https://reference.wolfram.com/language/ref/externalevaluationsystem/Julia.html

ViralBShah avatar Jan 18 '23 05:01 ViralBShah

I believe this internally uses a different mechanism (via BSON). Still cool though

simonbyrne avatar Jan 18 '23 18:01 simonbyrne

This looks like an interface you might want to use: https://reference.wolfram.com/language/tutorial/TwoWayCommunicationWithExternalPrograms.html

simonbyrne avatar Mar 23 '23 22:03 simonbyrne