10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/normal_prior.h"
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <cstddef>
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "gtest/gtest.h"
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/eigen.h"
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/random.h"
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid RandomVector(Vector* v) {
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int r = 0; r < v->rows(); ++r)
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    (*v)[r] = 2 * RandDouble() - 1;
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid RandomMatrix(Matrix* m) {
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int r = 0; r < m->rows(); ++r) {
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    for (int c = 0; c < m->cols(); ++c) {
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      (*m)(r, c) = 2 * RandDouble() - 1;
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongTEST(NormalPriorTest, ResidualAtRandomPosition) {
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  srand(5);
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int num_rows = 1; num_rows < 5; ++num_rows) {
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    for (int num_cols = 1; num_cols < 5; ++num_cols) {
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      Vector b(num_cols);
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      RandomVector(&b);
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      Matrix A(num_rows, num_cols);
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      RandomMatrix(&A);
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double * x = new double[num_cols];
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      for (int i = 0; i < num_cols; ++i)
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        x[i] = 2 * RandDouble() - 1;
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double * jacobian = new double[num_rows * num_cols];
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      Vector residuals(num_rows);
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      NormalPrior prior(A, b);
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      prior.Evaluate(&x, residuals.data(), &jacobian);
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // Compare the norm of the residual
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double residual_diff_norm =
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong          (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm();
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      EXPECT_NEAR(residual_diff_norm, 0, 1e-10);
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // Compare the jacobians
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      MatrixRef J(jacobian, num_rows, num_cols);
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double jacobian_diff_norm = (J - A).norm();
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      EXPECT_NEAR(jacobian_diff_norm, 0.0, 1e-10);
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      delete []x;
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      delete []jacobian;
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongTEST(NormalPriorTest, ResidualAtRandomPositionNullJacobians) {
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  srand(5);
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  for (int num_rows = 1; num_rows < 5; ++num_rows) {
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    for (int num_cols = 1; num_cols < 5; ++num_cols) {
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      Vector b(num_cols);
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      RandomVector(&b);
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      Matrix A(num_rows, num_cols);
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      RandomMatrix(&A);
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double * x = new double[num_cols];
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      for (int i = 0; i < num_cols; ++i)
1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        x[i] = 2 * RandDouble() - 1;
1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double* jacobians[1];
1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      jacobians[0] = NULL;
1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      Vector residuals(num_rows);
1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      NormalPrior prior(A, b);
1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      prior.Evaluate(&x, residuals.data(), jacobians);
1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // Compare the norm of the residual
1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      double residual_diff_norm =
1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong          (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm();
1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      EXPECT_NEAR(residual_diff_norm, 0, 1e-10);
1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      prior.Evaluate(&x, residuals.data(), NULL);
1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      // Compare the norm of the residual
1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      residual_diff_norm =
1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong          (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm();
1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      EXPECT_NEAR(residual_diff_norm, 0, 1e-10);
1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      delete []x;
1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
134