Description
The second_derivative function is used to compute the second derivative of a univariate function that maps $\mathbb{R}$ to $\mathbb{R}$. For example, x -> sin(x) is a univariate function that might be passed to the second_derivative function.
The second_derivative function comes in three core variants:
- A pure function that directly computes the second derivative of a function
fat a fixed numeric value ofxand returns a number as a result. - A mutating function that computes the second derivative of a function
fat a fixed numeric value ofxand stores the result into a user-provided output array. This function returnsnothingsince its action is focused on mutation. - A higher-order function that constructs a new function
f_prime2that will compute the second derivative offat any pointxthat is provided as an argument tof_prime2. This newly constructed function is the return value for this variant of thesecond_derivativefunction.
Primary Methods
The primary methods that most users will want to use are the following:
- Use
second_derivative(f::Function, x::AbstractFloat)to approximate the second derivative offatx. This is the pure variant described above. - Use
second_derivative!(output::AbstractArray, f::Function, x::AbstractFloat)to approximate the second derivative offatxand store the result into theoutputarray. - Use
second_derivative(f::Function)to generate a new function that approximates the true second derivative function off. The new functionf_prime2can be evaluated at any pointxthat is desired after it is constructed.
Detailed Method-Level Documentation
#
FiniteDiff.second_derivative — Method.
second_derivative(f::Function, x::AbstractFloat)
Description
Evaluate the second derivative of f at x using finite differences. In mathematical notation, we calculate,
where $\epsilon$ is chosen to be small enough to approximate the second derivative, but not so small as to suffer from extreme numerical inaccuracy.
Arguments
f::Function: The function to be differentiated.x::AbstractFloat: The point at which to evaluate the derivative off. Its type must implementeps.
Returns
y::Real: The second derivative offevaluated atx.
Examples
import FiniteDiff: second_derivative y = second_derivative(sin, 1.0)
#
FiniteDiff.second_derivative! — Method.
second_derivative!(
output::AbstractArray,
f::Function,
x::AbstractFloat,
)
Description
Evaluate the second derivative of f at x using finite differences. In mathematical notation, we calculate,
where $\epsilon$ is chosen to be small enough to approximate the second derivative, but not so small as to suffer from extreme numerical inaccuracy.
The first argument, output, will be mutated so that the value of the second derivative is its first element.
Arguments
output::AbstractArray: An array whose first element will be mutated.f::Function: The function to be differentiated.x::AbstractFloat: The point at which to evaluate the derivative off. Its type must implementeps.
Returns
void::Nothing: This function is called for its side-effects.
Examples
import FiniteDiff: second_derivative y = Array(Float64, 1) second_derivative!(y, sin, 1.0)
#
FiniteDiff.second_derivative — Method.
second_derivative(f::Function; mutates::Bool=false)
Description
Construct a new function that will evaluate the second derivative of f at any point x. A keyword argument specifies whether the resulting function should be mutating or non-mutating.
Arguments
f::Function: The function to be differentiated.
Keyword Arguments
mutates::Bool = false: Determine whether the resulting function will mutate its inputs or will be a pure function.
Returns
void::Nothing: This function is called for its side-effects.
Examples
import FiniteDiff: second_derivative f′′ = second_derivative(sin) f′′(1.0)