Description
The hessian function is used to compute the Hessian of a multivariate function that maps $\mathbb{R}^n$ to $\mathbb{R}$. For example, x -> sin(x[1]) + cos(x[2]) is a multivariate function that might be passed to the hessian function.
The hessian function comes in three core variants:
- A pure function that directly computes the Hessian of a function
fat a fixed value ofxand returns a newly allocated array as a result. - A mutating function that computes the Hessian of a function
fat a fixed value ofxand stores the result into a user-provided output array. This function returnsnothingsince its action is focused on mutation. To ensure thread safety, this mutating variant also requires that a buffer be provided that is part of the internal implementation of the function, but can be provided explicitly to minimize unnecessary memory allocations. - A higher-order function that constructs a new function
f_primethat will compute the Hessian offat any pointxthat is provided as an argument tof_prime. This newly constructed function is the return value for this variant of thehessianfunction.
Primary Methods
The primary methods that most users will want to use are the following:
- Use
hessian(f::Function, x::AbstractArray)to approximate the Hessian offatx. This is the pure variant described above. - Use
hessian!(output::AbstractArray, f::Function, x::AbstractArray, buffer::AbstractArray)to approximate the Hessian offatxand store the result into theoutputarray. - Use
hessian(f::Function)to generate a new function that approximates the true Hessian function off. The new functionf_primecan be evaluated at any pointxthat is desired after it is constructed.
Detailed Method-Level Documentation
#
FiniteDiff.hessian! — Method.
hessian!{T <: AbstractFloat}(
output::AbstractArray,
f::Function,
x::AbstractArray{T},
buffer::AbstractArray{T},
)
Description
Evaluate the Hessian of f at x using finite differences. Store the results into output. Work with a user-provided buffer to ensure that we can work without copies, but also without mutating x. In Rust jargon, we take ownership of both output and buffer, but do not require ownership of x. We just need read-only access to x.
Arguments
output::AbstractArray: An array that will be mutated to contain the gradient.f::Function: The function to be differentiated.x::AbstractArray{T}: The value ofxat which to evaluate the Hessian off.buffer::AbstractArray{T}: A buffer that is equivalent tosimilar(x). Used as a temporary copy ofxthat can be mutated.
Returns
nothing::Void: This function is called for its side effects.
Examples
import FiniteDiff: hessian! x = [0.0, 0.0] output, buffer = similar(x, 2, 2), similar(x, 2) hessian!(output, x -> sin(x[1]) + 2 * sin(x[2]), x, buffer)
#
FiniteDiff.hessian — Method.
hessian{T <: AbstractFloat}(f::Function, x::AbstractArray{T})
Description
Evaluate the Hessian of f at x using finite differences. See hessian!(f, x, buffer) for more details.
Arguments
f::Function: The function to be differentiated.x::AbstractArray{T <: AbstractArray}: The value ofxat which to evaluate the Hessian off.
Returns
output::Array{T <: AbstractArray}: The Hessian offatx.
Examples
import FiniteDiff: hessian x = [0.0, 0.0] H = hessian(x -> sin(x[1]) + 2 * sin(x[2]), x)
#
FiniteDiff.hessian — Method.
hessian(f::Function; mutates::Bool = false)
Description
Construct a new function that will evaluate the Hessian of f at any value of x. The user can whether to return a mutating or non-mutating function using a keyword argument.
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
h::Function: A function that will evaluate the Hessian offat anyx.
Examples
import FiniteDiff: hessian h = hessian(x -> sin(x[1]) + 2 * sin(x[2]), mutates = false) x = [0.0, 0.0] h(x)