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 | #include "ceres/triplet_sparse_matrix.h" |
| 32 | |
| 33 | #include <algorithm> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 34 | #include <memory> |
| 35 | #include <random> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 37 | #include "ceres/compressed_row_sparse_matrix.h" |
| 38 | #include "ceres/crs_matrix.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 39 | #include "ceres/internal/eigen.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 40 | #include "ceres/internal/export.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 41 | #include "ceres/types.h" |
| 42 | #include "glog/logging.h" |
| 43 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 44 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 45 | |
| 46 | TripletSparseMatrix::TripletSparseMatrix() |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 47 | : num_rows_(0), num_cols_(0), max_num_nonzeros_(0), num_nonzeros_(0) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 48 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 49 | TripletSparseMatrix::~TripletSparseMatrix() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 50 | |
| 51 | TripletSparseMatrix::TripletSparseMatrix(int num_rows, |
| 52 | int num_cols, |
| 53 | int max_num_nonzeros) |
| 54 | : num_rows_(num_rows), |
| 55 | num_cols_(num_cols), |
| 56 | max_num_nonzeros_(max_num_nonzeros), |
| 57 | num_nonzeros_(0) { |
| 58 | // All the sizes should at least be zero |
| 59 | CHECK_GE(num_rows, 0); |
| 60 | CHECK_GE(num_cols, 0); |
| 61 | CHECK_GE(max_num_nonzeros, 0); |
| 62 | AllocateMemory(); |
| 63 | } |
| 64 | |
| 65 | TripletSparseMatrix::TripletSparseMatrix(const int num_rows, |
| 66 | const int num_cols, |
| 67 | const std::vector<int>& rows, |
| 68 | const std::vector<int>& cols, |
| 69 | const std::vector<double>& values) |
| 70 | : num_rows_(num_rows), |
| 71 | num_cols_(num_cols), |
| 72 | max_num_nonzeros_(values.size()), |
| 73 | num_nonzeros_(values.size()) { |
| 74 | // All the sizes should at least be zero |
| 75 | CHECK_GE(num_rows, 0); |
| 76 | CHECK_GE(num_cols, 0); |
| 77 | CHECK_EQ(rows.size(), cols.size()); |
| 78 | CHECK_EQ(rows.size(), values.size()); |
| 79 | AllocateMemory(); |
| 80 | std::copy(rows.begin(), rows.end(), rows_.get()); |
| 81 | std::copy(cols.begin(), cols.end(), cols_.get()); |
| 82 | std::copy(values.begin(), values.end(), values_.get()); |
| 83 | } |
| 84 | |
| 85 | TripletSparseMatrix::TripletSparseMatrix(const TripletSparseMatrix& orig) |
| 86 | : SparseMatrix(), |
| 87 | num_rows_(orig.num_rows_), |
| 88 | num_cols_(orig.num_cols_), |
| 89 | max_num_nonzeros_(orig.max_num_nonzeros_), |
| 90 | num_nonzeros_(orig.num_nonzeros_) { |
| 91 | AllocateMemory(); |
| 92 | CopyData(orig); |
| 93 | } |
| 94 | |
| 95 | TripletSparseMatrix& TripletSparseMatrix::operator=( |
| 96 | const TripletSparseMatrix& rhs) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 97 | if (this == &rhs) { |
| 98 | return *this; |
| 99 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 100 | num_rows_ = rhs.num_rows_; |
| 101 | num_cols_ = rhs.num_cols_; |
| 102 | num_nonzeros_ = rhs.num_nonzeros_; |
| 103 | max_num_nonzeros_ = rhs.max_num_nonzeros_; |
| 104 | AllocateMemory(); |
| 105 | CopyData(rhs); |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | bool TripletSparseMatrix::AllTripletsWithinBounds() const { |
| 110 | for (int i = 0; i < num_nonzeros_; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 111 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 112 | if ((rows_[i] < 0) || (rows_[i] >= num_rows_) || |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 113 | (cols_[i] < 0) || (cols_[i] >= num_cols_)) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 114 | return false; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 115 | } |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 116 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void TripletSparseMatrix::Reserve(int new_max_num_nonzeros) { |
| 122 | CHECK_LE(num_nonzeros_, new_max_num_nonzeros) |
| 123 | << "Reallocation will cause data loss"; |
| 124 | |
| 125 | // Nothing to do if we have enough space already. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 126 | if (new_max_num_nonzeros <= max_num_nonzeros_) return; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 128 | std::unique_ptr<int[]> new_rows = |
| 129 | std::make_unique<int[]>(new_max_num_nonzeros); |
| 130 | std::unique_ptr<int[]> new_cols = |
| 131 | std::make_unique<int[]>(new_max_num_nonzeros); |
| 132 | std::unique_ptr<double[]> new_values = |
| 133 | std::make_unique<double[]>(new_max_num_nonzeros); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 134 | |
| 135 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 136 | new_rows[i] = rows_[i]; |
| 137 | new_cols[i] = cols_[i]; |
| 138 | new_values[i] = values_[i]; |
| 139 | } |
| 140 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 141 | rows_ = std::move(new_rows); |
| 142 | cols_ = std::move(new_cols); |
| 143 | values_ = std::move(new_values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | max_num_nonzeros_ = new_max_num_nonzeros; |
| 145 | } |
| 146 | |
| 147 | void TripletSparseMatrix::SetZero() { |
| 148 | std::fill(values_.get(), values_.get() + max_num_nonzeros_, 0.0); |
| 149 | num_nonzeros_ = 0; |
| 150 | } |
| 151 | |
| 152 | void TripletSparseMatrix::set_num_nonzeros(int num_nonzeros) { |
| 153 | CHECK_GE(num_nonzeros, 0); |
| 154 | CHECK_LE(num_nonzeros, max_num_nonzeros_); |
| 155 | num_nonzeros_ = num_nonzeros; |
| 156 | } |
| 157 | |
| 158 | void TripletSparseMatrix::AllocateMemory() { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 159 | rows_ = std::make_unique<int[]>(max_num_nonzeros_); |
| 160 | cols_ = std::make_unique<int[]>(max_num_nonzeros_); |
| 161 | values_ = std::make_unique<double[]>(max_num_nonzeros_); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void TripletSparseMatrix::CopyData(const TripletSparseMatrix& orig) { |
| 165 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 166 | rows_[i] = orig.rows_[i]; |
| 167 | cols_[i] = orig.cols_[i]; |
| 168 | values_[i] = orig.values_[i]; |
| 169 | } |
| 170 | } |
| 171 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 172 | void TripletSparseMatrix::RightMultiplyAndAccumulate(const double* x, |
| 173 | double* y) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 174 | for (int i = 0; i < num_nonzeros_; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 175 | y[rows_[i]] += values_[i] * x[cols_[i]]; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 179 | void TripletSparseMatrix::LeftMultiplyAndAccumulate(const double* x, |
| 180 | double* y) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 181 | for (int i = 0; i < num_nonzeros_; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 182 | y[cols_[i]] += values_[i] * x[rows_[i]]; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | void TripletSparseMatrix::SquaredColumnNorm(double* x) const { |
| 187 | CHECK(x != nullptr); |
| 188 | VectorRef(x, num_cols_).setZero(); |
| 189 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 190 | x[cols_[i]] += values_[i] * values_[i]; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void TripletSparseMatrix::ScaleColumns(const double* scale) { |
| 195 | CHECK(scale != nullptr); |
| 196 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 197 | values_[i] = values_[i] * scale[cols_[i]]; |
| 198 | } |
| 199 | } |
| 200 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 201 | void TripletSparseMatrix::ToCRSMatrix(CRSMatrix* crs_matrix) const { |
| 202 | CompressedRowSparseMatrix::FromTripletSparseMatrix(*this)->ToCRSMatrix( |
| 203 | crs_matrix); |
| 204 | } |
| 205 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 206 | void TripletSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const { |
| 207 | dense_matrix->resize(num_rows_, num_cols_); |
| 208 | dense_matrix->setZero(); |
| 209 | Matrix& m = *dense_matrix; |
| 210 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 211 | m(rows_[i], cols_[i]) += values_[i]; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void TripletSparseMatrix::AppendRows(const TripletSparseMatrix& B) { |
| 216 | CHECK_EQ(B.num_cols(), num_cols_); |
| 217 | Reserve(num_nonzeros_ + B.num_nonzeros_); |
| 218 | for (int i = 0; i < B.num_nonzeros_; ++i) { |
| 219 | rows_.get()[num_nonzeros_] = B.rows()[i] + num_rows_; |
| 220 | cols_.get()[num_nonzeros_] = B.cols()[i]; |
| 221 | values_.get()[num_nonzeros_++] = B.values()[i]; |
| 222 | } |
| 223 | num_rows_ = num_rows_ + B.num_rows(); |
| 224 | } |
| 225 | |
| 226 | void TripletSparseMatrix::AppendCols(const TripletSparseMatrix& B) { |
| 227 | CHECK_EQ(B.num_rows(), num_rows_); |
| 228 | Reserve(num_nonzeros_ + B.num_nonzeros_); |
| 229 | for (int i = 0; i < B.num_nonzeros_; ++i, ++num_nonzeros_) { |
| 230 | rows_.get()[num_nonzeros_] = B.rows()[i]; |
| 231 | cols_.get()[num_nonzeros_] = B.cols()[i] + num_cols_; |
| 232 | values_.get()[num_nonzeros_] = B.values()[i]; |
| 233 | } |
| 234 | num_cols_ = num_cols_ + B.num_cols(); |
| 235 | } |
| 236 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 237 | void TripletSparseMatrix::Resize(int new_num_rows, int new_num_cols) { |
| 238 | if ((new_num_rows >= num_rows_) && (new_num_cols >= num_cols_)) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 239 | num_rows_ = new_num_rows; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 240 | num_cols_ = new_num_cols; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | num_rows_ = new_num_rows; |
| 245 | num_cols_ = new_num_cols; |
| 246 | |
| 247 | int* r_ptr = rows_.get(); |
| 248 | int* c_ptr = cols_.get(); |
| 249 | double* v_ptr = values_.get(); |
| 250 | |
| 251 | int dropped_terms = 0; |
| 252 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 253 | if ((r_ptr[i] < num_rows_) && (c_ptr[i] < num_cols_)) { |
| 254 | if (dropped_terms) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 255 | r_ptr[i - dropped_terms] = r_ptr[i]; |
| 256 | c_ptr[i - dropped_terms] = c_ptr[i]; |
| 257 | v_ptr[i - dropped_terms] = v_ptr[i]; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 258 | } |
| 259 | } else { |
| 260 | ++dropped_terms; |
| 261 | } |
| 262 | } |
| 263 | num_nonzeros_ -= dropped_terms; |
| 264 | } |
| 265 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 266 | std::unique_ptr<TripletSparseMatrix> |
| 267 | TripletSparseMatrix::CreateSparseDiagonalMatrix(const double* values, |
| 268 | int num_rows) { |
| 269 | std::unique_ptr<TripletSparseMatrix> m = |
| 270 | std::make_unique<TripletSparseMatrix>(num_rows, num_rows, num_rows); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 271 | for (int i = 0; i < num_rows; ++i) { |
| 272 | m->mutable_rows()[i] = i; |
| 273 | m->mutable_cols()[i] = i; |
| 274 | m->mutable_values()[i] = values[i]; |
| 275 | } |
| 276 | m->set_num_nonzeros(num_rows); |
| 277 | return m; |
| 278 | } |
| 279 | |
| 280 | void TripletSparseMatrix::ToTextFile(FILE* file) const { |
| 281 | CHECK(file != nullptr); |
| 282 | for (int i = 0; i < num_nonzeros_; ++i) { |
| 283 | fprintf(file, "% 10d % 10d %17f\n", rows_[i], cols_[i], values_[i]); |
| 284 | } |
| 285 | } |
| 286 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 287 | std::unique_ptr<TripletSparseMatrix> TripletSparseMatrix::CreateFromTextFile( |
| 288 | FILE* file) { |
| 289 | CHECK(file != nullptr); |
| 290 | int num_rows = 0; |
| 291 | int num_cols = 0; |
| 292 | std::vector<int> rows; |
| 293 | std::vector<int> cols; |
| 294 | std::vector<double> values; |
| 295 | while (true) { |
| 296 | int row, col; |
| 297 | double value; |
| 298 | if (fscanf(file, "%d %d %lf", &row, &col, &value) != 3) { |
| 299 | break; |
| 300 | } |
| 301 | rows.push_back(row); |
| 302 | cols.push_back(col); |
| 303 | values.push_back(value); |
| 304 | num_rows = std::max(num_rows, row + 1); |
| 305 | num_cols = std::max(num_cols, col + 1); |
| 306 | } |
| 307 | VLOG(1) << "Read " << rows.size() << " nonzeros from file."; |
| 308 | return std::make_unique<TripletSparseMatrix>( |
| 309 | num_rows, num_cols, rows, cols, values); |
| 310 | } |
| 311 | |
| 312 | std::unique_ptr<TripletSparseMatrix> TripletSparseMatrix::CreateRandomMatrix( |
| 313 | const TripletSparseMatrix::RandomMatrixOptions& options, |
| 314 | std::mt19937& prng) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 315 | CHECK_GT(options.num_rows, 0); |
| 316 | CHECK_GT(options.num_cols, 0); |
| 317 | CHECK_GT(options.density, 0.0); |
| 318 | CHECK_LE(options.density, 1.0); |
| 319 | |
| 320 | std::vector<int> rows; |
| 321 | std::vector<int> cols; |
| 322 | std::vector<double> values; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 323 | std::uniform_real_distribution<double> uniform01(0.0, 1.0); |
| 324 | std::normal_distribution<double> standard_normal; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 325 | while (rows.empty()) { |
| 326 | rows.clear(); |
| 327 | cols.clear(); |
| 328 | values.clear(); |
| 329 | for (int r = 0; r < options.num_rows; ++r) { |
| 330 | for (int c = 0; c < options.num_cols; ++c) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 331 | if (uniform01(prng) <= options.density) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 332 | rows.push_back(r); |
| 333 | cols.push_back(c); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 334 | values.push_back(standard_normal(prng)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 340 | return std::make_unique<TripletSparseMatrix>( |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 341 | options.num_rows, options.num_cols, rows, cols, values); |
| 342 | } |
| 343 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 344 | } // namespace ceres::internal |