Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Great question, but I don't think anyone will be able to figure this out :( –  Tom Wellington Jan 21 '13 at 16:16
1  
Depending on what you are trying to accomplish, you could just call Mathematica from Clojure like any other command line program –  jVincent Jan 21 '13 at 16:25
4  
Seth Chandler did a presentation on linking Clojure and Mathematica, which may be of interest: youtube.com/watch?v=g9KdQbq_JLE –  Joel Klein Jan 21 '13 at 17:26
2  
@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. –  Leonid Shifrin Jan 21 '13 at 17:35
1  
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. –  Joel Klein Jan 21 '13 at 20:25

2 Answers 2

up vote 14 down vote accepted

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.