diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/parallel.hs | 2 | ||||
-rw-r--r-- | examples/pca1.hs | 2 | ||||
-rw-r--r-- | examples/pca2.hs | 2 | ||||
-rw-r--r-- | examples/vector-map.hs | 74 | ||||
-rw-r--r-- | examples/vector.hs | 5 |
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 | ||
3 | import System(getArgs) | 3 | import System.Environment(getArgs) |
4 | import Numeric.LinearAlgebra | 4 | import Numeric.LinearAlgebra |
5 | import Control.Parallel.Strategies | 5 | import Control.Parallel.Strategies |
6 | import System.Time | 6 | import 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 | ||
3 | import Numeric.LinearAlgebra | 3 | import Numeric.LinearAlgebra |
4 | import System.Directory(doesFileExist) | 4 | import System.Directory(doesFileExist) |
5 | import System(system) | 5 | import System.Process(system) |
6 | import Control.Monad(when) | 6 | import Control.Monad(when) |
7 | 7 | ||
8 | type Vec = Vector Double | 8 | type 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 @@ | |||
3 | import Numeric.LinearAlgebra | 3 | import Numeric.LinearAlgebra |
4 | import Graphics.Plot | 4 | import Graphics.Plot |
5 | import System.Directory(doesFileExist) | 5 | import System.Directory(doesFileExist) |
6 | import System(system) | 6 | import System.Process(system) |
7 | import Control.Monad(when) | 7 | import Control.Monad(when) |
8 | 8 | ||
9 | type Vec = Vector Double | 9 | type 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 | |||
6 | import Data.Packed.Vector | ||
7 | import Numeric.LinearAlgebra.Interface | ||
8 | |||
9 | import Control.Monad.State | ||
10 | |||
11 | ------------------------------------------- | ||
12 | |||
13 | -- an instance of MonadIO, a monad transformer | ||
14 | type VectorMonadT = StateT Int IO | ||
15 | |||
16 | v :: Vector Int | ||
17 | v = fromList $ take 10 [0..] | ||
18 | |||
19 | test1 :: Vector Int -> IO (Vector Int) | ||
20 | test1 = do | ||
21 | mapVectorM (\x -> do | ||
22 | putStr $ (show x) ++ " " | ||
23 | return (x + 1)) | ||
24 | |||
25 | -- we can have an arbitrary monad AND do IO | ||
26 | addInitialM :: Vector Int -> VectorMonadT () | ||
27 | addInitialM = 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 | ||
34 | sumEvens :: Vector Int -> Int | ||
35 | sumEvens = foldVectorWithIndex (\x a b -> if x `mod` 2 == 0 then a + b else b) 0 | ||
36 | |||
37 | -- sum and print running total of evens | ||
38 | sumEvensAndPrint :: Vector Int -> VectorMonadT () | ||
39 | sumEvensAndPrint = 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 | |||
49 | indexPlusSum :: Vector Int -> VectorMonadT () | ||
50 | indexPlusSum 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 | |||
63 | main = 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 | |||
14 | fromVector v = unsafeFromForeignPtr p i n where | 14 | fromVector v = unsafeFromForeignPtr p i n where |
15 | (p,i,n) = V.unsafeToForeignPtr v | 15 | (p,i,n) = V.unsafeToForeignPtr v |
16 | 16 | ||
17 | toVector :: H.Vector t -> V.Vector t | 17 | toVector :: Storable t => H.Vector t -> V.Vector t |
18 | toVector v = V.unsafeFromForeignPtr p i n where | 18 | toVector 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 | ||
23 | v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7) | 23 | v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7) |
24 | 24 | ||
25 | w = subVector 2 3 (linspace 10 (0,2)) | 25 | w = subVector 2 3 (linspace 5 (0,1)) :: Vector Double |
26 | 26 | ||
27 | main = do | 27 | main = 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 | |||