clojuratica - mathematica and clojure

12
Clojure Library “Clojuratica” Nick Brandaleone Boston Clojure Group May 9, 2013 [email protected]

Upload: nick-brandaleone

Post on 15-Nov-2014

154 views

Category:

Software


12 download

DESCRIPTION

How to use the power of Mathematica (software by Wolfram Research) in your Clojure code.

TRANSCRIPT

Page 1: Clojuratica - Mathematica and Clojure

Clojure  Library  “Clojuratica”    

Nick Brandaleone Boston Clojure Group

May 9, 2013

[email protected]

Page 2: Clojuratica - Mathematica and Clojure

Brief  Bio  §  Nick Brandaleone

§  Math/CS background §  Career

§  First 10 years – Sys/Net admin/programmer §  Last 10+ years – Technical Sales §  Current languages - Ruby/Rails. Clojure/Haskell §  Now – CTO of small company (web, R, customer facing)

§  Took LISP in college §  Did NOT get it

§  Used Mathematica in grad school §  Got it!! (Concise, powerful, parallelism, verbs)

Page 3: Clojuratica - Mathematica and Clojure

Mathematica  Overview    §  Mathematica is:

§  Commercial software package (Wolfram) for doing mathematics (numeric/symbolic math, statistics, visualization, image-processing, etc…)

§  Competes with MatLab, Maple, Scilab, R, Julia

§  Uses LISP-like syntax, with C “under the hood”

Page 4: Clojuratica - Mathematica and Clojure

Mathematica  examples   From Project Euler

§  What is the largest prime factor of the

number 600851475143 ? ①  FactorInteger[600851475143]

{{71, 1}, {839, 1}, {1471, 1}, {6857, 1}} ②  First[Last[FactorInteger[600851475143]]]

6857

Page 5: Clojuratica - Mathematica and Clojure

Another  example  §  What is the smallest number divisible by

each of the numbers 1 to 20? ①  DivNum[m_] :=

Module[{n = m, list = Range[m]}, While [! (And @@ Divisible[n, list]), n++]; Print [n]]

②  From Number Theory…Compute the prime factorization of each number from 1 to 20, and multiply the greatest power of each prime together

Page 6: Clojuratica - Mathematica and Clojure

Example  continued    §  Union[Flatten[FactorInteger[Range[20]], 1]] //.

{s___, {x_, y_}, {x_, i_}, l___} -> {s, {x, i}, l} {{1, 1}, {2, 4}, {3, 2}, {5, 1}, {7, 1}, {11, 1}, {13, 1}, {17, 1}, {19, 1}}

§  Transpose[%] {{1, 2, 3, 5, 7, 11, 13, 17, 19}, {1, 4, 2, 1, 1, 1, 1, 1, 1}}

§  Apply[Times, %[[1]]^%[[2]]] 232792560

Page 7: Clojuratica - Mathematica and Clojure

Clojuratica  Library  §  Clojuratica library allows Clojure to call into

the Mathematica Kernel. §  Syntactic integration §  Translation of data structures §  Passing of first-class function to Clojure

§  Mathematica supports C and Java calls through “mathlink/kernellink” API

§  It is possible to have Mathematica call into C or Java programs as well

Page 8: Clojuratica - Mathematica and Clojure

Clojuratica  Status    §  Written by Garth Sheldon-Coulson. Smart guy.

Grad student at MIT and Harvard Law School. §  Little work has been done on library since late

2009. Google group is quiet as well. §  Library still works with minor tweaks. §  Current version is 2.0a3. §  Hosted at http://clojuratica.weebly.com/ §  See http://drcabana.org/2012/10/23/

installation-and-configuration-of-clojuratica/

Page 9: Clojuratica - Mathematica and Clojure

Why  use  Clojuratica?  §  Mathematica is:

a.  J Fast (highly optimized/parallelized) b.  J Has enormous number of math related

functions c.  J Curated Data Sets d.  J Similar functional look and feel e.  L Unfortunately, I do not see how to

leverage its visualization strengths (perhaps image export?)

f.  L Need license $$

Page 10: Clojuratica - Mathematica and Clojure

To  the  REPL!  math is the fn calling into Mathematica

§  (math (FactorInteger 600852475143))

[[3 2] [29 1] [5087 1] [452549 1]] §  (def hello (math (Function [x] (StringJoin "Hello, "

x “! This is a Mathematica function's output.")))) §  (hello "World")

"Hello, World! This is a Mathematica function's output."

Page 11: Clojuratica - Mathematica and Clojure

More  REPL  §  (math (CityData "Boston" "Population"))

617594 §  (math (DateDifference "1969, 1, 31", "2013,

5, 9")) 16169

§  (math (Integrate (Power x n) x)) (* (Power (+ 1 n) -1) (Power x (+ 1 n)))

Page 12: Clojuratica - Mathematica and Clojure

The  End