```{-# 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\".
--
-----------------------------------------------------------------------------
, Tower
, auto
-- * Taylor Series
, taylor
, taylor0
-- * Maclaurin Series
, maclaurin
, maclaurin0
-- * Derivatives
, diff    -- first derivative of (a -> a)
, diff'   -- answer and first derivative of (a -> a)
, diffs   -- answer and all derivatives of (a -> a)
, diffs0  -- zero padded derivatives of (a -> a)
, diffsF  -- answer and all derivatives of (a -> f a)
, diffs0F -- zero padded derivatives of (a -> f a)
-- * Directional Derivatives
, du      -- directional derivative of (a -> a)
, du'     -- answer and directional derivative of (a -> a)
, dus     -- answer and all directional derivatives of (a -> a)
, dus0    -- answer and all zero padded directional derivatives of (a -> a)
, duF     -- directional derivative of (a -> f a)
, duF'    -- answer and directional derivative of (a -> f a)
, dusF    -- answer and all directional derivatives of (a -> f a)
, dus0F   -- answer and all zero padded directional derivatives of (a -> a)
) where

import qualified Numeric.AD.Rank1.Tower as Rank1

diffs :: Num a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
diffs :: forall a.
Num a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
diffs forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Num a => (Tower a -> Tower a) -> a -> [a]
Rank1.diffs (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE diffs #-}

diffs0 :: Num a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
diffs0 :: forall a.
Num a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
diffs0 forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Num a => (Tower a -> Tower a) -> a -> [a]
Rank1.diffs0 (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE diffs0 #-}

diffsF :: (Functor f, Num a) => (forall s. AD s (Tower a) -> f (AD s (Tower a))) -> a -> f [a]
diffsF :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(forall s. AD s (Tower a) -> f (AD s (Tower a))) -> a -> f [a]
diffsF forall s. AD s (Tower a) -> f (AD s (Tower a))
f = forall (f :: * -> *) a.
(Functor f, Num a) =>
(Tower a -> f (Tower a)) -> a -> f [a]
Rank1.diffsF (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. AD s (Tower a) -> f (AD s (Tower a))
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE diffsF #-}

diffs0F :: (Functor f, Num a) => (forall s. AD s (Tower a) -> f (AD s (Tower a))) -> a -> f [a]
diffs0F :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(forall s. AD s (Tower a) -> f (AD s (Tower a))) -> a -> f [a]
diffs0F forall s. AD s (Tower a) -> f (AD s (Tower a))
f = forall (f :: * -> *) a.
(Functor f, Num a) =>
(Tower a -> f (Tower a)) -> a -> f [a]
Rank1.diffs0F (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. AD s (Tower a) -> f (AD s (Tower a))
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE diffs0F #-}

taylor :: Fractional a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> a -> [a]
taylor :: forall a.
Fractional a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> a -> [a]
taylor forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
Rank1.taylor (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a

taylor0 :: Fractional a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> a -> [a]
taylor0 :: forall a.
Fractional a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> a -> [a]
taylor0 forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
Rank1.taylor0 (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE taylor0 #-}

maclaurin :: Fractional a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
maclaurin :: forall a.
Fractional a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
maclaurin forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Fractional a => (Tower a -> Tower a) -> a -> [a]
Rank1.maclaurin (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE maclaurin #-}

maclaurin0 :: Fractional a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
maclaurin0 :: forall a.
Fractional a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> [a]
maclaurin0 forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Fractional a => (Tower a -> Tower a) -> a -> [a]
Rank1.maclaurin0 (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE maclaurin0 #-}

diff :: Num a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> a
diff :: forall a.
Num a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> a
diff forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Num a => (Tower a -> Tower a) -> a -> a
Rank1.diff (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE diff #-}

diff' :: Num a => (forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> (a, a)
diff' :: forall a.
Num a =>
(forall s. AD s (Tower a) -> AD s (Tower a)) -> a -> (a, a)
diff' forall s. AD s (Tower a) -> AD s (Tower a)
f = forall a. Num a => (Tower a -> Tower a) -> a -> (a, a)
Rank1.diff' (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. AD s (Tower a) -> AD s (Tower a)
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. a -> AD s a
{-# INLINE diff' #-}

du :: (Functor f, Num a) => (forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f (a, a) -> a
du :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f (a, a) -> a
du forall s. f (AD s (Tower a)) -> AD s (Tower a)
f = forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f (a, a) -> a
Rank1.du (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s (Tower a)) -> AD s (Tower a)
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
{-# INLINE du #-}

du' :: (Functor f, Num a) => (forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f (a, a) -> (a, a)
du' :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(forall s. f (AD s (Tower a)) -> AD s (Tower a))
-> f (a, a) -> (a, a)
du' forall s. f (AD s (Tower a)) -> AD s (Tower a)
f = forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f (a, a) -> (a, a)
Rank1.du' (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s (Tower a)) -> AD s (Tower a)
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
{-# INLINE du' #-}

duF :: (Functor f, Functor g, Num a) => (forall s. f (AD s (Tower a)) -> g (AD s (Tower a))) -> f (a, a) -> g a
duF :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(forall s. f (AD s (Tower a)) -> g (AD s (Tower a)))
-> f (a, a) -> g a
duF forall s. f (AD s (Tower a)) -> g (AD s (Tower a))
f = forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f (a, a) -> g a
Rank1.duF (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 (Tower a)) -> g (AD s (Tower a))
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
{-# INLINE duF #-}

duF' :: (Functor f, Functor g, Num a) => (forall s. f (AD s (Tower a)) -> g (AD s (Tower a))) -> f (a, a) -> g (a, a)
duF' :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(forall s. f (AD s (Tower a)) -> g (AD s (Tower a)))
-> f (a, a) -> g (a, a)
duF' forall s. f (AD s (Tower a)) -> g (AD s (Tower a))
f = forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f (a, a) -> g (a, a)
Rank1.duF' (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 (Tower a)) -> g (AD s (Tower a))
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
{-# INLINE duF' #-}

dus :: (Functor f, Num a) => (forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f [a] -> [a]
dus :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f [a] -> [a]
dus forall s. f (AD s (Tower a)) -> AD s (Tower a)
f = forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f [a] -> [a]
Rank1.dus (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s (Tower a)) -> AD s (Tower a)
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
{-# INLINE dus #-}

dus0 :: (Functor f, Num a) => (forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f [a] -> [a]
dus0 :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(forall s. f (AD s (Tower a)) -> AD s (Tower a)) -> f [a] -> [a]
dus0 forall s. f (AD s (Tower a)) -> AD s (Tower a)
f = forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f [a] -> [a]
Rank1.dus0 (forall s a. AD s a -> a
runADforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s. f (AD s (Tower a)) -> AD s (Tower a)
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
{-# INLINE dus0 #-}

dusF :: (Functor f, Functor g, Num a) => (forall s. f (AD s (Tower a)) -> g (AD s (Tower a))) -> f [a] -> g [a]
dusF :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(forall s. f (AD s (Tower a)) -> g (AD s (Tower a)))
-> f [a] -> g [a]
dusF forall s. f (AD s (Tower a)) -> g (AD s (Tower a))
f = forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f [a] -> g [a]
Rank1.dusF (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 (Tower a)) -> g (AD s (Tower a))
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
{-# INLINE dusF #-}

dus0F :: (Functor f, Functor g, Num a) => (forall s. f (AD s (Tower a)) -> g (AD s (Tower a))) -> f [a] -> g [a]
dus0F :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(forall s. f (AD s (Tower a)) -> g (AD s (Tower a)))
-> f [a] -> g [a]
dus0F forall s. f (AD s (Tower a)) -> g (AD s (Tower a))
f = forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f [a] -> g [a]
Rank1.dus0F (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 (Tower a)) -> g (AD s (Tower a))
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