{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK show-extensions #-}

-- | Module    :  Prelude.InfBackprop
-- Copyright   :  (C) 2023 Alexey Tochin
-- License     :  BSD3 (see the file LICENSE)
-- Maintainer  :  Alexey Tochin <Alexey.Tochin@gmail.com>
--
-- Backpropagation differentiable versions of basic functions.
module Prelude.InfBackprop
  ( -- * Elementary functions
    linear,
    (+),
    (-),
    negate,
    (*),
    (/),

    -- * Tuple manipulations
    dup,
    setFirst,
    setSecond,
    forget,
    forgetFirst,
    forgetSecond,

    -- * Exponential family functions
    log,
    logBase,
    exp,
    (**),
    pow,

    -- * Trigonometric functions
    cos,
    sin,
    tan,
    asin,
    acos,
    atan,
    atan2,
    sinh,
    cosh,
    tanh,
    asinh,
    acosh,
    atanh,

    -- * Tools
    simpleDifferentiable,
  )
where

import Control.CatBifunctor (first, second, (***))
import Control.Category ((<<<), (>>>))
import InfBackprop.Common (Backprop (MkBackprop), BackpropFunc, const)
import IsomorphismClass.Isomorphism (iso)
import NumHask (Additive, Distributive, Divisive, ExpField, Subtractive, TrigField, fromInteger, zero)
import qualified NumHask as NH
import NumHask.Prelude (one)
import qualified NumHask.Prelude as NHP
import Prelude (flip, uncurry, ($), (==))
import qualified Prelude as P

-- | Returns a differentiable morphism given forward function and backpropagation derivative differential morphism.
--
-- ==== __Examples of usage__
--
-- >>> import qualified NumHask as NH
-- >>> cos = simpleDifferentiable NH.cos (sin >>> negate)
simpleDifferentiable :: forall x. Distributive x => (x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable :: forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
f BackpropFunc x x
df = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> x
call' BackpropFunc x (x, x)
forward' BackpropFunc (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x
f

    forward' :: BackpropFunc x (x, x)
    forward' :: BackpropFunc x (x, x)
forward' = forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
f BackpropFunc x x
df)

    backward' :: BackpropFunc (x, x) x
    backward' :: BackpropFunc (x, x) x
backward' = forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second BackpropFunc x x
df forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- Tuple manipulations

-- | Duplication differentiable operation.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call dup (42.0 :: Float)
-- (42.0,42.0)
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> derivative (dup >>> (*)) x
-- (1·x)+(1·x)
dup ::
  forall x.
  Additive x =>
  BackpropFunc x (x, x)
dup :: forall x. Additive x => BackpropFunc x (x, x)
dup = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> (x, x)
call' BackpropFunc x ((x, x), ())
forward' BackpropFunc ((x, x), ()) x
backward'
  where
    call' :: x -> (x, x)
    call' :: x -> (x, x)
call' x
x = (x
x, x
x)
    forward' :: BackpropFunc x ((x, x), ())
    forward' :: BackpropFunc x ((x, x), ())
forward' = forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc ((x, x), ()) x
    backward' :: BackpropFunc ((x, x), ()) x
backward' = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (y, ()) y) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Additive x => BackpropFunc (x, x) x
(+)

-- | Transforms any function to unit @()@.
-- It is not differentiable until @StartBackprop@ is defined for @()@.
-- However 'forget' is useful if need to remove some data in the differentiable pipeline.
--
-- ==== __Examples of usage__
--
-- >>> import InfBackprop (call, derivative)
--
-- >>> f = first forget >>> (iso :: BackpropFunc ((), a) a) :: Additive a => BackpropFunc (a, a) a
--
-- >>> call f (24, 42)
-- 42
--
-- >>> derivative f (24, 42)
-- (0,1)
forget ::
  forall x.
  Additive x =>
  BackpropFunc x ()
forget :: forall x. Additive x => BackpropFunc x ()
forget = forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const ()

-- | Remove the first element of a tuple.
--
-- ==== __Examples of usage__
--
-- >>> import InfBackprop (call, derivative)
--
-- >>> call forgetFirst (24, 42)
-- 42
--
-- >>> derivative forgetFirst (24, 42)
-- (0,1)
forgetFirst ::
  forall x y.
  Additive x =>
  BackpropFunc (x, y) y
forgetFirst :: forall x y. Additive x => BackpropFunc (x, y) y
forgetFirst = forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
<<< forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first forall x. Additive x => BackpropFunc x ()
forget

-- | Remove the second element of a tuple.
--
-- ==== __Examples of usage__
--
-- >>> import InfBackprop (call, derivative)
--
-- >>> call forgetSecond (24, 42)
-- 24
--
-- >>> derivative forgetSecond (24, 42)
-- (1,0)
forgetSecond ::
  forall x y.
  Additive y =>
  BackpropFunc (x, y) x
forgetSecond :: forall x y. Additive y => BackpropFunc (x, y) x
forgetSecond = forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
<<< forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second forall x. Additive x => BackpropFunc x ()
forget

-- | Transforms a 2-argument differentiable function into a single argument function by fixing its first argument.
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call (setFirst 8 (/)) 4 :: Float
-- 2.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> y = variable "y"
-- >>> derivative (setFirst x (*)) y
-- 1·x
setFirst ::
  forall x y c.
  Additive c =>
  c ->
  BackpropFunc (c, x) y ->
  BackpropFunc x y
setFirst :: forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst c
c BackpropFunc (c, x) y
f = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc x ((), x)) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const c
c) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> BackpropFunc (c, x) y
f

