1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2017 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
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"
44 tuple<LinearSolverType, DenseLinearAlgebraLibraryType, bool, int>
47 std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
48 Param param = info.param;
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);
57 class DenseLinearSolverTest : public ::testing::TestWithParam<Param> {};
59 TEST_P(DenseLinearSolverTest, _) {
60 Param param = GetParam();
61 const bool regularized = testing::get<2>(param);
63 scoped_ptr<LinearLeastSquaresProblem> problem(
64 CreateLinearLeastSquaresProblemFromId(testing::get<3>(param)));
65 DenseSparseMatrix lhs(*down_cast<TripletSparseMatrix*>(problem->A.get()));
67 const int num_cols = lhs.num_cols();
68 const int num_rows = lhs.num_rows();
70 Vector rhs = Vector::Zero(num_rows + num_cols);
71 rhs.head(num_rows) = ConstVectorRef(problem->b.get(), num_rows);
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));
78 LinearSolver::PerSolveOptions per_solve_options;
80 per_solve_options.D = problem->D.get();
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);
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());
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());
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();
104 EXPECT_NEAR(residual, 0.0, 10 * std::numeric_limits<double>::epsilon());
107 // TODO(sameeragarwal): Should we move away from hard coded linear
108 // least squares problem to randomly generated ones?
109 #ifndef CERES_NO_LAPACK
111 INSTANTIATE_TEST_CASE_P(
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)),
122 INSTANTIATE_TEST_CASE_P(
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)),
133 } // namespace internal