log-domain-0.13.2: Log-domain arithmetic
Copyright(c) Edward Kmett 2013-2015
LicenseBSD3
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell2010

Numeric.Log

Description

 
Synopsis

Documentation

newtype Log a Source #

Log-domain Float and Double values.

Constructors

Exp 

Fields

Instances

Instances details
Monad Log Source # 
Instance details

Defined in Numeric.Log

Methods

(>>=) :: Log a -> (a -> Log b) -> Log b #

(>>) :: Log a -> Log b -> Log b #

return :: a -> Log a #

Functor Log Source # 
Instance details

Defined in Numeric.Log

Methods

fmap :: (a -> b) -> Log a -> Log b #

(<$) :: a -> Log b -> Log a #

Applicative Log Source # 
Instance details

Defined in Numeric.Log

Methods

pure :: a -> Log a #

(<*>) :: Log (a -> b) -> Log a -> Log b #

liftA2 :: (a -> b -> c) -> Log a -> Log b -> Log c #

(*>) :: Log a -> Log b -> Log b #

(<*) :: Log a -> Log b -> Log a #

Foldable Log Source # 
Instance details

Defined in Numeric.Log

Methods

fold :: Monoid m => Log m -> m #

foldMap :: Monoid m => (a -> m) -> Log a -> m #

foldMap' :: Monoid m => (a -> m) -> Log a -> m #

foldr :: (a -> b -> b) -> b -> Log a -> b #

foldr' :: (a -> b -> b) -> b -> Log a -> b #

foldl :: (b -> a -> b) -> b -> Log a -> b #

foldl' :: (b -> a -> b) -> b -> Log a -> b #

foldr1 :: (a -> a -> a) -> Log a -> a #

foldl1 :: (a -> a -> a) -> Log a -> a #

toList :: Log a -> [a] #

null :: Log a -> Bool #

length :: Log a -> Int #

elem :: Eq a => a -> Log a -> Bool #

maximum :: Ord a => Log a -> a #

minimum :: Ord a => Log a -> a #

sum :: Num a => Log a -> a #

product :: Num a => Log a -> a #

Traversable Log Source # 
Instance details

Defined in Numeric.Log

Methods

traverse :: Applicative f => (a -> f b) -> Log a -> f (Log b) #

sequenceA :: Applicative f => Log (f a) -> f (Log a) #

mapM :: Monad m => (a -> m b) -> Log a -> m (Log b) #

sequence :: Monad m => Log (m a) -> m (Log a) #

Eq1 Log Source # 
Instance details

Defined in Numeric.Log

Methods

liftEq :: (a -> b -> Bool) -> Log a -> Log b -> Bool #

Serial1 Log Source # 
Instance details

Defined in Numeric.Log

Methods

serializeWith :: MonadPut m => (a -> m ()) -> Log a -> m () #

deserializeWith :: MonadGet m => m a -> m (Log a) #

Comonad Log Source # 
Instance details

Defined in Numeric.Log

Methods

extract :: Log a -> a #

duplicate :: Log a -> Log (Log a) #

extend :: (Log a -> b) -> Log a -> Log b #

ComonadApply Log Source # 
Instance details

Defined in Numeric.Log

Methods

(<@>) :: Log (a -> b) -> Log a -> Log b #

(@>) :: Log a -> Log b -> Log b #

(<@) :: Log a -> Log b -> Log a #

Distributive Log Source # 
Instance details

Defined in Numeric.Log

Methods

distribute :: Functor f => f (Log a) -> Log (f a) #

collect :: Functor f => (a -> Log b) -> f a -> Log (f b) #

distributeM :: Monad m => m (Log a) -> Log (m a) #

collectM :: Monad m => (a -> Log b) -> m a -> Log (m b) #

Hashable1 Log Source # 
Instance details

Defined in Numeric.Log

Methods

liftHashWithSalt :: (Int -> a -> Int) -> Int -> Log a -> Int #

Traversable1 Log Source # 
Instance details

Defined in Numeric.Log

Methods

traverse1 :: Apply f => (a -> f b) -> Log a -> f (Log b) #

sequence1 :: Apply f => Log (f b) -> f (Log b) #

Apply Log Source # 
Instance details

Defined in Numeric.Log

Methods

(<.>) :: Log (a -> b) -> Log a -> Log b #

(.>) :: Log a -> Log b -> Log b #

(<.) :: Log a -> Log b -> Log a #

liftF2 :: (a -> b -> c) -> Log a -> Log b -> Log c #

Bind Log Source # 
Instance details

Defined in Numeric.Log

Methods

(>>-) :: Log a -> (a -> Log b) -> Log b #

join :: Log (Log a) -> Log a #