-- | Transforms a 2-argument differentiable function into a single argument function by fixing its second argument.
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call (setSecond 4 (/)) 8 :: Float
-- 2.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> y = variable "y"
-- >>> derivative (setSecond y (*)) x
-- 1·y
setSecond ::
  forall x y c.
  Additive c =>
  c ->
  BackpropFunc (x, c) y ->
  BackpropFunc x y
setSecond :: forall x y c.
Additive c =>
c -> BackpropFunc (x, c) y -> BackpropFunc x y
setSecond c
c BackpropFunc (x, c) y
f = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc x (x, ())) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const c
c) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> BackpropFunc (x, c) y
f

-- Elementary functions

-- | Linear differentiable function.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (fmap, Float)
-- >>> import InfBackprop (pow, call, derivative)
-- >>> myFunc = linear 2 :: BackpropFunc Float Float
--
-- >>> f = call myFunc :: Float -> Float
-- >>> fmap f [-3, -2, -1, 0, 1, 2, 3]
-- [-6.0,-4.0,-2.0,0.0,2.0,4.0,6.0]
--
-- >>> df = derivative myFunc :: Float -> Float
-- >>> fmap df [-3, -2, -1, 0, 1, 2, 3]
-- [2.0,2.0,2.0,2.0,2.0,2.0,2.0]
linear ::
  forall x.
  NH.Distributive x =>
  x ->
  BackpropFunc x x
linear :: forall x. Distributive x => x -> BackpropFunc x x
linear x
c = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> x
call' BackpropFunc x (x, ())
forward' BackpropFunc (x, ()) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x -> x
f x
c
      where
        f :: x -> x -> x
f = forall a. Multiplicative a => a -> a -> a
(NH.*)
    forward' :: BackpropFunc x (x, ())
    forward' :: BackpropFunc x (x, ())
forward' = forall x. Distributive x => x -> BackpropFunc x x
linear x
c forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) x
    backward' :: BackpropFunc (x, ()) x
backward' = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (x, ()) x) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => x -> BackpropFunc x x
linear x
c

-- | Summation differentiable binary operation.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
--
-- >>> call (+) (2, 3) :: Float
-- 5.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> y = variable "y"
-- >>> derivative (+) (x, y)
-- (1,1)
(+) ::
  forall x.
  Additive x =>
  BackpropFunc (x, x) x
+ :: forall x. Additive x => BackpropFunc (x, x) x
(+) = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop (x, x) -> x
call' BackpropFunc (x, x) (x, ())
forward' BackpropFunc (x, ()) (x, x)
backward'
  where
    call' :: (x, x) -> x
    call' :: (x, x) -> x
call' = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Additive a => a -> a -> a
(NH.+)
    forward' :: BackpropFunc (x, x) (x, ())
    forward' :: BackpropFunc (x, x) (x, ())
forward' = forall x. Additive x => BackpropFunc (x, x) x
(+) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) (x, x)
    backward' :: BackpropFunc (x, ()) (x, x)
backward' = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (x, ()) x) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Additive x => BackpropFunc x (x, x)
dup

-- | Negate differentiable function.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float, ($))
-- >>> import InfBackprop (call, derivative)
--
-- >>> call negate 42 :: Float
-- -42.0
--
-- >>> derivative negate 42 :: Float
-- -1.0
negate ::
  forall x.
  Subtractive x =>
  BackpropFunc x x
