Find first derivatives of functions automagically
Through the magic of dual numbers, we can calculate the derivative of a function while calculating its value.
By using OCaml's operator overloading, we can abstract away the implementation of these dual numbers and make it almost seamless in operation.
After opening Auto_differentiate, one can easily use the
D.val_deriv function along with D.operators to construct a
function and its derivative.
For example, consider the following piecewise function:
The normal implementation of this would be using:
letfx=if x <. 5.then2.*. x **2.-.3.*. x +.5.else5./. x +.39.)To calculate the derivative, along with the function's value itself, we only need to update the function as follows:
let f, f' =D.val_deriv (funx ->
letopenD.Operatorsinif x <~$5.then~$2.* x **2.-~$3.* x +~$5.else~$5./ x +~$39.)Now, f calculates the value, while f' calculates the derivative.
The values outputted for this derivative f' match as per WolframAlpha's output when its is checked numerically:
The results for numerical computation can be checked using
corebuild test.native
./test.native
Obviously, the indeterminate case is not handled properly, and
instead, a value is returned based upon the x >= 5 case.
Thanks to Demofox, for introducing me to Dual Numbers and Automatic Differentiation in their blogpost.

