blob: f16e8f2fa9ba5b02f95e1f3413db88c5a5b31472 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// http://ceres-solver.org/
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// A simple C++ interface to the Eigen's Sparse Cholesky routines.
32
33#ifndef CERES_INTERNAL_EIGENSPARSE_H_
34#define CERES_INTERNAL_EIGENSPARSE_H_
35
36// This include must come before any #ifndef check on Ceres compile options.
Austin Schuh3de38b02024-06-25 18:25:10 -070037#include "ceres/internal/config.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038
39#ifdef CERES_USE_EIGEN_SPARSE
40
41#include <memory>
42#include <string>
43
44#include "Eigen/SparseCore"
Austin Schuh3de38b02024-06-25 18:25:10 -070045#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080046#include "ceres/linear_solver.h"
47#include "ceres/sparse_cholesky.h"
48
Austin Schuh3de38b02024-06-25 18:25:10 -070049namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080050
Austin Schuh3de38b02024-06-25 18:25:10 -070051class EigenSparse {
52 public:
53 static constexpr bool IsNestedDissectionAvailable() noexcept {
54#ifdef CERES_NO_EIGEN_METIS
55 return false;
56#else
57 return true;
58#endif
59 }
60};
61
62class CERES_NO_EXPORT EigenSparseCholesky : public SparseCholesky {
Austin Schuh70cc9552019-01-21 19:46:48 -080063 public:
64 // Factory
65 static std::unique_ptr<SparseCholesky> Create(
66 const OrderingType ordering_type);
67
68 // SparseCholesky interface.
Austin Schuh3de38b02024-06-25 18:25:10 -070069 ~EigenSparseCholesky() override;
70 LinearSolverTerminationType Factorize(CompressedRowSparseMatrix* lhs,
71 std::string* message) override = 0;
72 CompressedRowSparseMatrix::StorageType StorageType() const override = 0;
73 LinearSolverTerminationType Solve(const double* rhs,
74 double* solution,
75 std::string* message) override = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080076};
77
78// Even though the input is double precision linear system, this class
79// solves it by computing a single precision Cholesky factorization.
Austin Schuh3de38b02024-06-25 18:25:10 -070080class CERES_NO_EXPORT FloatEigenSparseCholesky : public SparseCholesky {
Austin Schuh70cc9552019-01-21 19:46:48 -080081 public:
82 // Factory
83 static std::unique_ptr<SparseCholesky> Create(
84 const OrderingType ordering_type);
85
86 // SparseCholesky interface.
Austin Schuh3de38b02024-06-25 18:25:10 -070087 ~FloatEigenSparseCholesky() override;
88 LinearSolverTerminationType Factorize(CompressedRowSparseMatrix* lhs,
89 std::string* message) override = 0;
90 CompressedRowSparseMatrix::StorageType StorageType() const override = 0;
91 LinearSolverTerminationType Solve(const double* rhs,
92 double* solution,
93 std::string* message) override = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080094};
95
Austin Schuh3de38b02024-06-25 18:25:10 -070096} // namespace ceres::internal
97
98#else
99
100namespace ceres::internal {
101
102class EigenSparse {
103 public:
104 static constexpr bool IsNestedDissectionAvailable() noexcept { return false; }
105};
106
107} // namespace ceres::internal
Austin Schuh70cc9552019-01-21 19:46:48 -0800108
109#endif // CERES_USE_EIGEN_SPARSE
110
111#endif // CERES_INTERNAL_EIGENSPARSE_H_