Supported methods are 'step' or 'quad': derivatives may be
computed using either a finite difference with a small step
size \(h\) (default), or numerical quadrature.

direction

Direction of finite difference: can be -1 for a left
difference, 0 for a central difference (default), or +1
for a right difference; more generally can be any complex number.

addprec

Extra precision for \(h\) used to account for the function’s
sensitivity to perturbations (default = 10).

relative

Choose \(h\) relative to the magnitude of \(x\), rather than an
absolute value; useful for large or tiny \(x\) (default = False).

h

As an alternative to addprec and relative, manually
select the step size \(h\).

singular

If True, evaluation exactly at the point \(x\) is avoided; this is
useful for differentiating functions with removable singularities.
Default = False.

radius

Radius of integration contour (with method='quad').
Default = 0.25. A larger radius typically is faster and more
accurate, but it must be chosen so that \(f\) has no
singularities within the radius from the evaluation point.

A finite difference requires \(n+1\) function evaluations and must be
performed at \((n+1)\) times the target precision. Accordingly, \(f\) must
support fast evaluation at high precision.

With integration, a larger number of function evaluations is
required, but not much extra precision is required. For high order
derivatives, this method may thus be faster if f is very expensive to
evaluate at high precision.

Further examples

The direction option is useful for computing left- or right-sided
derivatives of nonsmooth functions:

More generally, if the direction is nonzero, a right difference
is computed where the step size is multiplied by sign(direction).
For example, with direction=+j, the derivative from the positive
imaginary direction will be computed:

>>> diff(abs,0,direction=j)(0.0 - 1.0j)

With integration, the result may have a small imaginary part
even even if the result is purely real:

With method='step', diffs() uses only \(O(k)\)
function evaluations to generate the first \(k\) derivatives,
rather than the roughly \(O(k^2)\) evaluations
required if one calls diff()\(k\) separate times.

With \(n < \infty\), the generator stops as soon as the
\(n\)-th derivative has been generated. If the exact number of
needed derivatives is known in advance, this is further
slightly more efficient.

where \(f\) is a given (presumably well-behaved) function,
\(x\) is the evaluation point, \(n\) is the order, and \(x_0\) is
the reference point of integration (\(m\) is an arbitrary
parameter selected automatically).

Another useful test function is the exponential function, whose
integration / differentiation formula easy generalizes
to arbitrary order. Here we first compute a third derivative,
and then a triply nested integral. (The reference point \(x_0\)
is set to \(-\infty\) to avoid nonzero endpoint terms.):