Setting up Mathematica to talk to Clojuratica for Windows

Getting Clojure and Mathematica to communicate under Windows is perhaps slightly easier to set up than it is under Mac and Clojure, but this comes at a price. Because we can't create symbolic links, we can't use Maven to manage the JLink.jar file. Because of this, we can't use Leiningen to manage our project and its dependencies. Instead, we'll need to download everything and manage the dependencies and classpath on our own.

Getting ready

To prepare for this, we need to download a number of resources, as follows:

Unzip each of these as subdirectories of your project directory. For example, my project directory is named clj-interop and it contains the subdirectories apache-ant-1.99.4, Clojuratica-master, and clojure-1.66.0.

Of course, you'll also need to have Mathematica (http://www.wolfram.com/mathematica/) installed.

How to do it...

First, we need to get Clojuratica built, and then we'll worry about running Clojure in the right environment in order to be able to talk to Mathematica.

  1. In the Windows console or PowerShell terminal, change to the Clojuratica-master directory and use Ant to build the Clojuratica JAR file:
    PS C:clj-interop> cd .Clojuratica-master
    PS C:clj-interopClojuratica-master> ..apache-ant-1.99.4inant jar
    ...
    BUILD SUCCESSFUL
  2. Go back up a directory and start Clojure. When you do this, pass a classpath in Java with Clojure, Clojuratica, and JLink, which is the library that handles communication between Clojure and Mathematica, as shown here:
    PS C:clj-interopClojuratica-master> cd ..
    PS C:clj-interop> java -cp ".clojure-1.66.0clojure-1.66.0.jar;.Clojuratica-masterclojuratica.jar;C:Program FilesWolfram ResearchMathematica9.0SystemFilesLinksJLinkJLink.jar;." clojure
    Clojure 1.66.0
    user=>
  3. You'll also need a module that contains the code to start the system and define the math macro. Create a file named mma.clj with contents, as follows:
    (ns mma)
    
    (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 "
           ""C:/Program Files/Wolfram Research/Mathematica/9.0/MathKernel.exe""))
    (def-math-macro math math-evaluate)
  4. Now, in the Clojure REPL interpreter, simply use this namespace:
    user=> (use 'mma)
    nil

How it works...

Just as in the previous recipe, this recipe uses Clojuratica to provide a Clojure-friendly layer between the rest of Clojure and Mathematica's JLink library. You'll need to use the mma namespace in every script or REPL session that you want to communicate with Mathematica in.

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

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