[Haskell-cafe] Re: Matrices

Patrick Perry patperry at stanford.edu
Sun Apr 19 21:14:53 EDT 2009


Hi Cetin,

This is probably the easiest way:

 > (m - es)^2/es
listMatrix (2,2)  
[0.6163270413689804,0.600918865334756,0.33217626255600896,0.3238718559921087 
]

This will create 2 temporary arrays.  Alternatively,

 > let res = listMatrix (shape m) [ (o-e)^2 / e | o <- colElems m | e  
<- colElems es ] where colElems = concatMap elems . cols
 > res
listMatrix (2,2)  
[0.6163270413689804,0.600918865334756,0.33217626255600896,0.3238718559921087 
]

In a later version of the library, "colElems" will probably be built- 
in.  This version has the disadvantage that it doesn't use BLAS calls.

If you *really* want to get tricky and eliminate temporary arrays:

 > runSTMatrix $ do
	res <- newCopyMatrix m
	subMatrix res es
	modifyWith (^2) res
	divMatrix res es
	return res


Now, to get the sum:

 > sumAbs $ vectorFromMatrix res
1.8732940252518542

or

 > sum $ elems res
1.8732940252518542

The first version will usually be more efficient, since it calls the  
BLAS1 function "dasum".


Patrick



More information about the Haskell-Cafe mailing list