From d18a86d37d55a39d4ec9b16397dd59f35aa13688 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Mon, 21 Jun 2010 08:23:15 +0000 Subject: Binary instances for Vector and Matrix by Vivian McPhail --- lib/Data/Packed/Matrix.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib/Data/Packed/Matrix.hs') diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 3147e13..0c21b97 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -44,6 +44,27 @@ import Text.Printf(printf) import Data.List(transpose,intersperse) import Data.Complex +import Data.Binary +import Foreign.Storable +import Control.Monad(replicateM) + +------------------------------------------------------------------- + +instance (Binary a, Element a, Storable a) => Binary (Matrix a) where + put m = do + let r = rows m + let c = cols m + put r + put c + mapM_ (\i -> mapM_ (\j -> put $ m @@> (i,j)) [0..(c-1)]) [0..(r-1)] + get = do + r <- get + c <- get + xs <- replicateM r $ replicateM c get + return $ fromLists xs + +------------------------------------------------------------------- + -- | creates a matrix from a vertical list of matrices joinVert :: Element t => [Matrix t] -> Matrix t joinVert ms = case common cols ms of -- cgit v1.2.3