-----------------------------------------------------------------------------
-- |
-- Copyright   : (c) Edward Kmett 2010-2021
-- License     : BSD3
-- Maintainer  : ekmett@gmail.com
-- Stability   : experimental
-- Portability : GHC only
--
-- Higher order derivatives via a \"dual number tower\".
--
-----------------------------------------------------------------------------

module Numeric.AD.Rank1.Sparse.Double
  ( SparseDouble
  , auto
  -- * Sparse Gradients
  , grad
  , grad'
  , gradWith
  , gradWith'
  -- * Variadic Gradients
  -- $vgrad
  , Grad
  , vgrad
  -- * Higher-Order Gradients
  , grads
  -- * Variadic Higher-Order Gradients
  , Grads
  , vgrads

  -- * Sparse Jacobians (synonyms)
  , jacobian
  , jacobian'
  , jacobianWith
  , jacobianWith'
  , jacobians

  -- * Sparse Hessians
  , hessian
  , hessian'

  , hessianF
  , hessianF'

  ) where

import Control.Comonad
import Control.Comonad.Cofree
import Numeric.AD.Jet
import Numeric.AD.Internal.Sparse.Double
import Numeric.AD.Internal.Combinators
import Numeric.AD.Mode

second :: (a -> b) -> (c, a) -> (c, b)
second :: forall a b c. (a -> b) -> (c, a) -> (c, b)
second a -> b
g (c
a,a
b) = (c
a, a -> b
g a
b)
{-# INLINE second #-}

grad
  :: Traversable f
  => (f SparseDouble -> SparseDouble)
  -> f Double -> f Double
grad :: forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f Double
grad f SparseDouble -> SparseDouble
f f Double
as = forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> f Double
d f Double
as forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) b.
Traversable f =>
(f SparseDouble -> b) -> f Double -> b
apply f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE grad #-}

grad'
  :: Traversable f
  => (f SparseDouble -> SparseDouble)
  -> f Double -> (Double, f Double)
grad' :: forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> (Double, f Double)
grad' f SparseDouble -> SparseDouble
f f Double
as = forall (f :: * -> *).
Traversable f =>
f Double -> SparseDouble -> (Double, f Double)
d' f Double
as forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) b.
Traversable f =>
(f SparseDouble -> b) -> f Double -> b
apply f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE grad' #-}

gradWith
  :: Traversable f
  => (Double -> Double -> b)
  -> (f SparseDouble -> SparseDouble)
  -> f Double
  -> f b
gradWith :: forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> f b
gradWith Double -> Double -> b
g f SparseDouble -> SparseDouble
f f Double
as = forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT Double -> Double -> b
g f Double
as forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f Double
grad f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE gradWith #-}

gradWith'
  :: Traversable f
  => (Double -> Double -> b)
  -> (f SparseDouble -> SparseDouble)
  -> f Double
  -> (Double, f b)
gradWith' :: forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> (Double, f b)
gradWith' Double -> Double -> b
g f SparseDouble -> SparseDouble
f f Double
as = forall a b c. (a -> b) -> (c, a) -> (c, b)
second (forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT Double -> Double -> b
g f Double
as) forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> (Double, f Double)
grad' f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE gradWith' #-}

jacobian
  :: (Traversable f, Functor g)
  => (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (f Double)
jacobian :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f Double)
jacobian f SparseDouble -> g SparseDouble
f f Double
as = forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> f Double
d f Double
as forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) b.
Traversable f =>
(f SparseDouble -> b) -> f Double -> b
apply f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE jacobian #-}

jacobian'
  :: (Traversable f, Functor g)
  => (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (Double, f Double)
jacobian' :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f Double)
jacobian' f SparseDouble -> g SparseDouble
f f Double
as = forall (f :: * -> *).
Traversable f =>
f Double -> SparseDouble -> (Double, f Double)
d' f Double
as forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) b.
Traversable f =>
(f SparseDouble -> b) -> f Double -> b
apply f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE jacobian' #-}

jacobianWith
  :: (Traversable f, Functor g)
  => (Double -> Double -> b)
  -> (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (f b)
jacobianWith :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (f SparseDouble -> g SparseDouble) -> f Double -> g (f b)
jacobianWith Double -> Double -> b
g f SparseDouble -> g SparseDouble
f f Double
as = forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT Double -> Double -> b
g f Double
as forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f Double)
jacobian f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE jacobianWith #-}

