Vec : a library for fixed-length lists and low-dimensional linear algebra
Scott E. Dillard firstname.lastname@example.org
Vectors are represented by lists with type-encoded lengths. The constructor is :., which acts like a cons both at the value and type levels, with () taking the place of nil. So x:.y:.z:.() is a 3d vector. The library provides a set of common list-like functions (map, fold, etc) for working with vectors. Built up from these functions are a small but useful set of linear algebra operations: matrix multiplication, determinants, solving linear systems, inverting matrices.
To the point of simplicity, vectors and matrices are instances of Num and Fractional. All arithmetic is done component-wise and literals construct uniform vectors and matrices. There are many interesting projects aiming to overhaul Haskell's number classes, but for now the type of (*) is a -> a -> a so that's what we're working with. It is easy to incorporate this library into a more mathematically consistent class hierarchy (provided you can design one.)
The rule is simple : If the method is unary, it's a map. If it's binary, it's a zipWith.
(:.) is strict in both arguments, but it is also polymorphic, so at runtime vectors will be realized as linked lists, albeit with less pattern matching. However the library provides packed representations for 2,3 and 4d vectors of Ints, Floats and Doubles. Vec3F x y z constructs a packed vector of unboxed Floats. Functions pack and unpack convert between packed and unpacked types. When vector operations are bracketed by pack and unpack, GHC can unfold them into very efficient code. The Storable instances for vectors also generate fast code. Without optimizations, the code falls back into linked-list mode. The optimizations depend on inlining, so you may need to increase your unfolding threshold in certain situations.
This library makes heavy use of functional dependencies. I have tried to tweak things so that they "just work." However, every now and then you will get incomprehensible error messages, usually about how this isn't an instance of that. These are how type errors typically manifest, so first double check to make sure you aren't trying to mix vectors of different dimension or component types. If you still get these errors, manual type annotations usually make them go away.
See previous work by David Menendez, http://haskell.org/pipermail/haskell/2005-May/015815.html
and of course Oleg Kiselyov, http://okmij.org/ftp/papers/number-parameterized-types.pdf
Other vector and linear algebra packages :
vector-space, by Conal Elliott : http://hackage.haskell.org/package/vector-space
hmatrix, by Alberto Ruiz : http://hackage.haskell.org/package/hmatrix
blas bindings, by Patrick Perry : http://hackage.haskell.org/package/blas
templatized geometry library (C++), by Oliver Kreylos : http://graphics.cs.ucdavis.edu/~okreylos/ResDev/Geometry/index.html
|Produced by Haddock version 2.3.0|