Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / dense_linear_solver_test.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2017 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 //   this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 //   this list of conditions and the following disclaimer in the documentation
12 //   and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 //   used to endorse or promote products derived from this software without
15 //   specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
30
31 #include "ceres/casts.h"
32 #include "ceres/internal/scoped_ptr.h"
33 #include "ceres/linear_least_squares_problems.h"
34 #include "ceres/linear_solver.h"
35 #include "ceres/triplet_sparse_matrix.h"
36 #include "ceres/types.h"
37 #include "glog/logging.h"
38 #include "gtest/gtest.h"
39
40 namespace ceres {
41 namespace internal {
42
43 typedef ::testing::
44     tuple<LinearSolverType, DenseLinearAlgebraLibraryType, bool, int>
45         Param;
46
47 std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
48   Param param = info.param;
49   std::stringstream ss;
50   ss << LinearSolverTypeToString(::testing::get<0>(param)) << "_"
51      << DenseLinearAlgebraLibraryTypeToString(::testing::get<1>(param)) << "_"
52      << (::testing::get<2>(param) ? "Regularized" : "Unregularized") << "_"
53      << ::testing::get<3>(param);
54   return ss.str();
55 }
56
57 class DenseLinearSolverTest : public ::testing::TestWithParam<Param> {};
58
59 TEST_P(DenseLinearSolverTest, _) {
60   Param param = GetParam();
61   const bool regularized = testing::get<2>(param);
62
63   scoped_ptr<LinearLeastSquaresProblem> problem(
64       CreateLinearLeastSquaresProblemFromId(testing::get<3>(param)));
65   DenseSparseMatrix lhs(*down_cast<TripletSparseMatrix*>(problem->A.get()));
66
67   const int num_cols = lhs.num_cols();
68   const int num_rows = lhs.num_rows();
69
70   Vector rhs = Vector::Zero(num_rows + num_cols);
71   rhs.head(num_rows) = ConstVectorRef(problem->b.get(), num_rows);
72
73   LinearSolver::Options options;
74   options.type = ::testing::get<0>(param);
75   options.dense_linear_algebra_library_type = ::testing::get<1>(param);
76   scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
77
78   LinearSolver::PerSolveOptions per_solve_options;
79   if (regularized) {
80     per_solve_options.D = problem->D.get();
81   }
82
83   Vector solution(num_cols);
84   LinearSolver::Summary summary =
85       solver->Solve(&lhs, rhs.data(), per_solve_options, solution.data());
86   EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
87
88   // If solving for the regularized solution, add the diagonal to the
89   // matrix. This makes subsequent computations simpler.
90   if (testing::get<2>(param)) {
91     lhs.AppendDiagonal(problem->D.get());
92   };
93
94   Vector tmp = Vector::Zero(num_rows + num_cols);
95   lhs.RightMultiply(solution.data(), tmp.data());
96   Vector actual_normal_rhs = Vector::Zero(num_cols);
97   lhs.LeftMultiply(tmp.data(), actual_normal_rhs.data());
98
99   Vector expected_normal_rhs = Vector::Zero(num_cols);
100   lhs.LeftMultiply(rhs.data(), expected_normal_rhs.data());
101   const double residual = (expected_normal_rhs - actual_normal_rhs).norm() /
102                           expected_normal_rhs.norm();
103
104   EXPECT_NEAR(residual, 0.0, 10 * std::numeric_limits<double>::epsilon());
105 }
106
107 // TODO(sameeragarwal): Should we move away from hard coded linear
108 // least squares problem to randomly generated ones?
109 #ifndef CERES_NO_LAPACK
110
111 INSTANTIATE_TEST_CASE_P(
112     DenseLinearSolver,
113     DenseLinearSolverTest,
114     ::testing::Combine(::testing::Values(DENSE_QR, DENSE_NORMAL_CHOLESKY),
115                        ::testing::Values(EIGEN, LAPACK),
116                        ::testing::Values(true, false),
117                        ::testing::Values(0, 1)),
118     ParamInfoToString);
119
120 #else
121
122 INSTANTIATE_TEST_CASE_P(
123     DenseLinearSolver,
124     DenseLinearSolverTest,
125     ::testing::Combine(::testing::Values(DENSE_QR, DENSE_NORMAL_CHOLESKY),
126                        ::testing::Values(EIGEN),
127                        ::testing::Values(true, false),
128                        ::testing::Values(0, 1)),
129     ParamInfoToString);
130
131 #endif
132
133 }  // namespace internal
134 }  // namespace ceres