static private CheckGradient ( double[]>.Func |
||
value | double[]>.Func | |
probe | double | |
return | void |
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.");
}
/// <summary> /// Finds the minimum value of a function. The solution vector /// will be made available at the <see cref="IOptimizationMethod.Solution"/> property. /// </summary> /// /// <returns>Returns <c>true</c> if the method converged to a <see cref="IOptimizationMethod.Solution"/>. /// In this case, the found value will also be available at the <see cref="IOptimizationMethod.Value"/> /// property.</returns> /// public override bool Minimize() { if (Gradient == null) { throw new InvalidOperationException("gradient"); } NonlinearObjectiveFunction.CheckGradient(Gradient, Solution); return(base.Minimize()); }