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

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

本文参考如下文档:

时间: 2024-07-30 07:49:25

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

SVN1.6服务端和客户端安装配置指导

本节向大家描述SVN1.6服务端和客户端安装配置步骤,随着SVN的快速发展,版本也进行了升级更新,本节就和大家一起学习一下SVN1.6服务端和客户端安装配置步骤,欢迎大家一起来学习.下面是具体介绍.1.软件下载下载SVN1.6服务器程序.http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91[注意]如果之前已经安装了TortoiseSVN客户端,必须选择与之配套的SVN服务端版本,否则会出现各种问题,可以从Tor

Archlinux 安装配置指导 2015-05-24

因为用的Linode VPS的系统是Archlinux的,想在本地弄个系统做测试用,这样比较方便.然后发现自己在6年前做的一个Archlinux 安装配置Flash,好怀念的赶脚. 时过进迁,没想到Archlinux现在没有安装向导了. 已经不再适用了. 安装前准备 下载安装光盘 https://www.archlinux.org/download/ 启动光盘并进入系统 1. 划分硬盘 使用Fdisk-l  命令查看硬盘分区 其中 /dev/sda 就是我们需要安装的目标磁盘 cfdisk 进行

Anaconda多环境多版本python配置指导

Anaconda多环境多版本python配置指导 来自:http://www.jianshu.com/p/d2e15200ee9b 原文地址:http://conda.pydata.org/docs/test-drive.html conda测试指南 在开始这个conda测试之前,你应该已经下载并安装好了Anaconda或者Miniconda注意:在安装之后,你应该关闭并重新打开windows命令行. 一.Conda测试过程: 使用conda.首先我们将要确认你已经安装好了conda 配置环境.

    Junipersrx100防火墙配置指导

Junipersrx100防火墙配置指导 # 一.初始化安装 1.1设备登录 Juniper SRX系列防火墙.开机之后,第一次必须通过console 口(通用超级终端缺省配置)连接SRX   , 输入root 用户名登陆,密码为空,进入到SRX设备之后可以开始加载基线配置. 特别注意:SRX低端系列防火墙,在第一次登陆时,执行命令 "show configuration" 你会发现系统本身已经具备一些配置内容(包括DNS名称.DHCP服务器等),建议删除这些配置,重新配置. Dele

Ubuntu16.04 安装配置Caffe

Caffe已经是第三次安装配置了,为什么是第三次呢?因为我实在是低估了深度学习对于硬件的要求.第一次我在自己笔记本上配置的单核,CPU only ...  结果是,样例数据跑了4小时,这还怎么玩?第二次在台式机上,因为台式机比较low,I5处理器4核,没有NVIDIA的GPU.我把别人训练好的模型下载下来,然后自己测试,发现真的成功了,心里小激动~ 然而,当我自己训练模型时,我训练7天.....  关键是7天了还在跑..... 心想,我这个穷逼难道要自己掏钱买个服务器?那怎么可能.还好,老师人非

SVN+FTP服务器搭建(一)——SVN安装配置篇

Subversion是一个自由,开源的版本控制系统.在Subversion管理下,文件和目录可以超越时空.Subversion将文件存放在中心版本库里.这个版本库很像一个普通的文件服务器,不同的是,它可以记录每一次文件和目录的修改情况.这样就可以籍此将数据恢复到以前的版本,并可以查看数据的更改细节.正因为如此,许多人将版本控制系统当作一种神奇的“时间机器”. 就服务器而言,个人认为最好用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用. 需要的工具(务必下载服务器

Zabbix Agent-Windows平台配置指导

zabbix是一个CS结构的监控系统,支持ping,snmp等很多的监控,但是大部分的监控任务需要客户端agentd的支持才能用.server端侦听在10051端口,客户端侦听在10050端口.1.安装介质ZABBIX客户端http://www.zabbix.com/downloads/2.0.6/zabbix_agents_2.0.6.win.zip 环境及约束操作系统:Windows 2003 2.安装过程1) 将zabbix文件夹解压后放在C:\ 2) C:\zabbix\下 新建一个za

multipath 安装配置

二. 安装配置 2.1 安装Multipath 查看相关包: [[email protected] ~]# rpm -qa|grep device-mapper device-mapper-multipath-0.4.7-30.el5 device-mapper-event-1.02.32-1.el5 device-mapper-1.02.32-1.el5 如果没有安装,从系统的安装文件里找到这集个包: device-mapper-1.02.32-1.el5.i386.rpm device-ma

Linux 下编译并安装配置 Qt 4.53全过程

最近准备做 Nokia 的 Symbian,Maemo 下触摸屏开发.考虑到程序的跨平台可移植性,最终选择使用 Qt 开发.相对来说,国内关于 Qt 相关文档并不算很多.作者将 Linux 下编译并安装配置 Qt 全过程总结了一下,只希望可以方便更多的朋友! 1.获得源代码         src 官网下载地址:ftp://ftp.qt.nokia.com/qt/source/        2009 年 10 月 1 日发布的 qt-x11-opensource-src-4.5.3.tar.g