{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PatternGuards #-}
module Numeric.AD
( AD
, Mode(auto)
, Scalar
, grad
, grad'
, gradWith
, gradWith'
, grads
, Grad , vgrad, vgrad'
, Grads, vgrads
, jacobian
, jacobian'
, jacobianWith
, jacobianWith'
, jacobians
, jacobianT
, jacobianWithT
, hessian
, hessian'
, hessianF
, hessianF'
, hessianProduct
, hessianProduct'
, diff
, diffF
, diff'
, diffF'
, diffs
, diffsF
, diffs0
, diffs0F
, du
, du'
, duF
, duF'
, dus
, dus0
, dusF
, dus0F
, taylor
, taylor0
, maclaurin
, maclaurin0
, gradientDescent
, gradientAscent
, conjugateGradientDescent
, conjugateGradientAscent
, stochasticGradientDescent
, Jet(..)
, headJet
, tailJet
, jet
) where
import Data.Functor.Compose
import Data.Reflection (Reifies)
import Data.Typeable
import Numeric.AD.Internal.Forward (Forward)
import Numeric.AD.Internal.Kahn (Grad, vgrad, vgrad')
import Numeric.AD.Internal.On
import Numeric.AD.Internal.Reverse (Reverse, Tape)
import Numeric.AD.Internal.Sparse (Sparse, Grads, vgrads)
import Numeric.AD.Internal.Type
import Numeric.AD.Jet
import Numeric.AD.Mode
import qualified Numeric.AD.Rank1.Forward as Forward1
import Numeric.AD.Mode.Forward
( diff, diff', diffF, diffF'
, du, du', duF, duF'
, jacobianT, jacobianWithT
)
import Numeric.AD.Mode.Tower
( diffsF, diffs0F, diffs, diffs0
, taylor, taylor0, maclaurin, maclaurin0
, dus, dus0, dusF, dus0F
)
import qualified Numeric.AD.Mode.Reverse as Reverse
import Numeric.AD.Mode.Reverse
( grad, grad', gradWith, gradWith'
, jacobian, jacobian', jacobianWith, jacobianWith'
)
import qualified Numeric.AD.Rank1.Sparse as Sparse1
import Numeric.AD.Mode.Sparse
( grads, jacobians, hessian', hessianF'
)
import Numeric.AD.Newton
hessianProduct
:: (Traversable f, Num a)
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s (Forward a)))
-> On (Reverse s (Forward a))
)
-> f (a, a)
-> f a
hessianProduct :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a)))
-> f (a, a) -> f a
hessianProduct forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a))
f = (f (Forward a) -> f (Forward a)) -> f (a, a) -> f a
forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Forward a) -> g (Forward a)) -> f (a, a) -> g a
Forward1.duF ((forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s (Forward a)) -> Reverse s (Forward a))
-> f (Forward a) -> f (Forward a)
forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s a) -> Reverse s a)
-> f a -> f a
grad (On (Reverse s (Forward a)) -> Reverse s (Forward a)
forall t. On t -> t
off (On (Reverse s (Forward a)) -> Reverse s (Forward a))
-> (f (Reverse s (Forward a)) -> On (Reverse s (Forward a)))
-> f (Reverse s (Forward a))
-> Reverse s (Forward a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a))
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a))
f (f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a)))
-> (f (Reverse s (Forward a)) -> f (On (Reverse s (Forward a))))
-> f (Reverse s (Forward a))
-> On (Reverse s (Forward a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s (Forward a) -> On (Reverse s (Forward a)))
-> f (Reverse s (Forward a)) -> f (On (Reverse s (Forward a)))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s (Forward a) -> On (Reverse s (Forward a))
forall t. t -> On t
On))
hessianProduct'
:: (Traversable f, Num a)
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s (Forward a)))
-> On (Reverse s (Forward a))
)
-> f (a, a)
-> f (a, a)
hessianProduct' :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a)))
-> f (a, a) -> f (a, a)
hessianProduct' forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a))
f = (f (Forward a) -> f (Forward a)) -> f (a, a) -> f (a, a)
forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Forward a) -> g (Forward a)) -> f (a, a) -> g (a, a)
Forward1.duF' ((forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s (Forward a)) -> Reverse s (Forward a))
-> f (Forward a) -> f (Forward a)
forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s a) -> Reverse s a)
-> f a -> f a
grad (On (Reverse s (Forward a)) -> Reverse s (Forward a)
forall t. On t -> t
off (On (Reverse s (Forward a)) -> Reverse s (Forward a))
-> (f (Reverse s (Forward a)) -> On (Reverse s (Forward a)))
-> f (Reverse s (Forward a))
-> Reverse s (Forward a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a))
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a))
f (f (On (Reverse s (Forward a))) -> On (Reverse s (Forward a)))
-> (f (Reverse s (Forward a)) -> f (On (Reverse s (Forward a))))
-> f (Reverse s (Forward a))
-> On (Reverse s (Forward a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s (Forward a) -> On (Reverse s (Forward a)))
-> f (Reverse s (Forward a)) -> f (On (Reverse s (Forward a)))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s (Forward a) -> On (Reverse s (Forward a))
forall t. t -> On t
On))
hessian
:: (Traversable f, Num a)
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s (Sparse a)))
-> On (Reverse s (Sparse a))
)
-> f a
-> f (f a)
hessian :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Sparse a))) -> On (Reverse s (Sparse a)))
-> f a -> f (f a)
hessian forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Sparse a))) -> On (Reverse s (Sparse a))
f = (f (Sparse a) -> f (Sparse a)) -> f a -> f (f a)
forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(f (Sparse a) -> g (Sparse a)) -> f a -> g (f a)
Sparse1.jacobian ((forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s (Sparse a)) -> Reverse s (Sparse a))
-> f (Sparse a) -> f (Sparse a)
forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s a) -> Reverse s a)
-> f a -> f a
grad (On (Reverse s (Sparse a)) -> Reverse s (Sparse a)
forall t. On t -> t
off (On (Reverse s (Sparse a)) -> Reverse s (Sparse a))
-> (f (Reverse s (Sparse a)) -> On (Reverse s (Sparse a)))
-> f (Reverse s (Sparse a))
-> Reverse s (Sparse a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s (Sparse a))) -> On (Reverse s (Sparse a))
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Sparse a))) -> On (Reverse s (Sparse a))
f (f (On (Reverse s (Sparse a))) -> On (Reverse s (Sparse a)))
-> (f (Reverse s (Sparse a)) -> f (On (Reverse s (Sparse a))))
-> f (Reverse s (Sparse a))
-> On (Reverse s (Sparse a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s (Sparse a) -> On (Reverse s (Sparse a)))
-> f (Reverse s (Sparse a)) -> f (On (Reverse s (Sparse a)))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s (Sparse a) -> On (Reverse s (Sparse a))
forall t. t -> On t
On))
hessianF
:: (Traversable f, Functor g, Num a)
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s (Sparse a)))
-> g (On (Reverse s (Sparse a)))
)
-> f a
-> g (f (f a))
hessianF :: forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Sparse a))) -> g (On (Reverse s (Sparse a))))
-> f a -> g (f (f a))
hessianF forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Sparse a))) -> g (On (Reverse s (Sparse a)))
f f a
as = Compose g f (f a) -> g (f (f a))
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose (Compose g f (f a) -> g (f (f a)))
-> Compose g f (f a) -> g (f (f a))
forall a b. (a -> b) -> a -> b
$ (f (Sparse a) -> Compose g f (Sparse a))
-> f a -> Compose g f (f a)
forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(f (Sparse a) -> g (Sparse a)) -> f a -> g (f a)
Sparse1.jacobian (g (f (Sparse a)) -> Compose g f (Sparse a)
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (g (f (Sparse a)) -> Compose g f (Sparse a))
-> (f (Sparse a) -> g (f (Sparse a)))
-> f (Sparse a)
-> Compose g f (Sparse a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s (Sparse a)) -> g (Reverse s (Sparse a)))
-> f (Sparse a) -> g (f (Sparse a))
forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s a) -> g (Reverse s a))
-> f a -> g (f a)
Reverse.jacobian ((On (Reverse s (Sparse a)) -> Reverse s (Sparse a))
-> g (On (Reverse s (Sparse a))) -> g (Reverse s (Sparse a))
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap On (Reverse s (Sparse a)) -> Reverse s (Sparse a)
forall t. On t -> t
off (g (On (Reverse s (Sparse a))) -> g (Reverse s (Sparse a)))
-> (f (Reverse s (Sparse a)) -> g (On (Reverse s (Sparse a))))
-> f (Reverse s (Sparse a))
-> g (Reverse s (Sparse a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s (Sparse a))) -> g (On (Reverse s (Sparse a)))
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s (Sparse a))) -> g (On (Reverse s (Sparse a)))
f (f (On (Reverse s (Sparse a))) -> g (On (Reverse s (Sparse a))))
-> (f (Reverse s (Sparse a)) -> f (On (Reverse s (Sparse a))))
-> f (Reverse s (Sparse a))
-> g (On (Reverse s (Sparse a)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s (Sparse a) -> On (Reverse s (Sparse a)))
-> f (Reverse s (Sparse a)) -> f (On (Reverse s (Sparse a)))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s (Sparse a) -> On (Reverse s (Sparse a))
forall t. t -> On t
On)) f a
as