Haskell Matrix Library...

Keean Schupke k.schupke at imperial.ac.uk
Thu Jun 9 08:08:48 EDT 2005

Henning Thielemann wrote:

>>- would people want a library like MatLab where matrices are instances
>>of Num/Fractional/Floating and can be used in normal math equations...
>I'm pretty unhappy with all these automatisms in MatLab which prevent fast
>detection of mistakes, e.g. treating vectors as column or row matrices,
>random collapses of singleton dimensions, automatic extension of
>singletons to vectors with equal components.
> What is the natural definition of matrix multiplication? The element-wise
>multiplication (which is commutative like the multiplication of other Num
>instances) or the matrix multiplication? I vote for separate functions or
>infix operators for matrix multiplications, linear equation system
>solvers, matrix-vector-multiplication, matrix and vector scaling. I wished
>to have an interface to LAPACK for the floating point types because you
>can hardly compete with algorithms you write in one afternoon.
> Btw. when considering the class hierarchy of the numeric prelude project
>  http://cvs.haskell.org/darcs/numericprelude/
> you would naturally put matrices and vectors in Additive, Module and
>VectorSpace class but not in Fractional.
I think we can have the best of both worlds...

>>- operations like cos/sin/log can be applied to a matrix (and apply to
>>each element like in MatLab) ...
>MatLab must provide these automatisms because it doesn't have proper
>higher functions. I think the Haskell way is to provide a 'map' for
>matrices. Otherwise the question is: What is the most natural 'exp' on
>matrices? The elementwise application of 'exp' to each element or the
>matrix exponentation?
MatLab has "exp" which works element wise and "expm" which does the

I have made the matrix an instance of Num/Fractional and Float. The
float operations all apply element wise.
I have constructors "scalar" and "vector" to produce a 1x1 and a
row-matrix respectively. The '*' operator
does matrix multiplication (as in MatLab) and I have a new class for
matrices which defines ".*" for
element wise multiplication. Like matlab, if you try to multiply a
scalar (1x1 matrix) by another matrix
you get element wise multiplication instead of matrix multiplication...
(as a scalar can only really be multiplied
by another scalar this is unabiguous).

What about implementation?

At the moment I am coding the vector operations in Haskell (a foreign
interface to lapack or Intel MKL can
come later)... and am using a UArray - is this portable enough - or
should I parameterise by array type
(using IArray) even though this might result in type ambiguities?


More information about the Libraries mailing list