Clojuratica
簡單說就是把 Clojure
和 Mathematica
組合起來了,能夠經過 Clojure
來調用 Mathematica
的數千個函數。html
按照做者的說法是 Clojuratica
集兩者之長處,很是高大上,並且還易於使用,下面是 Clojuratica
站點對其優勢的描述:java
Clojuratica lets you write and evaluate Mathematica code in Clojure
with full syntactic integration
. Now Clojure programs can take advantage of Mathematica's enormous range of numerical and symbolic mathematics algorithms and fast matrix algebra routines.git
Clojuratica provides the seamless and transparent translation of native data structures
between Clojure and Mathematica. This includes high-precision numbers, matrices, N-dimensional arrays, and evaluated and unevaluated Mathematica expressions and formulae.github
Clojuratica lets you call, pass, and store Mathematica functions just as if they were first-class functions in Clojure
. This is high-level functional programming at its finest. You can write a function in whichever language is more suited to the task and never think again about which platform is evaluating calls to that function.express
Clojuratica facilitates the "Clojurization" of Mathematica's existing parallel-computing capabilities
. Mathematica is not designed for threads or concurrency. It has excellent support for parallel computation, but parallel evaluations are initiated from a single-threaded master kernel which blocks until all parallel evaluations return. By contrast, Clojuratica includes a concurrency framework that lets multiple Clojure threads execute Mathematica expressions without blocking others. Now it is easy to run a simulation in Clojure with 1,000 independent threads asynchronously evaluating processor-intensive expressions in Mathematica. The computations will be farmed out adaptively and transparently to however many Mathematica kernels are available on any number of processor cores, either locally or across a cluster, grid, or network.app
不過該項目最近的版本更新是2009年的Version 2 alpha 2
。less
由於 Clojure
和 Mathmetica
都是多平臺可用,所以 Clojuratica
理論上同時支持 Windows/OSX/Linux/Unix
多個平臺,本文的環境是 Mac OSX 10.9
。eclipse
安裝配置 Clojuratica
,須要事先安裝好以下軟件:async
具體的安裝步驟就很少說了,使用 brew install git
和 brew install ant
安裝好 git
和 ant
,再用 git
安裝 Clojure
,詳細教程能夠本身搜索。maven
周邊環境基本就這些要求。
先進入你準備存放 Clojuratica
項目的本地目錄 ~/GitHub
cd ~/GitHub/
而後從 github
上把 Clojuratica
項目克隆下來
Air:GitHub admin$ git clone https://github.com/drcabana/Clojuratica
進入 Clojuratica
目錄
Air:GitHub admin$ cd Clojuratica/ Air:Clojuratica admin$ ls build.xml clojuratica.jar doc src Air:Clojuratica admin$
用 ant
安裝 Clojuratica
Air:Clojuratica admin$ ant jar Buildfile: /Users/admin/GitHub/Clojuratica/build.xml init: compile: jar: [jar] Building jar: /Users/admin/GitHub/Clojuratica/clojuratica.jar [echo] JAR written to /Users/admin/GitHub/Clojuraticaclojuratica.jar BUILD SUCCESSFUL Total time: 0 seconds Air:Clojuratica admin$
很好,Clojuratica
安裝成功。
接下來要作一些手工配置。
首先要把 Clojuratica
項目內的 src/mma
目錄中的三個 .m
文件拷貝到 Mathematica
文件夾裏 的 Autoload
目錄中,文件以下:
Air:Clojuratica admin$ cd ./src/mma Air:mma admin$ ls ClojurianScopes.m ClojurianScopes.nb FunctionalExtras.m HashMaps.m HashMaps.nb Air:mma admin$
這裏須要根據不一樣版本的 Mathematica
來肯定目的地路徑,個人版本是 Mathematica 8.0
,因此對應的路徑爲 /Applications/Mathematica/SystemFiles/Autoload/
,因此命令以下:
Air:mma admin$ cp *.m /Applications/Mathematica/Autoload/
接下來須要進入 ~/.lein
目錄,修改一下配置文件 profiles.clj
,主要是增長這一句:
[lein-localrepo "0.4.1"]
下面是個人配置:
Air:mma admin$ cat ~/.lein/profiles.clj {:user {:plugins [[lein-swank "1.4.0"] [lein-localrepo "0.4.1"] ] } } Air:mma admin$
而後要用 lein
把 clojuratica.jar
和 JLink.jar
安裝到 localrepo
,命令以下:
lein localrepo install ~/GitHub/Clojuratica/clojuratica.jar local.repo/clojuratica 2.0_alpha3 lein localrepo install /Applications/Mathematica.app/Links/JLink/JLink.jar local.repo/JLink 8.0
不過第二條命令貌似沒有成功,因此須要進入目錄 .m2/repository/local/repo
中手動創建一個符號連接:
Air:mma admin$ cd ~/.m2/repository/local/repo/JLink Air:JLink admin$ ls 8.0 maven-metadata-local.xml Air:JLink admin$ ln -s /Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar JLink-8.0.jar
最後用 tree
命令查看一下:
Air:JLink admin$ tree ~/.m2/repository/local/repo/JLink/ /Users/admin/.m2/repository/local/repo/JLink/ ├── 8.0 │ ├── JLink-8.0.jar -> /Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar │ └── _maven.repositories └── maven-metadata-local.xml 1 directory, 3 files Air:JLink admin$
很是好,如今基本配置所有完成,接下來就能夠建立一個 Clojuratica
的新項目了。
首先,用 lein
建立一個新項目,假設咱們要把新項目 mmaclj2
建立在 ~/cljProject/
目錄,命令以下:
Air:JLink admin$ cd ~/cljProject/ Air:cljProject admin$ lein new mmaclj2 Generating a project called mmaclj2 based on the 'default' template. The default template is intended for library projects, not applications. To see other templates (app, plugin, etc), try `lein help new`. Air:cljProject admin$
進入新建項目的目錄,查看一下:
Air:cljProject admin$ cd mmaclj2/ Air:mmaclj2 admin$ ls LICENSE README.md doc project.clj resources src test Air:mmaclj2 admin$
首先須要修改當前目錄下的 project.clj
文件,咱們能夠先看一下它的內容:
Air:mmaclj2 admin$ cat project.clj (defproject mmaclj2 "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.6.0"]]) Air:mmaclj2 admin$
在依賴項中增長 JLink
和 clojuratica
,改成以下:
(defproject mmaclj2 "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.6.0"] [local.repo/JLink "8.0"] [local.repo/clojuratica "2.0_alpha3"]])
而後須要進入 ./src/mmaclj2/
目錄中:
Air:mmaclj2 admin$ cd ./src/mmaclj2/ Air:mmaclj2 admin$ ls core.clj Air:mmaclj2 admin$ pwd /Users/admin/cljProject/mmaclj2/src/mmaclj2 Air:mmaclj2 admin$
在這裏新增一個 connect.clj
文件,這段代碼用於創建 Clojure
跟 Mathematica
的鏈接,內容以下:
(ns mmaclj2.connect (:use clojuratica) (:import [com.wolfram.jlink MathLinkFactory])) (def kernel-link (MathLinkFactory/createKernelLink "-linkmode launch -linkname '/Applications/Mathematica.app/Contents/MacOS/MathKernel' '-mathlink'")) (.discardAnswer kernel-link) (def math-evaluate (math-evaluator kernel-link)) (def-math-macro math math-evaluate)
這裏必定要注意第一句 ns mmaclj2.connect
,必需要跟你的項目名稱 mmaclj2
一致。
很好,到如今爲止,咱們成功地建立了一個新的項目,而且完成這個項目的相關配置,接下來就能夠嘗試着運行了。
首先在這個項目的目錄內經過 lein repl
來啓動交互接口,具體命令以下:
Air:mmaclj2 admin$ lein repl nREPL server started on port 55552 on host 127.0.0.1 - nrepl://127.0.0.1:55552 REPL-y 0.3.1 Clojure 1.6.0 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e user=>
出現 user=>
提示符說明已經成功運行 Clojuratica
交互環境,接下來運行咱們的項目代碼:
user=> (use 'mmaclj2.connect) nil user=>
返回 nil
說明成功加載,也就是說咱們如今已經成功鏈接到 Mathematica
了,如今就能夠執行一些實驗操做了:
user=> (math (Plus 1 1)) 2 user=> (math (FactorInteger 12345)) [[3 1] [5 1] [823 1]] user=> (math #_=> (Plus 1 1) #_=> (FactorInteger 12345)) [[3 1] [5 1] [823 1]] user=>
由於我對 Mathmetica
也不是特別熟悉,因此上面幾個示範的例子也都是從官網教程拷貝過來的,因此若是但願更深刻的使用,仍是多看看官網的教程吧,
做者:FreeBlues
本文地址:
:Clojuratica 安裝配置指導: Clojure + Mathematica
本文參考以下文檔: