Description
The derivative
function is used to compute the 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 derivative
function.
The derivative
function comes in three core variants:
- A pure function that directly computes the derivative of a function
f
at a fixed numeric value ofx
and returns a number as a result. - A mutating function that computes the derivative of a function
f
at a fixed numeric value ofx
and stores the result into a user-provided output array. This function returnsnothing
since its action is focused on mutation. - A higher-order function that constructs a new function
f_prime
that will compute the derivative off
at any pointx
that is provided as an argument tof_prime
. This newly constructed function is the return value for this variant of thederivative
function.
In addition, the derivative
function allows one to chose the mode of numerical differentiation that is used to compute an approximate derivative. The default is use central finite differences, which evaluates f
at two nearby points and provides much higher accuracy than other strategies at a cost of roughly double the amount of computation. Other modes of numerical differentiation are available, but must be opted-in to explicitly.
Primary Methods
The primary methods that most users will want to use are the following:
- Use
derivative(f::Function, x::AbstractFloat)
to approximate the derivative off
atx
. This is the pure variant described above. Calling this function is equivalent to callingderivative(f::Function, x::AbstractFloat, ::CentralMode)
, but leaves the selection of the finite difference mode up to the package author's discretion. - Use
derivative!(output::AbstractArray, f::Function, x::AbstractFloat)
to approximate the derivative off
atx
and store the result into theoutput
array. - Use
derivative(f::Function)
to generate a new function that approximates the true derivative function off
. The new functionf_prime
can be evaluated at any pointx
that is desired after it is constructed.
Detailed Method-Level Documentation
#
FiniteDiff.derivative
— Method.
derivative(f::Function, x::AbstractFloat)
Description
Evaluate the derivative of f
at x
using finite differences without specifying the mode of finite differences. Defaults to using central finite differences, which is equivalent to the user calling derivative(f, x, CentralMode())
instead of derivative(f, x)
.
See the documentation for derivative(f, x, CentralMode())
for more details.
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 derivative off
evaluated atx
.
Examples
import FiniteDiff: derivative y = derivative(sin, 0.0)
#
FiniteDiff.derivative!
— Function.
derivative!( output::AbstractArray, f::Function, x::AbstractFloat, m::Mode = CentralMode(), )
Description
Evaluate the derivative of f
at x
using finite differences. The first argument output
will be mutated so that its first element will contain the result.
See the documentation for the non-mutating versions of derivative
for additional information about the effects of choosing a specific mode of finite differences.
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
.m::Mode
: An instance of theMode
type. This will determine the mode of finite differences that will be used.
Returns
nothing::Void
: This function is called for its side effects.
Examples
import FiniteDiff: derivative, CentralMode y = Array(Float64, 1) derivative!(y, sin, 0.0, CentralMode())
#
FiniteDiff.derivative
— Function.
derivative(f::Function, mode::Mode = CentralMode(); mutates::Bool=false)
Description
Construct a new function that will evaluate the derivative of f
at any value of x
using finite differences. The user can specify the mode of finite differences to use by providing a second positional argument. The user can also indicate whether to return a mutating or non-mutating function using the keyword argument, mutates
, which should be either true
or false
.
Arguments
f::Function
: The function to be differentiated.m::Mode
: An instance of theMode
type. This will determine the mode of finite differences that will be used.
Keyword Arguments
mutates::Bool = false
: Determine whether the resulting function will mutate its inputs or will be a pure function.
Examples
import FiniteDiff: derivative f′ = derivative(sin) f′(0.0)
#
FiniteDiff.derivative
— Method.
derivative(f::Function, x::AbstractFloat, ::ForwardMode)
Description
Evaluate the derivative of f
at x
using forward finite differences. In mathematical notation, we calculate,
where $\epsilon$ is chosen to be small enough to approximate the 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
.::ForwardMode
: An instance of theForwardMode
type.
Returns
y::Real
: The derivative off
evaluated atx
.
Examples
import FiniteDiff: derivative, ForwardMode y = derivative(sin, 0.0, ForwardMode())
#
FiniteDiff.derivative
— Method.
derivative(f::Function, x::AbstractFloat, ::BackwardMode)
Description
Evaluate the derivative of f
at x
using backward finite differences. In mathematical notation, we calculate,
where $\epsilon$ is chosen to be small enough to approximate the 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
.::BackwardMode
: An instance of theBackwardMode
type.
Returns
y::Real
: The derivative off
evaluated atx
.
Examples
import FiniteDiff: derivative, BackwardMode y = derivative(sin, 0.0, BackwardMode())
#
FiniteDiff.derivative
— Method.
derivative(f::Function, x::AbstractFloat, ::CentralMode)
Description
Evaluate the derivative of f
at x
using central finite differences. In mathematical notation, we calculate,
where $\epsilon$ is chosen to be small enough to approximate the 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
.::CentralMode
: An instance of theCentralMode
type.
Returns
y::Real
: The derivative off
evaluated atx
.
Examples
import FiniteDiff: derivative, CentralMode y = derivative(sin, 0.0, CentralMode())
#
FiniteDiff.derivative
— Method.
derivative(f::Function, x::AbstractFloat, ::ComplexMode)
Description
Evaluate the derivative of f
at x
using complex finite differences. In mathematical notation, we calculate,
where $\epsilon$ is chosen to be as small as possible.
NOTE: This mode of finite differences will work correctly only when:
f
supports complex inputs.f
is an analytic function in the complex analysis sense of the word.
Arguments
f::Function
: The function to be differentiated.x::AbstractFloat
: The point at which to evaluate the derivative off
. Its type must implementeps
.::ForwardMode
: An instance of theForwardMode
type.
Returns
y::Real
: The derivative off
evaluated atx
.
Examples
import FiniteDiff: derivative, ComplexMode y = derivative(sin, 0.0, ComplexMode())