# [Haskell-cafe] Simple matrix

Udo Stenzel u.stenzel at web.de
Thu Jun 22 05:57:37 EDT 2006

Bjorn Lisper wrote:
> Here is one way to do it. First, you have to interpret operations on
> matrices as being elementwise applied. E.g, (*) is interpreted as zipWith
> (zipWith (*)) rather than matrix multiply

What's this, the principle of greatest surprise at work?  Nonono, (*)
should be matrix multiplication, fromInteger x should be (x * I) and I
should be the identity matrix.  Now all we need is an infinitely large
I, and that gives:

instance Num a => Num [[a]] where
(+) = zipWith (zipWith (+))
(-) = zipWith (zipWith (-))
negate = map (map negate)
fromInteger x = fix (((x : repeat 0) :) . map (0:))
m * n = [ [ sum \$ zipWith (*) v w | w <- transpose n ] | v <- m ]

Udo.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060622/789a8a0c/attachment.bin

More information about the Haskell-Cafe mailing list