-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Automatic Differentiation -- -- Forward, reverse, and higher-order automatic differentiation -- combinators with a common API. -- -- Type-level "branding" is used to prevent the end user from confusing -- infinitesimals. @package ad @version 0.13 module Numeric.AD.Classes class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a -- |
--   'one' = 'lift' 1
--   
one :: (Mode t, Num a) => t a -- | Jacobian is used by deriveMode but is not exposed via -- Mode to prevent its abuse by end users via the AD data -- type. class (Mode t, Mode (D t)) => Jacobian t where { type family D t :: * -> *; } unary :: (Jacobian t, Num a) => (a -> a) -> D t a -> t a -> t a lift1 :: (Jacobian t, Num a) => (a -> a) -> (D t a -> D t a) -> t a -> t a lift1_ :: (Jacobian t, Num a) => (a -> a) -> (D t a -> D t a -> D t a) -> t a -> t a binary :: (Jacobian t, Num a) => (a -> a -> a) -> D t a -> D t a -> t a -> t a -> t a lift2 :: (Jacobian t, Num a) => (a -> a -> a) -> (D t a -> D t a -> (D t a, D t a)) -> t a -> t a -> t a lift2_ :: (Jacobian t, Num a) => (a -> a -> a) -> (D t a -> D t a -> D t a -> (D t a, D t a)) -> t a -> t a -> t a -- | Primal is used by deriveMode but is not exposed via -- the Mode class to prevent its abuse by end users via the AD -- data type. -- -- It provides direct access to the result, stripped of its derivative -- information, but this is unsafe in general as (lift . primal) would -- discard derivative information. The end user is protected from -- accidentally using this function by the universal quantification on -- the various combinators we expose. class Primal t primal :: (Primal t, Num a) => t a -> a -- | deriveLifted t provides -- --
--   instance Lifted $t
--   
-- -- given supplied instances for -- --
--   instance Lifted $t => Primal $t where ...
--   instance Lifted $t => Jacobian $t where ...
--   
-- -- The seemingly redundant Lifted $t constraints are -- caused by Template Haskell staging restrictions. deriveLifted :: Q Type -> Q [Dec] -- | deriveNumeric f g provides the following instances: -- --
--   instance ('Lifted' $f, 'Num' a, 'Enum' a) => 'Enum' ($g a)
--   instance ('Lifted' $f, 'Num' a, 'Eq' a) => 'Eq' ($g a)
--   instance ('Lifted' $f, 'Num' a, 'Ord' a) => 'Ord' ($g a)
--   instance ('Lifted' $f, 'Num a, 'Bounded' a) => 'Bounded' ($g a)
--   instance ('Lifted' $f, 'Show' a) => 'Show' ($g a)
--   instance ('Lifted' $f, 'Num' a) => 'Num' ($g a)
--   instance ('Lifted' $f, 'Fractional' a) => 'Fractional' ($g a)
--   instance ('Lifted' $f, 'Floating' a) => 'Floating' ($g a)
--   instance ('Lifted' $f, 'RealFloat' a) => 'RealFloat' ($g a)
--   instance ('Lifted' $f, 'RealFrac' a) => 'RealFrac' ($g a)
--   instance ('Lifted' $f, 'Real' a) => 'Real' ($g a)
--   
deriveNumeric :: Q Type -> Q Type -> Q [Dec] -- | Higher-order versions of the stock numerical methods. class Lifted t showsPrec1 :: (Lifted t, Show a) => Int -> t a -> ShowS (==!) :: (Lifted t, Num a, Eq a) => t a -> t a -> Bool compare1 :: (Lifted t, Num a, Ord a) => t a -> t a -> Ordering fromInteger1 :: (Lifted t, Num a) => Integer -> t a (+!) :: (Lifted t, Num a) => t a -> t a -> t a (*!) :: (Lifted t, Num a) => t a -> t a -> t a (-!) :: (Lifted t, Num a) => t a -> t a -> t a negate1 :: (Lifted t, Num a) => t a -> t a signum1 :: (Lifted t, Num a) => t a -> t a abs1 :: (Lifted t, Num a) => t a -> t a (/!) :: (Lifted t, Fractional a) => t a -> t a -> t a recip1 :: (Lifted t, Fractional a) => t a -> t a fromRational1 :: (Lifted t, Fractional a) => Rational -> t a toRational1 :: (Lifted t, Real a) => t a -> Rational pi1 :: (Lifted t, Floating a) => t a exp1 :: (Lifted t, Floating a) => t a -> t a sqrt1 :: (Lifted t, Floating a) => t a -> t a log1 :: (Lifted t, Floating a) => t a -> t a (**!) :: (Lifted t, Floating a) => t a -> t a -> t a logBase1 :: (Lifted t, Floating a) => t a -> t a -> t a sin1 :: (Lifted t, Floating a) => t a -> t a atan1 :: (Lifted t, Floating a) => t a -> t a acos1 :: (Lifted t, Floating a) => t a -> t a asin1 :: (Lifted t, Floating a) => t a -> t a tan1 :: (Lifted t, Floating a) => t a -> t a cos1 :: (Lifted t, Floating a) => t a -> t a sinh1 :: (Lifted t, Floating a) => t a -> t a atanh1 :: (Lifted t, Floating a) => t a -> t a acosh1 :: (Lifted t, Floating a) => t a -> t a asinh1 :: (Lifted t, Floating a) => t a -> t a tanh1 :: (Lifted t, Floating a) => t a -> t a cosh1 :: (Lifted t, Floating a) => t a -> t a properFraction1 :: (Lifted t, RealFrac a, Integral b) => t a -> (b, t a) truncate1 :: (Lifted t, RealFrac a, Integral b) => t a -> b floor1 :: (Lifted t, RealFrac a, Integral b) => t a -> b ceiling1 :: (Lifted t, RealFrac a, Integral b) => t a -> b round1 :: (Lifted t, RealFrac a, Integral b) => t a -> b floatRadix1 :: (Lifted t, RealFloat a) => t a -> Integer floatDigits1 :: (Lifted t, RealFloat a) => t a -> Int floatRange1 :: (Lifted t, RealFloat a) => t a -> (Int, Int) decodeFloat1 :: (Lifted t, RealFloat a) => t a -> (Integer, Int) encodeFloat1 :: (Lifted t, RealFloat a) => Integer -> Int -> t a exponent1 :: (Lifted t, RealFloat a) => t a -> Int significand1 :: (Lifted t, RealFloat a) => t a -> t a scaleFloat1 :: (Lifted t, RealFloat a) => Int -> t a -> t a isNaN1 :: (Lifted t, RealFloat a) => t a -> Bool isIEEE1 :: (Lifted t, RealFloat a) => t a -> Bool isNegativeZero1 :: (Lifted t, RealFloat a) => t a -> Bool isDenormalized1 :: (Lifted t, RealFloat a) => t a -> Bool isInfinite1 :: (Lifted t, RealFloat a) => t a -> Bool atan21 :: (Lifted t, RealFloat a) => t a -> t a -> t a succ1 :: (Lifted t, Num a, Enum a) => t a -> t a pred1 :: (Lifted t, Num a, Enum a) => t a -> t a toEnum1 :: (Lifted t, Num a, Enum a) => Int -> t a fromEnum1 :: (Lifted t, Num a, Enum a) => t a -> Int enumFrom1 :: (Lifted t, Num a, Enum a) => t a -> [t a] enumFromThen1 :: (Lifted t, Num a, Enum a) => t a -> t a -> [t a] enumFromTo1 :: (Lifted t, Num a, Enum a) => t a -> t a -> [t a] enumFromThenTo1 :: (Lifted t, Num a, Enum a) => t a -> t a -> t a -> [t a] minBound1 :: (Lifted t, Num a, Bounded a) => t a maxBound1 :: (Lifted t, Num a, Bounded a) => t a module Numeric.AD.Internal zipWithT :: (Foldable f, Traversable g) => (a -> b -> c) -> f a -> g b -> g c -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a newtype Id a Id :: a -> Id a probe :: a -> AD Id a unprobe :: AD Id a -> a probed :: f a -> f (AD Id a) unprobed :: f (AD Id a) -> f a instance Iso a (Id a) instance (Eq a) => Eq (Id a) instance (Ord a) => Ord (Id a) instance (Show a) => Show (Id a) instance (Enum a) => Enum (Id a) instance (Bounded a) => Bounded (Id a) instance (Num a) => Num (Id a) instance (Real a) => Real (Id a) instance (Fractional a) => Fractional (Id a) instance (Floating a) => Floating (Id a) instance (RealFrac a) => RealFrac (Id a) instance (RealFloat a) => RealFloat (Id a) instance (Monoid a) => Monoid (Id a) instance Primal Id instance Mode Id instance Lifted Id instance Monad Id instance Applicative Id instance Functor Id instance (Lifted f, Real a) => Real (AD f a) instance (Lifted f, RealFrac a) => RealFrac (AD f a) instance (Lifted f, RealFloat a) => RealFloat (AD f a) instance (Lifted f, Floating a) => Floating (AD f a) instance (Lifted f, Fractional a) => Fractional (AD f a) instance (Lifted f, Num a) => Num (AD f a) instance (Lifted f, Show a) => Show (AD f a) instance (Num a, Lifted f, Bounded a) => Bounded (AD f a) instance (Num a, Lifted f, Ord a) => Ord (AD f a) instance (Num a, Lifted f, Eq a) => Eq (AD f a) instance (Num a, Lifted f, Enum a) => Enum (AD f a) instance Iso (f a) (AD f a) instance (Lifted f) => Lifted (AD f) instance (Mode f) => Mode (AD f) instance (Primal f) => Primal (AD f) instance Iso a a -- | Unsafe and often partial combinators intended for internal usage. -- -- Handle with care. module Numeric.AD.Internal.Forward data Forward a Forward :: a -> a -> Forward a tangent :: AD Forward a -> a bundle :: a -> a -> AD Forward a unbundle :: AD Forward a -> (a, a) apply :: (Num a) => (AD Forward a -> b) -> a -> b bind :: (Traversable f, Num a) => (f (AD Forward a) -> b) -> f a -> f b bind2 :: (Traversable f, Num a) => (f (AD Forward a) -> b) -> f a -> (b, f b) transposeWith :: (Functor f, Foldable f, Traversable g) => (b -> f a -> c) -> f (g a) -> g b -> g c instance Lifted Forward instance Typeable1 Forward instance (Show a) => Show (Forward a) instance (Data a) => Data (Forward a) instance (Lifted Forward) => Jacobian Forward instance (Lifted Forward) => Mode Forward instance Primal Forward -- | Forward mode automatic differentiation module Numeric.AD.Forward grad :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a grad2 :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a) jacobian :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) jacobian2 :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) jacobianT :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> f (g a) -- | The diffUU function calculates the first derivative of a -- scalar-to-scalar function by Forward AD diffUU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diff2UU function calculates the result and first derivative -- of scalar-to-scalar function by Forward AD diff2UU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) -- | The diffUF function calculates the first derivative of -- scalar-to-nonscalar function by Forward AD diffUF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f a -- | The diff2UF function calculates the result and first derivative -- of a scalar-to-non-scalar function by Forward AD diff2UF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f (a, a) -- | The diff2 function calculates the first derivative of -- scalar-to-scalar function by Forward AD diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diff2 function calculates the result and first derivative -- of scalar-to-scalar function by Forward AD diff2 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a -- | Reverse-Mode Automatic Differentiation implementation details -- -- For reverse mode AD we use System.Mem.StableName.StableName -- to recover sharing information from the tape to avoid combinatorial -- explosion, and thus run asymptotically faster than it could without -- such sharing information, but the use of side-effects contained herein -- is benign. module Numeric.AD.Internal.Reverse -- | Reverse is a Mode using reverse-mode automatic -- differentiation that provides fast diffFU, diff2FU, -- grad, grad2 and a fast jacobian when you -- have a significantly smaller number of outputs than inputs. newtype Reverse a Reverse :: (Tape a (Reverse a)) -> Reverse a -- | A Tape records the information needed back propagate from the -- output to each input during Reverse Mode AD. data Tape a t Lift :: a -> Tape a t Var :: a -> !!Int -> Tape a t Binary :: a -> a -> a -> t -> t -> Tape a t Unary :: a -> a -> t -> Tape a t -- | This returns a list of contributions to the partials. The variable ids -- returned in the list are likely not unique! partials :: (Num a) => AD Reverse a -> [(Int, a)] -- | Return an Array of partials given bounds for the -- variable IDs. partialArray :: (Num a) => (Int, Int) -> AD Reverse a -> Array Int a -- | Return an IntMap of sparse partials partialMap :: (Num a) => AD Reverse a -> IntMap a derivative :: (Num a) => AD Reverse a -> a derivative2 :: (Num a) => AD Reverse a -> (a, a) -- | Used to mark variables for inspection during the reverse pass class Var t a | t -> a var :: (Var t a) => a -> Int -> t fromVar :: (Var t a) => t -> Int bind :: (Var t a, Traversable f) => f a -> (f t, (Int, Int)) unbind :: (Var t a, Functor f) => f t -> Array Int a -> f a unbindMap :: (Var t a, Functor f, Num a) => f t -> IntMap a -> f a instance Var (AD Reverse a) a instance Var (Reverse a) a instance Monad S instance Lifted Reverse instance (Show a) => Show (Reverse a) instance (Show a, Show t) => Show (Tape a t) instance (Lifted Reverse) => Jacobian Reverse instance Primal Reverse instance (Lifted Reverse) => Mode Reverse instance MuRef (Reverse a) -- | Mixed-Mode Automatic Differentiation. -- -- For reverse mode AD we use System.Mem.StableName.StableName -- to recover sharing information from the tape to avoid combinatorial -- explosion, and thus run asymptotically faster than it could without -- such sharing information, but the use of side-effects contained herein -- is benign. module Numeric.AD.Reverse -- | The grad function calculates the gradient of a -- non-scalar-to-scalar function with Reverse AD in a single pass. grad :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a -- | The grad2 function calculates the result and gradient of a -- non-scalar-to-scalar function with Reverse AD in a single pass. grad2 :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a) -- | The jacobian function calculates the jacobian of a -- non-scalar-to-non-scalar function with reverse AD lazily in m -- passes for m outputs. jacobian :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) -- | The jacobian2 function calculates both the result and the -- Jacobian of a nonscalar-to-nonscalar function, using m -- invocations of reverse AD, where m is the output -- dimensionality. Applying fmap snd to the result will recover -- the result of jacobian jacobian2 :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) diffUU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diff2UU function calculates the value and derivative, as a -- pair, of a scalar-to-scalar function. diff2UU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) diffFU :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a diff2FU :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a) diffUF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f a diff2UF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f (a, a) -- | The diff function is a synonym for diffUU. diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diff2 function is a synonym for diff2UU. diff2 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a module Numeric.AD.Internal.Tower -- | Tower is an AD Mode that calculates a tangent tower by -- forward AD, and provides fast diffsUU, diffsUF newtype Tower a Tower :: [a] -> Tower a getTower :: Tower a -> [a] zeroPad :: (Num a) => [a] -> [a] d :: (Num a) => [a] -> a d2 :: (Num a) => [a] -> (a, a) tangents :: Tower a -> Tower a bundle :: a -> Tower a -> Tower a apply :: (Num a) => (AD Tower a -> b) -> a -> b getADTower :: AD Tower a -> [a] instance Lifted Tower instance (Show a) => Show (Tower a) instance (Lifted Tower) => Jacobian Tower instance (Lifted Tower) => Mode Tower instance Primal Tower -- | Higher order derivatives via a "dual number tower". module Numeric.AD.Tower taylor :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -> [a] taylor0 :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -> [a] maclaurin :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] maclaurin0 :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffUU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a diff2UU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) diffsUU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffs0UU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffsUF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] diffs0UF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] diffs :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffs0 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a diff2 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a module Numeric.AD.Newton -- | The findZero function finds a zero of a scalar function using -- Newton's method; its output is a stream of increasingly accurate -- results. (Modulo the usual caveats.) -- -- Examples: -- --
--   take 10 $ findZero (\\x->x^2-4) 1  -- converge to 2.0
--   
-- --
--   module Data.Complex
--   take 10 $ findZero ((+1).(^2)) (1 :+ 1)  -- converge to (0 :+ 1)@
--   
findZero :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] -- | The inverseNewton function inverts a scalar function using -- Newton's method; its output is a stream of increasingly accurate -- results. (Modulo the usual caveats.) -- -- Example: -- --
--   take 10 $ inverseNewton sqrt 1 (sqrt 10)  -- converge to 10
--   
inverse :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -> [a] -- | The fixedPoint function find a fixedpoint of a scalar function -- using Newton's method; its output is a stream of increasingly accurate -- results. (Modulo the usual caveats.) fixedPoint :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] -- | The extremum function finds an extremum of a scalar function -- using Newton's method; produces a stream of increasingly accurate -- results. (Modulo the usual caveats.) extremum :: (Fractional a) => (forall t s. (Mode t, Mode s) => AD t (AD s a) -> AD t (AD s a)) -> a -> [a] -- | The gradientDescent function performs a multivariate -- optimization, based on the naive-gradient-descent in the file -- stalingrad/examples/flow-tests/pre-saddle-1a.vlad from the -- VLAD compiler Stalingrad sources. Its output is a stream of -- increasingly accurate results. (Modulo the usual caveats.) -- -- It uses reverse mode automatic differentiation to compute the -- gradient. gradientDescent :: (Traversable f, Fractional a, Ord a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> [f a] -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a -- | Mixed-Mode Automatic Differentiation. -- -- Each combinator exported from this module chooses an appropriate AD -- mode. module Numeric.AD -- | The grad function calculates the gradient of a -- non-scalar-to-scalar function with Reverse AD in a single pass. grad :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a -- | The grad2 function calculates the result and gradient of a -- non-scalar-to-scalar function with Reverse AD in a single pass. grad2 :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a) -- | Calculate the Jacobian of a non-scalar-to-non-scalar function, -- automatically choosing between forward and reverse mode AD based on -- the number of inputs and outputs jacobian :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) -- | Calculate the answer and Jacobian of a non-scalar-to-non-scalar -- function, automatically choosing between forward- and reverse- mode AD -- based on the relative, number of inputs and outputs. If you need to -- support functions where the output is only a Functor, consider -- using jacobianT from Numeric.AD.Forward or -- jacobian2 from Numeric.AD.Reverse directly. jacobian2 :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) -- | The diff2 function calculates the first derivative of -- scalar-to-scalar function by Forward AD diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diff2 function calculates the result and first derivative -- of scalar-to-scalar function by Forward AD diff2 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) diffs :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffs0 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] -- | The diffUU function calculates the first derivative of a -- scalar-to-scalar function by Forward AD diffUU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diffUF function calculates the first derivative of -- scalar-to-nonscalar function by Forward AD diffUF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f a -- | The diff2UU function calculates the result and first derivative -- of scalar-to-scalar function by Forward AD diff2UU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) -- | The diff2UF function calculates the result and first derivative -- of a scalar-to-non-scalar function by Forward AD diff2UF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f (a, a) diffFU :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a diff2FU :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a) diffsUU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffsUF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] diffs0UU :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffs0UF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] taylor :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -> [a] taylor0 :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -> [a] -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a -- | Allows the choice of AD Mode to be specified at the term level -- for benchmarking or more complicated usage patterns. module Numeric.AD.Directed diffUU :: (Num a) => Direction -> (forall s. (Mode s) => AD s a -> AD s a) -> a -> a diff2UU :: (Num a) => Direction -> (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) diff :: (Num a) => Direction -> (forall s. (Mode s) => AD s a -> AD s a) -> a -> a diff2 :: (Num a) => Direction -> (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) jacobian :: (Traversable f, Traversable g, Num a) => Direction -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) jacobian2 :: (Traversable f, Traversable g, Num a) => Direction -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) grad :: (Traversable f, Num a) => Direction -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a grad2 :: (Traversable f, Num a) => Direction -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a) data Direction Forward :: Direction Reverse :: Direction Tower :: Direction Mixed :: Direction class (Lifted t) => Mode t lift :: (Mode t, Num a) => a -> t a (<+>) :: (Mode t, Num a) => t a -> t a -> t a (*^) :: (Mode t, Num a) => a -> t a -> t a (^*) :: (Mode t, Num a) => t a -> a -> t a (^/) :: (Mode t, Fractional a) => t a -> a -> t a zero :: (Mode t, Num a) => t a -- | AD serves as a common wrapper for different Mode -- instances, exposing a traditional numerical tower. Universal -- quantification is used to limit the actions in user code to machinery -- that will return the same answers under all AD modes, allowing us to -- use modes interchangeably as both the type level "brand" and -- dictionary, providing a common API. newtype AD f a AD :: f a -> AD f a runAD :: AD f a -> f a instance Show Direction instance Eq Direction instance Ord Direction instance Read Direction instance Bounded Direction instance Enum Direction instance Ix Direction