summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Data/Packed/Internal/Vector.hs23
-rw-r--r--lib/Data/Packed/Vector.hs7
2 files changed, 27 insertions, 3 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index d97a86d..06db806 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -1,4 +1,4 @@
1{-# LANGUAGE MagicHash, CPP, UnboxedTuples, BangPatterns #-} 1{-# LANGUAGE MagicHash, CPP, UnboxedTuples, BangPatterns, FlexibleContexts #-}
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3-- | 3-- |
4-- Module : Data.Packed.Internal.Vector 4-- Module : Data.Packed.Internal.Vector
@@ -17,7 +17,7 @@ module Data.Packed.Internal.Vector (
17 Vector, dim, 17 Vector, dim,
18 fromList, toList, (|>), 18 fromList, toList, (|>),
19 join, (@>), safe, at, at', subVector, takesV, 19 join, (@>), safe, at, at', subVector, takesV,
20 mapVector, zipVector, 20 mapVector, zipVector, unzipVectorWith,
21 foldVector, foldVectorG, foldLoop, 21 foldVector, foldVectorG, foldLoop,
22 createVector, vec, 22 createVector, vec,
23 asComplex, asReal, 23 asComplex, asReal,
@@ -318,6 +318,25 @@ zipVector f u v = unsafePerformIO $ do
318 return w 318 return w
319{-# INLINE zipVector #-} 319{-# INLINE zipVector #-}
320 320
321-- | unzipWith for Vectors
322unzipVectorWith :: (Storable (a,b), Storable c, Storable d)
323 => (a -> c) -> (b -> d) -> Vector (a,b) -> (Vector c,Vector d)
324unzipVectorWith f g u = unsafePerformIO $ do
325 let n = dim u
326 v <- createVector n
327 w <- createVector n
328 unsafeWith u $ \pu ->
329 unsafeWith v $ \pv ->
330 unsafeWith w $ \pw -> do
331 let go (-1) = return ()
332 go !k = do (x,y) <- peekElemOff pu k
333 pokeElemOff pv k (f x)
334 pokeElemOff pw k (g y)
335 go (k-1)
336 go (n-1)
337 return (v,w)
338{-# INLINE unzipVectorWith #-}
339
321foldVector f x v = unsafePerformIO $ 340foldVector f x v = unsafePerformIO $
322 unsafeWith (v::Vector Double) $ \p -> do 341 unsafeWith (v::Vector Double) $ \p -> do
323 let go (-1) s = return s 342 let go (-1) s = return s
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index c5b8ba7..ad2ae10 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE FlexibleContexts #-}
1----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
2-- | 3-- |
3-- Module : Data.Packed.Vector 4-- Module : Data.Packed.Vector
@@ -23,7 +24,7 @@ module Data.Packed.Vector (
23-- vectorFMax, vectorFMin, vectorFMaxIndex, vectorFMinIndex, 24-- vectorFMax, vectorFMin, vectorFMaxIndex, vectorFMinIndex,
24-- vectorMax, vectorMin, 25-- vectorMax, vectorMin,
25 vectorMaxIndex, vectorMinIndex, 26 vectorMaxIndex, vectorMinIndex,
26 mapVector, zipVector, 27 mapVector, zipVector, unzipVector, unzipVectorWith,
27 fscanfVector, fprintfVector, freadVector, fwriteVector, 28 fscanfVector, fprintfVector, freadVector, fwriteVector,
28 foldLoop, foldVector, foldVectorG 29 foldLoop, foldVector, foldVectorG
29) where 30) where
@@ -149,3 +150,7 @@ vecdisp f v
149 . unwords . lines . tail . dropWhile (not . (`elem` " \n")) 150 . unwords . lines . tail . dropWhile (not . (`elem` " \n"))
150 . f . trans . reshape 1 151 . f . trans . reshape 1
151 $ v 152 $ v
153
154-- | unzip for Vectors
155unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b)
156unzipVector = unzipVectorWith id id