module Copilot.Library.Statistics
( max, min, sum, mean, meanNow ) where
import Copilot.Language
import Copilot.Language.Prelude
import Copilot.Library.Utils
sum :: ( Typed a, Num a ) => Int -> Stream a -> Stream a
sum n s = nfoldl1 n (+) s
max :: ( Typed a, Ord a ) => Int -> Stream a -> Stream a
max n s = nfoldl1 n largest s
where largest = \ x y -> mux ( x >= y ) x y
min :: ( Typed a, Ord a ) => Int -> Stream a -> Stream a
min n s = nfoldl1 n smallest s
where smallest = \ x y -> mux ( x <= y ) x y
mean :: ( Typed a, Fractional a ) => Int -> Stream a -> Stream a
mean n s = ( sum n s ) / ( fromIntegral n )
meanNow :: ( Typed a, Integral a ) => [ Stream a ] -> Stream a
meanNow [] =
badUsage "list of arguments to meanNow must be nonempty"
meanNow ls = ( foldl1 (+) ls ) `div` ( fromIntegral $ length ls )