I'm using Clojure and I'd really love to be able to use some of Mathematica's functions inside it. Can anyone help with mathlinking it?
-
$\begingroup$ Great question, but I don't think anyone will be able to figure this out :( $\endgroup$– Tom WellingtonCommented Jan 21, 2013 at 16:16
-
1$\begingroup$ Depending on what you are trying to accomplish, you could just call Mathematica from Clojure like any other command line program $\endgroup$– jVincentCommented Jan 21, 2013 at 16:25
-
4$\begingroup$ Seth Chandler did a presentation on linking Clojure and Mathematica, which may be of interest: youtube.com/watch?v=g9KdQbq_JLE $\endgroup$– Joel KleinCommented Jan 21, 2013 at 17:26
-
3$\begingroup$ @JoelKlein I think you should've kept this as an answer, just to give another point of entry. Even if this is just a video, at some point I am sure the associated notebook will also be available. This is a valuable piece of information, and keeping that as a comment will reduce its visibility. Besides, comments are not indexed by search engines, and may eventually be deleted. Anyone is free to express their own opinion (by downvoting in particular), but this does not mean that everyone here shares that opinion. $\endgroup$– Leonid ShifrinCommented Jan 21, 2013 at 17:35
-
1$\begingroup$ Shouldn't this question be "How can I connect Mathematica to Clojure" instead of "to Lisp"? The questioner clearly wants to know about Clojure, and the choice of Lisp implementation makes a big difference in how you connect to it. $\endgroup$– Joel KleinCommented Jan 21, 2013 at 20:25
2 Answers
To call Mathematica from Clojure, you will want to use Clojure's Java interop to access the Wolfram JLink classes to script Mathematica access.
Start by launching Clojure with a classpath that includes the JLink.jar, for example with this batch file:
@echo off
set MATHEMATICA=C:/M/9.0
set JLINKJAR=%MATHEMATICA%/SystemFiles/Links/JLink/JLink.jar
java -cp C:/Apps/clojure-1.4.0/clojure-1.4.0.jar;%JLINKJAR% clojure.main
Once inside Clojure, use the Java interop syntax to access JLink classes as described in the tutorial Writing Java Programs that Use Mathematica. Typically this starts by launching a kernel with the MathLinkFactory to obtain a KernelLink:
user=> (def kl (com.wolfram.jlink.MathLinkFactory/createKernelLink "-linkmode launch -linkname 'c:/M/9.0/math.exe -mathlink'"))
#'user/kl
Then discard the initial input packet from the kernel:
user=> (.discardAnswer kl)
nil
We can now perform evaluations of Mathematica code:
user=> (.evaluate kl "1+1")
nil
user=> (.waitForAnswer kl)
3
user=> (.getInteger kl)
2
When retrieving results from an evaluation, you need to know what type to expect, or you need to retrieve the result as a string and parse it.
Here's one way to evaluate a Mathematica expression that returns a list of integers and get the vector in Clojure:
user=> (.evaluate kl "RandomInteger[{0,99},20]")
nil
user=> (.waitForAnswer kl)
3
user=> (def res (vec (.getIntArray1 kl)))
#'user/res
user=> res
[91 12 98 20 71 43 59 64 98 64 20 93 32 84 41 90 38 27 51 76]
There's also a presentation by Seth Chandler on connecting Mathematica to Clojure: J/Link without Java
-
$\begingroup$ Is there a clojar for JLink so it can be included as a dependency? $\endgroup$ Commented Feb 10, 2018 at 11:35
I did not try it yet, but Clojuratica may be what you need. The project seems to no longer be maintained though (although I may be wrong). No idea how much of a problem this is, given changes in the Clojure since the time Clojuratica was released.
Of course, Seth's talk was also great and is a great resource as well, as Joel mentioned.