diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-06-21 08:23:15 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-06-21 08:23:15 +0000 |
commit | d18a86d37d55a39d4ec9b16397dd59f35aa13688 (patch) | |
tree | 1ed0ce908efc6a364398b0814692df9f04e894db | |
parent | b830956a037b6122f83ed6117596b2ef510eb911 (diff) |
Binary instances for Vector and Matrix by Vivian McPhail
-rw-r--r-- | THANKS | 3 | ||||
-rw-r--r-- | hmatrix.cabal | 5 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 21 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 19 |
4 files changed, 45 insertions, 3 deletions
@@ -73,5 +73,6 @@ and all the people in the Haskell mailing lists for their help. | |||
73 | - Max Suica simplified the installation on Windows and improved the instructions. | 73 | - Max Suica simplified the installation on Windows and improved the instructions. |
74 | 74 | ||
75 | - Vivian McPhail fixed configure.hs to allow non-root user installation in | 75 | - Vivian McPhail fixed configure.hs to allow non-root user installation in |
76 | case that /tmp is not writable. | 76 | case that /tmp is not writable. He also contributed Binary instances for |
77 | Vector and Matrix. | ||
77 | 78 | ||
diff --git a/hmatrix.cabal b/hmatrix.cabal index cbf4a55..e7a64c8 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -1,5 +1,5 @@ | |||
1 | Name: hmatrix | 1 | Name: hmatrix |
2 | Version: 0.9.3.0 | 2 | Version: 0.10.0.0 |
3 | License: GPL | 3 | License: GPL |
4 | License-file: LICENSE | 4 | License-file: LICENSE |
5 | Author: Alberto Ruiz | 5 | Author: Alberto Ruiz |
@@ -74,7 +74,8 @@ library | |||
74 | Build-Depends: base >= 4 && < 5, | 74 | Build-Depends: base >= 4 && < 5, |
75 | array, | 75 | array, |
76 | storable-complex, | 76 | storable-complex, |
77 | process | 77 | process, |
78 | ghc-binary | ||
78 | 79 | ||
79 | Extensions: ForeignFunctionInterface, | 80 | Extensions: ForeignFunctionInterface, |
80 | CPP | 81 | 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) | |||
44 | import Data.List(transpose,intersperse) | 44 | import Data.List(transpose,intersperse) |
45 | import Data.Complex | 45 | import Data.Complex |
46 | 46 | ||
47 | import Data.Binary | ||
48 | import Foreign.Storable | ||
49 | import Control.Monad(replicateM) | ||
50 | |||
51 | ------------------------------------------------------------------- | ||
52 | |||
53 | instance (Binary a, Element a, Storable a) => Binary (Matrix a) where | ||
54 | put m = do | ||
55 | let r = rows m | ||
56 | let c = cols m | ||
57 | put r | ||
58 | put c | ||
59 | mapM_ (\i -> mapM_ (\j -> put $ m @@> (i,j)) [0..(c-1)]) [0..(r-1)] | ||
60 | get = do | ||
61 | r <- get | ||
62 | c <- get | ||
63 | xs <- replicateM r $ replicateM c get | ||
64 | return $ fromLists xs | ||
65 | |||
66 | ------------------------------------------------------------------- | ||
67 | |||
47 | -- | creates a matrix from a vertical list of matrices | 68 | -- | creates a matrix from a vertical list of matrices |
48 | joinVert :: Element t => [Matrix t] -> Matrix t | 69 | joinVert :: Element t => [Matrix t] -> Matrix t |
49 | joinVert ms = case common cols ms of | 70 | 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 | |||
29 | import Numeric.GSL.Vector | 29 | import Numeric.GSL.Vector |
30 | -- import Data.Packed.ST | 30 | -- import Data.Packed.ST |
31 | 31 | ||
32 | import Data.Binary | ||
33 | import Foreign.Storable | ||
34 | import Control.Monad(replicateM) | ||
35 | |||
36 | ------------------------------------------------------------------- | ||
37 | |||
38 | instance (Binary a, Storable a) => Binary (Vector a) where | ||
39 | put v = do | ||
40 | let d = dim v | ||
41 | put d | ||
42 | mapM_ (\i -> put $ v @> i) [0..(d-1)] | ||
43 | get = do | ||
44 | d <- get | ||
45 | xs <- replicateM d get | ||
46 | return $ fromList xs | ||
47 | |||
48 | ------------------------------------------------------------------- | ||
49 | |||
50 | |||
32 | {- | Creates a real vector containing a range of values: | 51 | {- | Creates a real vector containing a range of values: |
33 | 52 | ||
34 | @\> linspace 5 (-3,7) | 53 | @\> linspace 5 (-3,7) |