Recursively nests dual numbers to enable exact computation of derivatives up to order n. The variable is seeded so that after evaluating a function f, the k-th derivative can be extracted with deriv_n(result, k).

dual_variable_n(x, order)

Arguments

x

A numeric value at which to differentiate.

order

A positive integer specifying the derivative order.

Value

A (possibly nested) dual number.

Examples

# 3rd derivative of x^4 at x=2: 4*3*2*x = 24*2 = 48
x <- dual_variable_n(2, order = 3)
r <- x^4
deriv_n(r, 3)  # 48
#> [1] 48