Evaluating Mathematica scripts from Clojuratica

Calling single functions is good and very useful, but sometimes we might have a number of operations in a file that we want to call from Clojure. Clojuratica allows you to do this as well.

Getting ready

First, we must have Clojuratica and Mathematica talking to each other. Either complete the Setting up Mathematica to dalk to Clojuratica for Mac OS X and Linux recipe or the Setting up Mathematica to talk to Clojuratica for Windows recipe. Also, we'll need to have called the init-mma function.

Also, we need to make sure that the Clojuratica namespace is imported into our script or REPL:

(use 'clojuratica)

Moreover, we need a Mathematica file to run. I created one called line-integral.m, and it contains these lines:

SyntaxInformation[
   lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, 
   "ArgumentsPattern" -> {_, _, _}};

lineIntegrate[r_?VectorQ, f_Function, {t_, tMin_, tMax_}] := 
 Module[{param, localR}, localR = r /. t -> param;
  Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param],
            {param, tMin, tMax}]]

lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}]

How to do it…

In Mathematica, we execute a file using the Get function. Here, we just use Clojuratica to call this on the appropriate filename:

user=> (math (Get "line-integral.m"))
(* 2 Pi)

How it works…

This uses the Mathematica function Get, which takes the name of a file that contains one or more Mathematica commands. It executes those and returns the value of the last command.

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

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