Function gradient
Calculate the gradient of a function of several variables.
Real[] gradient(Real, Func)
(
scope Func f,
Real[] x,
real scale = 1.00000,
Real[] buffer = null
);
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
. The function f
is evaluated
2n times, where n is the length of the vector x
.
Parameters
Name | Description |
---|---|
f | The function of which to find the gradient. |
x | The point at which to find the gradient. |
scale | A "characteristic scale" over which the function changes significantly. (optional) |
buffer | A buffer of the same length as x , for the returned
gradient vector. (optional) |
Example
// 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
- Wikipedia: Gradient.