blob: 1fb9709b42aec0f099d5315a3e5a878958a7d48e [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 "ceres/dense_qr_solver.h"
32
33#include <cstddef>
34
35#include "Eigen/Dense"
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070036#include "ceres/dense_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070037#include "ceres/internal/eigen.h"
38#include "ceres/internal/scoped_ptr.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080039#include "ceres/linear_solver.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070040#include "ceres/types.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080041#include "ceres/wall_time.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070042
43namespace ceres {
44namespace internal {
45
46DenseQRSolver::DenseQRSolver(const LinearSolver::Options& options)
47 : options_(options) {}
48
49LinearSolver::Summary DenseQRSolver::SolveImpl(
50 DenseSparseMatrix* A,
51 const double* b,
52 const LinearSolver::PerSolveOptions& per_solve_options,
53 double* x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -080054 EventLogger event_logger("DenseQRSolver::Solve");
55
Keir Mierle8ebb0732012-04-30 23:09:08 -070056 const int num_rows = A->num_rows();
57 const int num_cols = A->num_cols();
Keir Mierle8ebb0732012-04-30 23:09:08 -070058
59 if (per_solve_options.D != NULL) {
60 // Temporarily append a diagonal block to the A matrix, but undo
61 // it before returning the matrix to the user.
62 A->AppendDiagonal(per_solve_options.D);
63 }
64
65 // rhs = [b;0] to account for the additional rows in the lhs.
Sameer Agarwal509f68c2013-02-20 01:39:03 -080066 const int augmented_num_rows =
67 num_rows + ((per_solve_options.D != NULL) ? num_cols : 0);
Sameer Agarwal5bfa7e42012-10-05 10:06:27 -070068 if (rhs_.rows() != augmented_num_rows) {
69 rhs_.resize(augmented_num_rows);
70 rhs_.setZero();
71 }
72 rhs_.head(num_rows) = ConstVectorRef(b, num_rows);
Sameer Agarwal42a84b82013-02-01 12:22:53 -080073 event_logger.AddEvent("Setup");
Keir Mierle8ebb0732012-04-30 23:09:08 -070074
75 // Solve the system.
Sameer Agarwal5bfa7e42012-10-05 10:06:27 -070076 VectorRef(x, num_cols) = A->matrix().colPivHouseholderQr().solve(rhs_);
Sameer Agarwal42a84b82013-02-01 12:22:53 -080077 event_logger.AddEvent("Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -070078
Keir Mierle8ebb0732012-04-30 23:09:08 -070079 if (per_solve_options.D != NULL) {
80 // Undo the modifications to the matrix A.
81 A->RemoveDiagonal();
82 }
83
84 // We always succeed, since the QR solver returns the best solution
85 // it can. It is the job of the caller to determine if the solution
86 // is good enough or not.
87 LinearSolver::Summary summary;
88 summary.num_iterations = 1;
89 summary.termination_type = TOLERANCE;
Sameer Agarwal42a84b82013-02-01 12:22:53 -080090
91 event_logger.AddEvent("TearDown");
Keir Mierle8ebb0732012-04-30 23:09:08 -070092 return summary;
93}
94
95} // namespace internal
96} // namespace ceres