dynamic_compressed_row_sparse_matrix.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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: richie.stebbing@gmail.com (Richard Stebbing)
  30. //
  31. // A compressed row sparse matrix that provides an extended interface to
  32. // allow dynamic insertion of entries. This is provided for the use case
  33. // where the sparsity structure and number of non-zero entries is dynamic.
  34. // This flexibility is achieved by using an (internal) scratch space that
  35. // allows independent insertion of entries into each row (thread-safe).
  36. // Once insertion is complete, the `Finalize` method must be called to ensure
  37. // that the underlying `CompressedRowSparseMatrix` is consistent.
  38. //
  39. // This should only be used if you really do need a dynamic sparsity pattern.
  40. #ifndef CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_
  41. #define CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_
  42. #include <vector>
  43. #include "ceres/compressed_row_sparse_matrix.h"
  44. #include "ceres/internal/disable_warnings.h"
  45. #include "ceres/internal/export.h"
  46. namespace ceres::internal {
  47. class CERES_NO_EXPORT DynamicCompressedRowSparseMatrix final
  48. : public CompressedRowSparseMatrix {
  49. public:
  50. // Set the number of rows and columns for the underlying
  51. // `CompressedRowSparseMatrix` and set the initial number of maximum non-zero
  52. // entries. Note that following the insertion of entries, when `Finalize`
  53. // is called the number of non-zeros is determined and all internal
  54. // structures are adjusted as required. If you know the upper limit on the
  55. // number of non-zeros, then passing this value here can prevent future
  56. // memory reallocations which may improve performance. Otherwise, if no
  57. // upper limit is available a value of 0 is sufficient.
  58. //
  59. // Typical usage of this class is to define a new instance with a given
  60. // number of rows, columns and maximum number of non-zero elements
  61. // (if available). Next, entries are inserted at row and column positions
  62. // using `InsertEntry`. Finally, once all elements have been inserted,
  63. // `Finalize` must be called to make the underlying
  64. // `CompressedRowSparseMatrix` consistent.
  65. DynamicCompressedRowSparseMatrix(int num_rows,
  66. int num_cols,
  67. int initial_max_num_nonzeros);
  68. // Insert an entry at a given row and column position. This method is
  69. // thread-safe across rows i.e. different threads can insert values
  70. // simultaneously into different rows. It should be emphasized that this
  71. // method always inserts a new entry and does not check for existing
  72. // entries at the specified row and column position. Duplicate entries
  73. // for a given row and column position will result in undefined
  74. // behavior.
  75. void InsertEntry(int row, int col, const double& value);
  76. // Clear all entries for rows, starting from row index `row_start`
  77. // and proceeding for `num_rows`.
  78. void ClearRows(int row_start, int num_rows);
  79. // Make the underlying internal `CompressedRowSparseMatrix` data structures
  80. // consistent. Additional space for non-zero entries in the
  81. // `CompressedRowSparseMatrix` can be reserved by specifying
  82. // `num_additional_elements`. This is useful when it is known that rows will
  83. // be appended to the `CompressedRowSparseMatrix` (e.g. appending a diagonal
  84. // matrix to the jacobian) as it prevents need for future reallocation.
  85. void Finalize(int num_additional_elements);
  86. private:
  87. std::vector<std::vector<int>> dynamic_cols_;
  88. std::vector<std::vector<double>> dynamic_values_;
  89. };
  90. } // namespace ceres::internal
  91. #include "ceres/internal/reenable_warnings.h"
  92. #endif // CERES_INTERNAL_DYNAMIC_COMPRESSED_ROW_SPARSE_MATRIX_H_