summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-26 17:05:09 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-26 17:05:09 +0000
commit611ff4782c261a6c6e52fe7ed0c122a0eac22691 (patch)
tree5b9362a88a568cbb4e8ad442365e26bdef17946d /examples
parent632c2c5a8b934fd8b54c8be68d178aa49323077d (diff)
fix imports, examples
Diffstat (limited to 'examples')
-rw-r--r--examples/parallel.hs2
-rw-r--r--examples/pca1.hs2
-rw-r--r--examples/pca2.hs2
-rw-r--r--examples/vector-map.hs74
-rw-r--r--examples/vector.hs5
5 files changed, 5 insertions, 80 deletions
diff --git a/examples/parallel.hs b/examples/parallel.hs
index c82114f..566b729 100644
--- a/examples/parallel.hs
+++ b/examples/parallel.hs
@@ -1,6 +1,6 @@
1-- $ runhaskell parallel.hs 2000 1-- $ runhaskell parallel.hs 2000
2 2
3import System(getArgs) 3import System.Environment(getArgs)
4import Numeric.LinearAlgebra 4import Numeric.LinearAlgebra
5import Control.Parallel.Strategies 5import Control.Parallel.Strategies
6import System.Time 6import System.Time
diff --git a/examples/pca1.hs b/examples/pca1.hs
index 58b5577..a11eba9 100644
--- a/examples/pca1.hs
+++ b/examples/pca1.hs
@@ -2,7 +2,7 @@
2 2
3import Numeric.LinearAlgebra 3import Numeric.LinearAlgebra
4import System.Directory(doesFileExist) 4import System.Directory(doesFileExist)
5import System(system) 5import System.Process(system)
6import Control.Monad(when) 6import Control.Monad(when)
7 7
8type Vec = Vector Double 8type Vec = Vector Double
diff --git a/examples/pca2.hs b/examples/pca2.hs
index c38857c..e7ea95f 100644
--- a/examples/pca2.hs
+++ b/examples/pca2.hs
@@ -3,7 +3,7 @@
3import Numeric.LinearAlgebra 3import Numeric.LinearAlgebra
4import Graphics.Plot 4import Graphics.Plot
5import System.Directory(doesFileExist) 5import System.Directory(doesFileExist)
6import System(system) 6import System.Process(system)
7import Control.Monad(when) 7import Control.Monad(when)
8 8
9type Vec = Vector Double 9type Vec = Vector Double
diff --git a/examples/vector-map.hs b/examples/vector-map.hs
deleted file mode 100644
index 7796cc0..0000000
--- a/examples/vector-map.hs
+++ /dev/null
@@ -1,74 +0,0 @@
1-- use of vectorMapM
2--
3
4-------------------------------------------
5
6import Data.Packed.Vector
7import Numeric.LinearAlgebra.Interface
8
9import Control.Monad.State
10
11-------------------------------------------
12
13-- an instance of MonadIO, a monad transformer
14type VectorMonadT = StateT Int IO
15
16v :: Vector Int
17v = fromList $ take 10 [0..]
18
19test1 :: Vector Int -> IO (Vector Int)
20test1 = do
21 mapVectorM (\x -> do
22 putStr $ (show x) ++ " "
23 return (x + 1))
24
25-- we can have an arbitrary monad AND do IO
26addInitialM :: Vector Int -> VectorMonadT ()
27addInitialM = mapVectorM_ (\x -> do
28 i <- get
29 liftIO $ putStr $ (show $ x + i) ++ " "
30 put $ x + i
31 )
32
33-- sum the values of the even indiced elements
34sumEvens :: Vector Int -> Int
35sumEvens = foldVectorWithIndex (\x a b -> if x `mod` 2 == 0 then a + b else b) 0
36
37-- sum and print running total of evens
38sumEvensAndPrint :: Vector Int -> VectorMonadT ()
39sumEvensAndPrint = mapVectorWithIndexM_ (\ i x -> do
40 when (i `mod` 2 == 0) (do
41 v <- get
42 put $ v + x
43 v' <- get
44 liftIO $ putStr $ (show v') ++ " "
45 return ())
46 return ()
47 )
48
49indexPlusSum :: Vector Int -> VectorMonadT ()
50indexPlusSum v' = do
51 v <- mapVectorWithIndexM (\i x -> do
52 s <- get
53 let inc = x+s
54 liftIO $ putStr $ show (i,inc) ++ " "
55 put inc
56 return inc) v'
57 liftIO $ do
58 putStrLn ""
59 putStrLn $ show v
60
61-------------------------------------------
62
63main = do
64 v' <- test1 v
65 putStrLn ""
66 putStrLn $ show v'
67 evalStateT (addInitialM v) 0
68 putStrLn ""
69 putStrLn $ show (sumEvens v)
70 evalStateT (sumEvensAndPrint v) 0
71 putStrLn ""
72 evalStateT (indexPlusSum v) 0
73 return ()
74
diff --git a/examples/vector.hs b/examples/vector.hs
index eda9290..f531cbd 100644
--- a/examples/vector.hs
+++ b/examples/vector.hs
@@ -14,7 +14,7 @@ fromVector :: Storable t => V.Vector t -> H.Vector t
14fromVector v = unsafeFromForeignPtr p i n where 14fromVector v = unsafeFromForeignPtr p i n where
15 (p,i,n) = V.unsafeToForeignPtr v 15 (p,i,n) = V.unsafeToForeignPtr v
16 16
17toVector :: H.Vector t -> V.Vector t 17toVector :: Storable t => H.Vector t -> V.Vector t
18toVector v = V.unsafeFromForeignPtr p i n where 18toVector v = V.unsafeFromForeignPtr p i n where
19 (p,i,n) = unsafeToForeignPtr v 19 (p,i,n) = unsafeToForeignPtr v
20 20
@@ -22,11 +22,10 @@ toVector v = V.unsafeFromForeignPtr p i n where
22 22
23v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7) 23v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7)
24 24
25w = subVector 2 3 (linspace 10 (0,2)) 25w = subVector 2 3 (linspace 5 (0,1)) :: Vector Double
26 26
27main = do 27main = do
28 print v 28 print v
29 print $ fromVector v 29 print $ fromVector v
30 print w 30 print w
31 print $ toVector w 31 print $ toVector w
32