log-domain-0.9.2: Log-domain arithmetic

Copyright(c) Edward Kmett 2013
LicenseBSD3
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell98

Numeric.Log

Description

 

Synopsis

Documentation

newtype Log a Source

Log-domain Float and Double values.

Constructors

Exp 

Fields

ln :: a
 

Instances

Monad Log 
Functor Log 
Applicative Log 
Foldable Log 
Traversable Log 
Serial1 Log 
Comonad Log 
ComonadApply Log 
Distributive Log 
Hashable1 Log 
Traversable1 Log 
Foldable1 Log 
Apply Log 
Bind Log 
Extend Log 
(RealFloat a, Unbox a) => Vector Vector (Log a) 
(RealFloat a, Unbox a) => MVector MVector (Log a) 
(RealFloat a, Precise a, Enum a) => Enum (Log a) 
Eq a => Eq (Log a) 
(RealFloat a, Precise a) => Floating (Log a) 
(Precise a, RealFloat a, Eq a) => Fractional (Log a) 
Data a => Data (Log a) 
(Precise a, RealFloat a) => Num (Log a)

Handle subtraction.

>>> 3 - 1 :: Log Double
2.0000000000000004
>>> 1 - 3 :: Log Double
NaN
>>> 3 - 2 :: Log Float
1.0
>>> 1 - 3 :: Log Float
NaN
Ord a => Ord (Log a) 
(Floating a, Read a) => Read (Log a) 
(Precise a, RealFloat a, Ord a) => Real (Log a) 
(Floating a, Show a) => Show (Log a) 
Generic (Log a) 
Storable a => Storable (Log a) 
(Precise a, RealFloat a) => Monoid (Log a) 
Binary a => Binary (Log a) 
Serial a => Serial (Log a) 
Serialize a => Serialize (Log a) 
NFData a => NFData (Log a) 
Hashable a => Hashable (Log a) 
SafeCopy a0 => SafeCopy (Log a) 
(RealFloat a, Unbox a) => Unbox (Log a) 
Typeable (* -> *) Log 
data MVector s (Log a) = MV_Log (MVector s a) 
type Rep (Log a) 
data Vector (Log a) = V_Log (Vector a) 

class Floating a => Precise a where Source

This provides log1p and expm1 for working more accurately with small numbers.

Minimal complete definition

log1p, expm1

Methods

log1p :: a -> a Source

Computes log(1 + x)

This is far enough from 0 that the Taylor series is defined.

This can provide much more accurate answers for logarithms of numbers close to 1 (x near 0).

These arise when working wth log-scale probabilities a lot.

expm1 :: a -> a Source

The Taylor series for exp(x) is given by

exp(x) = 1 + x + x^2/2! + ...

When x is small, the leading 1 consumes all of the available precision.

This computes:

exp(x) - 1 = x + x^2/2! + ..

which can afford you a great deal of additional precision if you move things around algebraically to provide the 1 by other means.

log1pexp :: a -> a Source

log1mexp :: a -> a Source

sum :: (RealFloat a, Ord a, Precise 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
40001.3

This sum gives a more accurate result,

>>> Numeric.Log.sum xs
40004.01

NB: This does require two passes over the data.