Creating functions from Mathematica

We can use Mathematica to create functions that we can pass around and call from Clojure, similar to other functions. Moreover, we can also call these functions from within Mathematica and pass them around in Mathematica. As far as each environment is concerned, functions from the other environment are just black boxes that can be called, but even that much is still very useful.

Getting ready

We must first have Clojuratica and Mathematica talking to each other. Either complete the Setting up Mathematica to talk to Clojuratica for Mac OS X and Linux recipe or the Setting up Mathematica to talk to Clojuratica for Windows recipe. Also, we need to call the init-mma function beforehand.

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

(use 'clojuratica)

How to do it…

Here, we'll create a function that simply wraps the FactorInteger Mathematica function:

(def factor-int
  (math
    (Function [x] (FactorInteger x))))

We can call it like a regular function, as follows:

user=> (factor-int 1234567890)
[[2 1] [3 2] [5 1] [3607 1] [3803 1]]

How it works…

The key here is to use the Mathematica keyword Function in order to create the function and return it. We assigned this to factor-int, and from that point onwards, we can treat the value as a regular function value.

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

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