Blame internal/ceres/numeric_diff_test_utils.cc

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//         tbennun@gmail.com (Tal Ben-Nun)
Packit ea1746
Packit ea1746
#include "ceres/numeric_diff_test_utils.h"
Packit ea1746
Packit ea1746
#include <algorithm>
Packit ea1746
#include <cmath>
Packit ea1746
#include "ceres/cost_function.h"
Packit ea1746
#include "ceres/internal/macros.h"
Packit ea1746
#include "ceres/test_util.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "gtest/gtest.h"
Packit ea1746
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
bool EasyFunctor::operator()(const double* x1,
Packit ea1746
                             const double* x2,
Packit ea1746
                             double* residuals) const {
Packit ea1746
  residuals[0] = residuals[1] = residuals[2] = 0;
Packit ea1746
  for (int i = 0; i < 5; ++i) {
Packit ea1746
    residuals[0] += x1[i] * x2[i];
Packit ea1746
    residuals[2] += x2[i] * x2[i];
Packit ea1746
  }
Packit ea1746
  residuals[1] = residuals[0] * residuals[0];
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
void EasyFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
Packit ea1746
    const CostFunction& cost_function,
Packit ea1746
    NumericDiffMethodType method) const {
Packit ea1746
  // The x1[0] is made deliberately small to test the performance near
Packit ea1746
  // zero.
Packit ea1746
  double x1[] = { 1e-64, 2.0, 3.0, 4.0, 5.0 };
Packit ea1746
  double x2[] = { 9.0, 9.0, 5.0, 5.0, 1.0 };
Packit ea1746
  double *parameters[] = { &x1[0], &x2[0] };
Packit ea1746
Packit ea1746
  double dydx1[15];  // 3 x 5, row major.
Packit ea1746
  double dydx2[15];  // 3 x 5, row major.
Packit ea1746
  double *jacobians[2] = { &dydx1[0], &dydx2[0] };
Packit ea1746
Packit ea1746
  double residuals[3] = {-1e-100, -2e-100, -3e-100 };
Packit ea1746
Packit ea1746
  ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
Packit ea1746
                                     &residuals[0],
Packit ea1746
                                     &jacobians[0]));
Packit ea1746
Packit ea1746
  double expected_residuals[3];
Packit ea1746
  EasyFunctor functor;
Packit ea1746
  functor(x1, x2, expected_residuals);
Packit ea1746
  EXPECT_EQ(expected_residuals[0], residuals[0]);
Packit ea1746
  EXPECT_EQ(expected_residuals[1], residuals[1]);
Packit ea1746
  EXPECT_EQ(expected_residuals[2], residuals[2]);
Packit ea1746
Packit ea1746
  double tolerance = 0.0;
