jacobian

Calculate the Jacobian matrix associated with a set of m functions in n variables using a central-difference approximation to the Jacobian.

This method requires 2n function evaluations, but is more accurate than the faster jacobian2() function. The relative accuracy in the result is, at best, on the order of (real.epsilon)^(2/3).

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

Parameters

f Func

The set of functions. This is typically a function or delegate which takes a vector as input and returns a vector. If the function takes a second vector parameter, this is assumed to be a buffer for the return value, and will be used as such.

m int

The number of functions in f.

x Real[]

The point at which to take the derivative.

scale real

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

buffer Real[]

A buffer of length at least m*n, for storing the calculated Jacobian matrix. (optional)

Examples

// Let's say we want to find the Jacobian of the function
//      f(x,y) = (xy, x-y)
// at the point p = (1, 2).
real[] p = [ 1.0, 2.0 ];

// This is the simplest way to do it:
real[] f(real[] a)
{
    auto r = new real[2];
    r[0] = a[0] * a[1];
    r[1] = a[0] - a[1];
    return r;
}

auto j = jacobian(&f, 2, p);

// However, if we need to perform this operation many times,
// we may want to speed things up a bit. To avoid unnecessary
// repeated allocations, we can add a buffer parameter to the
// function:
real[] g(real[] a, real[] r)
{
    r[0] = a[0] * a[1];
    r[1] = a[0] - a[1];
    return r[0 .. 2];
}

auto jFaster = jacobian(&g, 2, p);

See Also

Meta