jacobianWith'
  :: (Traversable f, Functor g)
  => (Double -> Double -> b)
  -> (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (Double, f b)
jacobianWith' :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (f SparseDouble -> g SparseDouble)
-> f Double
-> g (Double, f b)
jacobianWith' Double -> Double -> b
g f SparseDouble -> g SparseDouble
f f Double
as = forall a b c. (a -> b) -> (c, a) -> (c, b)
second (forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT Double -> Double -> b
g f Double
as) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f Double)
jacobian' f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE jacobianWith' #-}

grads
  :: Traversable f
  => (f SparseDouble -> SparseDouble)
  -> f Double
  -> Cofree f Double
grads :: forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
grads f SparseDouble -> SparseDouble
f f Double
as = forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> Cofree f Double
ds f Double
as forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) b.
Traversable f =>
(f SparseDouble -> b) -> f Double -> b
apply f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE grads #-}

jacobians
  :: (Traversable f, Functor g)
  => (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (Cofree f Double)
jacobians :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Cofree f Double)
jacobians f SparseDouble -> g SparseDouble
f f Double
as = forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> Cofree f Double
ds f Double
as forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) b.
Traversable f =>
(f SparseDouble -> b) -> f Double -> b
apply f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE jacobians #-}

d2 :: Functor f
  => Cofree f a
  -> f (f a)
d2 :: forall (f :: * -> *) a. Functor f => Cofree f a -> f (f a)
d2 = forall (f :: * -> *) a. Jet f a -> a
headJet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Jet f a -> Jet f (f a)
tailJet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Jet f a -> Jet f (f a)
tailJet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => Cofree f a -> Jet f a
jet
{-# INLINE d2 #-}

d2'
  :: Functor f
  => Cofree f a
  -> (a, f (a, f a))
d2' :: forall (f :: * -> *) a. Functor f => Cofree f a -> (a, f (a, f a))
d2' (a
a :< f (Cofree f a)
as) = (a
a, forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
da :< f (Cofree f a)
das) -> (a
da, forall (w :: * -> *) a. Comonad w => w a -> a
extract forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Cofree f a)
das)) f (Cofree f a)
as)
{-# INLINE d2' #-}

hessian
  :: Traversable f
  => (f SparseDouble -> SparseDouble)
  -> f Double
  -> f (f Double)
hessian :: forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f (f Double)
hessian f SparseDouble -> SparseDouble
f f Double
as = forall (f :: * -> *) a. Functor f => Cofree f a -> f (f a)
d2 forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
grads f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE hessian #-}

hessian'
  :: Traversable f
  => (f SparseDouble -> SparseDouble)
  -> f Double
  -> (Double, f (Double, f Double))
hessian' :: forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble)
-> f Double -> (Double, f (Double, f Double))
hessian' f SparseDouble -> SparseDouble
f f Double
as = forall (f :: * -> *) a. Functor f => Cofree f a -> (a, f (a, f a))
d2' forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
grads f SparseDouble -> SparseDouble
f f Double
as
{-# INLINE hessian' #-}

hessianF
  :: (Traversable f, Functor g)
  => (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (f (f Double))
hessianF :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f (f Double))
hessianF f SparseDouble -> g SparseDouble
f f Double
as = forall (f :: * -> *) a. Functor f => Cofree f a -> f (f a)
d2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Cofree f Double)
jacobians f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE hessianF #-}

hessianF'
  :: (Traversable f, Functor g)
  => (f SparseDouble -> g SparseDouble)
  -> f Double
  -> g (Double, f (Double, f Double))
hessianF' :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f (Double, f Double))
hessianF' f SparseDouble -> g SparseDouble
f f Double
as = forall (f :: * -> *) a. Functor f => Cofree f a -> (a, f (a, f a))
d2' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Cofree f Double)
jacobians f SparseDouble -> g SparseDouble
f f Double
as
{-# INLINE hessianF' #-}

-- $vgrad
--
-- Variadic combinators for variadic mixed-mode automatic differentiation.
--
-- Unfortunately, variadicity comes at the expense of being able to use
-- quantification to avoid sensitivity confusion, so be careful when
-- counting the number of 'auto' calls you use when taking the gradient
-- of a function that takes gradients!