summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Vector.hs
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/Data/Packed/Internal/Vector.hs
parentf0a2248b9d9532e0814519408bff8b886bca2bbc (diff)
zipVector
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs27
1 files changed, 21 insertions, 6 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