inner_product_computer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2017 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_INNER_PRODUCT_COMPUTER_H_
  31. #define CERES_INTERNAL_INNER_PRODUCT_COMPUTER_H_
  32. #include <memory>
  33. #include <vector>
  34. #include "ceres/block_sparse_matrix.h"
  35. #include "ceres/compressed_row_sparse_matrix.h"
  36. #include "ceres/internal/disable_warnings.h"
  37. #include "ceres/internal/export.h"
  38. namespace ceres::internal {
  39. // This class is used to repeatedly compute the inner product
  40. //
  41. // result = m' * m
  42. //
  43. // where the sparsity structure of m remains constant across calls.
  44. //
  45. // Upon creation, the class computes and caches information needed to
  46. // compute v, and then uses it to efficiently compute the product
  47. // every time InnerProductComputer::Compute is called.
  48. //
  49. // See sparse_normal_cholesky_solver.cc for example usage.
  50. //
  51. // Note that the result matrix is a block upper or lower-triangular
  52. // matrix, i.e., it will contain entries in the upper or lower
  53. // triangular part of the matrix corresponding to the block that occur
  54. // along its diagonal.
  55. //
  56. // This is not a problem as sparse linear algebra libraries can ignore
  57. // these entries with ease and the space used is minimal/linear in the
  58. // size of the matrices.
  59. class CERES_NO_EXPORT InnerProductComputer {
  60. public:
  61. // Factory
  62. //
  63. // m is the input matrix
  64. //
  65. // Since m' * m is a symmetric matrix, we only compute half of the
  66. // matrix and the value of storage_type which must be
  67. // UPPER_TRIANGULAR or LOWER_TRIANGULAR determines which half is
  68. // computed.
  69. //
  70. // The user must ensure that the matrix m is valid for the life time
  71. // of this object.
  72. static std::unique_ptr<InnerProductComputer> Create(
  73. const BlockSparseMatrix& m,
  74. CompressedRowSparseMatrix::StorageType storage_type);
  75. // This factory method allows the user control over range of row
  76. // blocks of m that should be used to compute the inner product.
  77. //
  78. // a = m(start_row_block : end_row_block, :);
  79. // result = a' * a;
  80. static std::unique_ptr<InnerProductComputer> Create(
  81. const BlockSparseMatrix& m,
  82. int start_row_block,
  83. int end_row_block,
  84. CompressedRowSparseMatrix::StorageType storage_type);
  85. // Update result_ to be numerically equal to m' * m.
  86. void Compute();
  87. // Accessors for the result containing the inner product.
  88. //
  89. // Compute must be called before accessing this result for
  90. // the first time.
  91. const CompressedRowSparseMatrix& result() const { return *result_; }
  92. CompressedRowSparseMatrix* mutable_result() const { return result_.get(); }
  93. private:
  94. // A ProductTerm is a term in the block inner product of a matrix
  95. // with itself.
  96. struct ProductTerm {
  97. ProductTerm(const int row, const int col, const int index)
  98. : row(row), col(col), index(index) {}
  99. bool operator<(const ProductTerm& right) const {
  100. if (row == right.row) {
  101. if (col == right.col) {
  102. return index < right.index;
  103. }
  104. return col < right.col;
  105. }
  106. return row < right.row;
  107. }
  108. int row;
  109. int col;
  110. int index;
  111. };
  112. InnerProductComputer(const BlockSparseMatrix& m,
  113. int start_row_block,
  114. int end_row_block);
  115. void Init(CompressedRowSparseMatrix::StorageType storage_type);
  116. std::unique_ptr<CompressedRowSparseMatrix> CreateResultMatrix(
  117. const CompressedRowSparseMatrix::StorageType storage_type,
  118. int num_nonzeros);
  119. int ComputeNonzeros(const std::vector<ProductTerm>& product_terms,
  120. std::vector<int>* row_block_nnz);
  121. void ComputeOffsetsAndCreateResultMatrix(
  122. const CompressedRowSparseMatrix::StorageType storage_type,
  123. const std::vector<ProductTerm>& product_terms);
  124. const BlockSparseMatrix& m_;
  125. const int start_row_block_;
  126. const int end_row_block_;
  127. std::unique_ptr<CompressedRowSparseMatrix> result_;
  128. // For each term in the inner product, result_offsets_ contains the
  129. // location in the values array of the result_ matrix where it
  130. // should be stored.
  131. //
  132. // This is the principal look up table that allows this class to
  133. // compute the inner product fast.
  134. std::vector<int> result_offsets_;
  135. };
  136. } // namespace ceres::internal
  137. #include "ceres/internal/reenable_warnings.h"
  138. #endif // CERES_INTERNAL_INNER_PRODUCT_COMPUTER_H_