internal virtual double DerivativeAt(double x) { double[] g = function.DerivativeAt(VectorOf(x)); double d = 0.0; for (int j = 0; j < g.Length; j++) { d += g[j] * direction[j]; } return(d); }
public static void GradientCheck(IDiffFunction f, double[] x, int[] ind, double eps) { // System.err.print("Testing grad <"); double[] testGrad = EstimateGradient(f, x, ind, eps); // System.err.println(">"); double[] fullGrad = f.DerivativeAt(x); double[] fGrad = new double[ind.Length]; for (int i = 0; i < ind.Length; i++) { fGrad[i] = fullGrad[ind[i]]; } double[] diff = ArrayMath.PairwiseSubtract(testGrad, fGrad); System.Console.Error.WriteLine("1-norm:" + ArrayMath.Norm_1(diff)); NUnit.Framework.Assert.AreEqual(0.0, ArrayMath.Norm_1(diff), 2 * eps); System.Console.Error.WriteLine("2-norm:" + ArrayMath.Norm(diff)); NUnit.Framework.Assert.AreEqual(0.0, ArrayMath.Norm(diff), 2 * eps); System.Console.Error.WriteLine("inf-norm:" + ArrayMath.Norm_inf(diff)); NUnit.Framework.Assert.AreEqual(0.0, ArrayMath.Norm_inf(diff), 2 * eps); System.Console.Error.WriteLine("pearson:" + ArrayMath.PearsonCorrelation(testGrad, fGrad)); NUnit.Framework.Assert.AreEqual(1.0, ArrayMath.PearsonCorrelation(testGrad, fGrad), 2 * eps); }