{-# 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 = (f SparseDouble -> SparseDouble) -> f Double -> f Double
forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f Double
Rank1.grad (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (f SparseDouble -> SparseDouble) -> f Double -> (Double, f Double)
forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> (Double, f Double)
Rank1.grad' (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> f b
forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> f b
Rank1.gradWith Double -> Double -> b
g (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> (Double, f b)
forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f SparseDouble -> SparseDouble) -> f Double -> (Double, f b)
Rank1.gradWith' Double -> Double -> b
g (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (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)
Rank1.jacobian ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (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)
Rank1.jacobian' ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (Double -> Double -> b)
-> (f SparseDouble -> g SparseDouble) -> f Double -> g (f b)
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 ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (Double -> Double -> b)
-> (f SparseDouble -> g SparseDouble)
-> f Double
-> g (Double, f b)
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 ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> Cofree f Double
Rank1.grads (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (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)
Rank1.jacobians ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (f SparseDouble -> SparseDouble) -> f Double -> f (f Double)
forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble) -> f Double -> f (f Double)
Rank1.hessian (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (f SparseDouble -> SparseDouble)
-> f Double -> (Double, f (Double, f Double))
forall (f :: * -> *).
Traversable f =>
(f SparseDouble -> SparseDouble)
-> f Double -> (Double, f (Double, f Double))
Rank1.hessian' (AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(AD Any SparseDouble -> SparseDouble)
-> (f SparseDouble -> AD Any SparseDouble)
-> f SparseDouble
-> SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> AD Any SparseDouble
forall s. f (AD s SparseDouble) -> AD s SparseDouble
f(f (AD Any SparseDouble) -> AD Any SparseDouble)
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> AD Any SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (f SparseDouble -> g SparseDouble) -> f Double -> g (f (f Double))
forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble) -> f Double -> g (f (f Double))
Rank1.hessianF ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
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 = (f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f (Double, f Double))
forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f SparseDouble -> g SparseDouble)
-> f Double -> g (Double, f (Double, f Double))
Rank1.hessianF' ((AD Any SparseDouble -> SparseDouble)
-> g (AD Any SparseDouble) -> g SparseDouble
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any SparseDouble -> SparseDouble
forall s a. AD s a -> a
runAD(g (AD Any SparseDouble) -> g SparseDouble)
-> (f SparseDouble -> g (AD Any SparseDouble))
-> f SparseDouble
-> g SparseDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any SparseDouble) -> g (AD Any SparseDouble)
forall s. f (AD s SparseDouble) -> g (AD s SparseDouble)
f(f (AD Any SparseDouble) -> g (AD Any SparseDouble))
-> (f SparseDouble -> f (AD Any SparseDouble))
-> f SparseDouble
-> g (AD Any SparseDouble)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SparseDouble -> AD Any SparseDouble)
-> f SparseDouble -> f (AD Any SparseDouble)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SparseDouble -> AD Any SparseDouble
forall s a. a -> AD s a
AD)
{-# INLINE hessianF' #-}