module Numeric.AD.Rank1.Forward.Double
( ForwardDouble
, grad
, grad'
, gradWith
, gradWith'
, jacobian
, jacobian'
, jacobianWith
, jacobianWith'
, jacobianT
, jacobianWithT
, diff
, diff'
, diffF
, diffF'
, du
, du'
, duF
, duF'
) where
import Numeric.AD.Mode
import Numeric.AD.Internal.Forward.Double
du
:: Functor f
=> (f ForwardDouble -> ForwardDouble)
-> f (Double, Double)
-> Double
du :: forall (f :: * -> *).
Functor f =>
(f ForwardDouble -> ForwardDouble) -> f (Double, Double) -> Double
du f ForwardDouble -> ForwardDouble
f = ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> ForwardDouble
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Double -> Double -> ForwardDouble
bundle)
{-# INLINE du #-}
du'
:: Functor f
=> (f ForwardDouble -> ForwardDouble)
-> f (Double, Double)
-> (Double, Double)
du' :: forall (f :: * -> *).
Functor f =>
(f ForwardDouble -> ForwardDouble)
-> f (Double, Double) -> (Double, Double)
du' f ForwardDouble -> ForwardDouble
f = ForwardDouble -> (Double, Double)
unbundle forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> ForwardDouble
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Double -> Double -> ForwardDouble
bundle)
{-# INLINE du' #-}
duF
:: (Functor f, Functor g)
=> (f ForwardDouble -> g ForwardDouble)
-> f (Double, Double)
-> g Double
duF :: forall (f :: * -> *) (g :: * -> *).
(Functor f, Functor g) =>
(f ForwardDouble -> g ForwardDouble)
-> f (Double, Double) -> g Double
duF f ForwardDouble -> g ForwardDouble
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> g ForwardDouble
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Double -> Double -> ForwardDouble
bundle)
{-# INLINE duF #-}
duF'
:: (Functor f, Functor g)
=> (f ForwardDouble -> g ForwardDouble)
-> f (Double, Double)
-> g (Double, Double)
duF' :: forall (f :: * -> *) (g :: * -> *).
(Functor f, Functor g) =>
(f ForwardDouble -> g ForwardDouble)
-> f (Double, Double) -> g (Double, Double)
duF' f ForwardDouble -> g ForwardDouble
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForwardDouble -> (Double, Double)
unbundle forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> g ForwardDouble
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Double -> Double -> ForwardDouble
bundle)
{-# INLINE duF' #-}
diff
:: (ForwardDouble -> ForwardDouble)
-> Double
-> Double
diff :: (ForwardDouble -> ForwardDouble) -> Double -> Double
diff ForwardDouble -> ForwardDouble
f Double
a = ForwardDouble -> Double
tangent forall a b. (a -> b) -> a -> b
$ forall b. (ForwardDouble -> b) -> Double -> b
apply ForwardDouble -> ForwardDouble
f Double
a
{-# INLINE diff #-}
diff'
:: (ForwardDouble -> ForwardDouble)
-> Double
-> (Double, Double)
diff' :: (ForwardDouble -> ForwardDouble) -> Double -> (Double, Double)
diff' ForwardDouble -> ForwardDouble
f Double
a = ForwardDouble -> (Double, Double)
unbundle forall a b. (a -> b) -> a -> b
$ forall b. (ForwardDouble -> b) -> Double -> b
apply ForwardDouble -> ForwardDouble
f Double
a
{-# INLINE diff' #-}
diffF
:: Functor f
=> (ForwardDouble -> f ForwardDouble)
-> Double
-> f Double
diffF :: forall (f :: * -> *).
Functor f =>
(ForwardDouble -> f ForwardDouble) -> Double -> f Double
diffF ForwardDouble -> f ForwardDouble
f Double
a = ForwardDouble -> Double
tangent forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall b. (ForwardDouble -> b) -> Double -> b
apply ForwardDouble -> f ForwardDouble
f Double
a
{-# INLINE diffF #-}
diffF'
:: Functor f
=> (ForwardDouble -> f ForwardDouble)
-> Double
-> f (Double, Double)
diffF' :: forall (f :: * -> *).
Functor f =>
(ForwardDouble -> f ForwardDouble) -> Double -> f (Double, Double)
diffF' ForwardDouble -> f ForwardDouble
f Double
a = ForwardDouble -> (Double, Double)
unbundle forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall b. (ForwardDouble -> b) -> Double -> b
apply ForwardDouble -> f ForwardDouble
f Double
a
{-# INLINE diffF' #-}
jacobianT
:: (Traversable f, Functor g)
=> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> f (g Double)
jacobianT :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Functor g) =>
(f ForwardDouble -> g ForwardDouble) -> f Double -> f (g Double)
jacobianT f ForwardDouble -> g ForwardDouble
f = forall (f :: * -> *) b.
Traversable f =>
(f ForwardDouble -> b) -> f Double -> f b
bind (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> g ForwardDouble
f)
{-# INLINE jacobianT #-}
jacobianWithT
:: (Traversable f, Functor g)
=> (Double -> Double -> b)
-> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> f (g b)
jacobianWithT :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Functor g) =>
(Double -> Double -> b)
-> (f ForwardDouble -> g ForwardDouble) -> f Double -> f (g b)
jacobianWithT Double -> Double -> b
g f ForwardDouble -> g ForwardDouble
f = forall (f :: * -> *) b c.
Traversable f =>
(Double -> b -> c) -> (f ForwardDouble -> b) -> f Double -> f c
bindWith forall {f :: * -> *}. Functor f => Double -> f ForwardDouble -> f b
g' f ForwardDouble -> g ForwardDouble
f where
g' :: Double -> f ForwardDouble -> f b
g' Double
a f ForwardDouble
ga = Double -> Double -> b
g Double
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForwardDouble -> Double
tangent forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ForwardDouble
ga
{-# INLINE jacobianWithT #-}
jacobian
:: (Traversable f, Traversable g)
=> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> g (f Double)
jacobian :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Traversable g) =>
(f ForwardDouble -> g ForwardDouble) -> f Double -> g (f Double)
jacobian f ForwardDouble -> g ForwardDouble
f f Double
as = forall (f :: * -> *) (g :: * -> *) b a c.
(Functor f, Foldable f, Traversable g) =>
(b -> f a -> c) -> f (g a) -> g b -> g c
transposeWith (forall a b. a -> b -> a
const forall a. a -> a
id) f (g Double)
t g Double
p where
(g Double
p, f (g Double)
t) = forall (f :: * -> *) b.
Traversable f =>
(f ForwardDouble -> b) -> f Double -> (b, f b)
bind' (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> g ForwardDouble
f) f Double
as
{-# INLINE jacobian #-}
jacobianWith
:: (Traversable f, Traversable g)
=> (Double -> Double -> b)
-> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> g (f b)
jacobianWith :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Traversable g) =>
(Double -> Double -> b)
-> (f ForwardDouble -> g ForwardDouble) -> f Double -> g (f b)
jacobianWith Double -> Double -> b
g f ForwardDouble -> g ForwardDouble
f f Double
as = forall (f :: * -> *) (g :: * -> *) b a c.
(Functor f, Foldable f, Traversable g) =>
(b -> f a -> c) -> f (g a) -> g b -> g c
transposeWith (forall a b. a -> b -> a
const forall a. a -> a
id) f (g b)
t g ForwardDouble
p where
(g ForwardDouble
p, f (g b)
t) = forall (f :: * -> *) b c.
Traversable f =>
(Double -> b -> c)
-> (f ForwardDouble -> b) -> f Double -> (b, f c)
bindWith' forall {f :: * -> *}. Functor f => Double -> f ForwardDouble -> f b
g' f ForwardDouble -> g ForwardDouble
f f Double
as
g' :: Double -> f ForwardDouble -> f b
g' Double
a f ForwardDouble
ga = Double -> Double -> b
g Double
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForwardDouble -> Double
tangent forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ForwardDouble
ga
{-# INLINE jacobianWith #-}
jacobian'
:: (Traversable f, Traversable g)
=> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> g (Double, f Double)
jacobian' :: forall (f :: * -> *) (g :: * -> *).
(Traversable f, Traversable g) =>
(f ForwardDouble -> g ForwardDouble)
-> f Double -> g (Double, f Double)
jacobian' f ForwardDouble -> g ForwardDouble
f f Double
as = forall (f :: * -> *) (g :: * -> *) b a c.
(Functor f, Foldable f, Traversable g) =>
(b -> f a -> c) -> f (g a) -> g b -> g c
transposeWith forall {f :: * -> *}.
Functor f =>
ForwardDouble -> f ForwardDouble -> (Double, f Double)
row f (g ForwardDouble)
t g ForwardDouble
p where
(g ForwardDouble
p, f (g ForwardDouble)
t) = forall (f :: * -> *) b.
Traversable f =>
(f ForwardDouble -> b) -> f Double -> (b, f b)
bind' f ForwardDouble -> g ForwardDouble
f f Double
as
row :: ForwardDouble -> f ForwardDouble -> (Double, f Double)
row ForwardDouble
x f ForwardDouble
as' = (ForwardDouble -> Double
primal ForwardDouble
x, ForwardDouble -> Double
tangent forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ForwardDouble
as')
{-# INLINE jacobian' #-}
jacobianWith'
:: (Traversable f, Traversable g)
=> (Double -> Double -> b)
-> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> g (Double, f b)
jacobianWith' :: forall (f :: * -> *) (g :: * -> *) b.
(Traversable f, Traversable g) =>
(Double -> Double -> b)
-> (f ForwardDouble -> g ForwardDouble)
-> f Double
-> g (Double, f b)
jacobianWith' Double -> Double -> b
g f ForwardDouble -> g ForwardDouble
f f Double
as = forall (f :: * -> *) (g :: * -> *) b a c.
(Functor f, Foldable f, Traversable g) =>
(b -> f a -> c) -> f (g a) -> g b -> g c
transposeWith forall {b}. ForwardDouble -> b -> (Double, b)
row f (g b)
t g ForwardDouble
p where
(g ForwardDouble
p, f (g b)
t) = forall (f :: * -> *) b c.
Traversable f =>
(Double -> b -> c)
-> (f ForwardDouble -> b) -> f Double -> (b, f c)
bindWith' forall {f :: * -> *}. Functor f => Double -> f ForwardDouble -> f b
g' f ForwardDouble -> g ForwardDouble
f f Double
as
row :: ForwardDouble -> b -> (Double, b)
row ForwardDouble
x b
as' = (ForwardDouble -> Double
primal ForwardDouble
x, b
as')
g' :: Double -> f ForwardDouble -> f b
g' Double
a f ForwardDouble
ga = Double -> Double -> b
g Double
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. ForwardDouble -> Double
tangent forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ForwardDouble
ga
{-# INLINE jacobianWith' #-}
grad
:: Traversable f
=> (f ForwardDouble -> ForwardDouble)
-> f Double
-> f Double
grad :: forall (f :: * -> *).
Traversable f =>
(f ForwardDouble -> ForwardDouble) -> f Double -> f Double
grad f ForwardDouble -> ForwardDouble
f = forall (f :: * -> *) b.
Traversable f =>
(f ForwardDouble -> b) -> f Double -> f b
bind (ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> ForwardDouble
f)
{-# INLINE grad #-}
grad'
:: Traversable f
=> (f ForwardDouble -> ForwardDouble)
-> f Double
-> (Double, f Double)
grad' :: forall (f :: * -> *).
Traversable f =>
(f ForwardDouble -> ForwardDouble)
-> f Double -> (Double, f Double)
grad' f ForwardDouble -> ForwardDouble
f f Double
as = (ForwardDouble -> Double
primal ForwardDouble
b, ForwardDouble -> Double
tangent forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ForwardDouble
bs)
where
(ForwardDouble
b, f ForwardDouble
bs) = forall (f :: * -> *) b.
Traversable f =>
(f ForwardDouble -> b) -> f Double -> (b, f b)
bind' f ForwardDouble -> ForwardDouble
f f Double
as
{-# INLINE grad' #-}
gradWith
:: Traversable f
=> (Double -> Double -> b)
-> (f ForwardDouble -> ForwardDouble)
-> f Double
-> f b
gradWith :: forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f ForwardDouble -> ForwardDouble) -> f Double -> f b
gradWith Double -> Double -> b
g f ForwardDouble -> ForwardDouble
f = forall (f :: * -> *) b c.
Traversable f =>
(Double -> b -> c) -> (f ForwardDouble -> b) -> f Double -> f c
bindWith Double -> Double -> b
g (ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> ForwardDouble
f)
{-# INLINE gradWith #-}
gradWith'
:: Traversable f
=> (Double -> Double -> b)
-> (f ForwardDouble -> ForwardDouble)
-> f Double
-> (Double, f b)
gradWith' :: forall (f :: * -> *) b.
Traversable f =>
(Double -> Double -> b)
-> (f ForwardDouble -> ForwardDouble) -> f Double -> (Double, f b)
gradWith' Double -> Double -> b
g f ForwardDouble -> ForwardDouble
f f Double
as = (ForwardDouble -> Double
primal forall a b. (a -> b) -> a -> b
$ f ForwardDouble -> ForwardDouble
f (forall t. Mode t => Scalar t -> t
auto forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f Double
as), forall (f :: * -> *) b c.
Traversable f =>
(Double -> b -> c) -> (f ForwardDouble -> b) -> f Double -> f c
bindWith Double -> Double -> b
g (ForwardDouble -> Double
tangent forall b c a. (b -> c) -> (a -> b) -> a -> c
. f ForwardDouble -> ForwardDouble
f) f Double
as)
{-# INLINE gradWith' #-}