What is the linguistic way to program matrices in Haskell?

Simpler as it should be when I need to use matrices in Haskell I'm afraid. My strategy is to pick a specific type (REPA, Vector, List, IntMap, etc.) and a program for it. For example, I used REPA to solve the Euler 11 problem , and I use it frequently in marathons. Unfortunately, REPA is not a particularly friendly API - it requires compiling type annotations, thinking about representing and keeping track of it, transforming it around ... also, the indexes are reversed. All of this means that I spend a lot of time just looking at the docs and trying to align the types correctly, which is deadly when you're in a marathon.

I could just use vectors / lists, but that's awkward too, since then I need to use functions toIndex :: [Int] → Int; fromIndex Int → [Int]

every time I index the vector.

I also tried to create a wrapper for Data.Vector, for example data Matrix a = Matrix { shape :: [a], buffer :: Vector a }

, but I soon noticed that I also had to create a wrapper for each individual vector function, as well as different types to match mutable vectors, etc., and it was a mess.

In the end, all I need is an easy way to deal with matrices - something like:

matrix = Matrix.fromList [3,3] [1,2,3,4,5,6,7,8,9]
main = do
    matrix' <- set matrix [1,1] 0
    print $ get matrix [1,1]
    print $ sum matrix

      

Or anything else that allows me to just think of matrices as mathematical objects rather than a concrete implementation, but I haven't been able to find a simple linguistic way. How would you do it? I wonder if Lance can help in any way?

+3


source to share


1 answer


  • Do you mean really just matrices (i.e. arrays with two indices)? Then hmatrix is probably the best fit for you. Its matrix type is a little different from your wrapper Vector

    , and its interface is indeed very similar to "purely functional Matlab" (a philosophy I personally cannot follow, but oh well ...), anyway, you get efficient linear algebra operations. slicing, etc. by means of GSL routines. Of course, this library doesn't actually do stateful things, it maintains a purely functional interface and relies on optimizations in built-in routines.

  • Do you mean that generic multidimensional arrays / tensors are more interested in elementary operations rather than linear algebra, and need updated updates? Then the good old array library might be fine. It has been somewhat overshadowed in recent years Vector

    and repa

    , IMO, the indexing paradigm Data.Ix

    is actually pretty nice. Sorting a less mature, but also less "overloaded" version repa

    .

  • If you are mainly interested in linear algebra, you should at least check out some of the more abstract libraries. I really like the vector-space interface , which is very general and mathematical, it completely avoids writing operations to a specific base, which does a lot to catch "mathematical errors".

  • And although you mention lens

    es, there is linear - which is elegant and abstract in a different way. IMO misses the point of linearity a bit (mathematically), but still pretty cool and definitely offers more operations than vector-space

    .

  • Oh, and finally there is matrix . It is quite similar to hmatrix

    but implemented in native Haskell and not in the GSL bindings, which means it is not as rich in highly optimized LA algorithms, but also not as heavy in dependency. It also looks a little more elegant.



+4


source







All Articles