diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-07-09 17:57:31 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-07-09 17:57:31 +0000 |
commit | 238a01e0c3b51569c28ede4259cdf33bf18eb94f (patch) | |
tree | 06e24c47b4c114f698b92d106202a97ceaa25b82 | |
parent | c4531ceaa524b1f8a84dcdf3456d7a7b2831f902 (diff) |
vectorMapM, vectorMapM_
-rw-r--r-- | hmatrix.cabal | 2 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 36 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 3 |
3 files changed, 40 insertions, 1 deletions
diff --git a/hmatrix.cabal b/hmatrix.cabal index e7a64c8..009219c 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -72,7 +72,7 @@ flag vector | |||
72 | library | 72 | library |
73 | 73 | ||
74 | Build-Depends: base >= 4 && < 5, | 74 | Build-Depends: base >= 4 && < 5, |
75 | array, | 75 | array, mtl, |
76 | storable-complex, | 76 | storable-complex, |
77 | process, | 77 | process, |
78 | ghc-binary | 78 | ghc-binary |
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 06db806..6d39c6e 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -18,6 +18,7 @@ module Data.Packed.Internal.Vector ( | |||
18 | fromList, toList, (|>), | 18 | fromList, toList, (|>), |
19 | join, (@>), safe, at, at', subVector, takesV, | 19 | join, (@>), safe, at, at', subVector, takesV, |
20 | mapVector, zipVector, unzipVectorWith, | 20 | mapVector, zipVector, unzipVectorWith, |
21 | mapVectorM, mapVectorM_, | ||
21 | foldVector, foldVectorG, foldLoop, | 22 | foldVector, foldVectorG, foldLoop, |
22 | createVector, vec, | 23 | createVector, vec, |
23 | asComplex, asReal, | 24 | asComplex, asReal, |
@@ -35,6 +36,7 @@ import Foreign.C.String | |||
35 | import Foreign.C.Types(CInt,CChar) | 36 | import Foreign.C.Types(CInt,CChar) |
36 | import Data.Complex | 37 | import Data.Complex |
37 | import Control.Monad(when) | 38 | import Control.Monad(when) |
39 | import Control.Monad.Trans | ||
38 | 40 | ||
39 | #if __GLASGOW_HASKELL__ >= 605 | 41 | #if __GLASGOW_HASKELL__ >= 605 |
40 | import GHC.ForeignPtr (mallocPlainForeignPtrBytes) | 42 | import GHC.ForeignPtr (mallocPlainForeignPtrBytes) |
@@ -357,6 +359,39 @@ foldVectorG f s0 v = foldLoop g s0 (dim v) | |||
357 | 359 | ||
358 | ------------------------------------------------------------------- | 360 | ------------------------------------------------------------------- |
359 | 361 | ||
362 | -- | monadic map over Vectors | ||
363 | mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b) | ||
364 | mapVectorM f v = do | ||
365 | w <- liftIO $ createVector (dim v) | ||
366 | mapVectorM' f v w (dim v -1) | ||
367 | return w | ||
368 | where mapVectorM' f' v' w' 0 = do | ||
369 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 | ||
370 | y <- f' x | ||
371 | liftIO $ unsafeWith w' $ \q -> pokeElemOff q 0 y | ||
372 | mapVectorM' f' v' w' !k = do | ||
373 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k | ||
374 | y <- f' x | ||
375 | liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y | ||
376 | mapVectorM' f' v' w' (k-1) | ||
377 | {-# INLINE mapVectorM #-} | ||
378 | |||
379 | -- | monadic map over Vectors | ||
380 | mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m () | ||
381 | mapVectorM_ f v = do | ||
382 | mapVectorM' f v (dim v -1) | ||
383 | where mapVectorM' f' v' 0 = do | ||
384 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 | ||
385 | f' x | ||
386 | mapVectorM' f' v' !k = do | ||
387 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k | ||
388 | _ <- f' x | ||
389 | mapVectorM' f' v' (k-1) | ||
390 | {-# INLINE mapVectorM_ #-} | ||
391 | |||
392 | ------------------------------------------------------------------- | ||
393 | |||
394 | |||
360 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). | 395 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). |
361 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | 396 | fscanfVector :: FilePath -> Int -> IO (Vector Double) |
362 | fscanfVector filename n = do | 397 | fscanfVector filename n = do |
@@ -398,3 +433,4 @@ fwriteVector filename v = do | |||
398 | free charname | 433 | free charname |
399 | 434 | ||
400 | foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | 435 | foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV |
436 | |||
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index ad2ae10..b87372f 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs | |||
@@ -25,6 +25,7 @@ module Data.Packed.Vector ( | |||
25 | -- vectorMax, vectorMin, | 25 | -- vectorMax, vectorMin, |
26 | vectorMaxIndex, vectorMinIndex, | 26 | vectorMaxIndex, vectorMinIndex, |
27 | mapVector, zipVector, unzipVector, unzipVectorWith, | 27 | mapVector, zipVector, unzipVector, unzipVectorWith, |
28 | mapVectorM, mapVectorM_, | ||
28 | fscanfVector, fprintfVector, freadVector, fwriteVector, | 29 | fscanfVector, fprintfVector, freadVector, fwriteVector, |
29 | foldLoop, foldVector, foldVectorG | 30 | foldLoop, foldVector, foldVectorG |
30 | ) where | 31 | ) where |
@@ -154,3 +155,5 @@ vecdisp f v | |||
154 | -- | unzip for Vectors | 155 | -- | unzip for Vectors |
155 | unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) | 156 | unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) |
156 | unzipVector = unzipVectorWith id id | 157 | unzipVector = unzipVectorWith id id |
158 | |||
159 | |||