{-# OPTIONS_GHC -threaded #-}
module Data.Vector.DoubleZip (
normFst
, normSnd
, evalFstFV
, evalFstFVM
, evalSndFV
, evalSndFVM
, double42Float4
, float42Double4
) where
import qualified Data.Vector as V
import GHC.Float (double2Float,float2Double)
normFst :: (Fractional a, Ord a) => V.Vector (a, b) -> IO (V.Vector (a, b))
normFst v
| V.all (\(x,_) -> x == 0) v = putStrLn "Warning: Data.Vector.DoubleZip.normFst: Vector with all zero first parts of the elements." >> return v
| otherwise = V.mapM (\(x,y) -> return (x / abs (V.maximumBy (\t u -> compare (abs t) (abs u)) . V.map fst $ v),y)) v
normSnd :: (Fractional b, Ord b) => V.Vector (a, b) -> IO (V.Vector (a, b))
normSnd v
| V.all (\(_,y) -> y == 0) v = putStrLn "Warning: Data.Vector.DoubleZip.normSnd: Vector with all zero second parts of the elements." >> return v
| otherwise = V.mapM (\(x,y) -> return (x,y / abs (V.maximumBy (\t u -> compare (abs t) (abs u)) . V.map snd $ v))) v
evalFstFV :: (Fractional a, Ord a) => (a -> b) -> V.Vector a -> IO (V.Vector ((a, b), (a, b)))
evalFstFV f v
| compare (V.length v) 1 == GT = do
zipped <- normFst . V.zip v $ V.map f v
return . V.zip zipped . V.unsafeSlice 1 (V.length v - 1) $ zipped
| otherwise = return V.empty
evalFstFVM :: (Fractional a, Ord a) => (a -> IO b) -> V.Vector a -> IO (V.Vector ((a, b), (a, b)))
evalFstFVM f v
| compare (V.length v) 1 == GT = do
v1 <- V.mapM f v
zipped <- normFst . V.zip v $ v1
return . V.zip zipped . V.unsafeSlice 1 (V.length v - 1) $ zipped
| otherwise = return V.empty
evalSndFV :: (Fractional b, Ord b) => (a -> b) -> V.Vector a -> IO (V.Vector ((a, b), (a, b)))
evalSndFV f v
| compare (V.length v) 1 == GT = do
zipped <- normSnd . V.zip v $ V.map f v
return . V.zip zipped . V.unsafeSlice 1 (V.length v - 1) $ zipped
| otherwise = return V.empty
evalSndFVM :: (Fractional b, Ord b) => (a -> IO b) -> V.Vector a -> IO (V.Vector ((a, b), (a, b)))
evalSndFVM f v
| compare (V.length v) 1 == GT = do
v1 <- V.mapM f v
zipped <- normSnd . V.zip v $ v1
return . V.zip zipped . V.unsafeSlice 1 (V.length v - 1) $ zipped
| otherwise = return V.empty
double42Float4 :: ((Double,Double), (Double,Double)) -> ((Float,Float), (Float,Float))
double42Float4 ((x,y),(z,t)) = ((double2Float x,double2Float y),(double2Float z,double2Float t))
float42Double4 :: ((Float,Float), (Float,Float)) -> ((Double,Double), (Double,Double))
float42Double4 ((x,y),(z,t)) = ((float2Double x,float2Double y),(float2Double z,float2Double t))