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 +++++++++++++++++++++ lib/Data/Packed/Vector.hs | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'lib/Data') 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 diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index 472240c..f6b3fc6 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs @@ -29,6 +29,25 @@ import Data.Packed.Internal import Numeric.GSL.Vector -- import Data.Packed.ST +import Data.Binary +import Foreign.Storable +import Control.Monad(replicateM) + +------------------------------------------------------------------- + +instance (Binary a, Storable a) => Binary (Vector a) where + put v = do + let d = dim v + put d + mapM_ (\i -> put $ v @> i) [0..(d-1)] + get = do + d <- get + xs <- replicateM d get + return $ fromList xs + +------------------------------------------------------------------- + + {- | Creates a real vector containing a range of values: @\> linspace 5 (-3,7) -- cgit v1.2.3