Evaluates f at a dual variable seeded for order n, returning the function value and all derivatives from 1 to n.

differentiate_n(f, x, order)

Arguments

f

A function of one numeric argument.

x

A numeric value at which to differentiate.

order

A positive integer: the maximum derivative order.

Value

A named list with components value, d1, d2, ..., d<order>.

Examples

# All derivatives of sin(x) at x = pi/4
differentiate_n(sin, pi/4, order = 4)
#> $value
#> [1] 0.7071068
#> 
#> $d1
#> [1] 0.7071068
#> 
#> $d2
#> [1] -0.7071068
#> 
#> $d3
#> [1] -0.7071068
#> 
#> $d4
#> [1] 0.7071068
#> 
# $value = sin(pi/4)
# $d1 = cos(pi/4)
# $d2 = -sin(pi/4)
# $d3 = -cos(pi/4)
# $d4 = sin(pi/4)