{-# LANGUAGE CPP #-}
module Numeric.AD.Rank1.Forward
( Forward
, auto
, grad
, grad'
, gradWith
, gradWith'
, jacobian
, jacobian'
, jacobianWith
, jacobianWith'
, jacobianT
, jacobianWithT
, hessianProduct
, hessianProduct'
, diff
, diff'
, diffF
, diffF'
, du
, du'
, duF
, duF'
) where
#if __GLASGOW_HASKELL__ < 710
import Data.Traversable (Traversable)
import Control.Applicative
#endif
import Numeric.AD.Internal.Forward
import Numeric.AD.Internal.On
import Numeric.AD.Mode
du :: (Functor f, Num a) => (f (Forward a) -> Forward a) -> f (a, a) -> a
du f = tangent . f . fmap (uncurry bundle)
{-# INLINE du #-}
du' :: (Functor f, Num a) => (f (Forward a) -> Forward a) -> f (a, a) -> (a, a)
du' f = unbundle . f . fmap (uncurry bundle)
{-# INLINE du' #-}
duF :: (Functor f, Functor g, Num a) => (f (Forward a) -> g (Forward a)) -> f (a, a) -> g a
duF f = fmap tangent . f . fmap (uncurry bundle)
{-# INLINE duF #-}
duF' :: (Functor f, Functor g, Num a) => (f (Forward a) -> g (Forward a)) -> f (a, a) -> g (a, a)
duF' f = fmap unbundle . f . fmap (uncurry bundle)
{-# INLINE duF' #-}
diff :: Num a => (Forward a -> Forward a) -> a -> a
diff f a = tangent $ apply f a
{-# INLINE diff #-}
diff' :: Num a => (Forward a -> Forward a) -> a -> (a, a)
diff' f a = unbundle $ apply f a
{-# INLINE diff' #-}
diffF :: (Functor f, Num a) => (Forward a -> f (Forward a)) -> a -> f a
diffF f a = tangent <$> apply f a
{-# INLINE diffF #-}
diffF' :: (Functor f, Num a) => (Forward a -> f (Forward a)) -> a -> f (a, a)
diffF' f a = unbundle <$> apply f a
{-# INLINE diffF' #-}
jacobianT :: (Traversable f, Functor g, Num a) => (f (Forward a) -> g (Forward a)) -> f a -> f (g a)
jacobianT f = bind (fmap tangent . f)
{-# INLINE jacobianT #-}
jacobianWithT :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (f (Forward a) -> g (Forward a)) -> f a -> f (g b)
jacobianWithT g f = bindWith g' f where
g' a ga = g a . tangent <$> ga
{-# INLINE jacobianWithT #-}
#ifdef HLINT
{-# ANN jacobianWithT "HLint: ignore Eta reduce" #-}
#endif
jacobian :: (Traversable f, Traversable g, Num a) => (f (Forward a) -> g (Forward a)) -> f a -> g (f a)
jacobian f as = transposeWith (const id) t p where
(p, t) = bind' (fmap tangent . f) as
{-# INLINE jacobian #-}
jacobianWith :: (Traversable f, Traversable g, Num a) => (a -> a -> b) -> (f (Forward a) -> g (Forward a)) -> f a -> g (f b)
jacobianWith g f as = transposeWith (const id) t p where
(p, t) = bindWith' g' f as
g' a ga = g a . tangent <$> ga
{-# INLINE jacobianWith #-}
jacobian' :: (Traversable f, Traversable g, Num a) => (f (Forward a) -> g (Forward a)) -> f a -> g (a, f a)
jacobian' f as = transposeWith row t p where
(p, t) = bind' f as
row x as' = (primal x, tangent <$> as')
{-# INLINE jacobian' #-}
jacobianWith' :: (Traversable f, Traversable g, Num a) => (a -> a -> b) -> (f (Forward a) -> g (Forward a)) -> f a -> g (a, f b)
jacobianWith' g f as = transposeWith row t p where
(p, t) = bindWith' g' f as
row x as' = (primal x, as')
g' a ga = g a . tangent <$> ga
{-# INLINE jacobianWith' #-}
grad :: (Traversable f, Num a) => (f (Forward a) -> Forward a) -> f a -> f a
grad f = bind (tangent . f)
{-# INLINE grad #-}
grad' :: (Traversable f, Num a) => (f (Forward a) -> Forward a) -> f a -> (a, f a)
grad' f as = (primal b, tangent <$> bs) where
(b, bs) = bind' f as
{-# INLINE grad' #-}
gradWith :: (Traversable f, Num a) => (a -> a -> b) -> (f (Forward a) -> Forward a) -> f a -> f b
gradWith g f = bindWith g (tangent . f)
{-# INLINE gradWith #-}
gradWith' :: (Traversable f, Num a) => (a -> a -> b) -> (f (Forward a) -> Forward a) -> f a -> (a, f b)
gradWith' g f as = (primal $ f (Lift <$> as), bindWith g (tangent . f) as)
{-# INLINE gradWith' #-}
hessianProduct :: (Traversable f, Num a) => (f (On (Forward (Forward a))) -> On (Forward (Forward a))) -> f (a, a) -> f a
hessianProduct f = duF $ grad $ off . f . fmap On
{-# INLINE hessianProduct #-}
hessianProduct' :: (Traversable f, Num a) => (f (On (Forward (Forward a))) -> On (Forward (Forward a))) -> f (a, a) -> f (a, a)
hessianProduct' f = duF' $ grad $ off . f . fmap On
{-# INLINE hessianProduct' #-}