clojurecomputer-algebra-systems

Symbolic Matrix Library in clojure, advice needed


While there are plenty of java libraries for linear algebra, Clojure currently does not have an idiomatic computer algebra system that would include support for symbolic math. As a start I figure I can start working on something simple.

As a first step, I think getting the data structures right would be a good start.

Step 1: Implement a persistent matrix

I will be using deftype (or reify), and for now, for ease of implementation, I will use a hashmap for storage (Please suggest an alternative if think its better but state tradeoffs). (One can imagine many different implementations depending on your performance requirements, such as using arrays or delegating to an external java library and implementing some sort of transients interface.)

My question is, what interfaces/protocols should I be considering to implement? (In general, what is a good listing of all the protocols/interfaces that clojure uses?) Also is there any advice on how to implement these?

My list of things to implement:

-Assoc'ing would be useful, to modify sections of the matrix in an immutable manner

-treating the matrix as a function as an accessor of the elements, I was thinking you could pass a two-tuple to return a single element, a single value (index by width*y+x), hashmap to get columns, rows, or minor, via custom query hashmap/language.

Note, my goal at the moment is to design good abstractions that enable flexibility in choosing implementations.


Solution

  • I more or less manage the linear algebra modules in SymPy, a major Python symbolics package. I'll give you my perspective coming from a traditional computer algebra system.

    We have three separate implementations for three important use cases

    1. Mutable matrices -- Despite being in Python SymPy is immutable by default. We've actually broken this rule for matrices. Matrix algorithms are the standard example of where you really need to switch to mutability for performance reasons.
    2. Immutable matrices -- But you'd like to have the option to switch back. Our intended workflow is as follows

      1. Build an immutable matrix
      2. Switch to mutability and perform some algorithm
      3. Switch back to immutability and present this to the user
    3. Matrix Symbols -- Often you don't need to deal with explicit entries in the matrix but would rather deal with the idea of a matrix. See this scicomp.stackexchange post. This is my current work and I find it to be very exciting.

    There are other splits like dense representations vs sparsity. Symbolic linear algebra is a big and important field. I look forward to seeing the Clojure community's collective solution.