-----------------------------------------------------------------------------
-- |
-- Copyright   : (c) Edward Kmett 2010-2021
-- License     : BSD3
-- Maintainer  : ekmett@gmail.com
-- Stability   : experimental
-- Portability : GHC only
--
-----------------------------------------------------------------------------

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

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

  ) where

import Numeric.AD.Internal.Dense
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, Num a)
  => (f (Dense f a) -> Dense f a)
  -> f a
  -> f a
grad :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(f (Dense f a) -> Dense f a) -> f a -> f a
grad f (Dense f a) -> Dense f a
f f a
as = f a -> Dense f a -> f a
forall (f :: * -> *) a. f a -> Dense f a -> f a
ds (a
0 a -> f a -> f a
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f a
as) (Dense f a -> f a) -> Dense f a -> f a
forall a b. (a -> b) -> a -> b
$ (f (Dense f a) -> Dense f a) -> f a -> Dense f a
forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(f (Dense f a) -> b) -> f a -> b
apply f (Dense f a) -> Dense f a
f f a
as
{-# INLINE grad #-}

grad'
  :: (Traversable f, Num a)
  => (f (Dense f a) -> Dense f a)
  -> f a
  -> (a, f a)
grad' :: forall (f :: * -> *) a.
(Traversable f, Num a) =>
(f (Dense f a) -> Dense f a) -> f a -> (a, f a)
grad' f (Dense f a) -> Dense f a
f f a
as = f a -> Dense f a -> (a, f a)
forall a (f :: * -> *). Num a => f a -> Dense f a -> (a, f a)
ds' (a
0 a -> f a -> f a
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f a
as) (Dense f a -> (a, f a)) -> Dense f a -> (a, f a)
forall a b. (a -> b) -> a -> b
$ (f (Dense f a) -> Dense f a) -> f a -> Dense f a
forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(f (Dense f a) -> b) -> f a -> b
apply f (Dense f a) -> Dense f a
f f a
as
{-# INLINE grad' #-}

gradWith
  :: (Traversable f, Num a)
  => (a -> a -> b)
  -> (f (Dense f a) -> Dense f a)
  -> f a
  -> f b
gradWith :: forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(a -> a -> b) -> (f (Dense f a) -> Dense f a) -> f a -> f b
gradWith a -> a -> b
g f (Dense f a) -> Dense f a
f f a
as = (a -> a -> b) -> f a -> f a -> f b
forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT a -> a -> b
g f a
as (f a -> f b) -> f a -> f b
forall a b. (a -> b) -> a -> b
$ (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
grad f (Dense f a) -> Dense f a
f f a
as
{-# INLINE gradWith #-}

gradWith'
  :: (Traversable f, Num a)
  => (a -> a -> b)
  -> (f (Dense f a) -> Dense f a)
  -> f a
  -> (a, f b)
gradWith' :: forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(a -> a -> b) -> (f (Dense f a) -> Dense f a) -> f a -> (a, f b)
gradWith' a -> a -> b
g f (Dense f a) -> Dense f a
f f a
as = (f a -> f b) -> (a, f a) -> (a, f b)
forall a b c. (a -> b) -> (c, a) -> (c, b)
second ((a -> a -> b) -> f a -> f a -> f b
forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT a -> a -> b
g f a
as) ((a, f a) -> (a, f b)) -> (a, f a) -> (a, f b)
forall a b. (a -> b) -> a -> b
$ (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)
grad' f (Dense f a) -> Dense f a
f f a
as
{-# INLINE gradWith' #-}

jacobian
  :: (Traversable f, Functor g, Num a)
  => (f (Dense f a) -> g (Dense f a))
  -> f a
  -> g (f a)
jacobian :: forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(f (Dense f a) -> g (Dense f a)) -> f a -> g (f a)
jacobian f (Dense f a) -> g (Dense f a)
f f a
as = f a -> Dense f a -> f a
forall (f :: * -> *) a. f a -> Dense f a -> f a
ds (a
0 a -> f a -> f a
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f a
as) (Dense f a -> f a) -> g (Dense f a) -> g (f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f (Dense f a) -> g (Dense f a)) -> f a -> g (Dense f a)
forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(f (Dense f a) -> b) -> f a -> b
apply f (Dense f a) -> g (Dense f a)
f f a
as
{-# INLINE jacobian #-}

jacobian'
  :: (Traversable f, Functor g, Num a)
  => (f (Dense f a) -> g (Dense f a))
  -> f a
  -> g (a, f a)
jacobian' :: forall (f :: * -> *) (g :: * -> *) a.
(Traversable f, Functor g, Num a) =>
(f (Dense f a) -> g (Dense f a)) -> f a -> g (a, f a)
jacobian' f (Dense f a) -> g (Dense f a)
f f a
as = f a -> Dense f a -> (a, f a)
forall a (f :: * -> *). Num a => f a -> Dense f a -> (a, f a)
ds' (a
0 a -> f a -> f a
forall a b. a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f a
as) (Dense f a -> (a, f a)) -> g (Dense f a) -> g (a, f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (f (Dense f a) -> g (Dense f a)) -> f a -> g (Dense f a)
forall (f :: * -> *) a b.
(Traversable f, Num a) =>
(f (Dense f a) -> b) -> f a -> b
apply f (Dense f a) -> g (Dense f a)
f f a
as
{-# INLINE jacobian' #-}

jacobianWith
  :: (Traversable f, Functor g, Num a)
  => (a -> a -> b)
  -> (f (Dense f a) -> g (Dense f a))
  -> f a
  -> g (f b)
jacobianWith :: 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)
jacobianWith a -> a -> b
g f (Dense f a) -> g (Dense f a)
f f a
as = (a -> a -> b) -> f a -> f a -> f b
forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT a -> a -> b
g f a
as (f a -> f b) -> g (f a) -> g (f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (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)
jacobian f (Dense f a) -> g (Dense f a)
f f a
as
{-# INLINE jacobianWith #-}

jacobianWith'
  :: (Traversable f, Functor g, Num a)
  => (a -> a -> b)
  -> (f (Dense f a) -> g (Dense f a))
  -> f a
  -> g (a, f b)
jacobianWith' :: 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)
jacobianWith' a -> a -> b
g f (Dense f a) -> g (Dense f a)
f f a
as = (f a -> f b) -> (a, f a) -> (a, f b)
forall a b c. (a -> b) -> (c, a) -> (c, b)
second ((a -> a -> b) -> f a -> f a -> f b
forall (f :: * -> *) (g :: * -> *) a b c.
(Foldable f, Traversable g) =>
(a -> b -> c) -> f a -> g b -> g c
zipWithT a -> a -> b
g f a
as) ((a, f a) -> (a, f b)) -> g (a, f a) -> g (a, f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (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)
jacobian' f (Dense f a) -> g (Dense f a)
f f a
as
{-# INLINE jacobianWith' #-}