
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?

On 2 Feb 2010, at 13:15, 조광래 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?
Plug in the word "matrix" on http://haskell.org/; then there comes up matches "Prelude extensions" which has that type. There is also a Numeric Prelude. Hans

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
On Tue, Feb 2, 2010 at 7:15 AM, 조광래
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

http://hackage.haskell.org/package/hmatrix-0.8.1.1
it wraps gls, blas and lapack (so you need to install the libraries).
On Tue, Feb 2, 2010 at 4:08 PM, Job Vranish
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
On Tue, Feb 2, 2010 at 7:15 AM, 조광래
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- happy hacking... man

man
http://hackage.haskell.org/package/hmatrix-0.8.1.1
it wraps gls, blas and lapack (so you need to install the libraries).
There's also the blas package if you just want blas support. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On Feb 3, 2010, at 1:15 AM, 조광래 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
Stuck how, exactly? Writing a good matrix inversion procedure is seriously tricky in any programming language (unless you are using rational arithmetic, in which case it's tricky in a different way). Indeed, I think every book I've come across says "DON'T compute multMM x (inv y)" but instead use a "solve" method. You'll find matrix multiplication easy enough if you draw a few diagrams and think in terms of operations on rows and columns rather than in terms of array elements. For matrix inversion, Gaussian elimination with partial pivoting is tedious rather than difficult. The same conversion-to-triangular process will give you determinant.

This is identical to the homework problem posted on stackoverflow:
http://stackoverflow.com/questions/2182300/haskell-matrix-scalar-multilple-q...
Do not post homework problems to the cafe!
If you feel compelled to then identify an aspect that is tricky to
you, show us what you tried, and identify it as homework.
Thomas
On Tue, Feb 2, 2010 at 4:15 AM, 조광래
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Hans Aberg
-
Ivan Lazar Miljenovic
-
Job Vranish
-
man
-
Richard O'Keefe
-
Thomas DuBuisson
-
조광래