{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PatternGuards #-}
module Numeric.AD.Double
( 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
, conjugateGradientDescent
, conjugateGradientAscent
, Jet(..)
, headJet
, tailJet
, jet
) where
import Data.Functor.Compose
import Data.Reflection (Reifies)
import Data.Typeable
import Numeric.AD.Internal.Forward.Double (ForwardDouble)
import Numeric.AD.Internal.Kahn.Double (Grad, vgrad, vgrad')
import Numeric.AD.Internal.On
import Numeric.AD.Internal.Reverse (Reverse, Tape)
import Numeric.AD.Internal.Sparse.Double (SparseDouble, Grads, vgrads)
import Numeric.AD.Internal.Type
import Numeric.AD.Jet
import Numeric.AD.Mode
import qualified Numeric.AD.Rank1.Forward.Double as ForwardDouble1
import Numeric.AD.Mode.Forward.Double
( diff, diff', diffF, diffF'
, du, du', duF, duF'
, jacobianT, jacobianWithT
)
import Numeric.AD.Mode.Tower.Double
( 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.Double
( grad, grad', gradWith, gradWith'
, jacobian, jacobian', jacobianWith, jacobianWith'
)
import qualified Numeric.AD.Rank1.Sparse.Double as SparseDouble1
import Numeric.AD.Mode.Sparse.Double
( grads, jacobians, hessian', hessianF'
)
import Numeric.AD.Newton.Double
hessianProduct
:: Traversable f
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s ForwardDouble))
-> On (Reverse s ForwardDouble)
)
-> f (Double, Double)
-> f Double
hessianProduct :: forall (f :: * -> *).
Traversable f =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble))
-> f (Double, Double) -> f Double
hessianProduct forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble)
f = (f ForwardDouble -> f ForwardDouble)
-> f (Double, Double) -> f Double
forall (f :: * -> *) (g :: * -> *).
(Functor f, Functor g) =>
(f ForwardDouble -> g ForwardDouble)
-> f (Double, Double) -> g Double
ForwardDouble1.duF ((forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s ForwardDouble) -> Reverse s ForwardDouble)
-> f ForwardDouble -> f ForwardDouble
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
Reverse.grad (On (Reverse s ForwardDouble) -> Reverse s ForwardDouble
forall t. On t -> t
off (On (Reverse s ForwardDouble) -> Reverse s ForwardDouble)
-> (f (Reverse s ForwardDouble) -> On (Reverse s ForwardDouble))
-> f (Reverse s ForwardDouble)
-> Reverse s ForwardDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble)
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble)
f (f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble))
-> (f (Reverse s ForwardDouble)
-> f (On (Reverse s ForwardDouble)))
-> f (Reverse s ForwardDouble)
-> On (Reverse s ForwardDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s ForwardDouble -> On (Reverse s ForwardDouble))
-> f (Reverse s ForwardDouble) -> f (On (Reverse s ForwardDouble))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s ForwardDouble -> On (Reverse s ForwardDouble)
forall t. t -> On t
On))
hessianProduct'
:: Traversable f
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s ForwardDouble))
-> On (Reverse s ForwardDouble)
)
-> f (Double, Double)
-> f (Double, Double)
hessianProduct' :: forall (f :: * -> *).
Traversable f =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble))
-> f (Double, Double) -> f (Double, Double)
hessianProduct' forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble)
f = (f ForwardDouble -> f ForwardDouble)
-> f (Double, Double) -> f (Double, Double)
forall (f :: * -> *) (g :: * -> *).
(Functor f, Functor g) =>
(f ForwardDouble -> g ForwardDouble)
-> f (Double, Double) -> g (Double, Double)
ForwardDouble1.duF' ((forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s ForwardDouble) -> Reverse s ForwardDouble)
-> f ForwardDouble -> f ForwardDouble
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
Reverse.grad (On (Reverse s ForwardDouble) -> Reverse s ForwardDouble
forall t. On t -> t
off (On (Reverse s ForwardDouble) -> Reverse s ForwardDouble)
-> (f (Reverse s ForwardDouble) -> On (Reverse s ForwardDouble))
-> f (Reverse s ForwardDouble)
-> Reverse s ForwardDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble)
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble)
f (f (On (Reverse s ForwardDouble)) -> On (Reverse s ForwardDouble))
-> (f (Reverse s ForwardDouble)
-> f (On (Reverse s ForwardDouble)))
-> f (Reverse s ForwardDouble)
-> On (Reverse s ForwardDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s ForwardDouble -> On (Reverse s ForwardDouble))
-> f (Reverse s ForwardDouble) -> f (On (Reverse s ForwardDouble))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s ForwardDouble -> On (Reverse s ForwardDouble)
forall t. t -> On t
On))
hessian
:: Traversable f
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s SparseDouble))
-> On (Reverse s SparseDouble)
)
-> f Double
-> f (f Double)
hessian :: forall (f :: * -> *).
Traversable f =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s SparseDouble)) -> On (Reverse s SparseDouble))
-> f Double -> f (f Double)
hessian forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s SparseDouble)) -> On (Reverse s SparseDouble)
f = (f SparseDouble -> f SparseDouble) -> f Double -> f (f Double)
forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f Double)
SparseDouble1.jacobian ((forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s SparseDouble) -> Reverse s SparseDouble)
-> f SparseDouble -> f SparseDouble
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
Reverse.grad (On (Reverse s SparseDouble) -> Reverse s SparseDouble
forall t. On t -> t
off (On (Reverse s SparseDouble) -> Reverse s SparseDouble)
-> (f (Reverse s SparseDouble) -> On (Reverse s SparseDouble))
-> f (Reverse s SparseDouble)
-> Reverse s SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s SparseDouble)) -> On (Reverse s SparseDouble)
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s SparseDouble)) -> On (Reverse s SparseDouble)
f (f (On (Reverse s SparseDouble)) -> On (Reverse s SparseDouble))
-> (f (Reverse s SparseDouble) -> f (On (Reverse s SparseDouble)))
-> f (Reverse s SparseDouble)
-> On (Reverse s SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s SparseDouble -> On (Reverse s SparseDouble))
-> f (Reverse s SparseDouble) -> f (On (Reverse s SparseDouble))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s SparseDouble -> On (Reverse s SparseDouble)
forall t. t -> On t
On))
hessianF
:: (Traversable f, Functor g)
=> ( forall s. (Reifies s Tape, Typeable s)
=> f (On (Reverse s SparseDouble))
-> g (On (Reverse s SparseDouble))
)
-> f Double
-> g (f (f Double))
hessianF :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s SparseDouble)) -> g (On (Reverse s SparseDouble)))
-> f Double -> g (f (f Double))
hessianF forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s SparseDouble)) -> g (On (Reverse s SparseDouble))
f f Double
as = Compose g f (f Double) -> g (f (f Double))
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose (Compose g f (f Double) -> g (f (f Double)))
-> Compose g f (f Double) -> g (f (f Double))
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> Compose g f SparseDouble)
-> f Double -> Compose g f (f Double)
forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f Double)
SparseDouble1.jacobian (g (f SparseDouble) -> Compose g f SparseDouble
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (g (f SparseDouble) -> Compose g f SparseDouble)
-> (f SparseDouble -> g (f SparseDouble))
-> f SparseDouble
-> Compose g f SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall s.
(Reifies s Tape, Typeable s) =>
f (Reverse s SparseDouble) -> g (Reverse s SparseDouble))
-> f SparseDouble -> g (f SparseDouble)
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 SparseDouble) -> Reverse s SparseDouble)
-> g (On (Reverse s SparseDouble)) -> g (Reverse s SparseDouble)
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 SparseDouble) -> Reverse s SparseDouble
forall t. On t -> t
off (g (On (Reverse s SparseDouble)) -> g (Reverse s SparseDouble))
-> (f (Reverse s SparseDouble) -> g (On (Reverse s SparseDouble)))
-> f (Reverse s SparseDouble)
-> g (Reverse s SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (On (Reverse s SparseDouble)) -> g (On (Reverse s SparseDouble))
forall s.
(Reifies s Tape, Typeable s) =>
f (On (Reverse s SparseDouble)) -> g (On (Reverse s SparseDouble))
f (f (On (Reverse s SparseDouble))
-> g (On (Reverse s SparseDouble)))
-> (f (Reverse s SparseDouble) -> f (On (Reverse s SparseDouble)))
-> f (Reverse s SparseDouble)
-> g (On (Reverse s SparseDouble))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reverse s SparseDouble -> On (Reverse s SparseDouble))
-> f (Reverse s SparseDouble) -> f (On (Reverse s SparseDouble))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Reverse s SparseDouble -> On (Reverse s SparseDouble)
forall t. t -> On t
On)) f Double
as