module Numeric.AD.Rank1.Sparse
( Sparse
, auto
, grad
, grad'
, gradWith
, gradWith'
, Grad
, vgrad
, grads
, Grads
, vgrads
, jacobian
, jacobian'
, jacobianWith
, jacobianWith'
, jacobians
, hessian
, hessian'
, hessianF
, hessianF'
) where
import Control.Comonad
import Data.Traversable
import Control.Comonad.Cofree
import Numeric.AD.Jet
import Numeric.AD.Internal.Sparse
import Numeric.AD.Internal.Combinators
import Numeric.AD.Mode
second :: (a -> b) -> (c, a) -> (c, b)
second g (a,b) = (a, g b)
grad :: (Traversable f, Num a) => (f (Sparse a) -> Sparse a) -> f a -> f a
grad f as = d as $ apply f as
grad' :: (Traversable f, Num a) => (f (Sparse a) -> Sparse a) -> f a -> (a, f a)
grad' f as = d' as $ apply f as
gradWith :: (Traversable f, Num a) => (a -> a -> b) -> (f (Sparse a) -> Sparse a) -> f a -> f b
gradWith g f as = zipWithT g as $ grad f as
gradWith' :: (Traversable f, Num a) => (a -> a -> b) -> (f (Sparse a) -> Sparse a) -> f a -> (a, f b)
gradWith' g f as = second (zipWithT g as) $ grad' f as
jacobian :: (Traversable f, Functor g, Num a) => (f (Sparse a) -> g (Sparse a)) -> f a -> g (f a)
jacobian f as = d as <$> apply f as
jacobian' :: (Traversable f, Functor g, Num a) => (f (Sparse a) -> g (Sparse a)) -> f a -> g (a, f a)
jacobian' f as = d' as <$> apply f as
jacobianWith :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (f (Sparse a) -> g (Sparse a)) -> f a -> g (f b)
jacobianWith g f as = zipWithT g as <$> jacobian f as
jacobianWith' :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (f (Sparse a) -> g (Sparse a)) -> f a -> g (a, f b)
jacobianWith' g f as = second (zipWithT g as) <$> jacobian' f as
grads :: (Traversable f, Num a) => (f (Sparse a) -> Sparse a) -> f a -> Cofree f a
grads f as = ds as $ apply f as
jacobians :: (Traversable f, Functor g, Num a) => (f (Sparse a) -> g (Sparse a)) -> f a -> g (Cofree f a)
jacobians f as = ds as <$> apply f as
d2 :: Functor f => Cofree f a -> f (f a)
d2 = headJet . tailJet . tailJet . jet
d2' :: Functor f => Cofree f a -> (a, f (a, f a))
d2' (a :< as) = (a, fmap (\(da :< das) -> (da, extract <$> das)) as)
hessian :: (Traversable f, Num a) => (f (Sparse a) -> Sparse a) -> f a -> f (f a)
hessian f as = d2 $ grads f as
hessian' :: (Traversable f, Num a) => (f (Sparse a) -> Sparse a) -> f a -> (a, f (a, f a))
hessian' f as = d2' $ grads f as
hessianF :: (Traversable f, Functor g, Num a) => (f (Sparse a) -> g (Sparse a)) -> f a -> g (f (f a))
hessianF f as = d2 <$> jacobians f as
hessianF' :: (Traversable f, Functor g, Num a) => (f (Sparse a) -> g (Sparse a)) -> f a -> g (a, f (a, f a))
hessianF' f as = d2' <$> jacobians f as