/// <summary>
/// Computes the derivative for a simpler unidimensional function.
/// </summary>
///
/// <param name="function">The function to be differentiated.</param>
/// <param name="order">The derivative order that should be obtained. Default is 1.</param>
/// <param name="stepSize">The relative step size used to approximate the derivatives. Default is 0.01.</param>
/// <param name="value">The value <c>x</c> at which the derivative should be evaluated.</param>
///
/// <returns>The derivative of the function at the point <paramref name="value">x</paramref>.</returns>
///
public static double Derivative(Func <double, double> function, double value, int order, double stepSize)
{
double output = function(value);
double original = value;
if (original != 0.0)
{
stepSize *= System.Math.Abs(original);
}
// Create the interpolation points
double[] outputs = new double[coefficientCache.Length];
int center = (outputs.Length - 1) / 2;
for (int i = 0; i < outputs.Length; i++)
{
if (i != center)
{
// Recompute the function to measure its importance
outputs[i] = function(original + (i - center) * stepSize);
}
else
{
// The center point is the original function
outputs[i] = output;
}
}
return(FiniteDifferences.Interpolate(coefficientCache,
outputs, order, center, stepSize));
}