summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2008-11-15 11:50:24 +0000
committerAlberto Ruiz <aruiz@um.es>2008-11-15 11:50:24 +0000
commit1e9b6bb90273cdf192bdcf0ccf73af59241c1d95 (patch)
treef7c1656c2c3b883e8126519610a5428e7e9b7382 /lib
parentf0a2248b9d9532e0814519408bff8b886bca2bbc (diff)
zipVector
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs27
-rw-r--r--lib/Data/Packed/Vector.hs2
-rw-r--r--lib/Graphics/Plot.hs12
3 files changed, 26 insertions, 15 deletions
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 183liftVector f x = mapVector f x
184liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
185liftVector = mapVector
186 184
187-- | zipWith for Vectors 185liftVector2 f u v = zipVector f u v
188liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c
189liftVector2 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
199mapVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
202mapVector f v = unsafePerformIO $ do 200mapVector 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
213zipVector :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c
214zipVector 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
214foldVector f x v = unsafePerformIO $ 229foldVector 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
26import Data.Packed.Internal 26import 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
31import Data.Packed.Vector 31import Data.Packed
32import Data.Packed.Matrix
33import Numeric.LinearAlgebra(outer) 32import Numeric.LinearAlgebra(outer)
34import Numeric.GSL.Vector(FunCodeS(Max,Min),toScalarR)
35import Data.List(intersperse) 33import Data.List(intersperse)
36import System 34import System
37import Foreign hiding (rotate)
38
39 35
40size = dim 36size = 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)