Vec-0.9.0: Fixed-length lists and low-dimensional linear algebra.Source codeContentsIndex

Vec : a library for fixed-length lists and low-dimensional linear algebra

Scott E. Dillard

darcs :


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.


  • Simplicity : Beyond the initial complexities of type-level lists and numbers, I've tried to keep the API simple. There is no vector-space class, nor a complicated hierarchy of linear/affine/projective transformations. These can be added on top of the library easily.
  • Purity : The library is written in the functional style. For most functions this does not hinder performance at all, but some I am still working on (Gaussian elimination) so if this library is a bottleneck you can easily drop down to C.
  • Low Dimension : Although the dimensionality is limited only by what GHC will handle, the library is meant for 2,3 and 4 dimensions. For general linear algebra, check out the excellent hmatrix library and blas bindings.

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.

GHC Extensions

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.

Related Work

See previous work by David Menendez,

and of course Oleg Kiselyov,

Other vector and linear algebra packages :

vector-space, by Conal Elliott :

hmatrix, by Alberto Ruiz :

blas bindings, by Patrick Perry :

templatized geometry library (C++), by Oliver Kreylos :

module Data.Vec.Base
module Data.Vec.LinAlg
module Data.Vec.Packed
module Data.Vec.Nat
Produced by Haddock version 2.3.0