{-# LANGUAGE BangPatterns, CPP, FlexibleContexts, Rank2Types #-}
{-# OPTIONS_GHC -fsimpl-tick-factor=200 #-}
module Statistics.Function
(
minMax
, sort
, gsort
, sortBy
, partialSort
, indexed
, indices
, nextHighestPowerOfTwo
, within
, square
, unsafeModify
, for
, rfor
) where
#include "MachDeps.h"
import Control.Monad.ST (ST)
import Data.Bits ((.|.), shiftR)
import qualified Data.Vector.Algorithms.Intro as I
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as M
import Numeric.MathFunctions.Comparison (within)
sort :: U.Vector Double -> U.Vector Double
sort :: Vector Double -> Vector Double
sort = (forall s. Mutable Vector s Double -> ST s ())
-> Vector Double -> Vector Double
forall (v :: * -> *) a.
Vector v a =>
(forall s. Mutable v s a -> ST s ()) -> v a -> v a
G.modify MVector (PrimState (ST s)) Double -> ST s ()
Mutable Vector s Double -> ST s ()
forall s. Mutable Vector s Double -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> m ()
I.sort
{-# NOINLINE sort #-}
gsort :: (Ord e, G.Vector v e) => v e -> v e
gsort :: forall e (v :: * -> *). (Ord e, Vector v e) => v e -> v e
gsort = (forall s. Mutable v s e -> ST s ()) -> v e -> v e
forall (v :: * -> *) a.
Vector v a =>
(forall s. Mutable v s a -> ST s ()) -> v a -> v a
G.modify Mutable v s e -> ST s ()
Mutable v (PrimState (ST s)) e -> ST s ()
forall s. Mutable v s e -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> m ()
I.sort
{-# INLINE gsort #-}
sortBy :: (G.Vector v e) => I.Comparison e -> v e -> v e
sortBy :: forall (v :: * -> *) e. Vector v e => Comparison e -> v e -> v e
sortBy Comparison e
f = (forall s. Mutable v s e -> ST s ()) -> v e -> v e
forall (v :: * -> *) a.
Vector v a =>
(forall s. Mutable v s a -> ST s ()) -> v a -> v a
G.modify ((forall s. Mutable v s e -> ST s ()) -> v e -> v e)
-> (forall s. Mutable v s e -> ST s ()) -> v e -> v e
forall a b. (a -> b) -> a -> b
$ Comparison e -> Mutable v (PrimState (ST s)) e -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e) =>
Comparison e -> v (PrimState m) e -> m ()
I.sortBy Comparison e
f
{-# INLINE sortBy #-}
partialSort :: (G.Vector v e, Ord e) =>
Int
-> v e
-> v e
partialSort :: forall (v :: * -> *) e. (Vector v e, Ord e) => Int -> v e -> v e
partialSort Int
k = (forall s. Mutable v s e -> ST s ()) -> v e -> v e
forall (v :: * -> *) a.
Vector v a =>
(forall s. Mutable v s a -> ST s ()) -> v a -> v a
G.modify (Mutable v (PrimState (ST s)) e -> Int -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> Int -> m ()
`I.partialSort` Int
k)
{-# SPECIALIZE partialSort :: Int -> U.Vector Double -> U.Vector Double #-}
indices :: (G.Vector v a, G.Vector v Int) => v a -> v Int
indices :: forall (v :: * -> *) a. (Vector v a, Vector v Int) => v a -> v Int
indices v a
a = Int -> Int -> v Int
forall (v :: * -> *) a. (Vector v a, Enum a) => a -> a -> v a
G.enumFromTo Int
0 (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
G.length v a
a Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
{-# INLINE indices #-}
indexed :: (G.Vector v e, G.Vector v Int, G.Vector v (Int,e)) => v e -> v (Int,e)
indexed :: forall (v :: * -> *) e.
(Vector v e, Vector v Int, Vector v (Int, e)) =>
v e -> v (Int, e)
indexed v e
a = v Int -> v e -> v (Int, e)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v a -> v b -> v (a, b)
G.zip (v e -> v Int
forall (v :: * -> *) a. (Vector v a, Vector v Int) => v a -> v Int
indices v e
a) v e
a
{-# INLINE indexed #-}
data MM = MM {-# UNPACK #-} !Double {-# UNPACK #-} !Double
minMax :: (G.Vector v Double) => v Double -> (Double, Double)
minMax :: forall (v :: * -> *).
Vector v Double =>
v Double -> (Double, Double)
minMax = MM -> (Double, Double)
fini (MM -> (Double, Double))
-> (v Double -> MM) -> v Double -> (Double, Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MM -> Double -> MM) -> MM -> v Double -> MM
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' MM -> Double -> MM
go (Double -> Double -> MM
MM (Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0) (-Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0))
where
go :: MM -> Double -> MM
go (MM Double
lo Double
hi) Double
k = Double -> Double -> MM
MM (Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
lo Double
k) (Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
hi Double
k)
fini :: MM -> (Double, Double)
fini (MM Double
lo Double
hi) = (Double
lo, Double
hi)
{-# INLINE minMax #-}
nextHighestPowerOfTwo :: Int -> Int
nextHighestPowerOfTwo :: Int -> Int
nextHighestPowerOfTwo Int
n
#if WORD_SIZE_IN_BITS == 64
= Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
_i32
#else
= 1 + i16
#endif
where
i0 :: Int
i0 = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
i1 :: Int
i1 = Int
i0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i0 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
1
i2 :: Int
i2 = Int
i1 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i1 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
2
i4 :: Int
i4 = Int
i2 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i2 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
4
i8 :: Int
i8 = Int
i4 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i4 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
8
i16 :: Int
i16 = Int
i8 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i8 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
16
_i32 :: Int
_i32 = Int
i16 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i16 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
32
square :: Double -> Double
square :: Double -> Double
square Double
x = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x
for :: Monad m => Int -> Int -> (Int -> m ()) -> m ()
for :: forall (m :: * -> *).
Monad m =>
Int -> Int -> (Int -> m ()) -> m ()
for Int
n0 !Int
n Int -> m ()
f = Int -> m ()
loop Int
n0
where
loop :: Int -> m ()
loop Int
i | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = Int -> m ()
f Int
i m () -> m () -> m ()
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m ()
loop (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
{-# INLINE for #-}
rfor :: Monad m => Int -> Int -> (Int -> m ()) -> m ()
rfor :: forall (m :: * -> *).
Monad m =>
Int -> Int -> (Int -> m ()) -> m ()
rfor Int
n0 !Int
n Int -> m ()
f = Int -> m ()
loop Int
n0
where
loop :: Int -> m ()
loop Int
i | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = let i' :: Int
i' = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1 in Int -> m ()
f Int
i' m () -> m () -> m ()
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m ()
loop Int
i'
{-# INLINE rfor #-}
unsafeModify :: M.MVector s Double -> Int -> (Double -> Double) -> ST s ()
unsafeModify :: forall s. MVector s Double -> Int -> (Double -> Double) -> ST s ()
unsafeModify MVector s Double
v Int
i Double -> Double
f = do
Double
k <- MVector (PrimState (ST s)) Double -> Int -> ST s Double
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
M.unsafeRead MVector s Double
MVector (PrimState (ST s)) Double
v Int
i
MVector (PrimState (ST s)) Double -> Int -> Double -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> a -> m ()
M.unsafeWrite MVector s Double
MVector (PrimState (ST s)) Double
v Int
i (Double -> Double
f Double
k)
{-# INLINE unsafeModify #-}