blob: be9105621b694ee405c9c0ac014e64490eaf4bd6 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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 <glog/logging.h>
32#include "gtest/gtest.h"
33#include "ceres/casts.h"
34#include "ceres/compressed_row_sparse_matrix.h"
35#include "ceres/linear_least_squares_problems.h"
36#include "ceres/linear_solver.h"
37#include "ceres/triplet_sparse_matrix.h"
38#include "ceres/internal/scoped_ptr.h"
39#include "ceres/types.h"
40
41
42namespace ceres {
43namespace internal {
44
45class UnsymmetricLinearSolverTest : public ::testing::Test {
46 protected :
47 virtual void SetUp() {
48 scoped_ptr<LinearLeastSquaresProblem> problem(
49 CreateLinearLeastSquaresProblemFromId(0));
50
51 CHECK_NOTNULL(problem.get());
52 A_.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
53 b_.reset(problem->b.release());
54 D_.reset(problem->D.release());
55 sol1_.reset(problem->x.release());
56 sol2_.reset(problem->x_D.release());
57 x_.reset(new double[A_->num_cols()]);
58 }
59
60 void TestSolver(LinearSolverType linear_solver_type) {
61 LinearSolver::Options options;
62 options.type = linear_solver_type;
63 scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
64
65 LinearSolver::PerSolveOptions per_solve_options;
66
67 // Unregularized
68 LinearSolver::Summary summary =
69 solver->Solve(A_.get(), b_.get(), per_solve_options, x_.get());
70
71 EXPECT_EQ(summary.termination_type, TOLERANCE);
72
73 for (int i = 0; i < A_->num_cols(); ++i) {
74 EXPECT_NEAR(sol1_[i], x_[i], 1e-8);
75 }
76
77 // Regularized solution
78 per_solve_options.D = D_.get();
79 summary = solver->Solve(A_.get(), b_.get(), per_solve_options, x_.get());
80
81 EXPECT_EQ(summary.termination_type, TOLERANCE);
82
83 for (int i = 0; i < A_->num_cols(); ++i) {
84 EXPECT_NEAR(sol2_[i], x_[i], 1e-8);
85 }
86 }
87
88 scoped_ptr<TripletSparseMatrix> A_;
89 scoped_array<double> b_;
90 scoped_array<double> D_;
91 scoped_array<double> sol1_;
92 scoped_array<double> sol2_;
93
94 scoped_array<double> x_;
95};
96
97// TODO(keir): Reduce duplication.
98TEST_F(UnsymmetricLinearSolverTest, DenseQR) {
99 LinearSolver::Options options;
100 options.type = DENSE_QR;
101 scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
102
103 LinearSolver::PerSolveOptions per_solve_options;
104 DenseSparseMatrix A(*A_);
105
106 // Unregularized
107 LinearSolver::Summary summary =
108 solver->Solve(&A, b_.get(), per_solve_options, x_.get());
109
110 EXPECT_EQ(summary.termination_type, TOLERANCE);
111 for (int i = 0; i < A_->num_cols(); ++i) {
112 EXPECT_NEAR(sol1_[i], x_[i], 1e-8);
113 }
114
115 VectorRef x(x_.get(), A_->num_cols());
116 VectorRef b(b_.get(), A_->num_rows());
117 Vector r = A.matrix()*x - b;
118 LOG(INFO) << "r = A*x - b: \n" << r;
119
120 // Regularized solution
121 per_solve_options.D = D_.get();
122 summary = solver->Solve(&A, b_.get(), per_solve_options, x_.get());
123
124 EXPECT_EQ(summary.termination_type, TOLERANCE);
125 for (int i = 0; i < A_->num_cols(); ++i) {
126 EXPECT_NEAR(sol2_[i], x_[i], 1e-8);
127 }
128}
129
130#ifndef CERES_NO_SUITESPARSE
131TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholesky) {
132 LinearSolver::Options options;
133 options.type = SPARSE_NORMAL_CHOLESKY;
134 scoped_ptr<LinearSolver>solver(LinearSolver::Create(options));
135
136 LinearSolver::PerSolveOptions per_solve_options;
137 CompressedRowSparseMatrix A(*A_);
138
139 // Unregularized
140 LinearSolver::Summary summary =
141 solver->Solve(&A, b_.get(), per_solve_options, x_.get());
142
143 EXPECT_EQ(summary.termination_type, TOLERANCE);
144 for (int i = 0; i < A_->num_cols(); ++i) {
145 EXPECT_NEAR(sol1_[i], x_[i], 1e-8);
146 }
147
148 // Regularized solution
149 per_solve_options.D = D_.get();
150 summary = solver->Solve(&A, b_.get(), per_solve_options, x_.get());
151
152 EXPECT_EQ(summary.termination_type, TOLERANCE);
153 for (int i = 0; i < A_->num_cols(); ++i) {
154 EXPECT_NEAR(sol2_[i], x_[i], 1e-8);
155 }
156}
157#endif // CERES_NO_SUITESPARSE
158
159} // namespace internal
160} // namespace ceres