Blame internal/ceres/dense_qr_solver.cc

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#include "ceres/dense_qr_solver.h"
Packit ea1746
Packit ea1746
Packit ea1746
#include <cstddef>
Packit ea1746
#include "Eigen/Dense"
Packit ea1746
#include "ceres/dense_sparse_matrix.h"
Packit ea1746
#include "ceres/internal/eigen.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/lapack.h"
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "ceres/wall_time.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
DenseQRSolver::DenseQRSolver(const LinearSolver::Options& options)
Packit ea1746
    : options_(options) {
Packit ea1746
  work_.resize(1);
Packit ea1746
}
Packit ea1746
Packit ea1746
LinearSolver::Summary DenseQRSolver::SolveImpl(
Packit ea1746
    DenseSparseMatrix* A,
Packit ea1746
    const double* b,
Packit ea1746
    const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
    double* x) {
Packit ea1746
  if (options_.dense_linear_algebra_library_type == EIGEN) {
Packit ea1746
    return SolveUsingEigen(A, b, per_solve_options, x);
Packit ea1746
  } else {
Packit ea1746
    return SolveUsingLAPACK(A, b, per_solve_options, x);
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
LinearSolver::Summary DenseQRSolver::SolveUsingLAPACK(
Packit ea1746
    DenseSparseMatrix* A,
Packit ea1746
    const double* b,
Packit ea1746
    const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
    double* x) {
Packit ea1746
  EventLogger event_logger("DenseQRSolver::Solve");
Packit ea1746
Packit ea1746
  const int num_rows = A->num_rows();
Packit ea1746
  const int num_cols = A->num_cols();
Packit ea1746
Packit ea1746
  if (per_solve_options.D != NULL) {
Packit ea1746
    // Temporarily append a diagonal block to the A matrix, but undo
Packit ea1746
    // it before returning the matrix to the user.
Packit ea1746
    A->AppendDiagonal(per_solve_options.D);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // TODO(sameeragarwal): Since we are copying anyways, the diagonal
Packit ea1746
  // can be appended to the matrix instead of doing it on A.
Packit ea1746
  lhs_ =  A->matrix();
Packit ea1746
Packit ea1746
  if (per_solve_options.D != NULL) {
Packit ea1746
    // Undo the modifications to the matrix A.
Packit ea1746
    A->RemoveDiagonal();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // rhs = [b;0] to account for the additional rows in the lhs.
Packit ea1746
  if (rhs_.rows() != lhs_.rows()) {
Packit ea1746
    rhs_.resize(lhs_.rows());
Packit ea1746
  }
Packit ea1746
  rhs_.setZero();
Packit ea1746
  rhs_.head(num_rows) = ConstVectorRef(b, num_rows);
Packit ea1746
Packit ea1746
  if (work_.rows() == 1) {
Packit ea1746
    const int work_size =
Packit ea1746
        LAPACK::EstimateWorkSizeForQR(lhs_.rows(), lhs_.cols());
Packit ea1746
    VLOG(3) << "Working memory for Dense QR factorization: "
Packit ea1746
            << work_size * sizeof(double);
Packit ea1746
    work_.resize(work_size);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  LinearSolver::Summary summary;
Packit ea1746
  summary.num_iterations = 1;
Packit ea1746
  summary.termination_type = LAPACK::SolveInPlaceUsingQR(lhs_.rows(),
Packit ea1746
                                                         lhs_.cols(),
Packit ea1746
                                                         lhs_.data(),
Packit ea1746
                                                         work_.rows(),
Packit ea1746
                                                         work_.data(),
Packit ea1746
                                                         rhs_.data(),
Packit ea1746
                                                         &summary.message);
Packit ea1746
  event_logger.AddEvent("Solve");
Packit ea1746
  if (summary.termination_type == LINEAR_SOLVER_SUCCESS) {
Packit ea1746
    VectorRef(x, num_cols) = rhs_.head(num_cols);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  event_logger.AddEvent("TearDown");
Packit ea1746
  return summary;
Packit ea1746
}
Packit ea1746
Packit ea1746
LinearSolver::Summary DenseQRSolver::SolveUsingEigen(
Packit ea1746
    DenseSparseMatrix* A,
Packit ea1746
    const double* b,
Packit ea1746
    const LinearSolver::PerSolveOptions& per_solve_options,
Packit ea1746
    double* x) {
Packit ea1746
  EventLogger event_logger("DenseQRSolver::Solve");
Packit ea1746
Packit ea1746
  const int num_rows = A->num_rows();
Packit ea1746
  const int num_cols = A->num_cols();
Packit ea1746
Packit ea1746
  if (per_solve_options.D != NULL) {
Packit ea1746
    // Temporarily append a diagonal block to the A matrix, but undo
Packit ea1746
    // it before returning the matrix to the user.
Packit ea1746
    A->AppendDiagonal(per_solve_options.D);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // rhs = [b;0] to account for the additional rows in the lhs.
Packit ea1746
  const int augmented_num_rows =
Packit ea1746
      num_rows + ((per_solve_options.D != NULL) ? num_cols : 0);
Packit ea1746
  if (rhs_.rows() != augmented_num_rows) {
Packit ea1746
    rhs_.resize(augmented_num_rows);
Packit ea1746
    rhs_.setZero();
Packit ea1746
  }
Packit ea1746
  rhs_.head(num_rows) = ConstVectorRef(b, num_rows);
Packit ea1746
  event_logger.AddEvent("Setup");
Packit ea1746
Packit ea1746
  // Solve the system.
Packit ea1746
  VectorRef(x, num_cols) = A->matrix().householderQr().solve(rhs_);
Packit ea1746
  event_logger.AddEvent("Solve");
Packit ea1746
Packit ea1746
  if (per_solve_options.D != NULL) {
Packit ea1746
    // Undo the modifications to the matrix A.
Packit ea1746
    A->RemoveDiagonal();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // We always succeed, since the QR solver returns the best solution
Packit ea1746
  // it can. It is the job of the caller to determine if the solution
Packit ea1746
  // is good enough or not.
Packit ea1746
  LinearSolver::Summary summary;
Packit ea1746
  summary.num_iterations = 1;
Packit ea1746
  summary.termination_type = LINEAR_SOLVER_SUCCESS;
Packit ea1746
  summary.message = "Success.";
Packit ea1746
Packit ea1746
  event_logger.AddEvent("TearDown");
Packit ea1746
  return summary;
Packit ea1746
}
Packit ea1746
Packit ea1746
}   // namespace internal
Packit ea1746
}   // namespace ceres