Permalink
Browse files

fix imports, examples

  • Loading branch information...
albertoruiz committed Sep 26, 2010
1 parent 632c2c5 commit 611ff4782c261a6c6e52fe7ed0c122a0eac22691
Showing with 101 additions and 84 deletions.
  1. +1 −1 Setup.lhs
  2. +3 −1 configure.hs
  3. +1 −1 examples/parallel.hs
  4. +1 −1 examples/pca1.hs
  5. +1 −1 examples/pca2.hs
  6. +0 −74 examples/vector-map.hs
  7. +2 −3 examples/vector.hs
  8. +2 −2 lib/Numeric/LinearAlgebra/Tests.hs
  9. +90 −0 monadic.hs
View
@@ -1,7 +1,7 @@
#! /usr/bin/env runhaskell
> import Distribution.Simple
-> import System(system)
+> import System.Process(system)
> main = defaultMainWithHooks autoconfUserHooks {runTests = t}
View
@@ -17,7 +17,9 @@
-}
-import System
+import System.Process
+import System.Exit
+import System.Environment
import System.Directory(createDirectoryIfMissing)
import Data.List(isPrefixOf, intercalate)
import Distribution.Simple.LocalBuildInfo
View
@@ -1,6 +1,6 @@
-- $ runhaskell parallel.hs 2000
-import System(getArgs)
+import System.Environment(getArgs)
import Numeric.LinearAlgebra
import Control.Parallel.Strategies
import System.Time
View
@@ -2,7 +2,7 @@
import Numeric.LinearAlgebra
import System.Directory(doesFileExist)
-import System(system)
+import System.Process(system)
import Control.Monad(when)
type Vec = Vector Double
View
@@ -3,7 +3,7 @@
import Numeric.LinearAlgebra
import Graphics.Plot
import System.Directory(doesFileExist)
-import System(system)
+import System.Process(system)
import Control.Monad(when)
type Vec = Vector Double
View
@@ -1,74 +0,0 @@
--- use of vectorMapM
---
-
--------------------------------------------
-
-import Data.Packed.Vector
-import Numeric.LinearAlgebra.Interface
-
-import Control.Monad.State
-
--------------------------------------------
-
--- an instance of MonadIO, a monad transformer
-type VectorMonadT = StateT Int IO
-
-v :: Vector Int
-v = fromList $ take 10 [0..]
-
-test1 :: Vector Int -> IO (Vector Int)
-test1 = do
- mapVectorM (\x -> do
- putStr $ (show x) ++ " "
- return (x + 1))
-
--- we can have an arbitrary monad AND do IO
-addInitialM :: Vector Int -> VectorMonadT ()
-addInitialM = mapVectorM_ (\x -> do
- i <- get
- liftIO $ putStr $ (show $ x + i) ++ " "
- put $ x + i
- )
-
--- sum the values of the even indiced elements
-sumEvens :: Vector Int -> Int
-sumEvens = foldVectorWithIndex (\x a b -> if x `mod` 2 == 0 then a + b else b) 0
-
--- sum and print running total of evens
-sumEvensAndPrint :: Vector Int -> VectorMonadT ()
-sumEvensAndPrint = mapVectorWithIndexM_ (\ i x -> do
- when (i `mod` 2 == 0) (do
- v <- get
- put $ v + x
- v' <- get
- liftIO $ putStr $ (show v') ++ " "
- return ())
- return ()
- )
-
-indexPlusSum :: Vector Int -> VectorMonadT ()
-indexPlusSum v' = do
- v <- mapVectorWithIndexM (\i x -> do
- s <- get
- let inc = x+s
- liftIO $ putStr $ show (i,inc) ++ " "
- put inc
- return inc) v'
- liftIO $ do
- putStrLn ""
- putStrLn $ show v
-
--------------------------------------------
-
-main = do
- v' <- test1 v
- putStrLn ""
- putStrLn $ show v'
- evalStateT (addInitialM v) 0
- putStrLn ""
- putStrLn $ show (sumEvens v)
- evalStateT (sumEvensAndPrint v) 0
- putStrLn ""
- evalStateT (indexPlusSum v) 0
- return ()
-
View
@@ -14,19 +14,18 @@ fromVector :: Storable t => V.Vector t -> H.Vector t
fromVector v = unsafeFromForeignPtr p i n where
(p,i,n) = V.unsafeToForeignPtr v
-toVector :: H.Vector t -> V.Vector t
+toVector :: Storable t => H.Vector t -> V.Vector t
toVector v = V.unsafeFromForeignPtr p i n where
(p,i,n) = unsafeToForeignPtr v
-------------------------------------------
v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7)
-w = subVector 2 3 (linspace 10 (0,2))
+w = subVector 2 3 (linspace 5 (0,1)) :: Vector Double
main = do
print v
print $ fromVector v
print w
print $ toVector w
-
@@ -300,8 +300,8 @@ conjuTest m = mapVector conjugate (flatten (trans m)) == flatten (ctrans m)
---------------------------------------------------------------------
-succTest = utest "successive" $ successive_ (<) (fromList [1 :: Double,2,3,4]) == True
- && successive_ (<) (fromList [1 :: Double,3,2,4]) == False
+succTest = utest "successive" $ successive_ (>) (fromList [1 :: Double,2,3,4]) == True
+ && successive_ (<) (fromList [1 :: Double,3,2,4]) == False
---------------------------------------------------------------------
View
@@ -0,0 +1,90 @@
+-- monadic computations
+-- (contributed by Vivian McPhail)
+
+import Numeric.Container
+import Control.Monad.State
+import Control.Monad.Maybe
+
+-------------------------------------------
+
+-- an instance of MonadIO, a monad transformer
+type VectorMonadT = StateT Int IO
+
+v :: Vector Int
+v = 10 |> [0..]
+
+test1 :: Vector Int -> IO (Vector Int)
+test1 = mapVectorM $ \x -> do
+ putStr $ (show x) ++ " "
+ return (x + 1)
+
+-- we can have an arbitrary monad AND do IO
+addInitialM :: Vector Int -> VectorMonadT ()
+addInitialM = mapVectorM_ $ \x -> do
+ i <- get
+ liftIO $ putStr $ (show $ x + i) ++ " "
+ put $ x + i
+
+-- sum the values of the even indiced elements
+sumEvens :: Vector Int -> Int
+sumEvens = foldVectorWithIndex (\x a b -> if x `mod` 2 == 0 then a + b else b) 0
+
+-- sum and print running total of evens
+sumEvensAndPrint :: Vector Int -> VectorMonadT ()
+sumEvensAndPrint = mapVectorWithIndexM_ $ \ i x -> do
+ when (i `mod` 2 == 0) $ do
+ v <- get
+ put $ v + x
+ v' <- get
+ liftIO $ putStr $ (show v') ++ " "
+
+
+indexPlusSum :: Vector Int -> VectorMonadT ()
+indexPlusSum v' = do
+ let f i x = do
+ s <- get
+ let inc = x+s
+ liftIO $ putStr $ show (i,inc) ++ " "
+ put inc
+ return inc
+ v <- mapVectorWithIndexM f v'
+ liftIO $ do
+ putStrLn ""
+ putStrLn $ show v
+
+-------------------------------------------
+
+-- short circuit
+monoStep :: Double -> MaybeT (State Double) ()
+monoStep d = do
+ dp <- get
+ when (d < dp) (fail "negative difference")
+ put d
+{-# INLINE monoStep #-}
+
+isMonotoneIncreasing :: Vector Double -> Bool
+isMonotoneIncreasing v =
+ let res = evalState (runMaybeT $ (mapVectorM_ monoStep v)) (v @> 0)
+ in case res of
+ Nothing -> False
+ Just _ -> True
+
+w = fromList ([1..10]++[10,9..1]) :: Vector Double
+
+-------------------------------------------
+
+main = do
+ v' <- test1 v
+ putStrLn ""
+ putStrLn $ show v'
+ evalStateT (addInitialM v) 0
+ putStrLn ""
+ putStrLn $ show (sumEvens v)
+ evalStateT (sumEvensAndPrint v) 0
+ putStrLn ""
+ evalStateT (indexPlusSum v) 0
+ putStrLn "-----------------------"
+ print $ isMonotoneIncreasing w
+ print $ isMonotoneIncreasing (subVector 0 7 w)
+ print $ successive_ (>) v
+ print $ successive_ (>) w

0 comments on commit 611ff47

Please sign in to comment.