blob: b11e52210d75adea1768f107030e287682674384 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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: fredp@google.com (Fred Pighin)
30//
31// TODO(sameeragarwal): More comprehensive testing with larger and
32// more badly conditioned problem.
33
Austin Schuh70cc9552019-01-21 19:46:48 -080034#include "ceres/conjugate_gradients_solver.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
36#include <memory>
37
38#include "ceres/internal/eigen.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/linear_solver.h"
40#include "ceres/triplet_sparse_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/types.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080042#include "gtest/gtest.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080043
44namespace ceres {
45namespace internal {
46
47TEST(ConjugateGradientTest, Solves3x3IdentitySystem) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048 double diagonal[] = {1.0, 1.0, 1.0};
49 std::unique_ptr<TripletSparseMatrix> A(
50 TripletSparseMatrix::CreateSparseDiagonalMatrix(diagonal, 3));
Austin Schuh70cc9552019-01-21 19:46:48 -080051 Vector b(3);
52 Vector x(3);
53
54 b(0) = 1.0;
55 b(1) = 2.0;
56 b(2) = 3.0;
57
58 x(0) = 1;
59 x(1) = 1;
60 x(2) = 1;
61
62 LinearSolver::Options options;
63 options.max_num_iterations = 10;
64
65 LinearSolver::PerSolveOptions per_solve_options;
66 per_solve_options.r_tolerance = 1e-9;
67
68 ConjugateGradientsSolver solver(options);
69 LinearSolver::Summary summary =
70 solver.Solve(A.get(), b.data(), per_solve_options, x.data());
71
72 EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
73 ASSERT_EQ(summary.num_iterations, 1);
74
75 ASSERT_DOUBLE_EQ(1, x(0));
76 ASSERT_DOUBLE_EQ(2, x(1));
77 ASSERT_DOUBLE_EQ(3, x(2));
78}
79
Austin Schuh70cc9552019-01-21 19:46:48 -080080TEST(ConjuateGradientTest, Solves3x3SymmetricSystem) {
81 std::unique_ptr<TripletSparseMatrix> A(new TripletSparseMatrix(3, 3, 9));
82 Vector b(3);
83 Vector x(3);
84
85 // | 2 -1 0|
86 // A = |-1 2 -1| is symmetric positive definite.
87 // | 0 -1 2|
88 int* Ai = A->mutable_rows();
89 int* Aj = A->mutable_cols();
90 double* Ax = A->mutable_values();
91 int counter = 0;
92 for (int i = 0; i < 3; ++i) {
93 for (int j = 0; j < 3; ++j) {
94 Ai[counter] = i;
95 Aj[counter] = j;
96 ++counter;
97 }
98 }
99 Ax[0] = 2.;
100 Ax[1] = -1.;
101 Ax[2] = 0;
102 Ax[3] = -1.;
103 Ax[4] = 2;
104 Ax[5] = -1;
105 Ax[6] = 0;
106 Ax[7] = -1;
107 Ax[8] = 2;
108 A->set_num_nonzeros(9);
109
110 b(0) = -1;
111 b(1) = 0;
112 b(2) = 3;
113
114 x(0) = 1;
115 x(1) = 1;
116 x(2) = 1;
117
118 LinearSolver::Options options;
119 options.max_num_iterations = 10;
120
121 LinearSolver::PerSolveOptions per_solve_options;
122 per_solve_options.r_tolerance = 1e-9;
123
124 ConjugateGradientsSolver solver(options);
125 LinearSolver::Summary summary =
126 solver.Solve(A.get(), b.data(), per_solve_options, x.data());
127
128 EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
129
130 ASSERT_DOUBLE_EQ(0, x(0));
131 ASSERT_DOUBLE_EQ(1, x(1));
132 ASSERT_DOUBLE_EQ(2, x(2));
133}
134
135} // namespace internal
136} // namespace ceres