Clojure的计算机代数 [英] Computer algebra for Clojure

查看:83
本文介绍了Clojure的计算机代数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短版: 我对某些Clojure代码感兴趣,这将使我能够指定x的变换(例如,排列,旋转),函数f(x)的值不变,这样我就可以高效地生成满足r的x序列= f(x). Clojure的计算机代数是否有发展? 对于(一个简单的)示例

Short version: I am interested in some Clojure code which will allow me to specify the transformations of x (e.g. permutations, rotations) under which the value of a function f(x) is invariant, so that I can efficiently generate a sequence of x's that satisfy r = f(x). Is there some development in computer algebra for Clojure? For (a trivial) example

(defn #^{:domain #{3 4 7} 
         :range #{0,1,2}
         :invariance-group :full} 
          f [x]  (- x x))

我可以致电(原像f#{0}),它将有效地返回#{3 4 7}.自然地,它也将能够正确注释共同域.有什么建议吗?

I could call (preimage f #{0}) and it would efficiently return #{3 4 7}. Naturally, it would also be able to annotate the codomain correctly. Any suggestions?

更长的版本: 我有一个特定的问题,使我有兴趣了解Clojure的计算机代数的开发.谁能指出我这样的项目?我的特定问题涉及找到满足F(x)= r的所有单词组合,其中F是排名函数,r是正整数.在我的特殊情况下,f可以计算为总和

Longer version: I have a specific problem that makes me interested in finding out about development of computer algebra for Clojure. Can anyone point me to such a project? My specific problem involves finding all the combinations of words that satisfy F(x) = r, where F is a ranking function and r a positive integer. In my particular case f can be computed as a sum

F(x)= f(x [0])+ f(x [1])+ ... f(x [N-1])

F(x) = f(x[0]) + f(x[1]) + ... f(x[N-1])

此外,我有一组不相交的集合S = {s_i},使得s中的a,b的f(a)= f(b),S中的s.因此,一种生成所有x的策略使得F( x)= r应该依赖于F的这种因式分解和每个s_i下f的不变性.换句话说,我计算包含S元素的总和的所有排列,这些元素的总和等于r,并将它们与每个s_i中元素的所有组合组成.这在下面非常草率地完成:

Furthermore I have a set of disjoint sets S = {s_i}, such that f(a)=f(b) for a,b in s, s in S. So a strategy to generate all x such that F(x) = r should rely on this factorization of F and the invariance of f under each s_i. In words, I compute all permutations of sites containing elements of S that sum to r and compose them with all combinations of the elements in each s_i. This is done quite sloppily in the following:

(use 'clojure.contrib.combinatorics)
(use 'clojure.contrib.seq-utils)


(defn expand-counter [c]
 (flatten (for [m c] (let [x (m 0) y (m 1)] (repeat y x)))))

(defn partition-by-rank-sum [A N f r]
  (let [M (group-by f A)
    image-A (set (keys M))
    ;integer-partition computes restricted integer partitions,
    ;returning a multiset as key value pairs
    rank-partitions (integer-partition r (disj image-A 0))
    ]
    (apply concat (for [part rank-partitions]
        (let [k (- N (reduce + (vals part)))
          rank-map (if (pos? k) (assoc part 0 k) part) 
          all-buckets (lex-permutations (expand-counter rank-map))
          ]
          (apply concat (for [bucket all-buckets]
        (let [val-bucket (map M bucket)
              filled-buckets (apply cartesian-product val-bucket)]
          (map vec filled-buckets)))))))))

这可以完成工作,但是错过了基础图片.例如,如果关联运算是乘积而不是总和,那么我将不得不重写部分.

This gets the job done but misses the underlying picture. For example, if the associative operation were a product instead of a sum I would have to rewrite portions.

推荐答案

下面的系统尚不支持组合语法,尽管添加它们并不需要付出很大的努力,已经存在大量的好代码,并且这可能是一个很好的平台,可以将其移植到该平台上,因为基础知识很不错.我希望在这里使用短插头是不合适的,这是我所知道的唯一的严重Clojure CAS,但是,嘿,这是什么系统...

The system below does not yet support combinatorics, though it would not be a huge effort to add them, loads of good code already exists, and this could be a good platform to graft it onto, since the basics are pretty sound. I hope a short plug is not inappropriate here, this is the only serious Clojure CAS I know of, but hey, what a system...

=======

此主题的读者可能会对Gerry Sussman的 scmutils 系统正在移植到Clojure感兴趣. 这是一种非常先进的CAS,提供了诸如 Maple 样式的自动区分,文字函数等功能. 麻省理工学院将其用于动力学和微分几何的高级程序,以及相当多的电气工程知识.它也是Sussman& Wisdom的 SICP SICM (经典力学的解释)的续集"(LOL)中使用的系统. 尽管最初是Scheme程序,但这不是直接翻译,而是完全重新编写以利用Clojure的最佳功能.为了纪念原著和这本书,它被命名为 sicmutils 这项出色的工作是Colin Smith的工作,您可以在 https://github.com/littleredcomputer/sicmutils <中找到它./a>.

It may be of interest to readers of this thread that Gerry Sussman's scmutils system is being ported to Clojure. This is a very advanced CAS, offering things like automatic differentiation, literal functions, etc, much in the style of Maple. It is used at MIT for advanced programs on dynamics and differential geometry, and a fair bit of electrical engineering stuff. It is also the system used in Sussman&Wisdom's "sequel" (LOL) to SICP, SICM (Structure and Interpretation of Classical Mechanics). Although originally a Scheme program, this is not a direct translation, but a ground-up rewrite to take advantage of the best features of Clojure. It's been named sicmutils, both in honour of the original and of the book This superb effort is the work of Colin Smith and you can find it at https://github.com/littleredcomputer/sicmutils .

我相信,这可以构成Clojure令人惊叹的计算机代数系统的基础,并且可以与其他任何竞争产品竞争.正如您可以想象的那样,尽管它是一个巨大的野兽,并且还有很多东西需要移植,但基础知识已经足够多了,系统将有所区别,并且可以很好地处理文字和文字函数.这是一项正在进行的工作.该系统还使用Sussman提倡的通用"方法,可以将运算应用于功能,从而创建了一个伟大的抽象,简化了无止境的表示法.

I believe that this could form the basis of an amazing Computer Algebra System for Clojure, competitive with anything else available. Although it is quite a huge beast, as you can imagine, and tons of stuff remains to be ported, the basics are pretty much there, the system will differentiate, and handle literals and literal functions pretty well. It is a work in progress. The system also uses the "generic" approach advocated by Sussman, whereby operations can be applied to functions, creating a great abstraction that simplifies notation no end.

这是一个品尝者:

> (def unity (+ (square sin) (square cos)))
> (unity 2.0)  ==>  1.0
> (unity 'x)   ==> 1 ;; yes we can deal with symbols
> (def zero (D unity))  ;; Let's differentiate
> (zero 2.0)   ==> 0

SicmUtils引入了两种新的向量类型上"和下"(称为结构"),它们的工作原理与您期望的向量相当,但是具有一些特殊的数学(协变,逆变)特性,并且还具有一些编程功能属性,因为它们是可执行的!

SicmUtils introduces two new vector types "up" and "down" (called "structures"), they work pretty much as you would expect vectors to, but have some special mathematical (covariant, contravariant) properties, and also some programming properties, in that they are executable!

> (def fnvec (up sin cos tan))  => fnvec
> (fnvec 1)   ==> (up 0.8414709848078965 0.5403023058681398 1.5574077246549023)
> ;; differentiated
> ((D fnvec) 1)  ==>  (up 0.5403023058681398 -0.8414709848078965 3.425518820814759) 
> ;; derivative with symbolic argument
> ((D fnvec) 'θ) ==> (up (cos θ) (* -1 (sin θ)) (/ 1 (expt (cos θ) 2)))  

完全支持部分区分

> (defn ff [x y] (* (expt x 3)(expt y 5)))
> ((D ff) 'x 'y) ==> (down (* 3 (expt x 2) (expt y 5)) (* 5 (expt x 3) (expt y 4))) 
> ;; i.e. vector of results wrt to both variables

该系统还支持TeX输出,多项式因式分解以及许多其他功能.但是,仅仅由于缺乏人力资源,还没有完成很多容易实现的事情.图形输出和记事本/工作表"界面(使用Clojure的Gorilla)也正在开发中.

The system also supports TeX output, polynomial factorization, and a host of other goodies. Lots of stuff, however, that could be easily implemented has not been done purely out of lack of human resources. Graphic output and a "notepad/worksheet" interface (using Clojure's Gorilla) are also being worked on.

我希望这可以使您的食欲足够大,以使您能够访问该网站并获得刺激.您甚至不需要Clojure,也可以在提供的jar文件中运行它.

I hope this has gone some way towards whetting your appetite enough to visit the site and give it a whirl. You don't even need Clojure, you could run it off the provided jar file.

这篇关于Clojure的计算机代数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