{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Data.SRTree.Derivative 
-- Copyright   :  (c) Fabricio Olivetti 2021 - 2024
-- License     :  BSD3
-- Maintainer  :  fabricio.olivetti@gmail.com
-- Stability   :  experimental
-- Portability :  FlexibleInstances, DeriveFunctor, ScopedTypeVariables
--
-- Symbolic derivative of SRTree expressions
--
-----------------------------------------------------------------------------
module Data.SRTree.Derivative
        ( derivative
        , doubleDerivative
        , deriveByVar
        , deriveByParam
        )
        where

import Data.SRTree.Internal
import Data.SRTree.Recursion (Fix (..), mutu)
import Data.Attoparsec.ByteString.Char8 (double)

-- | Creates the symbolic partial derivative of a tree by variable `dx` (if `p` is `False`)
-- or parameter `dx` (if `p` is `True`).
-- This uses mutual recursion where the first recursion (alg1) holds the derivative w.r.t. 
-- the current node and the second (alg2) holds the original tree.
--
-- >>> showExpr . deriveBy False 0 $ 2 * "x0" * "x1"
-- "(2.0 * x1)"
-- >>> showExpr . deriveBy True 1 $ 2 * "x0" * "t0" - sqrt ("t1" * "x0")
-- "(-1.0 * ((1.0 / (2.0 * Sqrt((t1 * x0)))) * x0))"
deriveBy :: Bool -> Int -> Fix SRTree -> Fix SRTree
deriveBy :: Bool -> Int -> Fix SRTree -> Fix SRTree
deriveBy Bool
p Int
dx = (Fix SRTree -> Fix SRTree, Fix SRTree -> Fix SRTree)
-> Fix SRTree -> Fix SRTree
forall a b. (a, b) -> a
fst ((SRTree (Fix SRTree, Fix SRTree) -> Fix SRTree)
-> (SRTree (Fix SRTree, Fix SRTree) -> Fix SRTree)
-> (Fix SRTree -> Fix SRTree, Fix SRTree -> Fix SRTree)
forall (f :: * -> *) a b.
Functor f =>
(f (a, b) -> a) -> (f (a, b) -> b) -> (Fix f -> a, Fix f -> b)
mutu SRTree (Fix SRTree, Fix SRTree) -> Fix SRTree
forall {a}. Floating a => SRTree (a, a) -> a
alg1 SRTree (Fix SRTree, Fix SRTree) -> Fix SRTree
forall {a}. SRTree (a, Fix SRTree) -> Fix SRTree
alg2)
  where
      alg1 :: SRTree (a, a) -> a
alg1 (Var Int
ix)           = if Bool -> Bool
not Bool
p Bool -> Bool -> Bool
&& Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
dx then a
1 else a
0
      alg1 (Param Int
ix)         = if Bool
p Bool -> Bool -> Bool
&& Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
dx then a
1 else a
0
      alg1 (Const Double
_)          = a
0
      alg1 (Uni Function
f (a, a)
t)          = Function -> a -> a
forall a. Floating a => Function -> a -> a
derivative Function
f ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
t) a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
t
      alg1 (Bin Op
Add (a, a)
l (a, a)
r)      = (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
+ (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r
      alg1 (Bin Op
Sub (a, a)
l (a, a)
r)      = (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
- (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r
      alg1 (Bin Op
Mul (a, a)
l (a, a)
r)      = (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
+ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r
      alg1 (Bin Op
Div (a, a)
l (a, a)
r)      = ((a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
- (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r) a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Floating a => a -> a -> a
** a
2
      alg1 (Bin Op
Power (a, a)
l (a, a)
r)    = (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l a -> a -> a
forall a. Floating a => a -> a -> a
** ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
- a
1) a -> a -> a
forall a. Num a => a -> a -> a
* ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
+ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
log ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l) a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r)
      alg1 (Bin Op
PowerAbs (a, a)
l (a, a)
r) = (a -> a
forall a. Num a => a -> a
abs ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l) a -> a -> a
forall a. Floating a => a -> a -> a
** ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r)) a -> a -> a
forall a. Num a => a -> a -> a
* ((a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
log (a -> a
forall a. Num a => a -> a
abs ((a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l)) a -> a -> a
forall a. Num a => a -> a -> a
+ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l)
      alg1 (Bin Op
AQ (a, a)
l (a, a)
r)       = ((a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r) a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
- (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
l a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
r) a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r a -> a -> a
forall a. Num a => a -> a -> a
* (a, a) -> a
forall a b. (a, b) -> b
snd (a, a)
r) a -> a -> a
forall a. Floating a => a -> a -> a
** a
1.5

      alg2 :: SRTree (a, Fix SRTree) -> Fix SRTree
alg2 (Var Int
ix)    = Int -> Fix SRTree
var Int
ix
      alg2 (Param Int
ix)  = Int -> Fix SRTree
param Int
ix
      alg2 (Const Double
c)   = SRTree (Fix SRTree) -> Fix SRTree
forall (f :: * -> *). f (Fix f) -> Fix f
Fix (Double -> SRTree (Fix SRTree)
forall val. Double -> SRTree val
Const Double
c)
      alg2 (Uni Function
f (a, Fix SRTree)
t)   = SRTree (Fix SRTree) -> Fix SRTree
forall (f :: * -> *). f (Fix f) -> Fix f
Fix (Function -> Fix SRTree -> SRTree (Fix SRTree)
forall val. Function -> val -> SRTree val
Uni Function
f (Fix SRTree -> SRTree (Fix SRTree))
-> Fix SRTree -> SRTree (Fix SRTree)
forall a b. (a -> b) -> a -> b
$ (a, Fix SRTree) -> Fix SRTree
forall a b. (a, b) -> b
snd (a, Fix SRTree)
t)
      alg2 (Bin Op
f (a, Fix SRTree)
l (a, Fix SRTree)
r) = SRTree (Fix SRTree) -> Fix SRTree
forall (f :: * -> *). f (Fix f) -> Fix f
Fix (Op -> Fix SRTree -> Fix SRTree -> SRTree (Fix SRTree)
forall val. Op -> val -> val -> SRTree val
Bin Op
f ((a, Fix SRTree) -> Fix SRTree
forall a b. (a, b) -> b
snd (a, Fix SRTree)
l) ((a, Fix SRTree) -> Fix SRTree
forall a b. (a, b) -> b
snd (a, Fix SRTree)
r))

-- | Derivative of each supported function
-- For a function h(f) it returns the derivative dh/df
--
-- >>> derivative Log 2.0
-- 0.5
derivative :: Floating a => Function -> a -> a
derivative :: forall a. Floating a => Function -> a -> a
derivative Function
Id      = a -> a -> a
forall a b. a -> b -> a
const a
1
derivative Function
Abs     = \a
x -> a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Num a => a -> a
abs a
x
derivative Function
Sin     = a -> a
forall a. Floating a => a -> a
cos
derivative Function
Cos     = a -> a
forall a. Num a => a -> a
negate(a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.a -> a
forall a. Floating a => a -> a
sin
derivative Function
Tan     = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a
forall a. Floating a => a -> a -> a
**a
2.0) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
cos
derivative Function
Sinh    = a -> a
forall a. Floating a => a -> a
cosh
derivative Function
Cosh    = a -> a
forall a. Floating a => a -> a
sinh
derivative Function
Tanh    = (a
1a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a
forall a. Floating a => a -> a -> a
**a
2.0) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
tanh
derivative Function
ASin    = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
sqrt (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
1a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
ACos    = a -> a
forall a. Num a => a -> a
negate (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
sqrt (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
1a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
ATan    = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
1a -> a -> a
forall a. Num a => a -> a -> a
+) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
ASinh   = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
sqrt (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
1a -> a -> a
forall a. Num a => a -> a -> a
+) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
ACosh   = \a
x -> a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a -> a
forall a. Floating a => a -> a
sqrt (a
xa -> a -> a
forall a. Num a => a -> a -> a
-a
1) a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sqrt (a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1))
derivative Function
ATanh   = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
1a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
Sqrt    = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
2a -> a -> a
forall a. Num a => a -> a -> a
*) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
sqrt
derivative Function
SqrtAbs = \a
x -> a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
2.0 a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Num a => a -> a
abs a
x a -> a -> a
forall a. Floating a => a -> a -> a
** (a
3.0a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2.0))
derivative Function
Cbrt    = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
3a -> a -> a
forall a. Num a => a -> a -> a
*) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a
forall a. Floating a => a -> a -> a
**(a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
3)) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
Square  = (a
2a -> a -> a
forall a. Num a => a -> a -> a
*)
derivative Function
Exp     = a -> a
forall a. Floating a => a -> a
exp
derivative Function
Log     = a -> a
forall a. Fractional a => a -> a
recip
derivative Function
LogAbs  = a -> a
forall a. Fractional a => a -> a
recip
derivative Function
Recip   = a -> a
forall a. Num a => a -> a
negate (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
derivative Function
Cube    = (a
3a -> a -> a
forall a. Num a => a -> a -> a
*) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
{-# INLINE derivative #-}

-- | Second-order derivative of supported functions
--
-- >>> doubleDerivative Log 2.0
-- -0.25
doubleDerivative :: Floating a => Function -> a -> a
doubleDerivative :: forall a. Floating a => Function -> a -> a
doubleDerivative Function
Id      = a -> a -> a
forall a b. a -> b -> a
const a
0
doubleDerivative Function
Abs     = a -> a -> a
forall a b. a -> b -> a
const a
0
doubleDerivative Function
Sin     = a -> a
forall a. Num a => a -> a
negate(a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.a -> a
forall a. Floating a => a -> a
sin
doubleDerivative Function
Cos     = a -> a
forall a. Num a => a -> a
negate(a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.a -> a
forall a. Floating a => a -> a
cos
doubleDerivative Function
Tan     = \a
x -> a
2 a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sin a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a -> a
forall a. Floating a => a -> a
cos a
x) a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
3
doubleDerivative Function
Sinh    = a -> a
forall a. Floating a => a -> a
sinh
doubleDerivative Function
Cosh    = a -> a
forall a. Floating a => a -> a
cosh
doubleDerivative Function
Tanh    = \a
x -> -a
2 a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
tanh a
x a -> a -> a
forall a. Num a => a -> a -> a
* (a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
cosh a
x)a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2
doubleDerivative Function
ASin    = \a
x -> a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
1 a -> a -> a
forall a. Num a => a -> a -> a
- a
xa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)a -> a -> a
forall a. Floating a => a -> a -> a
**(a
3a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2)
doubleDerivative Function
ACos    = \a
x -> a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
1 a -> a -> a
forall a. Num a => a -> a -> a
- a
xa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)a -> a -> a
forall a. Floating a => a -> a -> a
**(a
3a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2)
doubleDerivative Function
ATan    = \a
x -> (-a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
x) a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
xa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
1)a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2
doubleDerivative Function
ASinh   = \a
x -> a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
xa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
1)a -> a -> a
forall a. Floating a => a -> a -> a
**(a
3a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2) -- check
doubleDerivative Function
ACosh   = \a
x -> a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a -> a
forall a. Floating a => a -> a
sqrt (a
xa -> a -> a
forall a. Num a => a -> a -> a
-a
1) a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sqrt (a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1)) -- check
doubleDerivative Function
ATanh   = a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
1a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2) -- check
doubleDerivative Function
Sqrt    = \a
x -> -a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
4 a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Floating a => a -> a
sqrt a
xa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3)
doubleDerivative Function
SqrtAbs = \a
x -> (-a
x)a -> a -> a
forall a. Num a => a -> a -> a
*a
xa -> a -> a
forall a. Fractional a => a -> a -> a
/(a
4 a -> a -> a
forall a. Num a => a -> a -> a
* a -> a
forall a. Num a => a -> a
abs a
x a -> a -> a
forall a. Floating a => a -> a -> a
** (a
3.5))
doubleDerivative Function
Cbrt    = \a
x -> -a
2 a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
9 a -> a -> a
forall a. Num a => a -> a -> a
* a
x a -> a -> a
forall a. Num a => a -> a -> a
* (a
xa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)a -> a -> a
forall a. Floating a => a -> a -> a
**(a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
3))
doubleDerivative Function
Square  = a -> a -> a
forall a b. a -> b -> a
const a
2
doubleDerivative Function
Exp     = a -> a
forall a. Floating a => a -> a
exp
doubleDerivative Function
Log     = a -> a
forall a. Num a => a -> a
negate (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
doubleDerivative Function
LogAbs  = a -> a
forall a. Num a => a -> a
negate (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2)
doubleDerivative Function
Recip   = (a -> a -> a
forall a. Num a => a -> a -> a
*a
2) (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Fractional a => a -> a
recip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3)
doubleDerivative Function
Cube    = (a
6a -> a -> a
forall a. Num a => a -> a -> a
*)
{-# INLINE doubleDerivative #-}

-- | Symbolic derivative by a variable
deriveByVar :: Int -> Fix SRTree -> Fix SRTree
deriveByVar :: Int -> Fix SRTree -> Fix SRTree
deriveByVar = Bool -> Int -> Fix SRTree -> Fix SRTree
deriveBy Bool
False
{-# INLINE deriveByVar #-}

-- | Symbolic derivative by a parameter
deriveByParam :: Int -> Fix SRTree -> Fix SRTree
deriveByParam :: Int -> Fix SRTree -> Fix SRTree
deriveByParam = Bool -> Int -> Fix SRTree -> Fix SRTree
deriveBy Bool
True
{-# INLINE deriveByParam #-}