module Data.Histogram.Bin.LogBinD (
LogBinD(..)
, logBinD
) where
import Control.Monad (liftM3)
import GHC.Float (double2Int)
import Data.Typeable (Typeable)
import Data.Data (Data)
import Text.Read (Read(..))
import Data.Histogram.Bin.Classes
import Data.Histogram.Parse
data LogBinD = LogBinD
Double
Double
Int
deriving (Eq,Data,Typeable)
logBinD :: Double -> Int -> Double -> LogBinD
logBinD lo n hi = LogBinD lo ((hi/lo) ** (1 / fromIntegral n)) n
floorD :: Double -> Int
floorD x | x < 0 = double2Int x 1
| otherwise = double2Int x
instance Bin LogBinD where
type BinValue LogBinD = Double
toIndex !(LogBinD base step _) !x = floorD $ logBase step (x / base)
fromIndex !(LogBinD base step _) !i | i >= 0 = base * step ** (fromIntegral i + 0.5)
| otherwise = 1 / 0
nBins !(LogBinD _ _ n) = n
instance IntervalBin LogBinD where
binInterval (LogBinD base step _) i = (x, x*step) where x = base * step ** (fromIntegral i)
instance Bin1D LogBinD where
lowerLimit (LogBinD lo _ _) = lo
upperLimit (LogBinD lo r n) = lo * r ^ n
unsafeSliceBin i j (LogBinD from step _) = LogBinD (from * step ^ i) step (ji+1)
instance VariableBin LogBinD where
binSizeN (LogBinD base step _) n = let x = base * step ^ n in x*step x
instance Show LogBinD where
show b =
unlines [ "# LogBinD"
, "# Lo = " ++ show (lowerLimit b)
, "# N = " ++ show (nBins b)
, "# Hi = " ++ show (upperLimit b)
]
instance Read LogBinD where
readPrec = do
keyword "LogBinD"
liftM3 logBinD (value "Lo") (value "N") (value "Hi")