// delegate computation to underlying function
final DerivativeStructure[] dsX = new DerivativeStructure[n];
for (int i = 0; i < n; ++i) {
if (i == k) {
dsX[i] = new DerivativeStructure(1, 1, 0, x[i]);
} else {
dsX[i] = new DerivativeStructure(1, 1, x[i]);
}
}
final DerivativeStructure y = f.value(dsX);
// extract partial derivative
return y.getPartialDerivative(1);
}
};
}
public MultivariateVectorFunction gradient() {
return new MultivariateVectorFunction() {
/** {@inheritDoc} */
public double[] value(final double[] x) {
final int n = x.length;
// delegate computation to underlying function
final DerivativeStructure[] dsX = new DerivativeStructure[n];
for (int i = 0; i < n; ++i) {
dsX[i] = new DerivativeStructure(n, 1, i, x[i]);
}
final DerivativeStructure y = f.value(dsX);
// extract gradient
final double[] gradient = new double[n];
final int[] orders = new int[n];
for (int i = 0; i < n; ++i) {
orders[i] = 1;
gradient[i] = y.getPartialDerivative(orders);
orders[i] = 0;
}
return gradient;