{-| Module : Main (file aern2-real-benchOp) Description : execute a CR operation for benchmarking Copyright : (c) Michal Konecny License : BSD3 Maintainer : mikkonecny@gmail.com Stability : experimental Portability : portable -} module Main where import MixedTypesNumPrelude -- import Prelude import Text.Printf import System.Environment import System.IO.Unsafe (unsafePerformIO) import System.Random (randomRIO) import Test.QuickCheck import AERN2.Utils.Bench import AERN2.MP import AERN2.Real import AERN2.Real.Tests () -- instance Arbitrary CauchyReal main :: IO () main = do args <- getArgs (computationDescription, results) <- processArgs args putStrLn $ computationDescription putStrLn $ "accuracies = " ++ show (map getAccuracy results) processArgs :: [String] -> IO (String, [MPBall]) processArgs [op, countS, accuracyS] = return (computationDescription, results) where computationDescription = printf "computing %s (%d times) using accuracy %d" op count ac ac :: Integer ac = read accuracyS count :: Integer count = read countS results = case op of "exp" -> map ((? (bitsS ac)) . exp) $ unsafePerformIO $ pickValues valuesSmall count "log" -> map ((~!) . (? (bitsS ac)) . log) $ unsafePerformIO $ pickValues valuesPositive count "sqrt" -> map ((~!) . (? (bitsS ac)) . sqrt) $ unsafePerformIO $ pickValues valuesPositive count "cos" -> map ((? (bitsS ac)) . cos) $ unsafePerformIO $ pickValues values count "add" -> map ((? (bitsS ac)) . (uncurry (+))) $ unsafePerformIO $ pickValues2 values values count "mul" -> map ((? (bitsS ac)) . (uncurry (*))) $ unsafePerformIO $ pickValues2 values values count "div" -> map ((~!) . (? (bitsS ac)) . (uncurry (/))) $ unsafePerformIO $ pickValues2 values valuesPositive count "logistic" -> map ((? (bitsS ac)) . (logistic 3.82 count)) $ [real 0.125] _ -> error $ "unknown op " ++ op processArgs _ = error "expecting arguments: " logistic :: Rational -> Integer -> CauchyReal -> CauchyReal logistic c n x | n == 0 = x | otherwise = logistic c (n-1) $ c * x * (1-x) pickValues2 :: [CauchyReal] -> [CauchyReal] -> Integer -> IO [(CauchyReal, CauchyReal)] pickValues2 vals1 vals2 count = do p1 <- pickValues vals1 count p2 <- pickValues vals2 count return $ zip p1 p2 pickValues :: [CauchyReal] -> Integer -> IO [CauchyReal] pickValues vals count = sequence $ [ do i1 <- randomRIO (1,maxIndex) let x = vals !! i1 return x | _j <- [1..count] ] maxIndex :: Integer maxIndex = 1000 valuesSmall :: [CauchyReal] valuesSmall = map makeSmall values where makeSmall :: CauchyReal -> CauchyReal makeSmall x | abs (getBall x) ! MPBall getBall xx = xx ? (bitsS 53) valuesPositive :: [CauchyReal] valuesPositive = filter ((!>! 0) . getBall) values where getBall :: CauchyReal -> MPBall getBall x = x ? (bitsS 53) values :: [CauchyReal] values = listFromGen (real <$> (arbitrary :: Gen Rational))