TEST(CentralDifference, Gradient){ // simple function y <- 3*a-b class Func : public Problem<double> { public: double value(const Vector<double> &x) { return 3*x[0]-x[1]; } }; Vector<double> x0(2); x0(0) = 0; x0(1) = 0; Func f; Vector<double> grad(2); // check from fast/bad to slower/better approximation of the gradient for (int accuracy = 0; accuracy < 4; ++accuracy) { f.finiteGradient(x0, grad, accuracy); EXPECT_NEAR(grad(0), 3, PRECISION); EXPECT_NEAR(grad(1), -1, PRECISION); } }
TYPED_TEST(CentralDifference, Gradient){ // simple function y <- 3*a-b class Func : public Problem<TypeParam, 2> { public: using typename Problem<TypeParam, 2>::TVector; TypeParam value(const TVector &x) { return 3*x[0]-x[1]; } }; typename Func::TVector x0; x0(0) = 0; x0(1) = 0; Func f; typename Func::TVector grad; // check from fast/bad to slower/better approximation of the gradient for (int accuracy = 0; accuracy < 4; ++accuracy) { f.finiteGradient(x0, grad, accuracy); EXPECT_NEAR(grad(0), 3, PRECISION); EXPECT_NEAR(grad(1), -1, PRECISION); } }