10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/linear_solver.h"
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/cgnr_solver.h"
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/dense_normal_cholesky_solver.h"
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/dense_qr_solver.h"
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/iterative_schur_complement_solver.h"
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/schur_complement_solver.h"
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/sparse_normal_cholesky_solver.h"
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h"
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "glog/logging.h"
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongLinearSolver::~LinearSolver() {
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
4879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos HernandezLinearSolverType LinearSolver::LinearSolverForZeroEBlocks(
4979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    LinearSolverType linear_solver_type) {
5079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  if (!IsSchurType(linear_solver_type)) {
5179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    return linear_solver_type;
5279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
5379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
5479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  if (linear_solver_type == SPARSE_SCHUR) {
5579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    return SPARSE_NORMAL_CHOLESKY;
5679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
5779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
5879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  if (linear_solver_type == DENSE_SCHUR) {
5979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // TODO(sameeragarwal): This is probably not a great choice.
6079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // Ideally, we should have a DENSE_NORMAL_CHOLESKY, that can take
6179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // a BlockSparseMatrix as input.
6279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    return DENSE_QR;
6379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
6479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
6579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  if (linear_solver_type == ITERATIVE_SCHUR) {
6679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    return CGNR;
6779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
6879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
6979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  return linear_solver_type;
7079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez}
7179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongLinearSolver* LinearSolver::Create(const LinearSolver::Options& options) {
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  switch (options.type) {
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case CGNR:
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new CgnrSolver(options);
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case SPARSE_NORMAL_CHOLESKY:
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#if defined(CERES_NO_SUITESPARSE) && defined(CERES_NO_CXSPARSE)
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return NULL;
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new SparseNormalCholeskySolver(options);
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case SPARSE_SCHUR:
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#if defined(CERES_NO_SUITESPARSE) && defined(CERES_NO_CXSPARSE)
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return NULL;
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new SparseSchurComplementSolver(options);
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case DENSE_SCHUR:
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new DenseSchurComplementSolver(options);
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case ITERATIVE_SCHUR:
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new IterativeSchurComplementSolver(options);
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case DENSE_QR:
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new DenseQRSolver(options);
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    case DENSE_NORMAL_CHOLESKY:
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return new DenseNormalCholeskySolver(options);
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    default:
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      LOG(FATAL) << "Unknown linear solver type :"
1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                 << options.type;
1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      return NULL;  // MSVC doesn't understand that LOG(FATAL) never returns.
1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
112