summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-07-09 17:57:31 +0000
committerVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-07-09 17:57:31 +0000
commit238a01e0c3b51569c28ede4259cdf33bf18eb94f (patch)
tree06e24c47b4c114f698b92d106202a97ceaa25b82
parentc4531ceaa524b1f8a84dcdf3456d7a7b2831f902 (diff)
vectorMapM, vectorMapM_
-rw-r--r--hmatrix.cabal2
-rw-r--r--lib/Data/Packed/Internal/Vector.hs36
-rw-r--r--lib/Data/Packed/Vector.hs3
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
72library 72library
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
35import Foreign.C.Types(CInt,CChar) 36import Foreign.C.Types(CInt,CChar)
36import Data.Complex 37import Data.Complex
37import Control.Monad(when) 38import Control.Monad(when)
39import Control.Monad.Trans
38 40
39#if __GLASGOW_HASKELL__ >= 605 41#if __GLASGOW_HASKELL__ >= 605
40import GHC.ForeignPtr (mallocPlainForeignPtrBytes) 42import 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
363mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b)
364mapVectorM 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
380mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m ()
381mapVectorM_ 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).
361fscanfVector :: FilePath -> Int -> IO (Vector Double) 396fscanfVector :: FilePath -> Int -> IO (Vector Double)
362fscanfVector filename n = do 397fscanfVector filename n = do
@@ -398,3 +433,4 @@ fwriteVector filename v = do
398 free charname 433 free charname
399 434
400foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV 435foreign 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
155unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) 156unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b)
156unzipVector = unzipVectorWith id id 157unzipVector = unzipVectorWith id id
158
159