triplet_sparse_matrix.h 6.8 KB

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