[Haskell-cafe] matrix question

Job Vranish jvranish at gmail.com
Tue Feb 2 10:08:41 EST 2010

I have a little haskell matrix library for fixed sized matricies on github:
which I've just realized is horribly out of date...I'll update it tonight
and probably push it to hackage too...

but if you're really want to stick with the [[Double]] type,
you can add a Foldable and Traversable instance to ZipLists and do this:

instance Foldable ZipList where
  foldMap f (ZipList x) = foldMap f x

instance Traversable ZipList where
  traverse f (ZipList x) = ZipList <$> traverse f x

toZipList a = ZipList $ fmap ZipList a
fromZipList a = getZipList $ fmap getZipList a

multMM :: (Num a) => [[a]] -> [[a]] -> [[a]]
multMM a b = fromZipList $ multMMA (toZipList a) (toZipList b)

-- I about fell off my chair when I discovered you could do matrix
multiplication like this:
multMMA :: (Traversable f, Num a, Applicative f, Applicative row,
Applicative col, Traversable col) =>
           row (f a) -> f (col a) -> row (col a)
multMMA a b = traverse (liftA2 dot a . pure) (sequenceA b)

dot :: (Foldable t, Num a, Applicative t) => t a -> t a -> a
dot a b = sum $ pure (*) <*> a <*> b

My matrix library uses a Gaussian elimination function (which operates on
lists) as well as det and inv functions which should be easily adaptable to
work on lists.

I'll make sure to push the updated code up tonight.

- Job

On Tue, Feb 2, 2010 at 7:15 AM, 조광래 <kwangraecho at gmail.com> wrote:

> define functions
> type Matrix=[[Double]]
> multMM :: Matrix -> Matrix -> Matrix --multiplies two matrices
> det :: Matrix -> Double --computes the determinant of a matrix
> inv :: Matrix -> Matrix --inverts a matrix
> i stuck on those problems
> can any one help me out?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100202/981e3188/attachment.html

More information about the Haskell-Cafe mailing list