Foldable1 Log Source # 
Instance details

Defined in Numeric.Log

Methods

fold1 :: Semigroup m => Log m -> m #

foldMap1 :: Semigroup m => (a -> m) -> Log a -> m #

toNonEmpty :: Log a -> NonEmpty a #

Extend Log Source # 
Instance details

Defined in Numeric.Log

Methods

duplicated :: Log a -> Log (Log a) #

extended :: (Log a -> b) -> Log a -> Log b #

(RealFloat a, Unbox a) => Vector Vector (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

basicUnsafeFreeze :: PrimMonad m => Mutable Vector (PrimState m) (Log a) -> m (Vector (Log a)) #

basicUnsafeThaw :: PrimMonad m => Vector (Log a) -> m (Mutable Vector (PrimState m) (Log a)) #

basicLength :: Vector (Log a) -> Int #

basicUnsafeSlice :: Int -> Int -> Vector (Log a) -> Vector (Log a) #

basicUnsafeIndexM :: Monad m => Vector (Log a) -> Int -> m (Log a) #

basicUnsafeCopy :: PrimMonad m => Mutable Vector (PrimState m) (Log a) -> Vector (Log a) -> m () #

elemseq :: Vector (Log a) -> Log a -> b -> b #

Unbox a => MVector MVector (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

basicLength :: MVector s (Log a) -> Int #

basicUnsafeSlice :: Int -> Int -> MVector s (Log a) -> MVector s (Log a) #

basicOverlaps :: MVector s (Log a) -> MVector s (Log a) -> Bool #

basicUnsafeNew :: PrimMonad m => Int -> m (MVector (PrimState m) (Log a)) #

basicInitialize :: PrimMonad m => MVector (PrimState m) (Log a) -> m () #

basicUnsafeReplicate :: PrimMonad m => Int -> Log a -> m (MVector (PrimState m) (Log a)) #

basicUnsafeRead :: PrimMonad m => MVector (PrimState m) (Log a) -> Int -> m (Log a) #

basicUnsafeWrite :: PrimMonad m => MVector (PrimState m) (Log a) -> Int -> Log a -> m () #

basicClear :: PrimMonad m => MVector (PrimState m) (Log a) -> m () #

basicSet :: PrimMonad m => MVector (PrimState m) (Log a) -> Log a -> m () #

basicUnsafeCopy :: PrimMonad m => MVector (PrimState m) (Log a) -> MVector (PrimState m) (Log a) -> m () #

basicUnsafeMove :: PrimMonad m => MVector (PrimState m) (Log a) -> MVector (PrimState m) (Log a) -> m () #

basicUnsafeGrow :: PrimMonad m => MVector (PrimState m) (Log a) -> Int -> m (MVector (PrimState m) (Log a)) #

(RealFloat a, Enum a) => Enum (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

succ :: Log a -> Log a #

pred :: Log a -> Log a #

toEnum :: Int -> Log a #

fromEnum :: Log a -> Int #

enumFrom :: Log a -> [Log a] #

enumFromThen :: Log a -> Log a -> [Log a] #

enumFromTo :: Log a -> Log a -> [Log a] #

enumFromThenTo :: Log a -> Log a -> Log a -> [Log a] #

Eq a => Eq (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

(==) :: Log a -> Log a -> Bool #

(/=) :: Log a -> Log a -> Bool #

RealFloat a => Floating (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

pi :: Log a #

exp :: Log a -> Log a #

log :: Log a -> Log a #

sqrt :: Log a -> Log a #

(**) :: Log a -> Log a -> Log a #

logBase :: Log a -> Log a -> Log a #

sin :: Log a -> Log a #

cos :: Log a -> Log a #

tan :: Log a -> Log a #

asin :: Log a -> Log a #

acos :: Log a -> Log a #

atan :: Log a -> Log a #

sinh :: Log a -> Log a #

cosh :: Log a -> Log a #

tanh :: Log a -> Log a #

asinh :: Log a -> Log a #

acosh :: Log a -> Log a #

atanh :: Log a -> Log a #

log1p :: Log a -> Log a #

expm1 :: Log a -> Log a #

log1pexp :: Log a -> Log a #

log1mexp :: Log a -> Log a #

RealFloat a => Fractional (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

(/) :: Log a -> Log a -> Log a #

recip :: Log a -> Log a #

fromRational :: Rational -> Log a #

Data a => Data (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Log a -> c (Log a) #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Log a) #

toConstr :: Log a -> Constr #

dataTypeOf :: Log a -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Log a)) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Log a)) #

gmapT :: (forall b. Data b => b -> b) -> Log a -> Log a #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Log a -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Log a -> r #

gmapQ :: (forall d. Data d => d -> u) -> Log a -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Log a -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Log a -> m (Log a) #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Log a -> m (Log a) #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Log a -> m (Log a) #

RealFloat a => Num (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

(+) :: Log a -> Log a -> Log a #

(-) :: Log a -> Log a -> Log a #

(*) :: Log a -> Log a -> Log a #

negate :: Log a -> Log a #

abs :: Log a -> Log a #

signum :: Log a -> Log a #

fromInteger :: Integer -> Log a #

Ord a => Ord (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

compare :: Log a -> Log a -> Ordering #

(<) :: Log a -> Log a -> Bool #

(<=) :: Log a -> Log a -> Bool #

(>) :: Log a -> Log a -> Bool #

(>=) :: Log a -> Log a -> Bool #

max :: Log a -> Log a -> Log a #

min :: Log a -> Log a -> Log a #

(Floating a, Read a) => Read (Log a) Source # 
Instance details

Defined in Numeric.Log

(RealFloat a, Ord a) => Real (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

toRational :: Log a -> Rational #

RealFloat a => RealFrac (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

properFraction :: Integral b => Log a -> (b, Log a) #

truncate :: Integral b => Log a -> b #

round :: Integral b => Log a -> b #

ceiling :: Integral b => Log a -> b #

floor :: Integral b => Log a -> b #

(Floating a, Show a) => Show (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

showsPrec :: Int -> Log a -> ShowS #

show :: Log a -> String #

showList :: [Log a] -> ShowS #

Generic (Log a) Source # 
Instance details

Defined in Numeric.Log

Associated Types

type Rep (Log a) :: Type -> Type #

Methods

from :: Log a -> Rep (Log a) x #

to :: Rep (Log a) x -> Log a #

RealFloat a => Semigroup (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

(<>) :: Log a -> Log a -> Log a #

sconcat :: NonEmpty (Log a) -> Log a #

stimes :: Integral b => b -> Log a -> Log a #

RealFloat a => Monoid (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

mempty :: Log a #

mappend :: Log a -> Log a -> Log a #

mconcat :: [Log a] -> Log a #

Storable a => Storable (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

sizeOf :: Log a -> Int #

alignment :: Log a -> Int #

peekElemOff :: Ptr (Log a) -> Int -> IO (Log a) #

pokeElemOff :: Ptr (Log a) -> Int -> Log a -> IO () #

peekByteOff :: Ptr b -> Int -> IO (Log a) #

pokeByteOff :: Ptr b -> Int -> Log a -> IO () #

peek :: Ptr (Log a) -> IO (Log a) #

poke :: Ptr (Log a) -> Log a -> IO () #

Binary a => Binary (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

put :: Log a -> Put #

get :: Get (Log a) #

putList :: [Log a] -> Put #

Serial a => Serial (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

serialize :: MonadPut m => Log a -> m () #

deserialize :: MonadGet m => m (Log a) #

Serialize a => Serialize (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

put :: Putter (Log a) #

get :: Get (Log a) #

NFData a => NFData (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

rnf :: Log a -> () #

Hashable a => Hashable (Log a) Source # 
Instance details

Defined in Numeric.Log

Methods

hashWithSalt :: Int -> Log a -> Int #

hash :: Log a -> Int #

(RealFloat a, Unbox a) => Unbox (Log a) Source # 
Instance details

Defined in Numeric.Log

newtype MVector s (Log a) Source # 
Instance details

Defined in Numeric.Log

newtype MVector s (Log a) = MV_Log (MVector s a)
type Rep (Log a) Source # 
Instance details

Defined in Numeric.Log

type Rep (Log a) = D1 ('MetaData "Log" "Numeric.Log" "log-domain-0.13.2-FS2B0RGNYIS8FJTLGCg4HI" 'True) (C1 ('MetaCons "Exp" 'PrefixI 'True) (S1 ('MetaSel ('Just "ln") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))
newtype Vector (Log a) Source # 
Instance details

Defined in Numeric.Log

newtype Vector (Log a) = V_Log (Vector a)

sum :: (RealFloat a, Foldable f) => f (Log a) -> Log a Source #

Efficiently and accurately compute the sum of a set of log-domain numbers

While folding with (+) accomplishes the same end, it requires an additional n-2 logarithms to sum n terms. In addition, here we introduce fewer opportunities for round-off error.

While for small quantities the naive sum accumulates error,

>>> let xs = Prelude.replicate 40000 (Exp 1e-4) :: [Log Float]
>>> Prelude.sum xs ~= 4.00e4
True

This sum gives a more accurate result,

>>> Numeric.Log.sum xs ~= 4.00e4
True

NB: This does require two passes over the data.