[Haskell-cafe] matrix computations based on the GSL

Henning Thielemann lemming at henning-thielemann.de
Tue Jul 5 14:17:58 EDT 2005


On Tue, 5 Jul 2005, Keean Schupke wrote:

> Henning Thielemann wrote:
>
> >I'm uncertain about how who want to put the different kinds of
> >multiplication into one method, even with multi-parameter type classes.
> >You need instances
> >
> > (*) :: Matrix -> Matrix -> Matrix
> > (*) :: RowVector -> Matrix -> RowVector
> > (*) :: Matrix -> ColumnVector -> ColumnVector
> > (*) :: RowVector -> ColumnVector -> Scalar
> > (*) :: ColumnVector -> RowVector -> Matrix
> > (*) :: Scalar -> RowVector -> RowVector
> > (*) :: RowVector -> Scalar -> RowVector
> > (*) :: Scalar -> ColumnVector -> ColumnVector
> > (*) :: ColumnVector -> Scalar -> ColumnVector
> >
> >but you have to make sure that it is not possible to write an expression
> >which needs
> > (*) :: Matrix -> RowVector -> RowVector

This was my reply to Jacques Carette who suggested to distinguish row and
column vectors by their _type_. His revised suggestion was to use phantom
types. Then the types changes to

(*) :: Matrix -> Matrix -> Matrix
(*) :: Vector Row -> Matrix -> Vector Row
(*) :: Matrix -> Vector Column -> Vector Column
(*) :: Vector Row -> Vector Column -> Scalar
...

but the problem remains ...

> >Further you need
> > transpose :: RowVector -> ColumnVector
> > transpose :: ColumnVector -> RowVector
> > transpose :: Matrix -> Matrix
> >and you must forbid, say
> > transpose :: RowVector -> RowVector
> >
> >
> Of course if they are all of type Matrix this problem disappears.

All type problems disappear when we switch to exclusive String
representation. 8-]

> What is the difference between a 1xN matrix and a vector? Please explain...

My objections to making everything a matrix were the objections I sketched
for MatLab.

The example, again: If you write some common expression like

transpose x * a * x

then both the human reader and the compiler don't know whether x is a
"true" matrix or if it simulates a column or a row vector. It may be that
'x' is a row vector and 'a' a 1x1 matrix, then the expression denotes a
square matrix of the size of the vector simulated by 'x'. It may be that
'x' is a column vector and 'a' a square matrix. Certainly in most cases I
want the latter one and I want to have a scalar as a result. But if
everything is a matrix then I have to check at run-time if the result is a
1x1 matrix and then I have to extract the only element from this matrix.
If I omit the 1x1 test mistakes can remain undiscovered. I blame the
common notation x^T * A * x for this trouble since the alternative
notation <x, A*x> can be immediately transcribed into computer language
code while retaining strong distinction between a vector and a matrix
type.

More examples? More theory?



More information about the Haskell-Cafe mailing list