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: fredp@google.com (Fred Pighin)
31 // TODO(sameeragarwal): More comprehensive testing with larger and
32 // more badly conditioned problem.
34 #include "gtest/gtest.h"
35 #include "ceres/conjugate_gradients_solver.h"
36 #include "ceres/linear_solver.h"
37 #include "ceres/triplet_sparse_matrix.h"
38 #include "ceres/internal/eigen.h"
39 #include "ceres/internal/scoped_ptr.h"
40 #include "ceres/types.h"
45 TEST(ConjugateGradientTest, Solves3x3IdentitySystem) {
46 double diagonal[] = { 1.0, 1.0, 1.0 };
47 scoped_ptr<TripletSparseMatrix>
48 A(TripletSparseMatrix::CreateSparseDiagonalMatrix(diagonal, 3));
60 LinearSolver::Options options;
61 options.max_num_iterations = 10;
63 LinearSolver::PerSolveOptions per_solve_options;
64 per_solve_options.r_tolerance = 1e-9;
66 ConjugateGradientsSolver solver(options);
67 LinearSolver::Summary summary =
68 solver.Solve(A.get(), b.data(), per_solve_options, x.data());
70 EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
71 ASSERT_EQ(summary.num_iterations, 1);
73 ASSERT_DOUBLE_EQ(1, x(0));
74 ASSERT_DOUBLE_EQ(2, x(1));
75 ASSERT_DOUBLE_EQ(3, x(2));
79 TEST(ConjuateGradientTest, Solves3x3SymmetricSystem) {
80 scoped_ptr<TripletSparseMatrix> A(new TripletSparseMatrix(3, 3, 9));
85 // A = |-1 2 -1| is symmetric positive definite.
87 int* Ai = A->mutable_rows();
88 int* Aj = A->mutable_cols();
89 double* Ax = A->mutable_values();
91 for (int i = 0; i < 3; ++i) {
92 for (int j = 0; j < 3; ++j) {
107 A->set_num_nonzeros(9);
117 LinearSolver::Options options;
118 options.max_num_iterations = 10;
120 LinearSolver::PerSolveOptions per_solve_options;
121 per_solve_options.r_tolerance = 1e-9;
123 ConjugateGradientsSolver solver(options);
124 LinearSolver::Summary summary =
125 solver.Solve(A.get(), b.data(), per_solve_options, x.data());
127 EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
129 ASSERT_DOUBLE_EQ(0, x(0));
130 ASSERT_DOUBLE_EQ(1, x(1));
131 ASSERT_DOUBLE_EQ(2, x(2));
134 } // namespace internal