{-# LANGUAGE Rank2Types #-}
-----------------------------------------------------------------------------
-- |
-- 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.Mode.Sparse.Double
  ( AD, SparseDouble, auto
  -- * Sparse Gradients
  , grad
  , grad'
  , grads
  , gradWith
  , gradWith'

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

  -- * Sparse Hessians
  , hessian
  , hessian'

  , hessianF
  , hessianF'
  ) where

import Control.Comonad.Cofree (Cofree)
import Numeric.AD.Internal.Sparse.Double (SparseDouble)
import qualified Numeric.AD.Rank1.Sparse.Double as Rank1
import Numeric.AD.Internal.Type
import Numeric.AD.Mode

-- | The 'grad' function calculates the gradient of a non-scalar-to-scalar function with sparse-mode AD in a single pass.
--
--
-- >>> grad (\[x,y,z] -> x*y+z) [1,2,3]
-- [2.0,1.0,1.0]
--
-- >>> grad (\[x,y] -> x**y) [0,2]
-- [0.0,NaN]
grad
  :: Traversable f
  => (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double
  -> f Double
grad :: forall (f :: * -> *).
Traversable f =>
(forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double -> f Double
grad forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f Double
Rank1.grad (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE grad #-}

grad'
  :: Traversable f
  => (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double
  -> (Double, f Double)
grad' :: forall (f :: * -> *).
Traversable f =>
(forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double -> (Double, f Double)
grad' forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> (Double, f Double)
Rank1.grad' (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE grad' #-}

gradWith
  :: Traversable f
  => (Double -> Double -> b)
  -> (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double
  -> f b
gradWith :: forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double
-> f b
gradWith Double -> Double -> b
g forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> f b
Rank1.gradWith Double -> Double -> b
g (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE gradWith #-}

gradWith'
  :: Traversable f
  => (Double -> Double -> b)
  -> (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double
  -> (Double, f b)
gradWith' :: forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double
-> (Double, f b)
gradWith' Double -> Double -> b
g forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> (Double, f b)
Rank1.gradWith' Double -> Double -> b
g (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE gradWith' #-}

jacobian
  :: (Traversable f, Functor g)
  => (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double -> g (f Double)
jacobian :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double -> g (f Double)
jacobian forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f Double)
Rank1.jacobian (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE jacobian #-}

jacobian'
  :: (Traversable f, Functor g)
  => (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double
  -> g (Double, f Double)
jacobian' :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double -> g (Double, f Double)
jacobian' forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f Double)
Rank1.jacobian' (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE jacobian' #-}

jacobianWith
  :: (Traversable f, Functor g)
  => (Double -> Double -> b)
  -> (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double
  -> g (f b)
jacobianWith :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double
-> g (f b)
jacobianWith Double -> Double -> b
g forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (f SparseDouble -> g SparseDouble) -> f Double -> g (f b)
Rank1.jacobianWith Double -> Double -> b
g (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE jacobianWith #-}

jacobianWith'
  :: (Traversable f, Functor g)
  => (Double -> Double -> b)
  -> (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double
  -> g (Double, f b)
jacobianWith' :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double
-> g (Double, f b)
jacobianWith' Double -> Double -> b
g forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (f SparseDouble -> g SparseDouble)
-> f Double
-> g (Double, f b)
Rank1.jacobianWith' Double -> Double -> b
g (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE jacobianWith' #-}

grads
  :: Traversable f
  => (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double -> Cofree f Double
grads :: forall (f :: * -> *).
Traversable f =>
(forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double -> Cofree f Double
grads forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
Rank1.grads (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE grads #-}

jacobians
  :: (Traversable f, Functor g)
  => (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double
  -> g (Cofree f Double)
jacobians :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double -> g (Cofree f Double)
jacobians forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Cofree f Double)
Rank1.jacobians (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE jacobians #-}

hessian
  :: Traversable f
  => (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double
  -> f (f Double)
hessian :: forall (f :: * -> *).
Traversable f =>
(forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double -> f (f Double)
hessian forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f (f Double)
Rank1.hessian (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE hessian #-}

hessian'
  :: Traversable f
  => (forall s. f (AD s SparseDouble) -> AD s SparseDouble)
  -> f Double -> (Double, f (Double, f Double))
hessian' :: forall (f :: * -> *).
Traversable f =>
(forall s. f (AD s SparseDouble) -> AD s SparseDouble)
-> f Double -> (Double, f (Double, f Double))
hessian' forall s. f (AD s SparseDouble) -> AD s SparseDouble
f = forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble)
-> f Double -> (Double, f (Double, f Double))
Rank1.hessian' (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> AD s SparseDouble
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE hessian' #-}

hessianF
  :: (Traversable f, Functor g)
  => (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double -> g (f (f Double))
hessianF :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double -> g (f (f Double))
hessianF forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f (f Double))
Rank1.hessianF (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE hessianF #-}

hessianF'
  :: (Traversable f, Functor g)
  => (forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
  -> f Double
  -> g (Double, f (Double, f Double))
hessianF' :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(forall s. f (AD s SparseDouble) -> g (AD s SparseDouble))
-> f Double -> g (Double, f (Double, f Double))
hessianF' forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f = forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f (Double, f Double))
Rank1.hessianF' (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. a -> AD s a
AD)
{-# INLINE hessianF' #-}