Setting up Mathematica to talk to Clojuratica for Mac OS X and Linux

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.

Getting ready

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.

How to do it…

For this recipe, we'll get the Clojuratica library, incorporate it into our project, and place the dependencies where they should be:

  1. To begin with, we'll install Clojuratica. Download the library from https://github.com/stuarthalloway/Clojuratica/archive/master.zip. Unzip it inside your project directory. Then, change to that directory and build the JAR file, as follows:
    $ cd Clojuratica/
    $ ant jar
    …
    BUILD SUCCESSFUL
    Total time: 0 seconds
  2. We'll also need to install this JAR file so that Maven (and Leiningen) can find it. This command will take care of that:
    $ mvn install:install-file -Dfile=./clojuratica.jar 
        -DartifactId=clojuratica -Dversion=2.0 
        -DgroupId=local.repo -Dpackaging=jar
    …
    [INFO] BUILD SUCCESS
    …
  3. Now, we'll add the Clojuratica source code to our project. In our Leiningen project.clj file, add the following line of code:
    :source-paths ["src" "Clojuratica/src/clj"]
  4. In a Mathematica notebook, evaluate $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.

    How to do it…
  5. We'll copy some files from Clojuratica into one of these directories (In the following command, substitute the destination with any directory from Mathematica's load path that you selected):
    $ cp src/mma/* ~/Library/Mathematica/Autoload/
  6. We need to install Mathematica's Java interface library, JLink, where Maven knows how to find it. To do this, we need to find it inside the Mathematica directory. On my machine (Mac), it's in /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...
  7. Unfortunately, the JLink library, which we just installed, expects to be called from within the Mathematica directory tree, not from within the Maven repository directory. In order to make it happy, we'll need to go into the local Maven repository, remove the installed JAR file, and add a symbolic link to the original one:
    $ 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
  8. We can now include the JLink library in our project.clj file, as shown here:
    :dependencies [[org.clojure/clojure "1.66.0"]
                   [local.repo/JLink "9.0"]]
  9. We can import the libraries we've just installed and define a few utility functions that we'll need to run whenever we want to interface with Mathematica:
    (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.

How it works…

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.

There's more…

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.34.62