-- 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.21 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
zipWithDefaultT :: (Foldable f, Traversable g) => a -> (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
data Pair a b
Pair :: a -> b -> Pair a b
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 (Eq a, Eq b) => Eq (Pair a b)
instance (Ord a, Ord b) => Ord (Pair a b)
instance (Show a, Show b) => Show (Pair a b)
instance (Read a, Read b) => Read (Pair a b)
instance Functor (Pair a)
instance Foldable (Pair a)
instance Traversable (Pair a)
instance Iso a a
-- | Defines the composition of two AD modes as an AD mode in its own right
module Numeric.AD.Internal.Composition
newtype (:.) f g a
O :: f (AD g a) -> :. f g a
runO :: :. f g a -> f (AD g a)
type On f g = g :. f
compose :: AD f (AD g a) -> AD (f :. g) a
decompose :: AD (f :. g) a -> AD f (AD g a)
instance (Mode f, Mode g) => Lifted (f :. g)
instance (Mode f, Mode g) => Mode (f :. g)
instance (Primal f, Mode g, Primal g) => Primal (f :. g)
-- | 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
bind' :: (Traversable f, Num a) => (f (AD Forward a) -> b) -> f a -> (b, f b)
bindWith :: (Traversable f, Num a) => (a -> b -> c) -> (f (AD Forward a) -> b) -> f a -> f c
bindWith' :: (Traversable f, Num a) => (a -> b -> c) -> (f (AD Forward a) -> b) -> f a -> (b, f c)
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
grad' :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f a)
gradWith :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f b
gradWith' :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f b)
jacobian :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a)
jacobian' :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a)
jacobianWith :: (Traversable f, Traversable g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f b)
jacobianWith' :: (Traversable f, Traversable g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f b)
-- | A fast, simple transposed Jacobian computed with forward-mode AD.
jacobianT :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> f (g a)
-- | A fast, simple transposed Jacobian computed with forward-mode AD.
jacobianWithT :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> f (g b)
-- | Compute the product of a vector with the Hessian using
-- forward-on-forward-mode AD.
hessianProduct :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> f a
-- | Compute the gradient and hessian product using forward-on-forward-mode
-- AD.
hessianProduct' :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> f (a, a)
-- | The diff function calculates the first derivative of a
-- scalar-to-scalar function by forward-mode AD
--
-- -- diff sin == cos --diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The d'UU function calculates the result and first derivative -- of scalar-to-scalar function by Forward AD -- --
-- d' sin == sin &&& cos -- d' f = f &&& d f --diff' :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) -- | The diffF function calculates the first derivative of -- scalar-to-nonscalar function by Forward AD diffF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f a -- | The diffF' function calculates the result and first derivative -- of a scalar-to-non-scalar function by Forward AD diffF' :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f (a, a) du :: (Functor f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> a du' :: (Functor f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> (a, a) duF :: (Functor f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f (a, a) -> g a duF' :: (Functor f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f (a, a) -> g (a, a) -- | The dUM function calculates the first derivative of -- scalar-to-scalar monadic function by Forward AD diffM :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m a -- | The d'UM function calculates the result and first derivative -- of a scalar-to-scalar monadic function by Forward AD diffM' :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m (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 derivative' :: (Num a) => AD Reverse a -> (a, a) -- | Used to mark variables for inspection during the reverse pass class (Primal v) => Var v var :: (Var v) => a -> Int -> v a varId :: (Var v) => v a -> Int bind :: (Traversable f, Var v) => f a -> (f (v a), (Int, Int)) unbind :: (Functor f, Var v) => f (v a) -> Array Int a -> f a unbindMap :: (Functor f, Var v, Num a) => f (v a) -> IntMap a -> f a unbindWith :: (Functor f, Var v, Num a) => (a -> b -> c) -> f (v a) -> Array Int b -> f c unbindMapWithDefault :: (Functor f, Var v, Num a) => b -> (a -> b -> c) -> f (v a) -> IntMap b -> f c instance Var (AD Reverse) instance Var Reverse 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 grad' function calculates the result and 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 -> (a, f a) -- | grad g f function calculates the gradient of a -- non-scalar-to-scalar function f with reverse-mode AD in a -- single pass. The gradient is combined element-wise with the argument -- using the function g. -- --
-- grad == gradWith (\_ dx -> dx) -- id == gradWith const --gradWith :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f b -- | grad' g f calculates the result and gradient of a -- non-scalar-to-scalar function f with Reverse AD in a -- single pass the gradient is combined element-wise with the argument -- using the function g. -- --
-- grad' == gradWith' (\_ dx -> dx) --gradWith' :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f b) -- | An alias for gradF jacobian :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) -- | An alias for gradF' jacobian' :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) -- | An alias for gradWithF. jacobianWith :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f b) -- | An alias for gradWithF' jacobianWith' :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f b) diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The d' function calculates the value and derivative, as a -- pair, of a scalar-to-scalar function. diff' :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) diffF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f a diffF' :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f (a, a) diffM :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m a diffM' :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m (a, a) gradM :: (Traversable f, Monad m, Num a) => (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (f a) gradM' :: (Traversable f, Monad m, Num a) => (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (a, f a) gradWithM :: (Traversable f, Monad m, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (f b) gradWithM' :: (Traversable f, Monad m, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (a, f b) -- | The gradF function calculates the jacobian of a -- non-scalar-to-non-scalar function with reverse AD lazily in m -- passes for m outputs. gradF :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) -- | The gradF' 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 -- gradF gradF' :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) -- | 'gradWithF g f' calculates the Jacobian of a non-scalar-to-non-scalar -- function f with reverse AD lazily in m passes for -- m outputs. -- -- Instead of returning the Jacobian matrix, the elements of the matrix -- are combined with the input using the g. -- --
-- gradF == gradWithF (\_ dx -> dx) -- gradWithF const == (\f x -> const x <$> f x) --gradWithF :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f b) -- | gradWithF g f' calculates both the result and the Jacobian of a -- nonscalar-to-nonscalar function f, using m -- invocations of reverse AD, where m is the output -- dimensionality. Applying fmap snd to the result will recover -- the result of gradWithF -- -- Instead of returning the Jacobian matrix, the elements of the matrix -- are combined with the input using the g. -- --
-- jacobian' == gradWithF' (\_ dx -> dx) --gradWithF' :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f b) -- | 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 d' :: (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] diff :: (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, 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] diffsF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] diffs0F :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] diffsM :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m [a] diffs0M :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m [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] findZeroM :: (Monad m, Fractional a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> MList m 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) -- converges to 10 --inverse :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -> [a] inverseM :: (Monad m, Fractional a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> a -> MList m 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.) -- --
-- take 10 $ fixedPoint cos 1 -- converges to 0.7390851332151607 --fixedPoint :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] fixedPointM :: (Monad m, Fractional a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> MList m 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.) -- --
-- take 10 $ extremum cos 1 -- convert to 0 --extremum :: (Fractional a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] extremumM :: (Monad m, Fractional a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> MList m 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] gradientDescentM :: (Traversable f, Monad m, Fractional a, Ord a) => (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> MList m (f a) gradientAscent :: (Traversable f, Fractional a, Ord a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> [f a] gradientAscentM :: (Traversable f, Monad m, Fractional a, Ord a) => (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> MList m (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 grad' function calculates the result and 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 -> (a, f a) -- | grad g f function calculates the gradient of a -- non-scalar-to-scalar function f with reverse-mode AD in a -- single pass. The gradient is combined element-wise with the argument -- using the function g. -- --
-- grad == gradWith (\_ dx -> dx) -- id == gradWith const --gradWith :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f b -- | grad' g f calculates the result and gradient of a -- non-scalar-to-scalar function f with Reverse AD in a -- single pass the gradient is combined element-wise with the argument -- using the function g. -- --
-- grad' == gradWith' (\_ dx -> dx) --gradWith' :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f b) -- | 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. -- -- If you need to support functions where the output is only a -- Functor or Monad, consider -- Numeric.AD.Reverse.jacobian or gradM from -- Numeric.AD.Reverse. 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 both 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 or Monad, consider -- Numeric.AD.Reverse.jacobian' or gradM' from -- Numeric.AD.Reverse. jacobian' :: (Traversable f, Traversable g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) -- | jacobianWith g f calculates 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. -- -- The resulting Jacobian matrix is then recombined element-wise with the -- input using g. -- -- If you need to support functions where the output is only a -- Functor or Monad, consider -- Numeric.AD.Reverse.jacobianWith or gradWithM from -- Numeric.AD.Reverse. jacobianWith :: (Traversable f, Traversable g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f b) -- | jacobianWith' g f calculates the answer and 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. -- -- The resulting Jacobian matrix is then recombined element-wise with the -- input using g. -- -- If you need to support functions where the output is only a -- Functor or Monad, consider -- Numeric.AD.Reverse.jacobianWith' or gradWithM' from -- Numeric.AD.Reverse. jacobianWith' :: (Traversable f, Traversable g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f b) -- | The gradF function calculates the jacobian of a -- non-scalar-to-non-scalar function with reverse AD lazily in m -- passes for m outputs. gradF :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f a) -- | The gradF' 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 -- gradF gradF' :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f a) -- | 'gradWithF g f' calculates the Jacobian of a non-scalar-to-non-scalar -- function f with reverse AD lazily in m passes for -- m outputs. -- -- Instead of returning the Jacobian matrix, the elements of the matrix -- are combined with the input using the g. -- --
-- gradF == gradWithF (\_ dx -> dx) -- gradWithF const == (\f x -> const x <$> f x) --gradWithF :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (f b) -- | gradWithF g f' calculates both the result and the Jacobian of a -- nonscalar-to-nonscalar function f, using m -- invocations of reverse AD, where m is the output -- dimensionality. Applying fmap snd to the result will recover -- the result of gradWithF -- -- Instead of returning the Jacobian matrix, the elements of the matrix -- are combined with the input using the g. -- --
-- jacobian' == gradWithF' (\_ dx -> dx) --gradWithF' :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> g (a, f b) -- | A fast, simple transposed Jacobian computed with forward-mode AD. jacobianT :: (Traversable f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> f (g a) -- | A fast, simple transposed Jacobian computed with forward-mode AD. jacobianWithT :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f a -> f (g b) -- | hessianProduct f wv computes the product of the -- hessian H of a non-scalar-to-scalar function f at -- w = fst $ wv with a vector v = snd $ -- wv using "Pearlmutter's method" from -- http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.29.6143, -- which states: -- --
-- H v = (d/dr) grad_w (w + r v) | r = 0 ---- -- Or in other words, we take the directional derivative of the gradient. hessianProduct :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> f a -- | hessianProduct' f wv computes both the gradient of a -- non-scalar-to-scalar f at w = fst $ wv -- and the product of the hessian H at w with a vector -- v = snd $ wv using "Pearlmutter's method". The outputs -- are returned wrapped in the same functor. -- --
-- H v = (d/dr) grad_w (w + r v) | r = 0 ---- -- Or in other words, we take the directional derivative of the gradient. hessianProduct' :: (Traversable f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> f (a, a) -- | The diff function calculates the first derivative of a -- scalar-to-scalar function by forward-mode AD -- --
-- diff sin == cos --diff :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> a -- | The diffF function calculates the first derivative of -- scalar-to-nonscalar function by Forward AD diffF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f a -- | The d'UU function calculates the result and first derivative -- of scalar-to-scalar function by Forward AD -- --
-- d' sin == sin &&& cos -- d' f = f &&& d f --diff' :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, a) -- | The diffF' function calculates the result and first derivative -- of a scalar-to-non-scalar function by Forward AD diffF' :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f (a, a) diffs :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffsF :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] diffs0 :: (Num a) => (forall s. (Mode s) => AD s a -> AD s a) -> a -> [a] diffs0F :: (Functor f, Num a) => (forall s. (Mode s) => AD s a -> f (AD s a)) -> a -> f [a] du :: (Functor f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> a du' :: (Functor f, Num a) => (forall s. (Mode s) => f (AD s a) -> AD s a) -> f (a, a) -> (a, a) duF :: (Functor f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f (a, a) -> g a duF' :: (Functor f, Functor g, Num a) => (forall s. (Mode s) => f (AD s a) -> g (AD s a)) -> f (a, a) -> g (a, 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] 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] -- | The dUM function calculates the first derivative of -- scalar-to-scalar monadic function by Forward AD diffM :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m a -- | The d'UM function calculates the result and first derivative -- of a scalar-to-scalar monadic function by Forward AD diffM' :: (Monad m, Num a) => (forall s. (Mode s) => AD s a -> m (AD s a)) -> a -> m (a, a) gradM :: (Traversable f, Monad m, Num a) => (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (f a) gradM' :: (Traversable f, Monad m, Num a) => (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (a, f a) gradWithM :: (Traversable f, Monad m, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (f b) gradWithM' :: (Traversable f, Monad m, Num a) => (a -> a -> b) -> (forall s. (Mode s) => f (AD s a) -> m (AD s a)) -> f a -> m (a, f b) -- | 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 grad :: (Traversable f, Num a) => Direction -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> f a grad' :: (Traversable f, Num a) => Direction -> (forall s. (Mode s) => f (AD s a) -> AD s a) -> f a -> (a, f 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) jacobian' :: (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) diff :: (Num a) => Direction -> (forall s. (Mode s) => AD s a -> AD s a) -> a -> a diff' :: (Num a) => Direction -> (forall s. (Mode s) => AD s a -> AD s a) -> a -> (a, 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