negate :: forall x. Subtractive x => BackpropFunc x x
negate = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> x
call' BackpropFunc x (x, ())
forward' BackpropFunc (x, ()) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = forall a. Subtractive a => a -> a
NH.negate
    forward' :: BackpropFunc x (x, ())
    forward' :: BackpropFunc x (x, ())
forward' = forall x. Subtractive x => BackpropFunc x x
negate forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) x
    backward' :: BackpropFunc (x, ()) x
backward' = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (y, ()) y) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Subtractive x => BackpropFunc x x
negate

-- | Subtraction differentiable binary operation.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
--
-- >>> call (-) (5, 3) :: Float
-- 2.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> y = variable "y"
-- >>> derivative (-) (x, y)
-- (1,-(1))
(-) :: forall x. (Subtractive x) => BackpropFunc (x, x) x
(-) = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop (x, x) -> x
call' BackpropFunc (x, x) (x, ())
forward' BackpropFunc (x, ()) (x, x)
backward'
  where
    call' :: (x, x) -> x
    call' :: (x, x) -> x
call' = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Subtractive a => a -> a -> a
(NH.-)
    forward' :: BackpropFunc (x, x) (x, ())
    forward' :: BackpropFunc (x, x) (x, ())
forward' = (-) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) (x, x)
    backward' :: BackpropFunc (x, ()) (x, x)
backward' = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (x, ()) x) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second forall x. Subtractive x => BackpropFunc x x
negate

-- | Product binnary operation
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call (*) (2, 3) :: Float
-- 6.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> y = variable "y"
-- >>> derivative (*) (x, y)
-- (1·y,1·x)
(*) :: Distributive x => BackpropFunc (x, x) x
* :: forall x. Distributive x => BackpropFunc (x, x) x
(*) = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop forall x. Distributive x => (x, x) -> x
call' forall x. Distributive x => BackpropFunc (x, x) (x, (x, x))
forward' forall x. Distributive x => BackpropFunc (x, (x, x)) (x, x)
backward'
  where
    call' :: Distributive x => (x, x) -> x
    call' :: forall x. Distributive x => (x, x) -> x
call' = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Multiplicative a => a -> a -> a
(NH.*)
    forward' :: Distributive x => BackpropFunc (x, x) (x, (x, x))
    forward' :: forall x. Distributive x => BackpropFunc (x, x) (x, (x, x))
forward' = forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first forall x. Distributive x => BackpropFunc (x, x) x
(*)
    backward' :: Distributive x => BackpropFunc (x, (x, x)) (x, x)
    backward' :: forall x. Distributive x => BackpropFunc (x, (x, x)) (x, x)
backward' =
      forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first forall x. Additive x => BackpropFunc x (x, x)
dup
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((dy, dy), (x1, x2)) ((dy, x1), (dy, x2)))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (a, b) (b, a))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*) forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- | Square differentiable operation
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call square 3 :: Float
-- 9.0
--
-- >>> derivative square 3 :: Float
-- 6.0
square :: Distributive x => BackpropFunc x x
square :: forall x. Distributive x => BackpropFunc x x
square = forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- | Division binary differentiable operation
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call (/) (6, 3) :: Float
-- 2.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> y = variable "y"
-- >>> derivative (/) (x, y)
-- (1·(1/y),1·(-(x)·(1/(y·y))))
(/) ::
  forall x.
  (Divisive x, Distributive x, Subtractive x) =>
  BackpropFunc (x, x) x
/ :: forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/) = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop (x, x) -> x
call' BackpropFunc (x, x) (x, (x, x))
forward' BackpropFunc (x, (x, x)) (x, x)
backward'
  where
    call' :: (x, x) -> x
    call' :: (x, x) -> x
call' = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Divisive a => a -> a -> a
(NH./)
    forward' :: BackpropFunc (x, x) (x, (x, x))
    forward' :: BackpropFunc (x, x) (x, (x, x))
forward' = forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/)
    backward' :: BackpropFunc (x, (x, x)) (x, x)
    backward' :: BackpropFunc (x, (x, x)) (x, x)
backward' =
      forall x. Additive x => BackpropFunc x (x, x)
