1 // 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 (at) google.com (Sameer Agarwal) 30 31 #include "ceres/casts.h" 32 #include "ceres/compressed_row_sparse_matrix.h" 33 #include "ceres/internal/scoped_ptr.h" 34 #include "ceres/linear_least_squares_problems.h" 35 #include "ceres/linear_solver.h" 36 #include "ceres/triplet_sparse_matrix.h" 37 #include "ceres/types.h" 38 #include "glog/logging.h" 39 #include "gtest/gtest.h" 40 41 42 namespace ceres { 43 namespace internal { 44 45 class 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 sol_unregularized_.reset(problem->x.release()); 56 sol_regularized_.reset(problem->x_D.release()); 57 } 58 59 void TestSolver( 60 LinearSolverType linear_solver_type, 61 SparseLinearAlgebraLibraryType sparse_linear_algebra_library) { 62 LinearSolver::Options options; 63 options.type = linear_solver_type; 64 options.sparse_linear_algebra_library = sparse_linear_algebra_library; 65 options.use_block_amd = false; 66 scoped_ptr<LinearSolver> solver(LinearSolver::Create(options)); 67 68 LinearSolver::PerSolveOptions per_solve_options; 69 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()); 73 74 scoped_ptr<SparseMatrix> transformed_A; 75 76 if (linear_solver_type == DENSE_QR || 77 linear_solver_type == DENSE_NORMAL_CHOLESKY) { 78 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; 83 } 84 // Unregularized 85 unregularized_solve_summary = 86 solver->Solve(transformed_A.get(), 87 b_.get(), 88 per_solve_options, 89 x_unregularized.data()); 90 91 // Regularized solution 92 per_solve_options.D = D_.get(); 93 regularized_solve_summary = 94 solver->Solve(transformed_A.get(), 95 b_.get(), 96 per_solve_options, 97 x_regularized.data()); 98 99 EXPECT_EQ(unregularized_solve_summary.termination_type, TOLERANCE); 100 101 for (int i = 0; i < A_->num_cols(); ++i) { 102 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); 108 } 109 } 110 111 scoped_ptr<TripletSparseMatrix> A_; 112 scoped_array<double> b_; 113 scoped_array<double> D_; 114 scoped_array<double> sol_unregularized_; 115 scoped_array<double> sol_regularized_; 116 }; 117 118 TEST_F(UnsymmetricLinearSolverTest, DenseQR) { 119 TestSolver(DENSE_QR, SUITE_SPARSE); 120 } 121 122 TEST_F(UnsymmetricLinearSolverTest, DenseNormalCholesky) { 123 TestSolver(DENSE_NORMAL_CHOLESKY, SUITE_SPARSE); 124 } 125 126 #ifndef CERES_NO_SUITESPARSE 127 TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingSuiteSparse) { 128 TestSolver(SPARSE_NORMAL_CHOLESKY, SUITE_SPARSE); 129 } 130 #endif 131 132 #ifndef CERES_NO_CXSPARSE 133 TEST_F(UnsymmetricLinearSolverTest, SparseNormalCholeskyUsingCXSparse) { 134 TestSolver(SPARSE_NORMAL_CHOLESKY, CX_SPARSE); 135 } 136 #endif 137 138 } // namespace internal 139 } // namespace ceres 140