Haskell Matrix Library...

Keean Schupke k.schupke at imperial.ac.uk
Fri Jun 10 06:33:21 EDT 2005


Just looked at the matlab code... It doesn't in general do too much
overloading - the only obvious example
I can find is with matrix multiplication '*'.  Here it multiplies
matrices normally unless either of the arguments
is a scalar, in which case the scalar is multipled by each element of
the other matrix. The other multiply
operator '.*' multiplies each element in one matix by the same element
in the other matrix (as in addition,
so matrices must be the same sizes)...

If I were going to implement everything in Haskell I would probably use
a data type like:

    data Matrix a e = Matrix [Int] (a Int e)
    type UMatrix e = Matrix UArray e

Where [Int] is a list of dimensions, ie for a scalar [ ], a vector [3],
2D matrix [3,4],
or a higher dimensional matrix [3,4,5,6].

It is possible to get static typing using type level naturals...
something like:

    data Matrix d a e = Matrix d (a Int e)

The use an HList (or equivalent) to encode the dimensionality of the
matrix in the type. For
example define [D0 x .. D9 x, DNil] as decimal type constructors, you
would then get:

     type Matrix2x3 a e = Matrix (HCons (D2 DNil) (HCons (D3 DNil)
HNil)) a Int e

However as the built in operators +,-,*,/ etc have the type "a -> a ->
a" you could not
define '*' to mean matrix multiplication.


At the moment I favour using dynamic size checking on the matrices, and
overloading all the
standard Haskell Numeric operators for matrices.

I seems easier to use FFI and LAPACK for the more complex matrix
operations... anyone know how to marshall data
for Fortran?

    Keean.

Bulat Ziganshin wrote:

>Hello Keean,
>
>Thursday, June 09, 2005, 8:52:55 PM, you wrote:
>
>KS>   (sometimes the best is the enemy of the good!)
>
>i think that best in this case will be splitting library to
>"low-level" and "high-level" parts, where low-level part implements
>functions like "miltipleMatrixByScalar", "miltipleMatrixByMatrix" and
>so on and high-level part implements matlab-like or strong-typed
>access to this functions via appropriate overloaded operators and so
>on. so in this case low-level==semantics and high-level==syntax
>
>moreover, low-level interface must be general enough to allow
>different implementations - haskell, template haskell, ffi->lipack
>
>
>  
>



More information about the Libraries mailing list