summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-07-13 22:59:22 +0000
committerVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-07-13 22:59:22 +0000
commitc104df60266d5e0bc94e5b0a7eedc1d949975fc1 (patch)
tree98416f80a8715d226e597dcfbc2d7aecb485a05a
parent7659d9c67f75e1f665d6b02663ee8767e97762b4 (diff)
fix mapVectorM(_) and add example
-rw-r--r--examples/vector-map.hs42
-rw-r--r--hmatrix.cabal1
-rw-r--r--lib/Data/Packed/Internal/Vector.hs38
3 files changed, 63 insertions, 18 deletions
diff --git a/examples/vector-map.hs b/examples/vector-map.hs
new file mode 100644
index 0000000..d778358
--- /dev/null
+++ b/examples/vector-map.hs
@@ -0,0 +1,42 @@
1-- use of vectorMapM
2--
3
4-------------------------------------------
5
6import Data.Packed.Vector
7import Numeric.LinearAlgebra.Interface
8
9import Control.Monad.State
10import Control.Monad.Trans
11
12-------------------------------------------
13
14-- an instance of MonadIO, a monad transformer
15type VectorMonadT = StateT Int IO
16
17v :: Vector Int
18v = fromList $ take 10 [0..]
19
20test1 :: Vector Int -> IO (Vector Int)
21test1 = do
22 mapVectorM (\x -> do
23 putStr $ (show) x ++ " "
24 return (x + 1))
25
26-- we can have an arbitrary monad AND do IO
27addInitialM :: Vector Int -> VectorMonadT ()
28addInitialM = mapVectorM_ (\x -> do
29 i <- get
30 liftIO $ putStr $ (show $ x + i) ++ " "
31 put $ x + i
32 )
33
34-------------------------------------------
35main = do
36 v' <- test1 v
37 putStrLn ""
38 putStrLn $ show v'
39 evalStateT (addInitialM v) 1
40 putStrLn ""
41 return ()
42
diff --git a/hmatrix.cabal b/hmatrix.cabal
index 04c3581..0bcc3aa 100644
--- a/hmatrix.cabal
+++ b/hmatrix.cabal
@@ -45,6 +45,7 @@ extra-source-files: examples/tests.hs
45 examples/devel/ej2/functions.c 45 examples/devel/ej2/functions.c
46 examples/Real.hs 46 examples/Real.hs
47 examples/vector.hs 47 examples/vector.hs
48 examples/vector-map.hs
48 49
49extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, 50extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h,
50 lib/Numeric/LinearAlgebra/LAPACK/clapack.h 51 lib/Numeric/LinearAlgebra/LAPACK/clapack.h
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 7dd1289..ec2bf3c 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -365,30 +365,32 @@ foldVectorG f s0 v = foldLoop g s0 (dim v)
365mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b) 365mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b)
366mapVectorM f v = do 366mapVectorM f v = do
367 w <- liftIO $ createVector (dim v) 367 w <- liftIO $ createVector (dim v)
368 mapVectorM' f v w (dim v -1) 368 mapVectorM' f v w 0 (dim v -1)
369 return w 369 return w
370 where mapVectorM' f' v' w' 0 = do 370 where mapVectorM' f' v' w' !k !t
371 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 371 | k == t = do
372 y <- f' x 372 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k
373 liftIO $ unsafeWith w' $ \q -> pokeElemOff q 0 y 373 y <- f' x
374 mapVectorM' f' v' w' !k = do 374 liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y
375 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k 375 | otherwise = do
376 y <- f' x 376 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k
377 liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y 377 y <- f' x
378 mapVectorM' f' v' w' (k-1) 378 liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y
379 mapVectorM' f' v' w' (k+1) t
379{-# INLINE mapVectorM #-} 380{-# INLINE mapVectorM #-}
380 381
381-- | monadic map over Vectors 382-- | monadic map over Vectors
382mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m () 383mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m ()
383mapVectorM_ f v = do 384mapVectorM_ f v = do
384 mapVectorM' f v (dim v -1) 385 mapVectorM' f v 0 (dim v -1)
385 where mapVectorM' f' v' 0 = do 386 where mapVectorM' f' v' !k !t
386 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 387 | k == t = do
387 f' x 388 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k
388 mapVectorM' f' v' !k = do 389 f' x
389 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k 390 | otherwise = do
390 _ <- f' x 391 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k
391 mapVectorM' f' v' (k-1) 392 _ <- f' x
393 mapVectorM' f' v' (k+1) t
392{-# INLINE mapVectorM_ #-} 394{-# INLINE mapVectorM_ #-}
393 395
394------------------------------------------------------------------- 396-------------------------------------------------------------------