2012-04-30 23:09:08 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2023-09-19 15:29:34 -07:00
|
|
|
// Copyright 2023 Google Inc. All rights reserved.
|
2015-03-17 22:30:16 -07:00
|
|
|
// http://ceres-solver.org/
|
2012-04-30 23:09:08 -07:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
|
|
|
// used to endorse or promote products derived from this software without
|
|
|
|
|
// specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
|
|
|
|
|
|
|
|
#include "ceres/sparse_normal_cholesky_solver.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <ctime>
|
2018-03-30 16:16:59 -07:00
|
|
|
#include <memory>
|
2012-05-29 00:27:57 -07:00
|
|
|
|
2017-06-13 00:12:00 -07:00
|
|
|
#include "ceres/block_sparse_matrix.h"
|
2024-08-08 07:18:44 -07:00
|
|
|
#include "ceres/event_logger.h"
|
2017-06-13 00:12:00 -07:00
|
|
|
#include "ceres/inner_product_computer.h"
|
2013-02-01 12:22:53 -08:00
|
|
|
#include "ceres/internal/eigen.h"
|
2018-04-06 08:39:16 -07:00
|
|
|
#include "ceres/iterative_refiner.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/linear_solver.h"
|
2017-04-12 12:48:44 -07:00
|
|
|
#include "ceres/sparse_cholesky.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/triplet_sparse_matrix.h"
|
|
|
|
|
#include "ceres/types.h"
|
2014-07-17 14:35:18 -07:00
|
|
|
|
2022-04-21 17:41:10 -07:00
|
|
|
namespace ceres::internal {
|
2017-04-25 09:32:08 -07:00
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
SparseNormalCholeskySolver::SparseNormalCholeskySolver(
|
|
|
|
|
const LinearSolver::Options& options)
|
2017-04-12 12:48:44 -07:00
|
|
|
: options_(options) {
|
2018-04-06 08:39:16 -07:00
|
|
|
sparse_cholesky_ = SparseCholesky::Create(options);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-09 00:56:10 +01:00
|
|
|
SparseNormalCholeskySolver::~SparseNormalCholeskySolver() = default;
|
2014-04-26 07:42:23 +01:00
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl(
|
2017-06-13 00:12:00 -07:00
|
|
|
BlockSparseMatrix* A,
|
2012-04-30 23:09:08 -07:00
|
|
|
const double* b,
|
|
|
|
|
const LinearSolver::PerSolveOptions& per_solve_options,
|
2017-04-25 09:32:08 -07:00
|
|
|
double* x) {
|
2017-04-12 12:48:44 -07:00
|
|
|
EventLogger event_logger("SparseNormalCholeskySolver::Solve");
|
|
|
|
|
LinearSolver::Summary summary;
|
|
|
|
|
summary.num_iterations = 1;
|
2022-05-14 14:14:22 -07:00
|
|
|
summary.termination_type = LinearSolverTerminationType::SUCCESS;
|
2017-04-12 12:48:44 -07:00
|
|
|
summary.message = "Success.";
|
|
|
|
|
|
2013-12-26 09:50:45 -08:00
|
|
|
const int num_cols = A->num_cols();
|
2018-04-06 08:39:16 -07:00
|
|
|
VectorRef xref(x, num_cols);
|
|
|
|
|
xref.setZero();
|
|
|
|
|
rhs_.resize(num_cols);
|
|
|
|
|
rhs_.setZero();
|
2022-08-10 09:55:43 -07:00
|
|
|
A->LeftMultiplyAndAccumulate(b, rhs_.data());
|
2017-04-12 12:48:44 -07:00
|
|
|
event_logger.AddEvent("Compute RHS");
|
2013-12-26 09:50:45 -08:00
|
|
|
|
2022-02-02 13:17:29 -08:00
|
|
|
if (per_solve_options.D != nullptr) {
|
2013-12-26 09:50:45 -08:00
|
|
|
// Temporarily append a diagonal block to the A matrix, but undo
|
|
|
|
|
// it before returning the matrix to the user.
|
2022-02-02 13:17:29 -08:00
|
|
|
std::unique_ptr<BlockSparseMatrix> regularizer =
|
|
|
|
|
BlockSparseMatrix::CreateDiagonalMatrix(per_solve_options.D,
|
|
|
|
|
A->block_structure()->cols);
|
2017-06-13 00:12:00 -07:00
|
|
|
event_logger.AddEvent("Diagonal");
|
2013-12-26 09:50:45 -08:00
|
|
|
A->AppendRows(*regularizer);
|
2017-06-13 00:12:00 -07:00
|
|
|
event_logger.AddEvent("Append");
|
2013-12-26 09:50:45 -08:00
|
|
|
}
|
2017-04-12 12:48:44 -07:00
|
|
|
event_logger.AddEvent("Append Rows");
|
2013-12-26 09:50:45 -08:00
|
|
|
|
2022-02-02 13:17:29 -08:00
|
|
|
if (inner_product_computer_.get() == nullptr) {
|
|
|
|
|
inner_product_computer_ =
|
|
|
|
|
InnerProductComputer::Create(*A, sparse_cholesky_->StorageType());
|
2017-06-13 00:12:00 -07:00
|
|
|
|
|
|
|
|
event_logger.AddEvent("InnerProductComputer::Create");
|
2017-04-25 09:32:08 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-13 00:12:00 -07:00
|
|
|
inner_product_computer_->Compute();
|
|
|
|
|
event_logger.AddEvent("InnerProductComputer::Compute");
|
|
|
|
|
|
2022-02-02 13:17:29 -08:00
|
|
|
if (per_solve_options.D != nullptr) {
|
2017-06-13 00:12:00 -07:00
|
|
|
A->DeleteRowBlocks(A->block_structure()->cols.size());
|
2013-12-26 09:50:45 -08:00
|
|
|
}
|
2018-04-06 08:39:16 -07:00
|
|
|
|
2017-04-12 12:48:44 -07:00
|
|
|
summary.termination_type = sparse_cholesky_->FactorAndSolve(
|
2018-04-06 08:39:16 -07:00
|
|
|
inner_product_computer_->mutable_result(),
|
|
|
|
|
rhs_.data(),
|
|
|
|
|
x,
|
|
|
|
|
&summary.message);
|
|
|
|
|
event_logger.AddEvent("SparseCholesky::FactorAndSolve");
|
2013-12-26 09:50:45 -08:00
|
|
|
return summary;
|
2012-05-29 00:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 17:41:10 -07:00
|
|
|
} // namespace ceres::internal
|