{-# 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 :: (x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
f BackpropFunc x x
df = (x -> x)
-> Backprop (->) x (x, x)
-> Backprop (->) (x, x) x
-> BackpropFunc 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
call' Backprop (->) x (x, x)
forward' Backprop (->) (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x
f

    forward' :: BackpropFunc x (x, x)
    forward' :: Backprop (->) x (x, x)
forward' = Backprop (->) x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup Backprop (->) x (x, x)
-> Backprop (->) (x, x) (x, x) -> Backprop (->) 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
>>> BackpropFunc x x -> Backprop (->) (x, x) (x, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first ((x -> x) -> BackpropFunc x x -> BackpropFunc x x
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' :: Backprop (->) (x, x) x
backward' = BackpropFunc x x -> Backprop (->) (x, x) (x, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second BackpropFunc x x
df Backprop (->) (x, x) (x, x)
-> Backprop (->) (x, x) x -> Backprop (->) (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
>>> Backprop (->) (x, x) x
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 :: BackpropFunc x (x, x)
dup = (x -> (x, x))
-> Backprop (->) x ((x, x), ())
-> Backprop (->) ((x, x), ()) 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' Backprop (->) x ((x, x), ())
forward' Backprop (->) ((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' :: Backprop (->) x ((x, x), ())
forward' = BackpropFunc x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup BackpropFunc x (x, x)
-> Backprop (->) (x, x) ((x, x), ())
-> Backprop (->) 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 y. Backprop (->) y (y, ())
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc ((x, x), ()) x
    backward' :: Backprop (->) ((x, x), ()) x
backward' = (forall y. Backprop (->) (y, ()) y
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (y, ()) y) Backprop (->) ((x, x), ()) (x, x)
-> Backprop (->) (x, x) x -> Backprop (->) ((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
>>> Backprop (->) (x, x) x
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 :: BackpropFunc x ()
forget = () -> BackpropFunc x ()
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 :: BackpropFunc (x, y) y
forgetFirst = Backprop (->) ((), y) y
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso Backprop (->) ((), y) y
-> Backprop (->) (x, y) ((), y) -> BackpropFunc (x, y) y
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
<<< Backprop (->) x () -> Backprop (->) (x, y) ((), y)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first Backprop (->) x ()
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 :: BackpropFunc (x, y) x
forgetSecond = Backprop (->) (x, ()) x
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso Backprop (->) (x, ()) x
-> Backprop (->) (x, y) (x, ()) -> BackpropFunc (x, y) x
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
<<< Backprop (->) y () -> Backprop (->) (x, y) (x, ())
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second Backprop (->) y ()
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 :: c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst c
c BackpropFunc (c, x) y
f = (Backprop (->) x ((), x)
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc x ((), x)) Backprop (->) x ((), x)
-> Backprop (->) ((), x) y -> BackpropFunc x y
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Backprop (->) () c -> Backprop (->) ((), x) (c, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (c -> Backprop (->) () c
forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const c
c) Backprop (->) ((), x) (c, x)
-> BackpropFunc (c, x) y -> Backprop (->) ((), x) y
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 :: c -> BackpropFunc (x, c) y -> BackpropFunc x y
setSecond c
c BackpropFunc (x, c) y
f = (Backprop (->) x (x, ())
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc x (x, ())) Backprop (->) x (x, ())
-> Backprop (->) (x, ()) y -> BackpropFunc x y
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Backprop (->) () c -> Backprop (->) (x, ()) (x, c)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (c -> Backprop (->) () c
forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const c
c) Backprop (->) (x, ()) (x, c)
-> BackpropFunc (x, c) y -> Backprop (->) (x, ()) y
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 :: x -> BackpropFunc x x
linear x
c = (x -> x)
-> Backprop (->) x (x, ())
-> Backprop (->) (x, ()) x
-> BackpropFunc 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
call' Backprop (->) x (x, ())
forward' Backprop (->) (x, ()) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x -> x
f x
c
      where
        f :: x -> x -> x
f = x -> x -> x
forall a. Multiplicative a => a -> a -> a
(NH.*)
    forward' :: BackpropFunc x (x, ())
    forward' :: Backprop (->) x (x, ())
forward' = x -> BackpropFunc x x
forall x. Distributive x => x -> BackpropFunc x x
linear x
c BackpropFunc x x
-> Backprop (->) x (x, ()) -> Backprop (->) 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 y. Backprop (->) y (y, ())
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) x
    backward' :: Backprop (->) (x, ()) x
backward' = (Backprop (->) (x, ()) x
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (x, ()) x) Backprop (->) (x, ()) x
-> BackpropFunc x x -> Backprop (->) (x, ()) x
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> x -> BackpropFunc x x
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
+ :: BackpropFunc (x, x) x
(+) = ((x, x) -> x)
-> Backprop (->) (x, x) (x, ())
-> Backprop (->) (x, ()) (x, 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' Backprop (->) (x, x) (x, ())
forward' Backprop (->) (x, ()) (x, x)
backward'
  where
    call' :: (x, x) -> x
    call' :: (x, x) -> x
call' = (x -> x -> x) -> (x, x) -> x
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry x -> x -> x
forall a. Additive a => a -> a -> a
(NH.+)
    forward' :: BackpropFunc (x, x) (x, ())
    forward' :: Backprop (->) (x, x) (x, ())
forward' = BackpropFunc (x, x) x
forall x. Additive x => BackpropFunc (x, x) x
(+) BackpropFunc (x, x) x
-> Backprop (->) x (x, ()) -> Backprop (->) (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 y. Backprop (->) y (y, ())
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) (x, x)
    backward' :: Backprop (->) (x, ()) (x, x)
backward' = (Backprop (->) (x, ()) x
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (x, ()) x) Backprop (->) (x, ()) x
-> Backprop (->) x (x, x) -> Backprop (->) (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
>>> Backprop (->) x (x, x)
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 :: BackpropFunc x x
negate = (x -> x)
-> Backprop (->) x (x, ())
-> Backprop (->) (x, ()) x
-> BackpropFunc 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
call' Backprop (->) x (x, ())
forward' Backprop (->) (x, ()) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x
forall a. Subtractive a => a -> a
NH.negate
    forward' :: BackpropFunc x (x, ())
    forward' :: Backprop (->) x (x, ())
forward' = BackpropFunc x x
forall x. Subtractive x => BackpropFunc x x
negate BackpropFunc x x
-> Backprop (->) x (x, ()) -> Backprop (->) 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 y. Backprop (->) y (y, ())
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) x
    backward' :: Backprop (->) (x, ()) x
backward' = (forall y. Backprop (->) (y, ()) y
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (y, ()) y) Backprop (->) (x, ()) x
-> BackpropFunc x x -> Backprop (->) (x, ()) x
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> BackpropFunc x x
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
(-) = ((x, x) -> x)
-> Backprop (->) (x, x) (x, ())
-> Backprop (->) (x, ()) (x, 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' Backprop (->) (x, x) (x, ())
forward' Backprop (->) (x, ()) (x, x)
backward'
  where
    call' :: (x, x) -> x
    call' :: (x, x) -> x
call' = (x -> x -> x) -> (x, x) -> x
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry x -> x -> x
forall a. Subtractive a => a -> a -> a
(NH.-)
    forward' :: BackpropFunc (x, x) (x, ())
    forward' :: Backprop (->) (x, x) (x, ())
forward' = (-) BackpropFunc (x, x) x
-> Backprop (->) x (x, ()) -> Backprop (->) (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 y. Backprop (->) y (y, ())
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc y (y, ()))
    backward' :: BackpropFunc (x, ()) (x, x)
    backward' :: Backprop (->) (x, ()) (x, x)
backward' = (Backprop (->) (x, ()) x
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (x, ()) x) Backprop (->) (x, ()) x
-> Backprop (->) x (x, x) -> Backprop (->) (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
>>> Backprop (->) x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup Backprop (->) x (x, x)
-> Backprop (->) (x, x) (x, x) -> Backprop (->) 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
>>> Backprop (->) x x -> Backprop (->) (x, x) (x, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second Backprop (->) x x
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
* :: BackpropFunc (x, x) x
(*) = ((x, x) -> x)
-> Backprop (->) (x, x) (x, (x, x))
-> Backprop (->) (x, (x, x)) (x, 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
forall x. Distributive x => (x, x) -> x
call' Backprop (->) (x, x) (x, (x, x))
forall x. Distributive x => BackpropFunc (x, x) (x, (x, x))
forward' Backprop (->) (x, (x, x)) (x, x)
forall x. Distributive x => BackpropFunc (x, (x, x)) (x, x)
backward'
  where
    call' :: Distributive x => (x, x) -> x
    call' :: (x, x) -> x
call' = (x -> x -> x) -> (x, x) -> x
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry x -> x -> x
forall a. Multiplicative a => a -> a -> a
(NH.*)
    forward' :: Distributive x => BackpropFunc (x, x) (x, (x, x))
    forward' :: BackpropFunc (x, x) (x, (x, x))
forward' = BackpropFunc (x, x) ((x, x), (x, x))
forall x. Additive x => BackpropFunc x (x, x)
dup BackpropFunc (x, x) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, (x, x))
-> BackpropFunc (x, x) (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
>>> Backprop (->) (x, x) x
-> Backprop (->) ((x, x), (x, x)) (x, (x, x))
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first Backprop (->) (x, x) x
forall x. Distributive x => BackpropFunc (x, x) x
(*)
    backward' :: Distributive x => BackpropFunc (x, (x, x)) (x, x)
    backward' :: BackpropFunc (x, (x, x)) (x, x)
backward' =
      Backprop (->) x (x, x)
-> Backprop (->) (x, (x, x)) ((x, x), (x, x))
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first Backprop (->) x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup
        Backprop (->) (x, (x, x)) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, x)
-> BackpropFunc (x, (x, 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 dy x1 x2.
Backprop (->) ((dy, dy), (x1, x2)) ((dy, x1), (dy, x2))
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((dy, dy), (x1, x2)) ((dy, x1), (dy, x2)))
        Backprop (->) ((x, x), (x, x)) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, x)
-> Backprop (->) ((x, x), (x, 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 a b. Backprop (->) (a, b) (b, a)
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (a, b) (b, a))
        Backprop (->) ((x, x), (x, x)) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, x)
-> Backprop (->) ((x, x), (x, 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
>>> BackpropFunc (x, x) x
forall x. Distributive x => BackpropFunc (x, x) x
(*) BackpropFunc (x, x) x
-> BackpropFunc (x, x) x -> Backprop (->) ((x, x), (x, 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)
*** BackpropFunc (x, x) x
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 :: BackpropFunc x x
square = BackpropFunc x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup BackpropFunc x (x, x) -> Backprop (->) (x, x) x -> 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
>>> Backprop (->) (x, x) x
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
/ :: BackpropFunc (x, x) x
(/) = ((x, x) -> x)
-> Backprop (->) (x, x) (x, (x, x))
-> Backprop (->) (x, (x, x)) (x, 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' Backprop (->) (x, x) (x, (x, x))
forward' Backprop (->) (x, (x, x)) (x, x)
backward'
  where
    call' :: (x, x) -> x
    call' :: (x, x) -> x
call' = (x -> x -> x) -> (x, x) -> x
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry x -> x -> x
forall a. Divisive a => a -> a -> a
(NH./)
    forward' :: BackpropFunc (x, x) (x, (x, x))
    forward' :: Backprop (->) (x, x) (x, (x, x))
forward' = BackpropFunc (x, x) ((x, x), (x, x))
forall x. Additive x => BackpropFunc x (x, x)
dup BackpropFunc (x, x) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, (x, x))
-> Backprop (->) (x, x) (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
>>> BackpropFunc (x, x) x -> Backprop (->) ((x, x), (x, x)) (x, (x, x))
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first BackpropFunc (x, x) x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/)
    backward' :: BackpropFunc (x, (x, x)) (x, x)
    backward' :: Backprop (->) (x, (x, x)) (x, x)
backward' =
      BackpropFunc x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup BackpropFunc x (x, x)
-> BackpropFunc (x, x) ((x, x), (x, x))
-> Backprop (->) (x, (x, x)) ((x, x), ((x, 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)
*** BackpropFunc (x, x) ((x, x), (x, x))
forall x. Additive x => BackpropFunc x (x, x)
dup
        Backprop (->) (x, (x, x)) ((x, x), ((x, x), (x, x)))
-> Backprop (->) ((x, x), ((x, x), (x, x))) (x, x)
-> Backprop (->) (x, (x, 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
>>> Backprop (->) ((x, x), (x, x)) (x, x)
-> Backprop (->) ((x, x), ((x, x), (x, x))) ((x, x), (x, x))
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (BackpropFunc (x, x) x
d1 BackpropFunc (x, x) x
-> BackpropFunc (x, x) x -> Backprop (->) ((x, x), (x, 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)
*** BackpropFunc (x, x) x
d2) -- ((dy, dy), ((x1, x2), (x1, x2)))
        Backprop (->) ((x, x), ((x, x), (x, x))) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, x)
-> Backprop (->) ((x, x), ((x, x), (x, 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 dy x1 x2.
Backprop (->) ((dy, dy), (x1, x2)) ((dy, x1), (dy, x2))
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) ))
        Backprop (->) ((x, x), (x, x)) ((x, x), (x, x))
-> Backprop (->) ((x, x), (x, x)) (x, x)
-> Backprop (->) ((x, x), (x, 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
>>> BackpropFunc (x, x) x
forall x. Distributive x => BackpropFunc (x, x) x
(*) BackpropFunc (x, x) x
-> BackpropFunc (x, x) x -> Backprop (->) ((x, x), (x, 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)
*** BackpropFunc (x, x) x
forall x. Distributive x => BackpropFunc (x, x) x
(*)
      where
        d1 :: BackpropFunc (x, x) x
d1 = (BackpropFunc x ()
forall x. Additive x => BackpropFunc x ()
forget BackpropFunc x ()
-> Backprop (->) x x -> Backprop (->) (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)
*** Backprop (->) x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip) Backprop (->) (x, x) ((), x)
-> Backprop (->) ((), x) 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
>>> (Backprop (->) ((), x) x
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((), x) x) -- (x1, x2) -> 1 / x2
        d2 :: BackpropFunc (x, x) x
d2 = (Backprop (->) x x
forall x. Subtractive x => BackpropFunc x x
negate Backprop (->) x x
-> Backprop (->) x x -> Backprop (->) (x, 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)
*** (Backprop (->) x x
forall x. Distributive x => BackpropFunc x x
square Backprop (->) x x -> Backprop (->) x x -> Backprop (->) x x
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Backprop (->) x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)) Backprop (->) (x, x) (x, x)
-> BackpropFunc (x, x) 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
>>> BackpropFunc (x, x) x
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 :: BackpropFunc x x
recip = x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst x
forall a. Multiplicative a => a
NH.one BackpropFunc (x, x) x
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 :: Integer -> BackpropFunc x x
pow Integer
n = (x -> x)
-> Backprop (->) x (x, x)
-> Backprop (->) (x, x) x
-> BackpropFunc 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
call' Backprop (->) x (x, x)
forward' Backprop (->) (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = (x -> Int -> x) -> Int -> x -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip x -> Int -> x
forall a. Divisive a => a -> Int -> a
(NH.^) (Integer -> Int
forall a. FromInteger a => Integer -> a
fromInteger Integer
n)
    forward' :: BackpropFunc x (x, x)
    forward' :: Backprop (->) x (x, x)
forward' = Backprop (->) x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup Backprop (->) x (x, x)
-> Backprop (->) (x, x) (x, x) -> Backprop (->) 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
>>> BackpropFunc x x -> Backprop (->) (x, x) (x, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (Integer -> BackpropFunc x x
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' :: Backprop (->) (x, x) x
backward' = BackpropFunc x x -> Backprop (->) (x, x) (x, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second BackpropFunc x x
der Backprop (->) (x, x) (x, x)
-> Backprop (->) (x, x) x -> Backprop (->) (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
>>> Backprop (->) (x, x) x
forall x. Distributive x => BackpropFunc (x, x) x
(*)
      where
        der :: BackpropFunc x x
der =
          if Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
            then x -> BackpropFunc x x
forall c x. (Additive c, Additive x) => c -> BackpropFunc x c
const x
forall a. Additive a => a
zero
            else Integer -> BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x,
 FromIntegral x Integer) =>
Integer -> BackpropFunc x x
pow (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
P.- Integer
1) BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc x x
forall x. Distributive x => x -> BackpropFunc x x
linear (Integer -> x
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 :: BackpropFunc x x
sqrt = (x -> x)
-> Backprop (->) x (x, x)
-> Backprop (->) (x, x) x
-> BackpropFunc 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
call' Backprop (->) x (x, x)
forward' Backprop (->) (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x
forall a. ExpField a => a -> a
NH.sqrt
    forward' :: BackpropFunc x (x, x)
    forward' :: Backprop (->) x (x, x)
forward' = (BackpropFunc x x
forall x. ExpField x => BackpropFunc x x
sqrt :: BackpropFunc x x) BackpropFunc x x
-> Backprop (->) x (x, x) -> Backprop (->) 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
>>> Backprop (->) x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup
    backward' :: BackpropFunc (x, x) x
    backward' :: Backprop (->) (x, x) x
backward' = BackpropFunc x x -> Backprop (->) (x, x) (x, x)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc x x
forall x. Distributive x => x -> BackpropFunc x x
linear x
forall a. Field a => a
NH.half) Backprop (->) (x, x) (x, x)
-> Backprop (->) (x, x) x -> Backprop (->) (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
>>> Backprop (->) (x, x) x
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
** :: BackpropFunc (a, a) a
(**) = ((a, a) -> a)
-> Backprop (->) (a, a) (a, (a, (a, a)))
-> Backprop (->) (a, (a, (a, a))) (a, a)
-> 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' Backprop (->) (a, a) (a, (a, (a, a)))
forward' Backprop (->) (a, (a, (a, a))) (a, a)
backward'
  where
    call' :: (a, a) -> a
    call' :: (a, a) -> a
call' = (a -> a -> a) -> (a, a) -> a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((a -> a -> a) -> (a, a) -> a) -> (a -> a -> a) -> (a, a) -> a
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> a -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> a -> a
forall a. ExpField a => a -> a -> a
(NH.**)
    forward' :: BackpropFunc (a, a) (a, (a, (a, a)))
    forward' :: Backprop (->) (a, a) (a, (a, (a, a)))
forward' =
      BackpropFunc (a, a) ((a, a), (a, a))
forall x. Additive x => BackpropFunc x (x, x)
dup -- ((n, x), (n, x))
        BackpropFunc (a, a) ((a, a), (a, a))
-> Backprop (->) ((a, a), (a, a)) (a, (a, (a, a)))
-> Backprop (->) (a, a) (a, (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
>>> Backprop (->) (a, a) (a, a)
-> Backprop (->) ((a, a), (a, a)) ((a, a), (a, a))
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (BackpropFunc (a, a) a
forall a.
(ExpField a, FromIntegral a Integer) =>
BackpropFunc (a, a) a
(**) BackpropFunc (a, a) a
-> Backprop (->) a (a, a) -> Backprop (->) (a, 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
>>> Backprop (->) a (a, a)
forall x. Additive x => BackpropFunc x (x, x)
dup) -- ((x^n, x^n), (n, x))
        Backprop (->) ((a, a), (a, a)) ((a, a), (a, a))
-> Backprop (->) ((a, a), (a, a)) (a, (a, (a, a)))
-> Backprop (->) ((a, a), (a, a)) (a, (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 b c. Backprop (->) ((a, b), c) (a, (b, 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' :: Backprop (->) (a, (a, (a, a))) (a, a)
backward' =
      Backprop (->) a (a, a)
forall x. Additive x => BackpropFunc x (x, x)
dup Backprop (->) a (a, a)
-> Backprop (->) (a, (a, a)) (a, a)
-> Backprop (->) (a, (a, (a, a))) ((a, a), (a, a))
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, (a, a)), (a, (a, a)))
forall x. Additive x => BackpropFunc x (x, x)
dup BackpropFunc (a, (a, a)) ((a, (a, a)), (a, (a, a)))
-> Backprop (->) ((a, (a, a)), (a, (a, a))) (a, a)
-> Backprop (->) (a, (a, 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
>>> (BackpropFunc (a, (a, a)) a
dn BackpropFunc (a, (a, a)) a
-> BackpropFunc (a, (a, a)) a
-> Backprop (->) ((a, (a, a)), (a, (a, a))) (a, a)
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))
        Backprop (->) (a, (a, (a, a))) ((a, a), (a, a))
-> Backprop (->) ((a, a), (a, a)) (a, a)
-> Backprop (->) (a, (a, (a, 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 b c d. Backprop (->) ((a, b), (c, d)) ((a, c), (b, d))
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))
        Backprop (->) ((a, a), (a, a)) ((a, a), (a, a))
-> Backprop (->) ((a, a), (a, a)) (a, a)
-> Backprop (->) ((a, a), (a, 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
>>> BackpropFunc (a, a) a
forall x. Distributive x => BackpropFunc (x, x) x
(*) BackpropFunc (a, a) a
-> BackpropFunc (a, a) a -> Backprop (->) ((a, a), (a, a)) (a, a)
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
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 =
          Backprop (->) (a, (a, a)) (a, a)
forall x y. Additive x => BackpropFunc (x, y) y
forgetFirst -- (n, x)
            Backprop (->) (a, (a, a)) (a, a)
-> BackpropFunc (a, a) a -> BackpropFunc (a, (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
>>> Backprop (->) a (a, a) -> Backprop (->) (a, a) ((a, a), a)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first Backprop (->) a (a, a)
forall x. Additive x => BackpropFunc x (x, x)
dup -- ((n, n), x)
            Backprop (->) (a, a) ((a, a), a)
-> Backprop (->) ((a, a), a) a -> 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 b c. Backprop (->) ((a, b), c) (a, (b, c))
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc ((a, b), c) (a, (b, c))) -- (n, (n, x))
            Backprop (->) ((a, a), a) (a, (a, a))
-> BackpropFunc (a, (a, a)) a -> Backprop (->) ((a, 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
>>> Backprop (->) (a, a) (a, a)
-> Backprop (->) (a, (a, a)) (a, (a, a))
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second (Backprop (->) a a -> Backprop (->) (a, a) (a, a)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p a c) (p b c)
first (a -> BackpropFunc (a, a) a -> Backprop (->) a a
forall x y c.
Additive c =>
c -> BackpropFunc (x, c) y -> BackpropFunc x y
setSecond (Integer -> a
forall a b. FromIntegral a b => b -> a
NH.fromIntegral (Integer
1 :: P.Integer)) (-))) -- (n, (n-1, x))
            Backprop (->) (a, (a, a)) (a, (a, a))
-> BackpropFunc (a, (a, a)) a -> BackpropFunc (a, (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
>>> BackpropFunc (a, a) a -> Backprop (->) (a, (a, a)) (a, a)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second BackpropFunc (a, a) a
forall a.
(ExpField a, FromIntegral a Integer) =>
BackpropFunc (a, a) a
(**) -- (n, x^(n-1))
            Backprop (->) (a, (a, a)) (a, a)
-> BackpropFunc (a, a) a -> BackpropFunc (a, (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
>>> BackpropFunc (a, a) a
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 = BackpropFunc (a, a) a -> Backprop (->) (a, (a, a)) (a, a)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second BackpropFunc (a, a) a
forall x y. Additive x => BackpropFunc (x, y) y
forgetFirst Backprop (->) (a, (a, a)) (a, a)
-> BackpropFunc (a, a) a -> BackpropFunc (a, (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
>>> Backprop (->) a a -> Backprop (->) (a, a) (a, a)
forall (p :: * -> * -> *) (cat :: * -> * -> *) a b c.
CatBiFunctor p cat =>
cat a b -> cat (p c a) (p c b)
second Backprop (->) a a
forall x. ExpField x => BackpropFunc x x
log Backprop (->) (a, a) (a, a)
-> BackpropFunc (a, a) a -> 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
>>> BackpropFunc (a, a) a
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 :: BackpropFunc x x
log = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. ExpField a => a -> a
NH.log BackpropFunc x x
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 :: BackpropFunc (a, a) a
logBase = (forall a b. Backprop (->) (a, b) (b, a)
forall (c :: * -> * -> *) a b.
(Isomorphism c, IsomorphicTo a b) =>
c a b
iso :: BackpropFunc (c, d) (d, c)) Backprop (->) (a, a) (a, a)
-> BackpropFunc (a, a) a -> 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
>>> BackpropFunc a a
forall x. ExpField x => BackpropFunc x x
log BackpropFunc a a -> BackpropFunc a a -> Backprop (->) (a, a) (a, a)
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
forall x. ExpField x => BackpropFunc x x
log Backprop (->) (a, a) (a, a)
-> BackpropFunc (a, a) a -> 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
>>> BackpropFunc (a, a) a
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 :: BackpropFunc x x
exp = (x -> x)
-> Backprop (->) x (x, x)
-> Backprop (->) (x, x) x
-> BackpropFunc 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
call' Backprop (->) x (x, x)
forward' Backprop (->) (x, x) x
backward'
  where
    call' :: x -> x
    call' :: x -> x
call' = x -> x
forall a. ExpField a => a -> a
NH.exp
    forward' :: BackpropFunc x (x, x)
    forward' :: Backprop (->) x (x, x)
forward' = (BackpropFunc x x
forall x. ExpField x => BackpropFunc x x
exp :: BackpropFunc x x) BackpropFunc x x
-> Backprop (->) x (x, x) -> Backprop (->) 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
>>> Backprop (->) x (x, x)
forall x. Additive x => BackpropFunc x (x, x)
dup
    backward' :: BackpropFunc (x, x) x
    backward' :: Backprop (->) (x, x) x
backward' = Backprop (->) (x, x) x
forall x. Distributive x => BackpropFunc (x, x) x
(*)

-- Trigonometric

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

-- | Cosine differentiable function.
cos :: TrigField x => BackpropFunc x x
cos :: BackpropFunc x x
cos = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.cos (BackpropFunc x x
forall x. TrigField x => BackpropFunc x x
sin BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. Subtractive x => BackpropFunc x x
negate)

-- | Tangent differentiable function.
tan :: TrigField x => BackpropFunc x x
tan :: BackpropFunc x x
tan = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.tan (BackpropFunc x x
forall x. TrigField x => BackpropFunc x x
cos BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Arcsine differentiable function.
asin :: (TrigField x, ExpField x) => BackpropFunc x x
asin :: BackpropFunc x x
asin = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.tan (BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst x
forall a. Multiplicative a => a
one (-) BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. ExpField x => BackpropFunc x x
sqrt BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Arccosine differentiable function.
acos :: (TrigField x, ExpField x) => BackpropFunc x x
acos :: BackpropFunc x x
acos = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.tan (BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst x
forall a. Multiplicative a => a
one (-) BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. ExpField x => BackpropFunc x x
sqrt BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. Subtractive x => BackpropFunc x x
negate)

-- | Arctangent differentiable function.
atan :: TrigField x => BackpropFunc x x
atan :: BackpropFunc x x
atan = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.atan (BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst x
forall a. Multiplicative a => a
one BackpropFunc (x, x) x
forall x. Additive x => BackpropFunc (x, x) x
(+) BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
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 :: BackpropFunc (a, a) a
atan2 = BackpropFunc (a, a) a
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc (x, x) x
(/) BackpropFunc (a, a) a -> Backprop (->) a a -> 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
>>> Backprop (->) a a
forall x. TrigField x => BackpropFunc x x
atan

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

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

-- | Hyperbolic tanget differentiable function.
tanh :: TrigField x => BackpropFunc x x
tanh :: BackpropFunc x x
tanh = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.tanh (BackpropFunc x x
forall x. TrigField x => BackpropFunc x x
cosh BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
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 :: BackpropFunc x x
asinh = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.asinh (BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst x
forall a. Multiplicative a => a
one BackpropFunc (x, x) x
forall x. Additive x => BackpropFunc (x, x) x
(+) BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. ExpField x => BackpropFunc x x
sqrt BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
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 :: BackpropFunc x x
acosh = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.tan (BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (x, c) y -> BackpropFunc x y
setSecond x
forall a. Multiplicative a => a
one (-) BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x. ExpField x => BackpropFunc x x
sqrt BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)

-- | Hyperbolic arctangent differentiable function.
atanh :: TrigField x => BackpropFunc x x
atanh :: BackpropFunc x x
atanh = (x -> x) -> BackpropFunc x x -> BackpropFunc x x
forall x.
Distributive x =>
(x -> x) -> BackpropFunc x x -> BackpropFunc x x
simpleDifferentiable x -> x
forall a. TrigField a => a -> a
NH.tan (BackpropFunc x x
forall x. Distributive x => BackpropFunc x x
square BackpropFunc x x -> BackpropFunc x x -> 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
>>> x -> BackpropFunc (x, x) x -> BackpropFunc x x
forall x y c.
Additive c =>
c -> BackpropFunc (c, x) y -> BackpropFunc x y
setFirst x
forall a. Multiplicative a => a
one (-) BackpropFunc x x -> BackpropFunc x x -> 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
>>> BackpropFunc x x
forall x.
(Divisive x, Distributive x, Subtractive x) =>
BackpropFunc x x
recip)