-----------------------------------------------------------------------------
-- |
-- 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 = f Double -> SparseDouble -> f Double
forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> f Double
d f Double
as (SparseDouble -> f Double) -> SparseDouble -> f Double
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> SparseDouble
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 = f Double -> SparseDouble -> (Double, f Double)
forall (f :: * -> *).
Traversable f =>
f Double -> SparseDouble -> (Double, f Double)
d' f Double
as (SparseDouble -> (Double, f Double))
-> SparseDouble -> (Double, f Double)
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> SparseDouble
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 = (Double -> Double -> b) -> f Double -> f Double -> f b
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 (f Double -> f b) -> f Double -> f b
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> f Double
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 = (f Double -> f b) -> (Double, f Double) -> (Double, f b)
forall a b c. (a -> b) -> (c, a) -> (c, b)
second ((Double -> Double -> b) -> f Double -> f Double -> f b
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) ((Double, f Double) -> (Double, f b))
-> (Double, f Double) -> (Double, f b)
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> (Double, f Double)
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 = f Double -> SparseDouble -> f Double
forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> f Double
d f Double
as (SparseDouble -> f Double) -> g SparseDouble -> g (f Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble) -> f Double -> g SparseDouble
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 = f Double -> SparseDouble -> (Double, f Double)
forall (f :: * -> *).
Traversable f =>
f Double -> SparseDouble -> (Double, f Double)
d' f Double
as (SparseDouble -> (Double, f Double))
-> g SparseDouble -> g (Double, f Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble) -> f Double -> g SparseDouble
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 = (Double -> Double -> b) -> f Double -> f Double -> f b
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 (f Double -> f b) -> g (f Double) -> g (f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble) -> f Double -> g (f Double)
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 = (f Double -> f b) -> (Double, f Double) -> (Double, f b)
forall a b c. (a -> b) -> (c, a) -> (c, b)
second ((Double -> Double -> b) -> f Double -> f Double -> f b
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) ((Double, f Double) -> (Double, f b))
-> g (Double, f Double) -> g (Double, f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f Double)
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 = f Double -> SparseDouble -> Cofree f Double
forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> Cofree f Double
ds f Double
as (SparseDouble -> Cofree f Double)
-> SparseDouble -> Cofree f Double
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> SparseDouble
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 = f Double -> SparseDouble -> Cofree f Double
forall (f :: * -> *) b.
Traversable f =>
f b -> SparseDouble -> Cofree f Double
ds f Double
as (SparseDouble -> Cofree f Double)
-> g SparseDouble -> g (Cofree f Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble) -> f Double -> g SparseDouble
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 = Jet f (f (f a)) -> f (f a)
forall (f :: * -> *) a. Jet f a -> a
headJet (Jet f (f (f a)) -> f (f a))
-> (Cofree f a -> Jet f (f (f a))) -> Cofree f a -> f (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Jet f (f a) -> Jet f (f (f a))
forall (f :: * -> *) a. Jet f a -> Jet f (f a)
tailJet (Jet f (f a) -> Jet f (f (f a)))
-> (Cofree f a -> Jet f (f a)) -> Cofree f a -> Jet f (f (f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Jet f a -> Jet f (f a)
forall (f :: * -> *) a. Jet f a -> Jet f (f a)
tailJet (Jet f a -> Jet f (f a))
-> (Cofree f a -> Jet f a) -> Cofree f a -> Jet f (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cofree f a -> Jet f a
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, (Cofree f a -> (a, f a)) -> f (Cofree f a) -> f (a, f a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
da :< f (Cofree f a)
das) -> (a
da, Cofree f a -> a
forall a. Cofree f a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (Cofree f a -> a) -> f (Cofree f a) -> f a
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 = Cofree f Double -> f (f Double)
forall (f :: * -> *) a. Functor f => Cofree f a -> f (f a)
d2 (Cofree f Double -> f (f Double))
-> Cofree f Double -> f (f Double)
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
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 = Cofree f Double -> (Double, f (Double, f Double))
forall (f :: * -> *) a. Functor f => Cofree f a -> (a, f (a, f a))
d2' (Cofree f Double -> (Double, f (Double, f Double)))
-> Cofree f Double -> (Double, f (Double, f Double))
forall a b. (a -> b) -> a -> b
$ (f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
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 = Cofree f Double -> f (f Double)
forall (f :: * -> *) a. Functor f => Cofree f a -> f (f a)
d2 (Cofree f Double -> f (f Double))
-> g (Cofree f Double) -> g (f (f Double))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble)
-> f Double -> g (Cofree f Double)
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 = Cofree f Double -> (Double, f (Double, f Double))
forall (f :: * -> *) a. Functor f => Cofree f a -> (a, f (a, f a))
d2' (Cofree f Double -> (Double, f (Double, f Double)))
-> g (Cofree f Double) -> g (Double, f (Double, f Double))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f SparseDouble -> g SparseDouble)
-> f Double -> g (Cofree f Double)
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!