Before we start interfacing with Mathematica, we have to download the libraries and set up our system to do so. This is a little complicated. We'll need to download the library in order to handle the interoperability and move a few files around.
Part of what makes this task so difficult is that several things vary, depending on your operating system. Moreover, in order to work with Leiningen, this recipe uses some features of the underlying operating system that aren't available for Windows (symbolic links), so this recipe won't work for the Windows platform. However, Windows users can refer to the next recipe, Setting up Mathematica to talk to Clojuratica for Windows.
We'll need to have Mathematica installed. You can get an evaluation copy from http://www.wolfram.com/mathematica/trial/, but if you're looking at this recipe, you probably already have it and are just looking for a way to connect to it from Clojure.
You'll also need to have Ant (http://ant.apache.org/) and Maven (http://maven.apache.org/) installed. These are Java development tools that are used to build and install the libraries in order to access Mathematica.
For this recipe, we'll get the Clojuratica library, incorporate it into our project, and place the dependencies where they should be:
$ cd Clojuratica/ $ ant jar … BUILD SUCCESSFUL Total time: 0 seconds
$ mvn install:install-file -Dfile=./clojuratica.jar -DartifactId=clojuratica -Dversion=2.0 -DgroupId=local.repo -Dpackaging=jar … [INFO] BUILD SUCCESS …
project.clj
file, add the following line of code::source-paths ["src" "Clojuratica/src/clj"]
$Path
to see the list of places where Mathematica looks for code to load automatically. Pick one to copy some files into. It shouldn't matter which you choose, except you probably want to use a directory under your home directory, not in the Mathematica installation directory. I'll use ~/Library/Mathematica/Autoload
. A good choice of directory for Linux might be ~/.Mathematica/Autoload
.By the way, also make a note about where Mathematica appears to be installed. We'll need that information in a few steps.
$ cp src/mma/* ~/Library/Mathematica/Autoload/
/Applications/Mathematica.app/SystemFiles/Links/JLink
. Under Linux, take a look inside /usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/JLink
. Once you find it, change to that directory and use Maven to install it. I used version 9.0, since this is the version of Mathematica that I'm using:$ cd /Applications/Mathematica.app/SystemFiles/Links/JLink/ $ mvn install:install-file -Dfile=./JLink.jar -DartifactId=JLink-Dversion=9.0 -DgroupId=local.repo -Dpackaging=jar ... [INFO] BUILD SUCCESS...
$ cd ~/.m2/repository/local/repo/JLink/9.0/ $ rm JLink-9.0.jar remove JLink-9.0.jar? y $ ln -s /Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar JLink-9.0.jar
project.clj
file, as shown here::dependencies [[org.clojure/clojure "1.66.0"] [local.repo/JLink "9.0"]]
(use 'clojuratica)
(import [com.wolfram.jlink MathLinkFactory])
(defn init-mma [mma-command]
(defonce math-evaluate
(math-evaluator
(doto
(MathLinkFactory/createKernelLink mma-command)
(.discardAnswer)))))
(init-mma
(str "-linkmode launch -linkname "
"/Applications/Mathematica.app/Contents/MacOS/MathKernel"))
(def-math-macro math math-evaluate)
Notice that the call to init-mma
includes the full path to MathKernel (I've highlighted this.). This will change depending on your system. For example, under Linux, the program will be /usr/local/Wolfram/Mathematica/9.0/Executables/math
. Substitute the path and name of the executable so that it works on your system.
This is fairly typical in order to set up a rough-around-the-edges complex computer system. There are lots of things that might vary from computer to computer and lots of things that could go wrong. Just have patience and grab some coffee. The Mathematica Stack Exchange is an excellent resource for problems (http://mathematica.stackexchange.com/).
One thing that I would like to point out is the last line from the final code block:
(def-math-macro math math-evaluate)
This defines a macro, which we've named math
, that has to wrap every call to Mathematica. We'll see examples of this in all of the following recipes that use Mathematica.
I originally got the steps for this from a blog post by David Cabana (http://drcabana.org/2012/10/23/installation-and-configuration-of-clojuratica/). Although, in the end, I had to do a few things in a slightly different manner, this was my original source. If the steps I have outlined here don't work for you, you might want to refer to this.
Also, as I previously mentioned, Mathematica is a good resource if you run into issues (http://mathematica.stackexchange.com/).
18.219.34.62