internal static void CheckGradient(Func<double[], double[]> value, double[] probe)
{
double[] original = (double[])probe.Clone();
double[] result = value(probe);
if (result == probe)
throw new InvalidOperationException(
"The gradient function should not return the parameter vector.");
if (probe.Length != result.Length)
throw new InvalidOperationException(
"The gradient vector should have the same length as the number of parameters.");
for (int i = 0; i < probe.Length; i++)
if (!probe[i].IsEqual(original[i], 0))
throw new InvalidOperationException("The gradient function shouldn't modify the parameter vector.");
}