blob: c8a15c02658556f850c0d5ad99df6ad8749a87ed [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
Keir Mierle8ebb0732012-04-30 23:09:08 -070031#include "ceres/casts.h"
32#include "ceres/compressed_row_sparse_matrix.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070033#include "ceres/internal/scoped_ptr.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070034#include "ceres/linear_least_squares_problems.h"
35#include "ceres/linear_solver.h"
36#include "ceres/triplet_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070037#include "ceres/types.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070038#include "glog/logging.h"
39#include "gtest/gtest.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070040
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());
Sameer Agarwalb0518732012-05-29 00:27:57 -070055 sol_unregularized_.reset(problem->x.release());
56 sol_regularized_.reset(problem->x_D.release());
Keir Mierle8ebb0732012-04-30 23:09:08 -070057 }
58
Sameer Agarwalb0518732012-05-29 00:27:57 -070059 void TestSolver(
60 LinearSolverType linear_solver_type,
61 SparseLinearAlgebraLibraryType sparse_linear_algebra_library) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070062 LinearSolver::Options options;
63 options.type = linear_solver_type;
Sameer Agarwalb0518732012-05-29 00:27:57 -070064 options.sparse_linear_algebra_library = sparse_linear_algebra_library;
Keir Mierle8ebb0732012-04-30 23:09:08 -070065 scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
66
67 LinearSolver::PerSolveOptions per_solve_options;
Sameer Agarwalb0518732012-05-29 00:27:57 -070068 LinearSolver::Summary unregularized_solve_summary;
69 LinearSolver::Summary regularized_solve_summary;
70 Vector x_unregularized(A_->num_cols());
71 Vector x_regularized(A_->num_cols());
Keir Mierle8ebb0732012-04-30 23:09:08 -070072
Sameer Agarwalb0518732012-05-29 00:27:57 -070073 scoped_ptr<SparseMatrix> transformed_A;
Keir Mierle8ebb0732012-04-30 23:09:08 -070074
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070075 if (linear_solver_type == DENSE_QR ||
76 linear_solver_type == DENSE_NORMAL_CHOLESKY) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070077 transformed_A.reset(new DenseSparseMatrix(*A_));
78 } else if (linear_solver_type == SPARSE_NORMAL_CHOLESKY) {
79 transformed_A.reset(new CompressedRowSparseMatrix(*A_));
80 } else {
81 LOG(FATAL) << "Unknown linear solver : " << linear_solver_type;
Keir Mierle8ebb0732012-04-30 23:09:08 -070082 }
Sameer Agarwalb0518732012-05-29 00:27:57 -070083 // Unregularized
84 unregularized_solve_summary =
85 solver->Solve(transformed_A.get(),
86 b_.get(),
87 per_solve_options,
88 x_unregularized.data());
Keir Mierle8ebb0732012-04-30 23:09:08 -070089
90 // Regularized solution
91 per_solve_options.D = D_.get();
Sameer Agarwalb0518732012-05-29 00:27:57 -070092 regularized_solve_summary =
93 solver->Solve(transformed_A.get(),
94 b_.get(),
95 per_solve_options,
96 x_regularized.data());
Keir Mierle8ebb0732012-04-30 23:09:08 -070097
Sameer Agarwalb0518732012-05-29 00:27:57 -070098 EXPECT_EQ(unregularized_solve_summary.termination_type, TOLERANCE);
Keir Mierle8ebb0732012-04-30 23:09:08 -070099
100 for (int i = 0; i < A_->num_cols(); ++i) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700101 EXPECT_NEAR(sol_unregularized_[i], x_unregularized[i], 1e-8);
102 }
103
104 EXPECT_EQ(regularized_solve_summary.termination_type, TOLERANCE);
105 for (int i = 0; i < A_->num_cols(); ++i) {
106 EXPECT_NEAR(sol_regularized_[i], x_regularized[i], 1e-8);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700107 }
108 }
109
110 scoped_ptr<TripletSparseMatrix> A_;
111 scoped_array<double> b_;
112 scoped_array<double> D_;
Sameer Agarwalb0518732012-05-29 00:27:57 -0700113 scoped_array<double> sol_unregularized_;
114 scoped_array<double> sol_regularized_;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700115};
116
Keir Mierle8ebb0732012-04-30 23:09:08 -0700117TEST_F(UnsymmetricLinearSolverTest, DenseQR) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700118 TestSolver(DENSE_QR, SUITE_SPARSE);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700119}
120
Sameer Agarwalb9f15a52012-08-18 13:06:19 -0700121TEST_F(UnsymmetricLinearSolverTest, DenseNormalCholesky) {
122 TestSolver(DENSE_NORMAL_CHOLESKY, SUITE_SPARSE);
123}
124
Keir Mierle8ebb0732012-04-30 23:09:08 -0700125#ifndef CERES_NO_SUITESPARSE
Sameer Agarwalb0518732012-05-29 00:27:57 -0700126TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingSuiteSparse) {
127 TestSolver(SPARSE_NORMAL_CHOLESKY, SUITE_SPARSE);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700128}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700129#endif
130
131#ifndef CERES_NO_CXSPARSE
132TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingCXSparse) {
133 TestSolver(SPARSE_NORMAL_CHOLESKY, CX_SPARSE);
134}
135#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700136
137} // namespace internal
138} // namespace ceres