gradient

Calculate the gradient of a function of several variables.

This function calculates a central-difference approximation to the gradient of a function f. The error in the result is, at best, on the order of sqrt(real.epsilon). The function f is evaluated 2n times, where n is the length of the vector x.

Real[]
gradient
(
Real
Func
)
(
scope Func f
,
Real[] x
,
real scale = 1.0
,
Real[] buffer = null
)

Parameters

f Func

The function of which to find the gradient.

x Real[]

The point at which to find the gradient.

scale real

A "characteristic scale" over which the function changes significantly. (optional)

buffer Real[]

A buffer of the same length as x, for the returned gradient vector. (optional)

Examples

// Let's find the gradient of f(x,y) = x exp(y) at
// the point p = (2,1).
real f(real[] x)  { return x[0] * exp(x[1]); }
real[] p = [2.0L, 1.0L];

auto g = gradient(&f, p);

See Also

Meta