码迷,mamicode.com
首页 > 其他好文 > 详细

Clojuratica 安装配置指导: Clojure + Mathematica 的神奇组合

时间:2015-05-17 18:56:54      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

Clojuratica 安装配置指导: Clojure + Mathematica 的神奇组合

目录

概念介绍:Clojuratica=Clojure+Mathematica

Clojuratica 简单说就是把 ClojureMathematica 组合起来了,可以通过 Clojure 来调用 Mathematica 的数千个函数。

按照作者的说法是 Clojuratica 集二者之长处,非常高大上,而且还易于使用,下面是 Clojuratica 站点对其优点的描述:

  • 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.

  • 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.

  • 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.

  • 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.

不过该项目最近的版本更新是2009年的Version 2 alpha 2

周边环境需求

因为 ClojureMathmetica 都是多平台可用,因此 Clojuratica 理论上同时支持 Windows/OSX/Linux/Unix 多个平台,本文的环境是 Mac OSX 10.9

安装配置 Clojuratica,需要事先安装好如下软件:

  • Git
  • ant
  • Clojure + lein

具体的安装步骤就不多说了,使用 brew install gitbrew install ant 安装好 gitant,再用 git 安装 Clojure,详细教程可以自己搜索。

周边环境基本就这些要求。

从GitHub安装Clojuratica

先进入你准备存放 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

首先要把 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$

然后要用 leinclojuratica.jarJLink.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 的新项目了。

创建一个新项目运行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$ 

在依赖项中增加 JLinkclojuratica,改为如下:

(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 文件,这段代码用于建立 ClojureMathematica 的连接,内容如下:

(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 一致。

很好,到现在为止,我们成功地创建了一个新的项目,并且完成这个项目的相关配置,接下来就可以尝试着运行了。

Clojuratica使用实例

首先在这个项目的目录内通过 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

本文参考如下文档:

Clojuratica 安装配置指导: Clojure + Mathematica 的神奇组合

标签:

原文地址:http://my.oschina.net/freeblues/blog/415931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!