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
本文参考以下文档: