blob: 0b0d593cf0cb64ae8ebeb158d386ae7858941aa2 [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;
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070065 options.use_block_amd = false;
Keir Mierle8ebb0732012-04-30 23:09:08 -070066 scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
67
68 LinearSolver::PerSolveOptions per_solve_options;
Sameer Agarwalb0518732012-05-29 00:27:57 -070069 LinearSolver::Summary unregularized_solve_summary;
70 LinearSolver::Summary regularized_solve_summary;
71 Vector x_unregularized(A_->num_cols());
72 Vector x_regularized(A_->num_cols());
Keir Mierle8ebb0732012-04-30 23:09:08 -070073
Sameer Agarwalb0518732012-05-29 00:27:57 -070074 scoped_ptr<SparseMatrix> transformed_A;
Keir Mierle8ebb0732012-04-30 23:09:08 -070075
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070076 if (linear_solver_type == DENSE_QR ||
77 linear_solver_type == DENSE_NORMAL_CHOLESKY) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070078 transformed_A.reset(new DenseSparseMatrix(*A_));
79 } else if (linear_solver_type == SPARSE_NORMAL_CHOLESKY) {
80 transformed_A.reset(new CompressedRowSparseMatrix(*A_));
81 } else {
82 LOG(FATAL) << "Unknown linear solver : " << linear_solver_type;
Keir Mierle8ebb0732012-04-30 23:09:08 -070083 }
Sameer Agarwalb0518732012-05-29 00:27:57 -070084 // Unregularized
85 unregularized_solve_summary =
86 solver->Solve(transformed_A.get(),
87 b_.get(),
88 per_solve_options,
89 x_unregularized.data());
Keir Mierle8ebb0732012-04-30 23:09:08 -070090
91 // Regularized solution
92 per_solve_options.D = D_.get();
Sameer Agarwalb0518732012-05-29 00:27:57 -070093 regularized_solve_summary =
94 solver->Solve(transformed_A.get(),
95 b_.get(),
96 per_solve_options,
97 x_regularized.data());
Keir Mierle8ebb0732012-04-30 23:09:08 -070098
Sameer Agarwalb0518732012-05-29 00:27:57 -070099 EXPECT_EQ(unregularized_solve_summary.termination_type, TOLERANCE);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700100
101 for (int i = 0; i < A_->num_cols(); ++i) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700102 EXPECT_NEAR(sol_unregularized_[i], x_unregularized[i], 1e-8);
103 }
104
105 EXPECT_EQ(regularized_solve_summary.termination_type, TOLERANCE);
106 for (int i = 0; i < A_->num_cols(); ++i) {
107 EXPECT_NEAR(sol_regularized_[i], x_regularized[i], 1e-8);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700108 }
109 }
110
111 scoped_ptr<TripletSparseMatrix> A_;
112 scoped_array<double> b_;
113 scoped_array<double> D_;
Sameer Agarwalb0518732012-05-29 00:27:57 -0700114 scoped_array<double> sol_unregularized_;
115 scoped_array<double> sol_regularized_;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700116};
117
Keir Mierle8ebb0732012-04-30 23:09:08 -0700118TEST_F(UnsymmetricLinearSolverTest, DenseQR) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700119 TestSolver(DENSE_QR, SUITE_SPARSE);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700120}
121
Sameer Agarwalb9f15a52012-08-18 13:06:19 -0700122TEST_F(UnsymmetricLinearSolverTest, DenseNormalCholesky) {
123 TestSolver(DENSE_NORMAL_CHOLESKY, SUITE_SPARSE);
124}
125
Keir Mierle8ebb0732012-04-30 23:09:08 -0700126#ifndef CERES_NO_SUITESPARSE
Sameer Agarwalb0518732012-05-29 00:27:57 -0700127TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingSuiteSparse) {
128 TestSolver(SPARSE_NORMAL_CHOLESKY, SUITE_SPARSE);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700129}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700130#endif
131
132#ifndef CERES_NO_CXSPARSE
133TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingCXSparse) {
134 TestSolver(SPARSE_NORMAL_CHOLESKY, CX_SPARSE);
135}
136#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700137
138} // namespace internal
139} // namespace ceres