dup forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** forall x. Additive x => BackpropFunc x (x, x)
dup
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (Backprop (->) (x, x) x
d1 forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** Backprop (->) (x, x) x
d2) -- ((dy, dy), ((x1, x2), (x1, x2)))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((dy, dy), (x1, x2)) ((dy, x1), (dy, x2))) -- ((dy, dy), (1 / x2, - x1 * x2^(-2) ))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*) forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** forall x. Distributive x => BackpropFunc (x, x) x
(*)
      where
        d1 :: Backprop (->) (x, x) x
d1 = (forall x. Additive x => BackpropFunc x ()
forget forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((), x) x) -- (x1, x2) -> 1 / x2
        d2 :: Backprop (->) (x, x) x
d2 = (forall x. Subtractive x => BackpropFunc x x
negate forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*) -- (x1, x2) -> - x1 * x2^(-2)

-- | The recip differentiable operation
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call recip 2 :: Float
-- 0.5
--
-- >>> derivative recip 2 :: Float
-- -0.25
recip ::
  forall x.
  (Divisive x, Distributive x, Subtractive x) =>
  BackpropFunc x x
recip :: forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip = forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst forall a. Multiplicative a => a
NH.one forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/)

-- | Integer power differentiable operation
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call (pow 3) 2 :: Float
-- 8.0
--
-- >>> derivative (pow 3) 2 :: Float
-- 12.0
pow ::
  forall x.
  ( Divisive x,
    Distributive x,
    Subtractive x,
    NH.FromIntegral x NHP.Integer
  ) =>
  NHP.Integer ->
  BackpropFunc x x
pow :: forall x.
(Divisive x, Distributive x, Subtractive x,
 FromIntegral x Integer) =>
Integer -> BackpropFunc x x
pow Integer
n = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> x
call' BackpropFunc x (x, x)
forward' BackpropFunc (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Divisive a => a -> Int -> a
(NH.^) (forall a. FromInteger a => Integer -> a
fromInteger Integer
n)
    forward' :: BackpropFunc x (x, x)
    forward' :: BackpropFunc x (x, x)
forward' = forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (forall x.
(Divisive x, Distributive x, Subtractive x,
 FromIntegral x Integer) =>
Integer -> BackpropFunc x x
pow Integer
n :: BackpropFunc x x)
    backward' :: BackpropFunc (x, x) x
    backward' :: BackpropFunc (x, x) x
backward' = forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second BackpropFunc x x
der forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*)
      where
        der :: BackpropFunc x x
der =
          if Integer
n forall a. Eq a => a -> a -> Bool
== Integer
0
            then forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const forall a. Additive a => a
zero
            else forall x.
(Divisive x, Distributive x, Subtractive x,
 FromIntegral x Integer) =>
Integer -> BackpropFunc x x
pow (Integer
n forall a. Num a => a -> a -> a
P.- Integer
1) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => x -> BackpropFunc x x
linear (forall a b. FromIntegral a b => b -> a
NH.fromIntegral Integer
n)

-- | Square root differentiable function.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call sqrt 16 :: Float
-- 4.0
--
-- >>> derivative sqrt 16 :: Float
-- 0.125
sqrt ::
  forall x.
  ExpField x =>
  BackpropFunc x x
sqrt :: forall x. ExpField x => BackpropFunc x x
sqrt = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> x
call' BackpropFunc x (x, x)
forward' BackpropFunc (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = forall a. ExpField a => a -> a
NH.sqrt
    forward' :: BackpropFunc x (x, x)
    forward' :: BackpropFunc x (x, x)
forward' = (forall x. ExpField x => BackpropFunc x x
sqrt :: BackpropFunc x x) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Additive x => BackpropFunc x (x, x)
dup
    backward' :: BackpropFunc (x, x) x
    backward' :: BackpropFunc (x, x) x
backward' = forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => x -> BackpropFunc x x
linear forall a. Field a => a
NH.half) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- | Power binary differentiable operation.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import NumHask (half)
-- >>> import InfBackprop (call, derivative)
-- >>> call (**) (0.5, 9) :: Float
-- 3.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> n = variable "n"
-- >>> derivative (**) (n, x)
-- (1·(n·(x^(n-1))),1·((x^n)·log(x)))
(**) ::
  forall a.
  ( ExpField a,
    NH.FromIntegral a P.Integer
  ) =>
  BackpropFunc (a, a) a
** :: forall a.
(ExpField a, FromIntegral a Integer) =>
BackpropFunc (a, a) a
(**) = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop (a, a) -> a
call' BackpropFunc (a, a) (a, (a, (a, a)))
forward' BackpropFunc (a, (a, (a, a))) (a, a)
backward'
  where
    call' :: (a, a) -> a
    call' :: (a, a) -> a
call' = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. ExpField a => a -> a -> a
(NH.**)
    forward' :: BackpropFunc (a, a) (a, (a, (a, a)))
    forward' :: BackpropFunc (a, a) (a, (a, (a, a)))
forward' =
      forall x. Additive x => BackpropFunc x (x, x)
dup -- ((n, x), (n, x))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (forall a.
(ExpField a, FromIntegral a Integer) =>
BackpropFunc (a, a) a
(**) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Additive x => BackpropFunc x (x, x)
dup) -- ((x^n, x^n), (n, x))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((a, b), c) (a, (b, c))) -- (x^n, (x^n, (n, x)))
    backward' :: BackpropFunc (a, (a, (a, a))) (a, a)
    backward' :: BackpropFunc (a, (a, (a, a))) (a, a)
backward' =
      forall x. Additive x => BackpropFunc x (x, x)
dup forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** (forall x. Additive x => BackpropFunc x (x, x)
dup forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (BackpropFunc (a, (a, a)) a
dn forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** BackpropFunc (a, (a, a)) a
dx)) -- ((dy, dy), (dn, dx))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((a, b), (c, d)) ((a, c), (b, d))) -- ((dy, dn), (dy, dx))
        forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*) forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** forall x. Distributive x => BackpropFunc (x, x) x
(*)
      where
        -- (x^n, (n, x)) -> n * x^(n-1)
        dn :: BackpropFunc (a, (a, a)) a
        dn :: BackpropFunc (a, (a, a)) a
dn =
          forall x y. Additive x => BackpropFunc (x, y) y
forgetFirst -- (n, x)
            forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first forall x. Additive x => BackpropFunc x (x, x)
dup -- ((n, n), x)
            forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((a, b), c) (a, (b, c))) -- (n, (n, x))
            forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (forall x y c.
Additive c =>
c -> BackpropFunc (x, c) y -> BackpropFunc x y
setSecond (forall a b. FromIntegral a b => b -> a
NH.fromIntegral (Integer
1 :: P.Integer)) (-))) -- (n, (n-1, x))
            forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second forall a.
(ExpField a, FromIntegral a Integer) =>
BackpropFunc (a, a) a
(**) -- (n, x^(n-1))
            forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*) -- (n * x^(n-1))
            -- (x^n, (n, x)) -> log x * x^n
        dx :: BackpropFunc (a, (a, a)) a
        dx :: BackpropFunc (a, (a, a)) a
dx = forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second forall x y. Additive x => BackpropFunc (x, y) y
forgetFirst forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second forall x. ExpField x => BackpropFunc x x
log forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- | Natural logarithm differentiable function.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call log 10 :: Float
-- 2.3025851
--
-- >>> derivative log 10 :: Float
-- 0.1
log :: ExpField x => BackpropFunc x x
log :: forall x. ExpField x => BackpropFunc x x
log = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. ExpField a => a -> a
NH.log forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip

-- | Natural logarithm differentiable function.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call logBase (2, 8) :: Float
-- 3.0
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> n = variable "n"
-- >>> derivative logBase (n, x)
-- ((1·(-(log(x))·(1/(log(n)·log(n)))))·(1/n),(1·(1/log(n)))·(1/x))
logBase :: ExpField a => BackpropFunc (a, a) a
logBase :: forall a. ExpField a => BackpropFunc (a, a) a
logBase = (forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (c, d) (d, c)) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. ExpField x => BackpropFunc x x
log forall (p :: * -> * -> *) (cat :: * -> * -> *) a1 b1 a2 b2.
CatBiFunctor p cat =>
cat a1 b1 -> cat a2 b2 -> cat (p a1 a2) (p b1 b2)
*** forall x. ExpField x => BackpropFunc x x
log forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/)

-- | Natural logarithm differentiable function.
--
-- ==== __Examples of usage__
--
-- >>> import Prelude (Float)
-- >>> import InfBackprop (call, derivative)
-- >>> call exp 2
-- 7.38905609893065
--
-- >>> import Debug.SimpleExpr.Expr (variable)
-- >>> x = variable "x"
-- >>> derivative exp x
-- 1·exp(x)
exp :: forall x. ExpField x => BackpropFunc x x
exp :: forall x. ExpField x => BackpropFunc x x
exp = forall (cat :: * -> * -> *) input output cache.
cat input output
-> Backprop cat input (output, cache)
-> Backprop cat (output, cache) input
-> Backprop cat input output
MkBackprop x -> x
call' BackpropFunc x (x, x)
forward' BackpropFunc (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = forall a. ExpField a => a -> a
NH.exp
    forward' :: BackpropFunc x (x, x)
    forward' :: BackpropFunc x (x, x)
