{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE BangPatterns #-}
module Numeric.AD.Rank1.Tower
( Tower
, auto
, taylor
, taylor0
, maclaurin
, maclaurin0
, diff
, diff'
, diffs
, diffs0
, diffsF
, diffs0F
, du
, du'
, dus
, dus0
, duF
, duF'
, dusF
, dus0F
) where
import Numeric.AD.Internal.Tower
import Numeric.AD.Mode
diffs
:: Num a
=> (Tower a -> Tower a)
-> a
-> [a]
diffs :: forall a. Num a => (Tower a -> Tower a) -> a -> [a]
diffs Tower a -> Tower a
f a
a = Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> Tower a -> [a]
forall a b. (a -> b) -> a -> b
$ (Tower a -> Tower a) -> a -> Tower a
forall a b. Num a => (Tower a -> b) -> a -> b
apply Tower a -> Tower a
f a
a
{-# INLINE diffs #-}
diffs0
:: Num a
=> (Tower a -> Tower a)
-> a
-> [a]
diffs0 :: forall a. Num a => (Tower a -> Tower a) -> a -> [a]
diffs0 Tower a -> Tower a
f a
a = [a] -> [a]
forall a. Num a => [a] -> [a]
zeroPad ((Tower a -> Tower a) -> a -> [a]
forall a. Num a => (Tower a -> Tower a) -> a -> [a]
diffs Tower a -> Tower a
f a
a)
{-# INLINE diffs0 #-}
diffsF
:: (Functor f, Num a)
=> (Tower a -> f (Tower a))
-> a
-> f [a]
diffsF :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(Tower a -> f (Tower a)) -> a -> f [a]
diffsF Tower a -> f (Tower a)
f a
a = Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> f (Tower a) -> f [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Tower a -> f (Tower a)) -> a -> f (Tower a)
forall a b. Num a => (Tower a -> b) -> a -> b
apply Tower a -> f (Tower a)
f a
a
{-# INLINE diffsF #-}
diffs0F
:: (Functor f, Num a)
=> (Tower a -> f (Tower a))
-> a
-> f [a]
diffs0F :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(Tower a -> f (Tower a)) -> a -> f [a]
diffs0F Tower a -> f (Tower a)
f a
a = [a] -> [a]
forall a. Num a => [a] -> [a]
zeroPad ([a] -> [a]) -> (Tower a -> [a]) -> Tower a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> f (Tower a) -> f [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Tower a -> f (Tower a)) -> a -> f (Tower a)
forall a b. Num a => (Tower a -> b) -> a -> b
apply Tower a -> f (Tower a)
f a
a
{-# INLINE diffs0F #-}
taylor
:: Fractional a
=> (Tower a -> Tower a)
-> a
-> a
-> [a]
taylor :: forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
taylor Tower a -> Tower a
f a
x a
dx = a -> a -> [a] -> [a]
go a
1 a
1 ((Tower a -> Tower a) -> a -> [a]
forall a. Num a => (Tower a -> Tower a) -> a -> [a]
diffs Tower a -> Tower a
f a
x) where
go :: a -> a -> [a] -> [a]
go !a
n !a
acc (a
a:[a]
as) = a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
acc a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> a -> [a] -> [a]
go (a
n a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) (a
acc a -> a -> a
forall a. Num a => a -> a -> a
* a
dx a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
n) [a]
as
go a
_ a
_ [] = []
taylor0
:: Fractional a
=> (Tower a -> Tower a)
-> a
-> a
-> [a]
taylor0 :: forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
taylor0 Tower a -> Tower a
f a
x a
dx = [a] -> [a]
forall a. Num a => [a] -> [a]
zeroPad ((Tower a -> Tower a) -> a -> a -> [a]
forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
taylor Tower a -> Tower a
f a
x a
dx)
{-# INLINE taylor0 #-}
maclaurin
:: Fractional a
=> (Tower a -> Tower a)
-> a
-> [a]
maclaurin :: forall a. Fractional a => (Tower a -> Tower a) -> a -> [a]
maclaurin Tower a -> Tower a
f = (Tower a -> Tower a) -> a -> a -> [a]
forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
taylor Tower a -> Tower a
f a
0
{-# INLINE maclaurin #-}
maclaurin0
:: Fractional a
=> (Tower a -> Tower a)
-> a
-> [a]
maclaurin0 :: forall a. Fractional a => (Tower a -> Tower a) -> a -> [a]
maclaurin0 Tower a -> Tower a
f = (Tower a -> Tower a) -> a -> a -> [a]
forall a. Fractional a => (Tower a -> Tower a) -> a -> a -> [a]
taylor0 Tower a -> Tower a
f a
0
{-# INLINE maclaurin0 #-}
diff
:: Num a
=> (Tower a -> Tower a)
-> a
-> a
diff :: forall a. Num a => (Tower a -> Tower a) -> a -> a
diff Tower a -> Tower a
f = [a] -> a
forall a. Num a => [a] -> a
d ([a] -> a) -> (a -> [a]) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tower a -> Tower a) -> a -> [a]
forall a. Num a => (Tower a -> Tower a) -> a -> [a]
diffs Tower a -> Tower a
f
{-# INLINE diff #-}
diff'
:: Num a
=> (Tower a -> Tower a)
-> a
-> (a, a)
diff' :: forall a. Num a => (Tower a -> Tower a) -> a -> (a, a)
diff' Tower a -> Tower a
f = [a] -> (a, a)
forall a. Num a => [a] -> (a, a)
d' ([a] -> (a, a)) -> (a -> [a]) -> a -> (a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tower a -> Tower a) -> a -> [a]
forall a. Num a => (Tower a -> Tower a) -> a -> [a]
diffs Tower a -> Tower a
f
{-# INLINE diff' #-}
du
:: (Functor f, Num a)
=> (f (Tower a) -> Tower a)
-> f (a, a) -> a
du :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f (a, a) -> a
du f (Tower a) -> Tower a
f = [a] -> a
forall a. Num a => [a] -> a
d ([a] -> a) -> (f (a, a) -> [a]) -> f (a, a) -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> (f (a, a) -> Tower a) -> f (a, a) -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> Tower a
f (f (Tower a) -> Tower a)
-> (f (a, a) -> f (Tower a)) -> f (a, a) -> Tower a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, a) -> Tower a) -> f (a, a) -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, a) -> Tower a
forall a. (a, a) -> Tower a
withD
{-# INLINE du #-}
du'
:: (Functor f, Num a)
=> (f (Tower a) -> Tower a)
-> f (a, a)
-> (a, a)
du' :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f (a, a) -> (a, a)
du' f (Tower a) -> Tower a
f = [a] -> (a, a)
forall a. Num a => [a] -> (a, a)
d' ([a] -> (a, a)) -> (f (a, a) -> [a]) -> f (a, a) -> (a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> (f (a, a) -> Tower a) -> f (a, a) -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> Tower a
f (f (Tower a) -> Tower a)
-> (f (a, a) -> f (Tower a)) -> f (a, a) -> Tower a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, a) -> Tower a) -> f (a, a) -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, a) -> Tower a
forall a. (a, a) -> Tower a
withD
{-# INLINE du' #-}
duF
:: (Functor f, Functor g, Num a)
=> (f (Tower a) -> g (Tower a))
-> f (a, a)
-> g a
duF :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f (a, a) -> g a
duF f (Tower a) -> g (Tower a)
f = (Tower a -> a) -> g (Tower a) -> g a
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a] -> a
forall a. Num a => [a] -> a
d ([a] -> a) -> (Tower a -> [a]) -> Tower a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tower a -> [a]
forall a. Tower a -> [a]
getADTower) (g (Tower a) -> g a)
-> (f (a, a) -> g (Tower a)) -> f (a, a) -> g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> g (Tower a)
f (f (Tower a) -> g (Tower a))
-> (f (a, a) -> f (Tower a)) -> f (a, a) -> g (Tower a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, a) -> Tower a) -> f (a, a) -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, a) -> Tower a
forall a. (a, a) -> Tower a
withD
{-# INLINE duF #-}
duF'
:: (Functor f, Functor g, Num a)
=> (f (Tower a) -> g (Tower a))
-> f (a, a)
-> g (a, a)
duF' :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f (a, a) -> g (a, a)
duF' f (Tower a) -> g (Tower a)
f = (Tower a -> (a, a)) -> g (Tower a) -> g (a, a)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a] -> (a, a)
forall a. Num a => [a] -> (a, a)
d' ([a] -> (a, a)) -> (Tower a -> [a]) -> Tower a -> (a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tower a -> [a]
forall a. Tower a -> [a]
getADTower) (g (Tower a) -> g (a, a))
-> (f (a, a) -> g (Tower a)) -> f (a, a) -> g (a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> g (Tower a)
f (f (Tower a) -> g (Tower a))
-> (f (a, a) -> f (Tower a)) -> f (a, a) -> g (Tower a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, a) -> Tower a) -> f (a, a) -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, a) -> Tower a
forall a. (a, a) -> Tower a
withD
{-# INLINE duF' #-}
dus
:: (Functor f, Num a)
=> (f (Tower a) -> Tower a)
-> f [a]
-> [a]
dus :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f [a] -> [a]
dus f (Tower a) -> Tower a
f = Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> (f [a] -> Tower a) -> f [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> Tower a
f (f (Tower a) -> Tower a)
-> (f [a] -> f (Tower a)) -> f [a] -> Tower a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> Tower a) -> f [a] -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Tower a
forall a. [a] -> Tower a
tower
{-# INLINE dus #-}
dus0
:: (Functor f, Num a)
=> (f (Tower a) -> Tower a)
-> f [a]
-> [a]
dus0 :: forall (f :: * -> *) a.
(Functor f, Num a) =>
(f (Tower a) -> Tower a) -> f [a] -> [a]
dus0 f (Tower a) -> Tower a
f = [a] -> [a]
forall a. Num a => [a] -> [a]
zeroPad ([a] -> [a]) -> (f [a] -> [a]) -> f [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tower a -> [a]
forall a. Tower a -> [a]
getADTower (Tower a -> [a]) -> (f [a] -> Tower a) -> f [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> Tower a
f (f (Tower a) -> Tower a)
-> (f [a] -> f (Tower a)) -> f [a] -> Tower a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> Tower a) -> f [a] -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Tower a
forall a. [a] -> Tower a
tower
{-# INLINE dus0 #-}
dusF
:: (Functor f, Functor g, Num a)
=> (f (Tower a) -> g (Tower a))
-> f [a]
-> g [a]
dusF :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f [a] -> g [a]
dusF f (Tower a) -> g (Tower a)
f = (Tower a -> [a]) -> g (Tower a) -> g [a]
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tower a -> [a]
forall a. Tower a -> [a]
getADTower (g (Tower a) -> g [a]) -> (f [a] -> g (Tower a)) -> f [a] -> g [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> g (Tower a)
f (f (Tower a) -> g (Tower a))
-> (f [a] -> f (Tower a)) -> f [a] -> g (Tower a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> Tower a) -> f [a] -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Tower a
forall a. [a] -> Tower a
tower
{-# INLINE dusF #-}
dus0F
:: (Functor f, Functor g, Num a)
=> (f (Tower a) -> g (Tower a))
-> f [a]
-> g [a]
dus0F :: forall (f :: * -> *) (g :: * -> *) a.
(Functor f, Functor g, Num a) =>
(f (Tower a) -> g (Tower a)) -> f [a] -> g [a]
dus0F f (Tower a) -> g (Tower a)
f = (Tower a -> [a]) -> g (Tower a) -> g [a]
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tower a -> [a]
forall a. Tower a -> [a]
getADTower (g (Tower a) -> g [a]) -> (f [a] -> g (Tower a)) -> f [a] -> g [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Tower a) -> g (Tower a)
f (f (Tower a) -> g (Tower a))
-> (f [a] -> f (Tower a)) -> f [a] -> g (Tower a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> Tower a) -> f [a] -> f (Tower a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Tower a
forall a. [a] -> Tower a
tower
{-# INLINE dus0F #-}