import System.Environment import Text.Printf import Text.Regex.Posix data Strata = Strata { lowerBound :: Double, upperBound :: Double, count :: Integer } deriving (Show, Eq) strataBounds :: [Double] strataBounds = [ 0.0, 1.0e-8, 1.0e-7, 1.0e-6, 1.0e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0, 2.0 ] newStrataCounts :: [Strata] newStrataCounts = newStrataCounts' strataBounds where newStrataCounts' :: [Double] -> [Strata] newStrataCounts' (lb:ub:bounds) = (Strata lb ub 0):(newStrataCounts' (ub:bounds)) newStrataCounts' (lb:[]) = [] main = do l <- getContents printStrataCounts $ stratify (lines l) printStrataCounts :: ([Strata], Integer) -> IO () printStrataCounts (s, n) = printStrataCounts' s n 0 where printStrataCounts' :: [Strata] -> Integer -> Integer -> IO () printStrataCounts' (s:ss) n total = do printf "[%1.2e, %1.2e) = %i (%1.2f%%) %i\n" (lowerBound s) (upperBound s) (count s) p (total+(count s)) printStrataCounts' ss n (total+(count s)) where p :: Double p = 100.0*(fromIntegral (count s) :: Double)/(fromIntegral n :: Double) printStrataCounts' [] n total = return () stratify :: [String] -> ([Strata], Integer) stratify l = stratify' l newStrataCounts 0 where stratify' :: [String] -> [Strata] -> Integer -> ([Strata], Integer) stratify' (l:ls) s n | (length r) > 0 = stratify' ls (stratifyElement (read (r!!1) :: Double) [] (head s) (tail s)) (n+1) | otherwise = stratify' ls s n where r = getAllTextSubmatches $ l =~ "^\\s*matrix\\(.*= ([0-9eE.+-]+)$" stratifyElement :: Double -> [Strata] -> Strata -> [Strata] -> [Strata] stratifyElement aij a b c | aij >= (lowerBound b) && aij < (upperBound b) = a ++ [Strata (lowerBound b) (upperBound b) ((count b)+1)] ++ c | (length c) > 1 = stratifyElement aij (a ++ [b]) (head c) (tail c) | otherwise = error $ "can not stratify aij = " ++ (show aij) stratify' [] s n = (s, n)