[Haskell-cafe] Haskell, C and Matrix Multiplication

Pedro Vasconcelos pbv at dcc.fc.up.pt
Mon Jan 17 15:48:39 CET 2011


On Mon, 17 Jan 2011 10:38:30 +0000
Blake Rain <blake.rain at gmail.com> wrote:

> So sorry, I meant:
> 
> mult :: (Num a) => [[a]] -> [[a]] -> [a]
> mult m1 m2 = [foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose
> m2]
> 

Shouldn't the result of multiplying an (n,k)-matrix by an (k,m)-matrix
be an (n,m)-matrix? The way you've written it the result will be a list
of length n*m.

> 
> > 
> > Regarding performance: did you make sure you're forcing the
> > evaluation of the result matrix? 
> 
> I thought I was. It's printing the results as expected. Could be I'm
> just imagining I am and suffering a delusion or misunderstanding.
> 

Another issue to look out for is let-floating: if you perform the same
matrix multiplication repeatedly, the compiler might very well lift
the matrix calculation outside the loop.

Pedro



More information about the Haskell-Cafe mailing list