Blame internal/ceres/sparse_cholesky.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2017 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
#ifndef CERES_INTERNAL_SPARSE_CHOLESKY_H_
Packit ea1746
#define CERES_INTERNAL_SPARSE_CHOLESKY_H_
Packit ea1746
Packit ea1746
// This include must come before any #ifndef check on Ceres compile options.
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// An interface that abstracts away the internal details of various
Packit ea1746
// sparse linear algebra libraries and offers a simple API for solving
Packit ea1746
// symmetric positive definite linear systems using a sparse Cholesky
Packit ea1746
// factorization.
Packit ea1746
//
Packit ea1746
// Instances of SparseCholesky are expected to cache the symbolic
Packit ea1746
// factorization of the linear system. They do this on the first call
Packit ea1746
// to Factorize or FactorAndSolve. Subsequent calls to Factorize and
Packit ea1746
// FactorAndSolve are expected to have the same sparsity structure.
Packit ea1746
//
Packit ea1746
// Example usage:
Packit ea1746
//
Packit ea1746
//  scoped_ptr<SparseCholesky>
Packit ea1746
//  sparse_cholesky(SparseCholesky::Create(SUITE_SPARSE, AMD));
Packit ea1746
//
Packit ea1746
//  CompressedRowSparseMatrix lhs = ...;
Packit ea1746
//  std::string message;
Packit ea1746
//  CHECK_EQ(sparse_cholesky->Factorize(&lhs, &message), LINEAR_SOLVER_SUCCESS);
Packit ea1746
//  Vector rhs = ...;
Packit ea1746
//  Vector solution = ...;
Packit ea1746
//  CHECK_EQ(sparse_cholesky->Solve(rhs.data(), solution.data(), &message),
Packit ea1746
//           LINEAR_SOLVER_SUCCESS);
Packit ea1746
Packit ea1746
class SparseCholesky {
Packit ea1746
 public:
Packit ea1746
  // Factory which returns an instance of SparseCholesky for the given
Packit ea1746
  // sparse linear algebra library and fill reducing ordering
Packit ea1746
  // strategy.
Packit ea1746
  //
Packit ea1746
  // Caller owns the result.
Packit ea1746
  static SparseCholesky* Create(
Packit ea1746
      SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type,
Packit ea1746
      OrderingType ordering_type);
Packit ea1746
Packit ea1746
  virtual ~SparseCholesky();
Packit ea1746
Packit ea1746
  // Due to the symmetry of the linear system, sparse linear algebra
Packit ea1746
  // libraries only use one half of the input matrix. Whether it is
Packit ea1746
  // the upper or the lower triangular part of the matrix depends on
Packit ea1746
  // the library and the re-ordering strategy being used. This
Packit ea1746
  // function tells the user the storage type expected of the input
Packit ea1746
  // matrix for the sparse linear algebra library and reordering
Packit ea1746
  // strategy used.
Packit ea1746
  virtual CompressedRowSparseMatrix::StorageType StorageType() const = 0;
Packit ea1746
Packit ea1746
  // Computes the numeric factorization of the given matrix.  If this
Packit ea1746
  // is the first call to Factorize, first the symbolic factorization
Packit ea1746
  // will be computed and cached and the numeric factorization will be
Packit ea1746
  // computed based on that.
Packit ea1746
  //
Packit ea1746
  // Subsequent calls to Factorize will use that symbolic
Packit ea1746
  // factorization assuming that the sparsity of the matrix has
Packit ea1746
  // remained constant.
Packit ea1746
  virtual LinearSolverTerminationType Factorize(
Packit ea1746
      CompressedRowSparseMatrix* lhs, std::string* message) = 0;
Packit ea1746
Packit ea1746
  // Computes the solution to the equation
Packit ea1746
  //
Packit ea1746
  // lhs * solution = rhs
Packit ea1746
  //
Packit ea1746
  // rhs and solution can point to the same memory location.
Packit ea1746
  virtual LinearSolverTerminationType Solve(const double* rhs,
Packit ea1746
                                            double* solution,
Packit ea1746
                                            std::string* message) = 0;
Packit ea1746
Packit ea1746
  // Convenience method which combines a call to Factorize and
Packit ea1746
  // Solve. Solve is only called if Factorize returns
Packit ea1746
  // LINEAR_SOLVER_SUCCESS.
Packit ea1746
  virtual LinearSolverTerminationType FactorAndSolve(
Packit ea1746
      CompressedRowSparseMatrix* lhs,
Packit ea1746
      const double* rhs,
Packit ea1746
      double* solution,
Packit ea1746
      std::string* message);
Packit ea1746
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_SPARSE_CHOLESKY_H_