module Statistics.Function
(
minMax
, sort
, partialSort
, indexed
, indices
, create
) where
import Control.Exception (assert)
import Control.Monad.Primitive (PrimMonad)
import Data.Vector.Algorithms.Combinators (apply)
import Data.Vector.Generic (unsafeFreeze)
import qualified Data.Vector.Algorithms.Intro as I
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as M
sort :: (Ord e, G.Vector v e) => v e -> v e
sort = apply I.sort
partialSort :: (G.Vector v e, Ord e) =>
Int
-> v e
-> v e
partialSort k = apply (\a -> I.partialSort a k)
indices :: (G.Vector v a, G.Vector v Int) => v a -> v Int
indices a = G.enumFromTo 0 (G.length a 1)
indexed :: (G.Vector v e, G.Vector v Int, G.Vector v (Int,e)) => v e -> v (Int,e)
indexed a = G.zip (indices a) a
data MM = MM !Double !Double
minMax :: (G.Vector v Double) => v Double -> (Double, Double)
minMax = fini . G.foldl' go (MM (1/0) (1/0))
where
go (MM lo hi) k = MM (min lo k) (max hi k)
fini (MM lo hi) = (lo, hi)
create :: (PrimMonad m, G.Vector v e) => Int -> (Int -> m e) -> m (v e)
create size itemAt = assert (size >= 0) $
M.new size >>= loop 0
where
loop k arr | k >= size = unsafeFreeze arr
| otherwise = do r <- itemAt k
M.write arr k r
loop (k+1) arr