forward' = (forall x. ExpField x => BackpropFunc x x
exp :: BackpropFunc x x) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Additive x => BackpropFunc x (x, x)
dup
    backward' :: BackpropFunc (x, x) x
    backward' :: BackpropFunc (x, x) x
backward' = forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- Trigonometric

-- | Sine differentiable function
sin :: TrigField x => BackpropFunc x x
sin :: forall x. TrigField x => BackpropFunc x x
sin = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.sin forall x. TrigField x => BackpropFunc x x
cos

-- | Cosine differentiable function.
cos :: TrigField x => BackpropFunc x x
cos :: forall x. TrigField x => BackpropFunc x x
cos = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.cos (forall x. TrigField x => BackpropFunc x x
sin forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Subtractive x => BackpropFunc x x
negate)

-- | Tangent differentiable function.
tan :: TrigField x => BackpropFunc x x
tan :: forall x. TrigField x => BackpropFunc x x
tan = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.tan (forall x. TrigField x => BackpropFunc x x
cos forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Arcsine differentiable function.
asin :: (TrigField x, ExpField x) => BackpropFunc x x
asin :: forall x. (TrigField x, ExpField x) => BackpropFunc x x
asin = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.tan (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst forall a. Multiplicative a => a
one (-) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. ExpField x => BackpropFunc x x
sqrt forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Arccosine differentiable function.
acos :: (TrigField x, ExpField x) => BackpropFunc x x
acos :: forall x. (TrigField x, ExpField x) => BackpropFunc x x
acos = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.tan (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst forall a. Multiplicative a => a
one (-) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. ExpField x => BackpropFunc x x
sqrt forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Subtractive x => BackpropFunc x x
negate)

-- | Arctangent differentiable function.
atan :: TrigField x => BackpropFunc x x
atan :: forall x. TrigField x => BackpropFunc x x
atan = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.atan (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst forall a. Multiplicative a => a
one forall x. Additive x => BackpropFunc (x, x) x
(+) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | 2-argument arctangent differentiable function.
atan2 :: TrigField a => BackpropFunc (a, a) a
atan2 :: forall a. TrigField a => BackpropFunc (a, a) a
atan2 = forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. TrigField x => BackpropFunc x x
atan

-- | Hyperbolic sine differentiable function.
sinh :: TrigField x => BackpropFunc x x
sinh :: forall x. TrigField x => BackpropFunc x x
sinh = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.sinh forall x. TrigField x => BackpropFunc x x
cosh

-- | Hyperbolic cosine differentiable function.
cosh :: TrigField x => BackpropFunc x x
cosh :: forall x. TrigField x => BackpropFunc x x
cosh = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.cosh forall x. TrigField x => BackpropFunc x x
sinh

-- | Hyperbolic tanget differentiable function.
tanh :: TrigField x => BackpropFunc x x
tanh :: forall x. TrigField x => BackpropFunc x x
tanh = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.tanh (forall x. TrigField x => BackpropFunc x x
cosh forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Hyperbolic arcsine differentiable function.
asinh :: (TrigField x, ExpField x) => BackpropFunc x x
asinh :: forall x. (TrigField x, ExpField x) => BackpropFunc x x
asinh = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.asinh (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst forall a. Multiplicative a => a
one forall x. Additive x => BackpropFunc (x, x) x
(+) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. ExpField x => BackpropFunc x x
sqrt forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Hyperbolic arccosine differentiable function.
acosh :: (TrigField x, ExpField x) => BackpropFunc x x
acosh :: forall x. (TrigField x, ExpField x) => BackpropFunc x x
acosh = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.tan (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x y c.
Additive c =>
c -> BackpropFunc (x, c) y -> BackpropFunc x y
setSecond forall a. Multiplicative a => a
one (-) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x. ExpField x => BackpropFunc x x
sqrt forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Hyperbolic arctangent differentiable function.
atanh :: TrigField x => BackpropFunc x x
atanh :: forall x. TrigField x => BackpropFunc x x
atanh = forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable forall a. TrigField a => a -> a
NH.tan (forall x. Distributive x => BackpropFunc x x
square forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst forall a. Multiplicative a => a
one (-) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)