Copyright (c) Edward Kmett 2012-2015 BSD3 ekmett@gmail.com experimental GHC only None Haskell2010

Description

Reverse-Mode Automatic Differentiation using a single Wengert list (or "tape").

This version uses Data.Reflection to find and update the tape.

This is asymptotically faster than using Kahn, which is forced to reify and topologically sort the graph, but it requires a fairly expensive rendezvous during construction when updated using multiple threads.

Synopsis

# Documentation

data Reverse s a where Source #

Constructors

 Zero :: Reverse s a Lift :: a -> Reverse s a Reverse :: !Int -> a -> Reverse s a
Instances
 (Reifies s Tape, Num a, Bounded a) => Bounded (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse MethodsminBound :: Reverse s a #maxBound :: Reverse s a # (Reifies s Tape, Num a, Enum a) => Enum (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methodssucc :: Reverse s a -> Reverse s a #pred :: Reverse s a -> Reverse s a #toEnum :: Int -> Reverse s a #fromEnum :: Reverse s a -> Int #enumFrom :: Reverse s a -> [Reverse s a] #enumFromThen :: Reverse s a -> Reverse s a -> [Reverse s a] #enumFromTo :: Reverse s a -> Reverse s a -> [Reverse s a] #enumFromThenTo :: Reverse s a -> Reverse s a -> Reverse s a -> [Reverse s a] # (Reifies s Tape, Num a, Eq a) => Eq (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methods(==) :: Reverse s a -> Reverse s a -> Bool #(/=) :: Reverse s a -> Reverse s a -> Bool # (Reifies s Tape, Floating a) => Floating (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methodspi :: Reverse s a #exp :: Reverse s a -> Reverse s a #log :: Reverse s a -> Reverse s a #sqrt :: Reverse s a -> Reverse s a #(**) :: Reverse s a -> Reverse s a -> Reverse s a #logBase :: Reverse s a -> Reverse s a -> Reverse s a #sin :: Reverse s a -> Reverse s a #cos :: Reverse s a -> Reverse s a #tan :: Reverse s a -> Reverse s a #asin :: Reverse s a -> Reverse s a #acos :: Reverse s a -> Reverse s a #atan :: Reverse s a -> Reverse s a #sinh :: Reverse s a -> Reverse s a #cosh :: Reverse s a -> Reverse s a #tanh :: Reverse s a -> Reverse s a #asinh :: Reverse s a -> Reverse s a #acosh :: Reverse s a -> Reverse s a #atanh :: Reverse s a -> Reverse s a #log1p :: Reverse s a -> Reverse s a #expm1 :: Reverse s a -> Reverse s a #log1pexp :: Reverse s a -> Reverse s a #log1mexp :: Reverse s a -> Reverse s a # (Reifies s Tape, Fractional a) => Fractional (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methods(/) :: Reverse s a -> Reverse s a -> Reverse s a #recip :: Reverse s a -> Reverse s a #fromRational :: Rational -> Reverse s a # (Reifies s Tape, Num a) => Num (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methods(+) :: Reverse s a -> Reverse s a -> Reverse s a #(-) :: Reverse s a -> Reverse s a -> Reverse s a #(*) :: Reverse s a -> Reverse s a -> Reverse s a #negate :: Reverse s a -> Reverse s a #abs :: Reverse s a -> Reverse s a #signum :: Reverse s a -> Reverse s a #fromInteger :: Integer -> Reverse s a # (Reifies s Tape, Num a, Ord a) => Ord (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methodscompare :: Reverse s a -> Reverse s a -> Ordering #(<) :: Reverse s a -> Reverse s a -> Bool #(<=) :: Reverse s a -> Reverse s a -> Bool #(>) :: Reverse s a -> Reverse s a -> Bool #(>=) :: Reverse s a -> Reverse s a -> Bool #max :: Reverse s a -> Reverse s a -> Reverse s a #min :: Reverse s a -> Reverse s a -> Reverse s a # (Reifies s Tape, Real a) => Real (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse MethodstoRational :: Reverse s a -> Rational # (Reifies s Tape, RealFloat a) => RealFloat (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse MethodsfloatRadix :: Reverse s a -> Integer #floatDigits :: Reverse s a -> Int #floatRange :: Reverse s a -> (Int, Int) #decodeFloat :: Reverse s a -> (Integer, Int) #encodeFloat :: Integer -> Int -> Reverse s a #exponent :: Reverse s a -> Int #significand :: Reverse s a -> Reverse s a #scaleFloat :: Int -> Reverse s a -> Reverse s a #isNaN :: Reverse s a -> Bool #isInfinite :: Reverse s a -> Bool #isDenormalized :: Reverse s a -> Bool #isNegativeZero :: Reverse s a -> Bool #isIEEE :: Reverse s a -> Bool #atan2 :: Reverse s a -> Reverse s a -> Reverse s a # (Reifies s Tape, RealFrac a) => RealFrac (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse MethodsproperFraction :: Integral b => Reverse s a -> (b, Reverse s a) #truncate :: Integral b => Reverse s a -> b #round :: Integral b => Reverse s a -> b #ceiling :: Integral b => Reverse s a -> b #floor :: Integral b => Reverse s a -> b # Show a => Show (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse MethodsshowsPrec :: Int -> Reverse s a -> ShowS #show :: Reverse s a -> String #showList :: [Reverse s a] -> ShowS # (Reifies s Tape, Erf a) => Erf (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methodserf :: Reverse s a -> Reverse s a #erfc :: Reverse s a -> Reverse s a #erfcx :: Reverse s a -> Reverse s a #normcdf :: Reverse s a -> Reverse s a # (Reifies s Tape, InvErf a) => InvErf (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Methodsinverf :: Reverse s a -> Reverse s a #inverfc :: Reverse s a -> Reverse s a #invnormcdf :: Reverse s a -> Reverse s a # (Reifies s Tape, Num a) => Mode (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Associated Typestype Scalar (Reverse s a) :: Type Source # MethodsisKnownZero :: Reverse s a -> Bool Source #auto :: Scalar (Reverse s a) -> Reverse s a Source #(*^) :: Scalar (Reverse s a) -> Reverse s a -> Reverse s a Source #(^*) :: Reverse s a -> Scalar (Reverse s a) -> Reverse s a Source #(^/) :: Reverse s a -> Scalar (Reverse s a) -> Reverse s a Source #zero :: Reverse s a Source # (Reifies s Tape, Num a) => Jacobian (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse Associated Typestype D (Reverse s a) :: Type Source # Methodsunary :: (Scalar (Reverse s a) -> Scalar (Reverse s a)) -> D (Reverse s a) -> Reverse s a -> Reverse s a Source #lift1 :: (Scalar (Reverse s a) -> Scalar (Reverse s a)) -> (D (Reverse s a) -> D (Reverse s a)) -> Reverse s a -> Reverse s a Source #lift1_ :: (Scalar (Reverse s a) -> Scalar (Reverse s a)) -> (D (Reverse s a) -> D (Reverse s a) -> D (Reverse s a)) -> Reverse s a -> Reverse s a Source #binary :: (Scalar (Reverse s a) -> Scalar (Reverse s a) -> Scalar (Reverse s a)) -> D (Reverse s a) -> D (Reverse s a) -> Reverse s a -> Reverse s a -> Reverse s a Source #lift2 :: (Scalar (Reverse s a) -> Scalar (Reverse s a) -> Scalar (Reverse s a)) -> (D (Reverse s a) -> D (Reverse s a) -> (D (Reverse s a), D (Reverse s a))) -> Reverse s a -> Reverse s a -> Reverse s a Source #lift2_ :: (Scalar (Reverse s a) -> Scalar (Reverse s a) -> Scalar (Reverse s a)) -> (D (Reverse s a) -> D (Reverse s a) -> D (Reverse s a) -> (D (Reverse s a), D (Reverse s a))) -> Reverse s a -> Reverse s a -> Reverse s a Source # type Scalar (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse type Scalar (Reverse s a) = a type D (Reverse s a) Source # Instance detailsDefined in Numeric.AD.Internal.Reverse type D (Reverse s a) = Id a

newtype Tape Source #

Constructors

 Tape FieldsgetTape :: IORef Head

data Head Source #

Constructors

data Cells where Source #

Constructors

 Nil :: Cells Unary :: !Int -> a -> Cells -> Cells Binary :: !Int -> !Int -> a -> a -> Cells -> Cells

reifyTape :: Int -> (forall s. Reifies s Tape => Proxy s -> r) -> r Source #

Construct a tape that starts with n variables.

partials :: forall s a. (Reifies s Tape, Num a) => Reverse s a -> [a] Source #

Extract the partials from the current chain for a given AD variable.

partialArrayOf :: (Reifies s Tape, Num a) => Proxy s -> (Int, Int) -> Reverse s a -> Array Int a Source #

Return an Array of partials given bounds for the variable IDs.

partialMapOf :: (Reifies s Tape, Num a) => Proxy s -> Reverse s a -> IntMap a Source #

Return an IntMap of sparse partials

derivativeOf :: (Reifies s Tape, Num a) => Proxy s -> Reverse s a -> a Source #

Helper that extracts the derivative of a chain when the chain was constructed with 1 variable.

derivativeOf' :: (Reifies s Tape, Num a) => Proxy s -> Reverse s a -> (a, a) Source #

Helper that extracts both the primal and derivative of a chain when the chain was constructed with 1 variable.

bind :: Traversable f => f a -> (f (Reverse s a), (Int, Int)) Source #

unbind :: Functor f => f (Reverse s a) -> Array Int a -> f a Source #

unbindMap :: (Functor f, Num a) => f (Reverse s a) -> IntMap a -> f a Source #

unbindWith :: (Functor f, Num a) => (a -> b -> c) -> f (Reverse s a) -> Array Int b -> f c Source #

unbindMapWithDefault :: (Functor f, Num a) => b -> (a -> b -> c) -> f (Reverse s a) -> IntMap b -> f c Source #

var :: a -> Int -> Reverse s a Source #

primal :: Num a => Reverse s a -> a Source #