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 --- THANKS | 3 ++- hmatrix.cabal | 5 +++-- lib/Data/Packed/Matrix.hs | 21 +++++++++++++++++++++ lib/Data/Packed/Vector.hs | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/THANKS b/THANKS index 8797578..d8a9e0e 100644 --- a/THANKS +++ b/THANKS @@ -73,5 +73,6 @@ and all the people in the Haskell mailing lists for their help. - Max Suica simplified the installation on Windows and improved the instructions. - Vivian McPhail fixed configure.hs to allow non-root user installation in - case that /tmp is not writable. + case that /tmp is not writable. He also contributed Binary instances for + Vector and Matrix. diff --git a/hmatrix.cabal b/hmatrix.cabal index cbf4a55..e7a64c8 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal @@ -1,5 +1,5 @@ Name: hmatrix -Version: 0.9.3.0 +Version: 0.10.0.0 License: GPL License-file: LICENSE Author: Alberto Ruiz @@ -74,7 +74,8 @@ library Build-Depends: base >= 4 && < 5, array, storable-complex, - process + process, + ghc-binary Extensions: ForeignFunctionInterface, CPP 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