Hyper-dual numbers can be used to compute first and second derivatives numerically without the cancellation errors of finite-differencing schemes. This Julia implementation is directly based on the C++ implementation by Jeffrey Fike and Juan J Alonso, both of Stanford University, department of Aeronautics and Astronautics and is described in the paper:
The Julia version was derived/written by Rob J Goedman (email@example.com). Latest tagged versions:
The Julia package is structured similar to the JuliaDiff/DualNumbers package, which aims for complete support for
HyperDual types for numerical functions within Julia's
Base. Currently, basic mathematical operations and trigonometric functions are supported.
The following functions are specific to hyperdual numbers:
Several other functions have been extended to accept hyperdual numbers, e.g.:
sqrt, etc., see the final part of hyperdual.jl.
JuliaDiff is a great starting point to learn about Julia packages related to Automatic Differentiation.
The example below demonstrates basic usage of hyperdual numbers by employing them to
perform automatic differentiation. The code for this example can be found in
First install the package by using the Julia package manager:
Then make the package available via
hyper() function to define a hyperdual number, e.g.:
hd0 = hyper() hd1 = hyper(1.0) hd2 = hyper(3.0, 1.0, 1.0, 0.0) hd3 = hyper(3//2, 1//1, 1//1,0//1)
Let's say we want to calculate the first and second derivative of
f(x) = e^x / (sqrt(sin(x)^3 + cos(x)^3))
To calculate these derivatives at a location
x, evaluate your function at
hyper(x, 1.0, 1.0, 0.0). For example:
t0 = hyper(1.5, 1.0, 1.0, 0.0) y = f(t0)
For this example, you'll get the result
4.497780053946162 + 4.053427893898621ϵ1 + 4.053427893898621ϵ2 + 9.463073681596601ϵ1ϵ2
The first term is the function value, the coefficients of both
ϵ2 (which correspond to the second and third arguments of
hyper) are equal to the first derivative, and the coefficient of
ϵ1ϵ2 is the second derivative.
You can extract these coefficients from the hyperdual number using the functions
println("f(1.5) = ", f(1.5)) println("f(t0) = ", real(f(t0))) println("f'(t0) = ", eps1(f(t0))) println("f'(t0) = ", eps2(f(t0))) println("f''(t0) = ", eps1eps2(f(t0)))
10 days ago