I have a little haskell matrix library for fixed sized matricies on github:
http://github.com/jvranish/VectorMatix
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
define functionstype Matrix=[[Double]]multMM :: Matrix -> Matrix -> Matrix --multiplies two matricesdet :: Matrix -> Double --computes the determinant of a matrixinv :: Matrix -> Matrix --inverts a matrixi stuck on those problemscan any one help me out?
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe