2121
2222namespace {
2323
24+ constexpr int kCentralDifference = 0 ;
25+ constexpr int kForwardDifference = 1 ;
26+ constexpr int kBackwardDifference = 2 ;
27+
2428double quadratic_cost (const Eigen::VectorXd &x) {
2529 return x (0 ) * x (0 ) + 3.0 * x (0 ) * x (1 ) + std::sin (x (1 ));
2630}
@@ -55,7 +59,9 @@ TEST(FiniteDifferenceTest, GradientMatchesAnalyticForAllModes) {
5559 const Eigen::Vector2d x (0.4 , -0.2 );
5660 const Eigen::Vector2d grad_expected = expected_gradient (x);
5761
58- for (const int mode : std::array<int , 3 >{0 , 1 , 2 }) {
62+ for (const int mode :
63+ std::array<int , 3 >{kCentralDifference , kForwardDifference ,
64+ kBackwardDifference }) {
5965 const Eigen::VectorXd grad =
6066 cddp::finite_difference_gradient (quadratic_cost, x, 1e-6 , mode);
6167 EXPECT_TRUE (grad.isApprox (grad_expected, 1e-4 )) << " mode=" << mode;
@@ -66,7 +72,9 @@ TEST(FiniteDifferenceTest, JacobianMatchesAnalyticForAllModes) {
6672 const Eigen::Vector2d x (-0.3 , 0.6 );
6773 const Eigen::Matrix2d jac_expected = expected_jacobian (x);
6874
69- for (const int mode : std::array<int , 3 >{0 , 1 , 2 }) {
75+ for (const int mode :
76+ std::array<int , 3 >{kCentralDifference , kForwardDifference ,
77+ kBackwardDifference }) {
7078 const Eigen::MatrixXd jac =
7179 cddp::finite_difference_jacobian (vector_function, x, 1e-6 , mode);
7280 EXPECT_TRUE (jac.isApprox (jac_expected, 1e-4 )) << " mode=" << mode;
@@ -77,7 +85,9 @@ TEST(FiniteDifferenceTest, HessianMatchesAnalyticForAllModes) {
7785 const Eigen::Vector2d x (0.4 , -0.2 );
7886 const Eigen::Matrix2d hess_expected = expected_hessian (x);
7987
80- for (const int mode : std::array<int , 3 >{0 , 1 , 2 }) {
88+ for (const int mode :
89+ std::array<int , 3 >{kCentralDifference , kForwardDifference ,
90+ kBackwardDifference }) {
8191 const Eigen::MatrixXd hess =
8292 cddp::finite_difference_hessian (quadratic_cost, x, 1e-5 , mode);
8393 EXPECT_TRUE (hess.isApprox (hess_expected, 2e-2 )) << " mode=" << mode;
0 commit comments