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 | #ifndef CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_ |
| 32 | #define CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_ |
| 33 | |
| 34 | #include <memory> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 35 | #include <random> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include <vector> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 38 | #include "ceres/crs_matrix.h" |
| 39 | #include "ceres/internal/disable_warnings.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include "ceres/internal/eigen.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 41 | #include "ceres/internal/export.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 42 | #include "ceres/sparse_matrix.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 43 | #include "ceres/types.h" |
| 44 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 45 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | |
| 47 | // An implementation of the SparseMatrix interface to store and |
| 48 | // manipulate sparse matrices in triplet (i,j,s) form. This object is |
| 49 | // inspired by the design of the cholmod_triplet struct used in the |
| 50 | // SuiteSparse package and is memory layout compatible with it. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 51 | class CERES_NO_EXPORT TripletSparseMatrix final : public SparseMatrix { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 52 | public: |
| 53 | TripletSparseMatrix(); |
| 54 | TripletSparseMatrix(int num_rows, int num_cols, int max_num_nonzeros); |
| 55 | TripletSparseMatrix(int num_rows, |
| 56 | int num_cols, |
| 57 | const std::vector<int>& rows, |
| 58 | const std::vector<int>& cols, |
| 59 | const std::vector<double>& values); |
| 60 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 61 | TripletSparseMatrix(const TripletSparseMatrix& orig); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 62 | |
| 63 | TripletSparseMatrix& operator=(const TripletSparseMatrix& rhs); |
| 64 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 65 | ~TripletSparseMatrix() override; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 66 | |
| 67 | // Implementation of the SparseMatrix interface. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 68 | void SetZero() final; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 69 | void RightMultiplyAndAccumulate(const double* x, double* y) const final; |
| 70 | void LeftMultiplyAndAccumulate(const double* x, double* y) const final; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 71 | void SquaredColumnNorm(double* x) const final; |
| 72 | void ScaleColumns(const double* scale) final; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 73 | void ToCRSMatrix(CRSMatrix* matrix) const; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 74 | void ToDenseMatrix(Matrix* dense_matrix) const final; |
| 75 | void ToTextFile(FILE* file) const final; |
| 76 | // clang-format off |
| 77 | int num_rows() const final { return num_rows_; } |
| 78 | int num_cols() const final { return num_cols_; } |
| 79 | int num_nonzeros() const final { return num_nonzeros_; } |
| 80 | const double* values() const final { return values_.get(); } |
| 81 | double* mutable_values() final { return values_.get(); } |
| 82 | // clang-format on |
| 83 | void set_num_nonzeros(int num_nonzeros); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 84 | |
| 85 | // Increase max_num_nonzeros and correspondingly increase the size |
| 86 | // of rows_, cols_ and values_. If new_max_num_nonzeros is smaller |
| 87 | // than max_num_nonzeros_, then num_non_zeros should be less than or |
| 88 | // equal to new_max_num_nonzeros, otherwise data loss is possible |
| 89 | // and the method crashes. |
| 90 | void Reserve(int new_max_num_nonzeros); |
| 91 | |
| 92 | // Append the matrix B at the bottom of this matrix. B should have |
| 93 | // the same number of columns as num_cols_. |
| 94 | void AppendRows(const TripletSparseMatrix& B); |
| 95 | |
| 96 | // Append the matrix B at the right of this matrix. B should have |
| 97 | // the same number of rows as num_rows_; |
| 98 | void AppendCols(const TripletSparseMatrix& B); |
| 99 | |
| 100 | // Resize the matrix. Entries which fall outside the new matrix |
| 101 | // bounds are dropped and the num_non_zeros changed accordingly. |
| 102 | void Resize(int new_num_rows, int new_num_cols); |
| 103 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 104 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 105 | int max_num_nonzeros() const { return max_num_nonzeros_; } |
| 106 | const int* rows() const { return rows_.get(); } |
| 107 | const int* cols() const { return cols_.get(); } |
| 108 | int* mutable_rows() { return rows_.get(); } |
| 109 | int* mutable_cols() { return cols_.get(); } |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 110 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 111 | |
| 112 | // Returns true if the entries of the matrix obey the row, column, |
| 113 | // and column size bounds and false otherwise. |
| 114 | bool AllTripletsWithinBounds() const; |
| 115 | |
| 116 | bool IsValid() const { return AllTripletsWithinBounds(); } |
| 117 | |
| 118 | // Build a sparse diagonal matrix of size num_rows x num_rows from |
| 119 | // the array values. Entries of the values array are copied into the |
| 120 | // sparse matrix. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 121 | static std::unique_ptr<TripletSparseMatrix> CreateSparseDiagonalMatrix( |
| 122 | const double* values, int num_rows); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 123 | |
| 124 | // Options struct to control the generation of random |
| 125 | // TripletSparseMatrix objects. |
| 126 | struct RandomMatrixOptions { |
| 127 | int num_rows; |
| 128 | int num_cols; |
| 129 | // 0 < density <= 1 is the probability of an entry being |
| 130 | // structurally non-zero. A given random matrix will not have |
| 131 | // precisely this density. |
| 132 | double density; |
| 133 | }; |
| 134 | |
| 135 | // Create a random CompressedRowSparseMatrix whose entries are |
| 136 | // normally distributed and whose structure is determined by |
| 137 | // RandomMatrixOptions. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 138 | static std::unique_ptr<TripletSparseMatrix> CreateRandomMatrix( |
| 139 | const TripletSparseMatrix::RandomMatrixOptions& options, |
| 140 | std::mt19937& prng); |
| 141 | |
| 142 | // Load a triplet sparse matrix from a text file. |
| 143 | static std::unique_ptr<TripletSparseMatrix> CreateFromTextFile(FILE* file); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | void AllocateMemory(); |
| 147 | void CopyData(const TripletSparseMatrix& orig); |
| 148 | |
| 149 | int num_rows_; |
| 150 | int num_cols_; |
| 151 | int max_num_nonzeros_; |
| 152 | int num_nonzeros_; |
| 153 | |
| 154 | // The data is stored as three arrays. For each i, values_[i] is |
| 155 | // stored at the location (rows_[i], cols_[i]). If the there are |
| 156 | // multiple entries with the same (rows_[i], cols_[i]), the values_ |
| 157 | // entries corresponding to them are summed up. |
| 158 | std::unique_ptr<int[]> rows_; |
| 159 | std::unique_ptr<int[]> cols_; |
| 160 | std::unique_ptr<double[]> values_; |
| 161 | }; |
| 162 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 163 | } // namespace ceres::internal |
| 164 | |
| 165 | #include "ceres/internal/reenable_warnings.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 166 | |
| 167 | #endif // CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H__ |