public void ScalarFunctonDerivativeTest()
 {
     Func<double, double> f = x => 1 / x;
     var J = new NumericalJacobian();
     var jeval = J.Evaluate(f, 2);
     Assert.AreEqual((double)-1 / 4, jeval[0], 1e-7);
 }
Ejemplo n.º 2
0
        /*Check that the returned gradient is equal to the numerical gradient*/
        public void GradientMatchNumerical()
        {
            //
            var v = Vector <double> .Build.Dense(3);

            //
            ScanMatching.rEBb = Matrix <double> .Build.Random(2, 50);

            ScanMatching.rPNn = Matrix <double> .Build.Random(2, 15);

            var hp = new HelperFunctions();
            Func <double[], double> f = x => hp.ExtractCost(x);

            var derivEst   = new NumericalJacobian(5, 2);
            var gradExpect = derivEst.Evaluate(f, v.ToArray());

            (var ftmp, var gradActual, var Htmp) = ScanMatching.Cost(v);



            // Not sure how to do assert array equal with precision.
            for (int i = 0; i < gradExpect.Length; i++)
            {
                Assert.Equal(gradExpect[i], gradActual.ToArray()[i], 5);
            }
        }
Ejemplo n.º 3
0
        public void ScalarFunctonDerivativeTest()
        {
            Func <double, double> f = x => 1 / x;
            var J     = new NumericalJacobian();
            var jeval = J.Evaluate(f, 2);

            Assert.AreEqual((double)-1 / 4, jeval[0], 1e-7);
        }
 public void OneEquationVectorFunctionTest()
 {
     Func<double[], double> f = x => Math.Sin(x[0] * x[1]) * Math.Exp(-x[0] / 2) + x[1] / x[0];
     var J = new NumericalJacobian();
     var jeval = J.Evaluate(f, new double[] { 1, 1 });
     Assert.AreEqual(-0.92747906175, jeval[0], 1e-9);
     Assert.AreEqual(1.32770991402, jeval[1], 1e-9);
     Assert.AreEqual(4, J.FunctionEvaluations);
     J.ResetFunctionEvaluations();
     Assert.AreEqual(0, J.FunctionEvaluations);
 }
Ejemplo n.º 5
0
        public void OneEquationVectorFunctionTest()
        {
            Func <double[], double> f = x => Math.Sin(x[0] * x[1]) * Math.Exp(-x[0] / 2) + x[1] / x[0];
            var J     = new NumericalJacobian();
            var jeval = J.Evaluate(f, new double[] { 1, 1 });

            Assert.AreEqual(-0.92747906175, jeval[0], 1e-9);
            Assert.AreEqual(1.32770991402, jeval[1], 1e-9);
            Assert.AreEqual(4, J.FunctionEvaluations);
            J.ResetFunctionEvaluations();
            Assert.AreEqual(0, J.FunctionEvaluations);
        }
 public void VectorFunctionJacobianTest()
 {
     Func<double[], double>[] f =
     {
         (x) => Math.Pow(x[0], 2)*x[1],
         (x) => 5*x[0] + Math.Sin(x[1])
     };
     var j = new NumericalJacobian();
     var jeval = j.Evaluate(f, new double[] { 1, 1 });
     Assert.AreEqual(2, jeval[0, 0]);
     Assert.AreEqual(1, jeval[0, 1]);
     Assert.AreEqual(5, jeval[1, 0]);
     Assert.AreEqual(Math.Cos(1), jeval[1, 1], 1e-7);
 }
Ejemplo n.º 7
0
        public void VectorFunctionJacobianTest()
        {
            Func <double[], double>[] f =
            {
                (x) => Math.Pow(x[0], 2) * x[1],
                (x) => 5 * x[0] + Math.Sin(x[1])
            };
            var j     = new NumericalJacobian();
            var jeval = j.Evaluate(f, new double[] { 1, 1 });

            Assert.AreEqual(2, jeval[0, 0]);
            Assert.AreEqual(1, jeval[0, 1]);
            Assert.AreEqual(5, jeval[1, 0]);
            Assert.AreEqual(Math.Cos(1), jeval[1, 1], 1e-7);
        }