{-# LANGUAGE Rank2Types #-}
-----------------------------------------------------------------------------
-- |
-- Copyright   : (c) Edward Kmett 2010-2021
-- License     : BSD3
-- Maintainer  : ekmett@gmail.com
-- Stability   : experimental
-- Portability : GHC only
--
-- First order dense forward mode using 'Traversable' functors
--
-----------------------------------------------------------------------------

module Numeric.AD.Mode.Dense
  ( AD, Dense, auto
  -- * Dense Gradients
  , grad
  , grad'
  , gradWith
  , gradWith'

  -- * Dense Jacobians (synonyms)
  , jacobian
  , jacobian'
  , jacobianWith
  , jacobianWith'
  ) where

import Numeric.AD.Internal.Dense (Dense)
import qualified Numeric.AD.Rank1.Dense 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 dense-mode AD in a single pass.
--
-- >>> grad (\[x,y,z] -> x*y+z) [1,2,3]
-- [2,1,1]
grad :: (Traversable f, Num a) => (forall s. f (AD s (Dense f a)) -> AD s (Dense f a)) -> f a -> f a
grad :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s. f (AD s (Dense f a)) -> AD s (Dense f a)) -> f a -> f a
grad forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f = (f (Dense f a) -> Dense f a) -> f a -> f a
forall (f :: * -> *) a.
(Traversable f, Num a) =>
(f (Dense f a) -> Dense f a) -> f a -> f a
Rank1.grad (AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(AD Any (Dense f a) -> Dense f a)
-> (f (Dense f a) -> AD Any (Dense f a))
-> f (Dense f a)
-> Dense f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> AD Any (Dense f a)
forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f(f (AD Any (Dense f a)) -> AD Any (Dense f a))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> AD Any (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE grad #-}

grad' :: (Traversable f, Num a) => (forall s. f (AD s (Dense f a)) -> AD s (Dense f a)) -> f a -> (a, f a)
grad' :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(forall s. f (AD s (Dense f a)) -> AD s (Dense f a))
-> f a -> (a, f a)
grad' forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f = (f (Dense f a) -> Dense f a) -> f a -> (a, f a)
forall (f :: * -> *) a.
(Traversable f, Num a) =>
(f (Dense f a) -> Dense f a) -> f a -> (a, f a)
Rank1.grad' (AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(AD Any (Dense f a) -> Dense f a)
-> (f (Dense f a) -> AD Any (Dense f a))
-> f (Dense f a)
-> Dense f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> AD Any (Dense f a)
forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f(f (AD Any (Dense f a)) -> AD Any (Dense f a))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> AD Any (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE grad' #-}

gradWith :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. f (AD s (Dense f a)) -> AD s (Dense f a)) -> f a -> f b
gradWith :: forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(a -> a -> b)
-> (forall s. f (AD s (Dense f a)) -> AD s (Dense f a))
-> f a
-> f b
gradWith a -> a -> b
g forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f = (a -> a -> b) -> (f (Dense f a) -> Dense f a) -> f a -> f b
forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(a -> a -> b) -> (f (Dense f a) -> Dense f a) -> f a -> f b
Rank1.gradWith a -> a -> b
g (AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(AD Any (Dense f a) -> Dense f a)
-> (f (Dense f a) -> AD Any (Dense f a))
-> f (Dense f a)
-> Dense f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> AD Any (Dense f a)
forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f(f (AD Any (Dense f a)) -> AD Any (Dense f a))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> AD Any (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE gradWith #-}

gradWith' :: (Traversable f, Num a) => (a -> a -> b) -> (forall s. f (AD s (Dense f a)) -> AD s (Dense f a)) -> f a -> (a, f b)
gradWith' :: forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(a -> a -> b)
-> (forall s. f (AD s (Dense f a)) -> AD s (Dense f a))
-> f a
-> (a, f b)
gradWith' a -> a -> b
g forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f = (a -> a -> b) -> (f (Dense f a) -> Dense f a) -> f a -> (a, f b)
forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(a -> a -> b) -> (f (Dense f a) -> Dense f a) -> f a -> (a, f b)
Rank1.gradWith' a -> a -> b
g (AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(AD Any (Dense f a) -> Dense f a)
-> (f (Dense f a) -> AD Any (Dense f a))
-> f (Dense f a)
-> Dense f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> AD Any (Dense f a)
forall s. f (AD s (Dense f a)) -> AD s (Dense f a)
f(f (AD Any (Dense f a)) -> AD Any (Dense f a))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> AD Any (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE gradWith' #-}

jacobian :: (Traversable f, Functor g, Num a) => (forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))) -> f a -> g (f a)
jacobian :: forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a)))
-> f a -> g (f a)
jacobian forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f = (f (Dense f a) -> g (Dense f a)) -> f a -> g (f a)
forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(f (Dense f a) -> g (Dense f a)) -> f a -> g (f a)
Rank1.jacobian ((AD Any (Dense f a) -> Dense f a)
-> g (AD Any (Dense f a)) -> g (Dense f a)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(g (AD Any (Dense f a)) -> g (Dense f a))
-> (f (Dense f a) -> g (AD Any (Dense f a)))
-> f (Dense f a)
-> g (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> g (AD Any (Dense f a))
forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f(f (AD Any (Dense f a)) -> g (AD Any (Dense f a)))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> g (AD Any (Dense f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE jacobian #-}

jacobian' :: (Traversable f, Functor g, Num a) => (forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))) -> f a -> g (a, f a)
jacobian' :: forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a)))
-> f a -> g (a, f a)
jacobian' forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f = (f (Dense f a) -> g (Dense f a)) -> f a -> g (a, f a)
forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(f (Dense f a) -> g (Dense f a)) -> f a -> g (a, f a)
Rank1.jacobian' ((AD Any (Dense f a) -> Dense f a)
-> g (AD Any (Dense f a)) -> g (Dense f a)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(g (AD Any (Dense f a)) -> g (Dense f a))
-> (f (Dense f a) -> g (AD Any (Dense f a)))
-> f (Dense f a)
-> g (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> g (AD Any (Dense f a))
forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f(f (AD Any (Dense f a)) -> g (AD Any (Dense f a)))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> g (AD Any (Dense f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE jacobian' #-}

jacobianWith :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))) -> f a -> g (f b)
jacobianWith :: forall (f :: * -> *) (g :: * -> *) a b.
(Traversable f, Functor g, Num a) =>
(a -> a -> b)
-> (forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a)))
-> f a
-> g (f b)
jacobianWith a -> a -> b
g forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f = (a -> a -> b) -> (f (Dense f a) -> g (Dense f a)) -> f a -> g (f b)
forall (f :: * -> *) (g :: * -> *) a b.
(Traversable f, Functor g, Num a) =>
(a -> a -> b) -> (f (Dense f a) -> g (Dense f a)) -> f a -> g (f b)
Rank1.jacobianWith a -> a -> b
g ((AD Any (Dense f a) -> Dense f a)
-> g (AD Any (Dense f a)) -> g (Dense f a)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(g (AD Any (Dense f a)) -> g (Dense f a))
-> (f (Dense f a) -> g (AD Any (Dense f a)))
-> f (Dense f a)
-> g (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> g (AD Any (Dense f a))
forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f(f (AD Any (Dense f a)) -> g (AD Any (Dense f a)))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> g (AD Any (Dense f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE jacobianWith #-}

jacobianWith' :: (Traversable f, Functor g, Num a) => (a -> a -> b) -> (forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))) -> f a -> g (a, f b)
jacobianWith' :: forall (f :: * -> *) (g :: * -> *) a b.
(Traversable f, Functor g, Num a) =>
(a -> a -> b)
-> (forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a)))
-> f a
-> g (a, f b)
jacobianWith' a -> a -> b
g forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f = (a -> a -> b)
-> (f (Dense f a) -> g (Dense f a)) -> f a -> g (a, f b)
forall (f :: * -> *) (g :: * -> *) a b.
(Traversable f, Functor g, Num a) =>
(a -> a -> b)
-> (f (Dense f a) -> g (Dense f a)) -> f a -> g (a, f b)
Rank1.jacobianWith' a -> a -> b
g ((AD Any (Dense f a) -> Dense f a)
-> g (AD Any (Dense f a)) -> g (Dense f a)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AD Any (Dense f a) -> Dense f a
forall s a. AD s a -> a
runAD(g (AD Any (Dense f a)) -> g (Dense f a))
-> (f (Dense f a) -> g (AD Any (Dense f a)))
-> f (Dense f a)
-> g (Dense f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.f (AD Any (Dense f a)) -> g (AD Any (Dense f a))
forall s. f (AD s (Dense f a)) -> g (AD s (Dense f a))
f(f (AD Any (Dense f a)) -> g (AD Any (Dense f a)))
-> (f (Dense f a) -> f (AD Any (Dense f a)))
-> f (Dense f a)
-> g (AD Any (Dense f a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Dense f a -> AD Any (Dense f a))
-> f (Dense f a) -> f (AD Any (Dense f a))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Dense f a -> AD Any (Dense f a)
forall s a. a -> AD s a
AD)
{-# INLINE jacobianWith' #-}