inner_product_computer_test.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #include "ceres/inner_product_computer.h"
  31. #include <memory>
  32. #include <numeric>
  33. #include <random>
  34. #include "Eigen/SparseCore"
  35. #include "ceres/block_sparse_matrix.h"
  36. #include "ceres/internal/eigen.h"
  37. #include "ceres/triplet_sparse_matrix.h"
  38. #include "glog/logging.h"
  39. #include "gtest/gtest.h"
  40. namespace ceres {
  41. namespace internal {
  42. #define COMPUTE_AND_COMPARE \
  43. { \
  44. inner_product_computer->Compute(); \
  45. CompressedRowSparseMatrix* actual_product_crsm = \
  46. inner_product_computer->mutable_result(); \
  47. Matrix actual_inner_product = \
  48. Eigen::Map<Eigen::SparseMatrix<double, Eigen::ColMajor>>( \
  49. actual_product_crsm->num_rows(), \
  50. actual_product_crsm->num_rows(), \
  51. actual_product_crsm->num_nonzeros(), \
  52. actual_product_crsm->mutable_rows(), \
  53. actual_product_crsm->mutable_cols(), \
  54. actual_product_crsm->mutable_values()); \
  55. EXPECT_EQ(actual_inner_product.rows(), actual_inner_product.cols()); \
  56. EXPECT_EQ(expected_inner_product.rows(), expected_inner_product.cols()); \
  57. EXPECT_EQ(actual_inner_product.rows(), expected_inner_product.rows()); \
  58. Matrix expected_t, actual_t; \
  59. if (actual_product_crsm->storage_type() == \
  60. CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR) { \
  61. expected_t = expected_inner_product.triangularView<Eigen::Upper>(); \
  62. actual_t = actual_inner_product.triangularView<Eigen::Upper>(); \
  63. } else { \
  64. expected_t = expected_inner_product.triangularView<Eigen::Lower>(); \
  65. actual_t = actual_inner_product.triangularView<Eigen::Lower>(); \
  66. } \
  67. EXPECT_LE((expected_t - actual_t).norm(), \
  68. 100 * std::numeric_limits<double>::epsilon() * actual_t.norm()) \
  69. << "expected: \n" \
  70. << expected_t << "\nactual: \n" \
  71. << actual_t; \
  72. }
  73. TEST(InnerProductComputer, NormalOperation) {
  74. const int kMaxNumRowBlocks = 10;
  75. const int kMaxNumColBlocks = 10;
  76. const int kNumTrials = 10;
  77. std::mt19937 prng;
  78. std::uniform_real_distribution<double> distribution(0.01, 1.0);
  79. // Create a random matrix, compute its outer product using Eigen and
  80. // ComputeOuterProduct. Convert both matrices to dense matrices and
  81. // compare their upper triangular parts.
  82. for (int num_row_blocks = 1; num_row_blocks < kMaxNumRowBlocks;
  83. ++num_row_blocks) {
  84. for (int num_col_blocks = 1; num_col_blocks < kMaxNumColBlocks;
  85. ++num_col_blocks) {
  86. for (int trial = 0; trial < kNumTrials; ++trial) {
  87. BlockSparseMatrix::RandomMatrixOptions options;
  88. options.num_row_blocks = num_row_blocks;
  89. options.num_col_blocks = num_col_blocks;
  90. options.min_row_block_size = 1;
  91. options.max_row_block_size = 5;
  92. options.min_col_block_size = 1;
  93. options.max_col_block_size = 10;
  94. options.block_density = distribution(prng);
  95. VLOG(2) << "num row blocks: " << options.num_row_blocks;
  96. VLOG(2) << "num col blocks: " << options.num_col_blocks;
  97. VLOG(2) << "min row block size: " << options.min_row_block_size;
  98. VLOG(2) << "max row block size: " << options.max_row_block_size;
  99. VLOG(2) << "min col block size: " << options.min_col_block_size;
  100. VLOG(2) << "max col block size: " << options.max_col_block_size;
  101. VLOG(2) << "block density: " << options.block_density;
  102. std::unique_ptr<BlockSparseMatrix> random_matrix(
  103. BlockSparseMatrix::CreateRandomMatrix(options, prng));
  104. TripletSparseMatrix tsm(random_matrix->num_rows(),
  105. random_matrix->num_cols(),
  106. random_matrix->num_nonzeros());
  107. random_matrix->ToTripletSparseMatrix(&tsm);
  108. std::vector<Eigen::Triplet<double>> triplets;
  109. for (int i = 0; i < tsm.num_nonzeros(); ++i) {
  110. triplets.emplace_back(tsm.rows()[i], tsm.cols()[i], tsm.values()[i]);
  111. }
  112. Eigen::SparseMatrix<double> eigen_random_matrix(
  113. random_matrix->num_rows(), random_matrix->num_cols());
  114. eigen_random_matrix.setFromTriplets(triplets.begin(), triplets.end());
  115. Matrix expected_inner_product =
  116. eigen_random_matrix.transpose() * eigen_random_matrix;
  117. std::unique_ptr<InnerProductComputer> inner_product_computer;
  118. inner_product_computer = InnerProductComputer::Create(
  119. *random_matrix,
  120. CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR);
  121. COMPUTE_AND_COMPARE;
  122. inner_product_computer = InnerProductComputer::Create(
  123. *random_matrix,
  124. CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR);
  125. COMPUTE_AND_COMPARE;
  126. }
  127. }
  128. }
  129. }
  130. TEST(InnerProductComputer, SubMatrix) {
  131. const int kNumRowBlocks = 10;
  132. const int kNumColBlocks = 20;
  133. const int kNumTrials = 5;
  134. std::mt19937 prng;
  135. std::uniform_real_distribution<double> distribution(0.01, 1.0);
  136. // Create a random matrix, compute its outer product using Eigen and
  137. // ComputeInnerProductComputer. Convert both matrices to dense matrices and
  138. // compare their upper triangular parts.
  139. for (int trial = 0; trial < kNumTrials; ++trial) {
  140. BlockSparseMatrix::RandomMatrixOptions options;
  141. options.num_row_blocks = kNumRowBlocks;
  142. options.num_col_blocks = kNumColBlocks;
  143. options.min_row_block_size = 1;
  144. options.max_row_block_size = 5;
  145. options.min_col_block_size = 1;
  146. options.max_col_block_size = 10;
  147. options.block_density = distribution(prng);
  148. VLOG(2) << "num row blocks: " << options.num_row_blocks;
  149. VLOG(2) << "num col blocks: " << options.num_col_blocks;
  150. VLOG(2) << "min row block size: " << options.min_row_block_size;
  151. VLOG(2) << "max row block size: " << options.max_row_block_size;
  152. VLOG(2) << "min col block size: " << options.min_col_block_size;
  153. VLOG(2) << "max col block size: " << options.max_col_block_size;
  154. VLOG(2) << "block density: " << options.block_density;
  155. std::unique_ptr<BlockSparseMatrix> random_matrix(
  156. BlockSparseMatrix::CreateRandomMatrix(options, prng));
  157. const std::vector<CompressedRow>& row_blocks =
  158. random_matrix->block_structure()->rows;
  159. const int num_row_blocks = row_blocks.size();
  160. for (int start_row_block = 0; start_row_block < num_row_blocks - 1;
  161. ++start_row_block) {
  162. for (int end_row_block = start_row_block + 1;
  163. end_row_block < num_row_blocks;
  164. ++end_row_block) {
  165. const int start_row = row_blocks[start_row_block].block.position;
  166. const int end_row = row_blocks[end_row_block].block.position;
  167. TripletSparseMatrix tsm(random_matrix->num_rows(),
  168. random_matrix->num_cols(),
  169. random_matrix->num_nonzeros());
  170. random_matrix->ToTripletSparseMatrix(&tsm);
  171. std::vector<Eigen::Triplet<double>> triplets;
  172. for (int i = 0; i < tsm.num_nonzeros(); ++i) {
  173. if (tsm.rows()[i] >= start_row && tsm.rows()[i] < end_row) {
  174. triplets.emplace_back(
  175. tsm.rows()[i], tsm.cols()[i], tsm.values()[i]);
  176. }
  177. }
  178. Eigen::SparseMatrix<double> eigen_random_matrix(
  179. random_matrix->num_rows(), random_matrix->num_cols());
  180. eigen_random_matrix.setFromTriplets(triplets.begin(), triplets.end());
  181. Matrix expected_inner_product =
  182. eigen_random_matrix.transpose() * eigen_random_matrix;
  183. std::unique_ptr<InnerProductComputer> inner_product_computer;
  184. inner_product_computer = InnerProductComputer::Create(
  185. *random_matrix,
  186. start_row_block,
  187. end_row_block,
  188. CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR);
  189. COMPUTE_AND_COMPARE;
  190. inner_product_computer = InnerProductComputer::Create(
  191. *random_matrix,
  192. start_row_block,
  193. end_row_block,
  194. CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR);
  195. COMPUTE_AND_COMPARE;
  196. }
  197. }
  198. }
  199. }
  200. #undef COMPUTE_AND_COMPARE
  201. } // namespace internal
  202. } // namespace ceres