Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 37 | #include "ceres/internal/config.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | |
| 39 | #ifdef CERES_USE_EIGEN_SPARSE |
| 40 | |
| 41 | #include <memory> |
| 42 | #include <string> |
| 43 | |
| 44 | #include "Eigen/SparseCore" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 45 | #include "ceres/internal/export.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | #include "ceres/linear_solver.h" |
| 47 | #include "ceres/sparse_cholesky.h" |
| 48 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 49 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 50 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 51 | class 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 | |
| 62 | class CERES_NO_EXPORT EigenSparseCholesky : public SparseCholesky { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 63 | public: |
| 64 | // Factory |
| 65 | static std::unique_ptr<SparseCholesky> Create( |
| 66 | const OrderingType ordering_type); |
| 67 | |
| 68 | // SparseCholesky interface. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 69 | ~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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // Even though the input is double precision linear system, this class |
| 79 | // solves it by computing a single precision Cholesky factorization. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 80 | class CERES_NO_EXPORT FloatEigenSparseCholesky : public SparseCholesky { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | public: |
| 82 | // Factory |
| 83 | static std::unique_ptr<SparseCholesky> Create( |
| 84 | const OrderingType ordering_type); |
| 85 | |
| 86 | // SparseCholesky interface. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 87 | ~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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 96 | } // namespace ceres::internal |
| 97 | |
| 98 | #else |
| 99 | |
| 100 | namespace ceres::internal { |
| 101 | |
| 102 | class EigenSparse { |
| 103 | public: |
| 104 | static constexpr bool IsNestedDissectionAvailable() noexcept { return false; } |
| 105 | }; |
| 106 | |
| 107 | } // namespace ceres::internal |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 108 | |
| 109 | #endif // CERES_USE_EIGEN_SPARSE |
| 110 | |
| 111 | #endif // CERES_INTERNAL_EIGENSPARSE_H_ |