Symbolic matrix library in clojure, recommendations

Although there are many java libraries for linear algebra, Clojure does not currently have an idiomatic computer algebra system that will include support for symbolic mathematics. In the beginning, I believe that I can start working on something simple.

As a first step, I think that the proper creation of data structures would be a good start.

Step 1: Implement the Constant Matrix

I will use deftype (or reify ), and for now, for ease of implementation, I will use a hash map for storage (please offer an alternative if you think this is better than inter-state trade-offs). (You can imagine many different implementations depending on your performance requirements, such as using arrays or delegating to an external java library and implementing some kind of transient interface.)

My question is: which interfaces / protocols should be considered for implementation? (In general, what is a good list of all the protocols / interfaces that Clojure uses?) Are there also any recommendations for their implementation?

My list of things to implement:

-Assoc'ing would be useful to modify matrix sections in an unchanged way

-describing a matrix as a function as an element access element, I thought you could pass two tuples to return one element, one value (index by width * y + x), hashmap to get columns, rows or minors through a user query hashmap / language.

Please note: my goal at the moment is to develop good abstractions that provide flexibility in choosing implementations.

+4
source share
5 answers

I more or less manage linear algebra modules in SymPy , a large Python symbology package. I will give you my perspective coming from a traditional computer algebra system.

We have three separate implementations for three important use cases.

  • Mutable matrices - Although SymPy is unchanged by default in Python. We really violated this rule for matrices. Matrix algorithms are a standard example of where you really need to switch to volatility for performance reasons.
  • Immutable Matrices. But you would like to be able to switch back. Our intended workflow is as follows

    • Build an immutable matrix
    • Switch to volatility and execute some algorithm
    • Return to immutability and present it to the user
  • Matrix characters. Often you do not need to deal with explicit elements in a matrix, but it is better to deal with the idea of ​​a matrix. See This scicomp.stackexchange post . This is my current job , and I find it very interesting.

There are other splits, such as dense representations against sparseness. Symbolic linear algebra is a large and important field. I look forward to a collective decision by the Clojure community.

+2
source

Although it is a Java library, I developed vectorz for use from Clojure.

It offers many data structures and algorithms for high-performance vector and matrix computing. You may find this helpful. I currently use it for both computer graphics and machine learning in Clojure.

Matrices and vectors are mutable, but I found this to be a necessary evil: using immutable vectors and matrices was too slow for many algorithms.

I would be interested in creating an idiomatic clojure shell (including immutable versions of vectors and matrices) if enough people find this useful and / or want to participate.

+1
source

For readers of this topic, it may seem that the Gerry Sussman scmutils system is migrating to Clojure. This is a very advanced CAS, offering things like automatic differentiation, literal functions, etc., mainly in Maple style. It is used at MIT for advanced programs in dynamics and differential geometry, as well as for partial electrical engineering. It is also the system used by Sussman & Wisdom "Continuation" (LOL) for SICP, SICM (structure and interpretation of classical mechanics). Although originally a Scheme program, it is not a direct translation, but a renaming to use the best Clojure features. It was named sycmutil, both in honor of the original and the book. This excellent effort is the work of Colin Smith, and you can find it at https://github.com/littleredcomputer/sicmutils .

I believe that this could be the basis of an amazing computer algebraic system for Clojure, competitive with any other available. Although this is a rather huge beast, as you can imagine, and tons of things still need to be ported, there are a lot of fundamentals there, the system will differentiate and do great with literals and literal functions. This is a work in progress. The system also uses the β€œgeneral” approach proposed by Sussman, in which operations can be applied to functions, creating a great abstraction that simplifies the notation endlessly.

Here's the taster:

 > (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 differentiate > (zero 2.0) ==> 0 

SicmUtils introduces two new types of vectors "up" and "down" (called "structures"), they work pretty much as the vectors expected, but they have some special mathematical (covariant, contravariant) properties, as well as some programming properties, because 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))) 

Partial differentiation is fully supported.

 > (defn ff [xy] (* (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))) > ;; ie vector of results wrt to both variables 

The system also supports TeX output, polynomial factorization, and many other useful properties. However, much of what could be easily implemented was not done solely due to a lack of human resources. The graphical output and the notepad / worksheet interface (using Clojure Gorilla) also work.

I hope this goes to increase your appetite enough to visit the site and give it a whirl. You don't even need Clojure, you can run it from the provided jar file.

==========

PS. By the way, in order to directly answer the original quest, yes, sicmutils supports symbolic structures: you can customize the matrix view, where the elements are formulas, for example. rotation matrix, and then calculate (multiply) it by a given coordinate. It is surprisingly flexible in this sense.

+1
source

This may not answer your question, but I found that while working with Incanter, I needed to have access to elements using wraparound. The same is for vectors, it is sometimes useful to have negative indexes for accessing elements in the opposite direction from the end or to indices out of range for accessing elements from the very beginning. Naturally, calculating offsets adds overhead, but sometimes it's a feature you're willing to pay for

0
source

MPL is a simple symbolic math library written in the portable R6RS circuit. Here's a short introduction .

Since Scheme is Lisp, the MPL should be simple enough to connect to Clojure.

0
source

Source: https://habr.com/ru/post/1446550/


All Articles