Packit ea1746
  switch (method) {
Packit ea1746
    default:
Packit ea1746
    case CENTRAL:
Packit ea1746
      tolerance = 3e-9;
Packit ea1746
      break;
Packit ea1746
Packit ea1746
    case FORWARD:
Packit ea1746
      tolerance = 2e-5;
Packit ea1746
      break;
Packit ea1746
Packit ea1746
    case RIDDERS:
Packit ea1746
      tolerance = 1e-13;
Packit ea1746
      break;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  for (int i = 0; i < 5; ++i) {
Packit ea1746
    ExpectClose(x2[i],                    dydx1[5 * 0 + i], tolerance);  // y1
Packit ea1746
    ExpectClose(x1[i],                    dydx2[5 * 0 + i], tolerance);
Packit ea1746
    ExpectClose(2 * x2[i] * residuals[0], dydx1[5 * 1 + i], tolerance);  // y2
Packit ea1746
    ExpectClose(2 * x1[i] * residuals[0], dydx2[5 * 1 + i], tolerance);
Packit ea1746
    ExpectClose(0.0,                      dydx1[5 * 2 + i], tolerance);  // y3
Packit ea1746
    ExpectClose(2 * x2[i],                dydx2[5 * 2 + i], tolerance);
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
bool TranscendentalFunctor::operator()(const double* x1,
Packit ea1746
                                       const double* x2,
Packit ea1746
                                       double* residuals) const {
Packit ea1746
  double x1x2 = 0;
Packit ea1746
  for (int i = 0; i < 5; ++i) {
Packit ea1746
    x1x2 += x1[i] * x2[i];
Packit ea1746
  }
Packit ea1746
  residuals[0] = sin(x1x2);
Packit ea1746
  residuals[1] = exp(-x1x2 / 10);
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
Packit ea1746
    const CostFunction& cost_function,
Packit ea1746
    NumericDiffMethodType method) const {
Packit ea1746
  struct {
Packit ea1746
    double x1[5];
Packit ea1746
    double x2[5];
Packit ea1746
  } kTests[] = {
Packit ea1746
    { { 1.0, 2.0, 3.0, 4.0, 5.0 },  // No zeros.
Packit ea1746
      { 9.0, 9.0, 5.0, 5.0, 1.0 },
Packit ea1746
    },
Packit ea1746
    { { 0.0, 2.0, 3.0, 0.0, 5.0 },  // Some zeros x1.
Packit ea1746
      { 9.0, 9.0, 5.0, 5.0, 1.0 },
Packit ea1746
    },
Packit ea1746
    { { 1.0, 2.0, 3.0, 1.0, 5.0 },  // Some zeros x2.
Packit ea1746
      { 0.0, 9.0, 0.0, 5.0, 0.0 },
Packit ea1746
    },
Packit ea1746
    { { 0.0, 0.0, 0.0, 0.0, 0.0 },  // All zeros x1.
Packit ea1746
      { 9.0, 9.0, 5.0, 5.0, 1.0 },
Packit ea1746
    },
Packit ea1746
    { { 1.0, 2.0, 3.0, 4.0, 5.0 },  // All zeros x2.
Packit ea1746
      { 0.0, 0.0, 0.0, 0.0, 0.0 },
Packit ea1746
    },
Packit ea1746
    { { 0.0, 0.0, 0.0, 0.0, 0.0 },  // All zeros.
Packit ea1746
      { 0.0, 0.0, 0.0, 0.0, 0.0 },
Packit ea1746
    },
Packit ea1746
  };
Packit ea1746
Packit ea1746
  for (int k = 0; k < CERES_ARRAYSIZE(kTests); ++k) {
Packit ea1746
    double *x1 = &(kTests[k].x1[0]);
Packit ea1746
    double *x2 = &(kTests[k].x2[0]);
Packit ea1746
    double *parameters[] = { x1, x2 };
Packit ea1746
Packit ea1746
    double dydx1[10];
Packit ea1746
    double dydx2[10];
Packit ea1746
    double *jacobians[2] = { &dydx1[0], &dydx2[0] };
Packit ea1746
Packit ea1746
    double residuals[2];
Packit ea1746
Packit ea1746
    ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
Packit ea1746
                                       &residuals[0],
Packit ea1746
                                       &jacobians[0]));
Packit ea1746
    double x1x2 = 0;
Packit ea1746
    for (int i = 0; i < 5; ++i) {
Packit ea1746
      x1x2 += x1[i] * x2[i];
Packit ea1746
    }
Packit ea1746
Packit ea1746
    double tolerance = 0.0;
Packit ea1746
    switch (method) {
Packit ea1746
      default:
Packit ea1746
      case CENTRAL:
Packit ea1746
        tolerance = 2e-7;
Packit ea1746
        break;
Packit ea1746
Packit ea1746
      case FORWARD:
Packit ea1746
        tolerance = 2e-5;
Packit ea1746
        break;
Packit ea1746
Packit ea1746
      case RIDDERS:
Packit ea1746
        tolerance = 3e-12;
Packit ea1746
        break;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    for (int i = 0; i < 5; ++i) {
Packit ea1746
      ExpectClose( x2[i] * cos(x1x2),              dydx1[5 * 0 + i], tolerance);
Packit ea1746
      ExpectClose( x1[i] * cos(x1x2),              dydx2[5 * 0 + i], tolerance);
Packit ea1746
      ExpectClose(-x2[i] * exp(-x1x2 / 10.) / 10., dydx1[5 * 1 + i], tolerance);
Packit ea1746
      ExpectClose(-x1[i] * exp(-x1x2 / 10.) / 10., dydx2[5 * 1 + i], tolerance);
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
bool ExponentialFunctor::operator()(const double* x1,
Packit ea1746
                                    double* residuals) const {
Packit ea1746
  residuals[0] = exp(x1[0]);
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
Packit ea1746
    const CostFunction& cost_function) const {
Packit ea1746
  // Evaluating the functor at specific points for testing.
Packit ea1746
  double kTests[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
Packit ea1746
Packit ea1746
  // Minimal tolerance w.r.t. the cost function and the tests.
Packit ea1746
  const double kTolerance = 2e-14;
Packit ea1746
Packit ea1746
  for (int k = 0; k < CERES_ARRAYSIZE(kTests); ++k) {
Packit ea1746
    double *parameters[] = { &kTests[k] };
Packit ea1746
    double dydx;
Packit ea1746
    double *jacobians[1] = { &dydx };
Packit ea1746
    double residual;
Packit ea1746
Packit ea1746
    ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
Packit ea1746
                                       &residual,
Packit ea1746
                                       &jacobians[0]));
Packit ea1746
Packit ea1746
Packit ea1746
    double expected_result = exp(kTests[k]);
Packit ea1746
Packit ea1746
    // Expect residual to be close to exp(x).
Packit ea1746
    ExpectClose(residual, expected_result, kTolerance);
Packit ea1746
Packit ea1746
    // Check evaluated differences. dydx should also be close to exp(x).
Packit ea1746
    ExpectClose(dydx, expected_result, kTolerance);
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
bool RandomizedFunctor::operator()(const double* x1,
Packit ea1746
                                   double* residuals) const {
Packit ea1746
  double random_value = static_cast<double>(rand()) /
Packit ea1746
      static_cast<double>(RAND_MAX);
Packit ea1746
Packit ea1746
  // Normalize noise to [-factor, factor].
Packit ea1746
  random_value *= 2.0;
Packit ea1746
  random_value -= 1.0;
Packit ea1746
  random_value *= noise_factor_;
Packit ea1746
Packit ea1746
  residuals[0] = x1[0] * x1[0] + random_value;
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
Packit ea1746
    const CostFunction& cost_function) const {
Packit ea1746
  double kTests[] = { 0.0, 1.0, 3.0, 4.0, 50.0 };
Packit ea1746
Packit ea1746
  const double kTolerance = 2e-4;
Packit ea1746
Packit ea1746
  // Initialize random number generator with given seed.
Packit ea1746
  srand(random_seed_);
Packit ea1746
Packit ea1746
  for (int k = 0; k < CERES_ARRAYSIZE(kTests); ++k) {
Packit ea1746
    double *parameters[] = { &kTests[k] };
Packit ea1746
    double dydx;
Packit ea1746
    double *jacobians[1] = { &dydx };
Packit ea1746
    double residual;
Packit ea1746
Packit ea1746
    ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
Packit ea1746
                                       &residual,
Packit ea1746
                                       &jacobians[0]));
Packit ea1746
Packit ea1746
    // Expect residual to be close to x^2 w.r.t. noise factor.
Packit ea1746
    ExpectClose(residual, kTests[k] * kTests[k], noise_factor_);
Packit ea1746
Packit ea1746
    // Check evaluated differences. (dy/dx = ~2x)
Packit ea1746
    ExpectClose(dydx, 2 * kTests[k], kTolerance);
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres