diff options
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 27 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 2 |
2 files changed, 22 insertions, 7 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 | 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 |