diff options
-rw-r--r-- | examples/benchmarks.hs | 12 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 27 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 2 | ||||
-rw-r--r-- | lib/Graphics/Plot.hs | 12 |
4 files changed, 37 insertions, 16 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs index bffa221..f5ca550 100644 --- a/examples/benchmarks.hs +++ b/examples/benchmarks.hs | |||
@@ -46,6 +46,16 @@ bench1 = do | |||
46 | time $ print $ constant (1::Double) 5000001 @> 7 | 46 | time $ print $ constant (1::Double) 5000001 @> 7 |
47 | time $ print $ constant i 5000001 @> 7 | 47 | time $ print $ constant i 5000001 @> 7 |
48 | time $ print $ conj (constant i 5000001) @> 7 | 48 | time $ print $ conj (constant i 5000001) @> 7 |
49 | putStrLn "some zips:" | ||
50 | time $ print $ (w / w2) @> 7 | ||
51 | time $ print $ (zipVector (/) w w2) @> 7 | ||
52 | putStrLn "some folds:" | ||
53 | let t = constant (1::Double) 5000002 | ||
54 | print $ t @> 7 | ||
55 | time $ print $ foldVector max (t@>0) t | ||
56 | time $ print $ vectorMax t | ||
57 | time $ print $ sqrt $ foldVector (\v s -> v*v+s) 0 t | ||
58 | time $ print $ pnorm PNorm2 t | ||
49 | 59 | ||
50 | sumVH v = go (d - 1) 0 | 60 | sumVH v = go (d - 1) 0 |
51 | where | 61 | where |
@@ -56,7 +66,7 @@ sumVH v = go (d - 1) 0 | |||
56 | 66 | ||
57 | innerH u v = go (d - 1) 0 | 67 | innerH u v = go (d - 1) 0 |
58 | where | 68 | where |
59 | d = dim u | 69 | d = min (dim u) (dim v) |
60 | go :: Int -> Double -> Double | 70 | go :: Int -> Double -> Double |
61 | go 0 s = s + (u @> 0) * (v @> 0) | 71 | go 0 s = s + (u @> 0) * (v @> 0) |
62 | go !j !s = go (j - 1) (s + (u @> j) * (v @> j)) | 72 | go !j !s = go (j - 1) (s + (u @> j) * (v @> j)) |
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index dd9b9b6..d410c4d 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -180,13 +180,9 @@ asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v) } | |||
180 | 180 | ||
181 | ---------------------------------------------------------------- | 181 | ---------------------------------------------------------------- |
182 | 182 | ||
183 | -- | map on Vectors | 183 | liftVector f x = mapVector f x |
184 | liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b | ||
185 | liftVector = mapVector | ||
186 | 184 | ||
187 | -- | zipWith for Vectors | 185 | liftVector2 f u v = zipVector f u v |
188 | liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c | ||
189 | liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) | ||
190 | 186 | ||
191 | ----------------------------------------------------------------- | 187 | ----------------------------------------------------------------- |
192 | 188 | ||
@@ -199,6 +195,8 @@ cloneVector (v@V {dim=n}) = do | |||
199 | 195 | ||
200 | ------------------------------------------------------------------ | 196 | ------------------------------------------------------------------ |
201 | 197 | ||
198 | -- | map on Vectors | ||
199 | mapVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b | ||
202 | mapVector f v = unsafePerformIO $ do | 200 | mapVector f v = unsafePerformIO $ do |
203 | w <- createVector (dim v) | 201 | w <- createVector (dim v) |
204 | withForeignPtr (fptr v) $ \p -> | 202 | withForeignPtr (fptr v) $ \p -> |
@@ -211,6 +209,23 @@ mapVector f v = unsafePerformIO $ do | |||
211 | return w | 209 | return w |
212 | {-# INLINE mapVector #-} | 210 | {-# INLINE mapVector #-} |
213 | 211 | ||
212 | -- | zipWith for Vectors | ||
213 | zipVector :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c | ||
214 | zipVector f u v = unsafePerformIO $ do | ||
215 | let n = min (dim u) (dim v) | ||
216 | w <- createVector n | ||
217 | withForeignPtr (fptr u) $ \pu -> | ||
218 | withForeignPtr (fptr v) $ \pv -> | ||
219 | withForeignPtr (fptr w) $ \pw -> do | ||
220 | let go (-1) = return () | ||
221 | go !k = do x <- peekElemOff pu k | ||
222 | y <- peekElemOff pv k | ||
223 | pokeElemOff pw k (f x y) | ||
224 | go (k-1) | ||
225 | go (n -1) | ||
226 | return w | ||
227 | {-# INLINE zipVector #-} | ||
228 | |||
214 | foldVector f x v = unsafePerformIO $ | 229 | foldVector f x v = unsafePerformIO $ |
215 | withForeignPtr (fptr (v::Vector Double)) $ \p -> do | 230 | withForeignPtr (fptr (v::Vector Double)) $ \p -> do |
216 | let go (-1) s = return s | 231 | let go (-1) s = return s |
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index b85f0bd..e53d455 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs | |||
@@ -20,7 +20,7 @@ module Data.Packed.Vector ( | |||
20 | constant, linspace, | 20 | constant, linspace, |
21 | vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, | 21 | vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, |
22 | liftVector, liftVector2, | 22 | liftVector, liftVector2, |
23 | foldLoop, foldVector, foldVectorG, mapVector | 23 | foldLoop, foldVector, foldVectorG, mapVector, zipVector |
24 | ) where | 24 | ) where |
25 | 25 | ||
26 | import Data.Packed.Internal | 26 | import Data.Packed.Internal |
diff --git a/lib/Graphics/Plot.hs b/lib/Graphics/Plot.hs index bcb1fb3..9b64a1d 100644 --- a/lib/Graphics/Plot.hs +++ b/lib/Graphics/Plot.hs | |||
@@ -1,7 +1,7 @@ | |||
1 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
2 | -- | | 2 | -- | |
3 | -- Module : Graphics.Plot | 3 | -- Module : Graphics.Plot |
4 | -- Copyright : (c) Alberto Ruiz 2005 | 4 | -- Copyright : (c) Alberto Ruiz 2005-8 |
5 | -- License : GPL-style | 5 | -- License : GPL-style |
6 | -- | 6 | -- |
7 | -- Maintainer : Alberto Ruiz (aruiz at um dot es) | 7 | -- Maintainer : Alberto Ruiz (aruiz at um dot es) |
@@ -28,14 +28,10 @@ module Graphics.Plot( | |||
28 | 28 | ||
29 | ) where | 29 | ) where |
30 | 30 | ||
31 | import Data.Packed.Vector | 31 | import Data.Packed |
32 | import Data.Packed.Matrix | ||
33 | import Numeric.LinearAlgebra(outer) | 32 | import Numeric.LinearAlgebra(outer) |
34 | import Numeric.GSL.Vector(FunCodeS(Max,Min),toScalarR) | ||
35 | import Data.List(intersperse) | 33 | import Data.List(intersperse) |
36 | import System | 34 | import System |
37 | import Foreign hiding (rotate) | ||
38 | |||
39 | 35 | ||
40 | size = dim | 36 | size = dim |
41 | 37 | ||
@@ -154,8 +150,8 @@ matrixToPGM m = header ++ unlines (map unwords ll) where | |||
154 | r = rows m | 150 | r = rows m |
155 | header = "P2 "++show c++" "++show r++" "++show (round maxgray :: Int)++"\n" | 151 | header = "P2 "++show c++" "++show r++" "++show (round maxgray :: Int)++"\n" |
156 | maxgray = 255.0 | 152 | maxgray = 255.0 |
157 | maxval = toScalarR Max $ flatten $ m | 153 | maxval = vectorMax $ flatten $ m |
158 | minval = toScalarR Min $ flatten $ m | 154 | minval = vectorMin $ flatten $ m |
159 | scale = if (maxval == minval) | 155 | scale = if (maxval == minval) |
160 | then 0.0 | 156 | then 0.0 |
161 | else maxgray / (maxval - minval) | 157 | else maxgray / (maxval - minval) |