Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia -> Mathematica #8

Open
thomasklau opened this issue Dec 6, 2016 · 11 comments
Open

Julia -> Mathematica #8

thomasklau opened this issue Dec 6, 2016 · 11 comments

Comments

@thomasklau
Copy link

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

@MikeInnes
Copy link
Collaborator

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.

@szhorvat
Copy link

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).

@simonbyrne
Copy link
Member

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

@szhorvat
Copy link

szhorvat commented Sep 4, 2019

@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
Copy link

szhorvat commented Sep 4, 2019

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.

@simonbyrne
Copy link
Member

Thanks @szhorvat, that is very good to know.

@simonbyrne
Copy link
Member

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?

@szhorvat
Copy link

szhorvat commented Sep 4, 2019

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.

@ViralBShah
Copy link
Contributor

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

@simonbyrne
Copy link
Member

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

@simonbyrne
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants