compressed_col_sparse_matrix_utils_test.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/compressed_col_sparse_matrix_utils.h"
  31. #include <algorithm>
  32. #include <numeric>
  33. #include <vector>
  34. #include "Eigen/SparseCore"
  35. #include "ceres/internal/export.h"
  36. #include "ceres/triplet_sparse_matrix.h"
  37. #include "glog/logging.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres::internal {
  40. TEST(_, BlockPermutationToScalarPermutation) {
  41. // Block structure
  42. // 0 --1- ---2--- ---3--- 4
  43. // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  44. std::vector<Block> blocks{{1, 0}, {2, 1}, {3, 3}, {3, 6}, {1, 9}};
  45. // Block ordering
  46. // [1, 0, 2, 4, 5]
  47. std::vector<int> block_ordering{{1, 0, 2, 4, 3}};
  48. // Expected ordering
  49. // [1, 2, 0, 3, 4, 5, 9, 6, 7, 8]
  50. std::vector<int> expected_scalar_ordering{{1, 2, 0, 3, 4, 5, 9, 6, 7, 8}};
  51. std::vector<int> scalar_ordering;
  52. BlockOrderingToScalarOrdering(blocks, block_ordering, &scalar_ordering);
  53. EXPECT_EQ(scalar_ordering.size(), expected_scalar_ordering.size());
  54. for (int i = 0; i < expected_scalar_ordering.size(); ++i) {
  55. EXPECT_EQ(scalar_ordering[i], expected_scalar_ordering[i]);
  56. }
  57. }
  58. static void FillBlock(const std::vector<Block>& row_blocks,
  59. const std::vector<Block>& col_blocks,
  60. const int row_block_id,
  61. const int col_block_id,
  62. std::vector<Eigen::Triplet<double>>* triplets) {
  63. for (int r = 0; r < row_blocks[row_block_id].size; ++r) {
  64. for (int c = 0; c < col_blocks[col_block_id].size; ++c) {
  65. triplets->push_back(
  66. Eigen::Triplet<double>(row_blocks[row_block_id].position + r,
  67. col_blocks[col_block_id].position + c,
  68. 1.0));
  69. }
  70. }
  71. }
  72. TEST(_, ScalarMatrixToBlockMatrix) {
  73. // Block sparsity.
  74. //
  75. // [1 2 3 2]
  76. // [1] x x
  77. // [2] x x
  78. // [2] x x
  79. // num_nonzeros = 1 + 3 + 4 + 4 + 1 + 2 = 15
  80. std::vector<Block> col_blocks{{1, 0}, {2, 1}, {3, 3}, {2, 5}};
  81. const int num_cols = NumScalarEntries(col_blocks);
  82. std::vector<Block> row_blocks{{1, 0}, {2, 1}, {2, 3}};
  83. const int num_rows = NumScalarEntries(row_blocks);
  84. std::vector<Eigen::Triplet<double>> triplets;
  85. FillBlock(row_blocks, col_blocks, 0, 0, &triplets);
  86. FillBlock(row_blocks, col_blocks, 2, 0, &triplets);
  87. FillBlock(row_blocks, col_blocks, 1, 1, &triplets);
  88. FillBlock(row_blocks, col_blocks, 2, 1, &triplets);
  89. FillBlock(row_blocks, col_blocks, 0, 2, &triplets);
  90. FillBlock(row_blocks, col_blocks, 1, 3, &triplets);
  91. Eigen::SparseMatrix<double> sparse_matrix(num_rows, num_cols);
  92. sparse_matrix.setFromTriplets(triplets.begin(), triplets.end());
  93. const std::vector<int> expected_compressed_block_rows{{0, 2, 1, 2, 0, 1}};
  94. const std::vector<int> expected_compressed_block_cols{{0, 2, 4, 5, 6}};
  95. std::vector<int> compressed_block_rows;
  96. std::vector<int> compressed_block_cols;
  97. CompressedColumnScalarMatrixToBlockMatrix(sparse_matrix.innerIndexPtr(),
  98. sparse_matrix.outerIndexPtr(),
  99. row_blocks,
  100. col_blocks,
  101. &compressed_block_rows,
  102. &compressed_block_cols);
  103. EXPECT_EQ(compressed_block_rows, expected_compressed_block_rows);
  104. EXPECT_EQ(compressed_block_cols, expected_compressed_block_cols);
  105. }
  106. class SolveUpperTriangularTest : public ::testing::Test {
  107. protected:
  108. const std::vector<int>& cols() const { return cols_; }
  109. const std::vector<int>& rows() const { return rows_; }
  110. const std::vector<double>& values() const { return values_; }
  111. private:
  112. const std::vector<int> cols_ = {0, 1, 2, 4, 7};
  113. const std::vector<int> rows_ = {0, 1, 1, 2, 0, 1, 3};
  114. const std::vector<double> values_ = {
  115. 0.50754, 0.80483, 0.14120, 0.3, 0.77696, 0.41860, 0.88979};
  116. };
  117. TEST_F(SolveUpperTriangularTest, SolveInPlace) {
  118. double rhs_and_solution[] = {1.0, 1.0, 2.0, 2.0};
  119. const double expected[] = {-1.4706, -1.0962, 6.6667, 2.2477};
  120. SolveUpperTriangularInPlace<int>(cols().size() - 1,
  121. rows().data(),
  122. cols().data(),
  123. values().data(),
  124. rhs_and_solution);
  125. for (int i = 0; i < 4; ++i) {
  126. EXPECT_NEAR(rhs_and_solution[i], expected[i], 1e-4) << i;
  127. }
  128. }
  129. TEST_F(SolveUpperTriangularTest, TransposeSolveInPlace) {
  130. double rhs_and_solution[] = {1.0, 1.0, 2.0, 2.0};
  131. double expected[] = {1.970288, 1.242498, 6.081864, -0.057255};
  132. SolveUpperTriangularTransposeInPlace<int>(cols().size() - 1,
  133. rows().data(),
  134. cols().data(),
  135. values().data(),
  136. rhs_and_solution);
  137. for (int i = 0; i < 4; ++i) {
  138. EXPECT_NEAR(rhs_and_solution[i], expected[i], 1e-4) << i;
  139. }
  140. }
  141. TEST_F(SolveUpperTriangularTest, RTRSolveWithSparseRHS) {
  142. double solution[4];
  143. // clang-format off
  144. double expected[] = { 6.8420e+00, 1.0057e+00, -1.4907e-16, -1.9335e+00,
  145. 1.0057e+00, 2.2275e+00, -1.9493e+00, -6.5693e-01,
  146. -1.4907e-16, -1.9493e+00, 1.1111e+01, 9.7381e-17,
  147. -1.9335e+00, -6.5693e-01, 9.7381e-17, 1.2631e+00 };
  148. // clang-format on
  149. for (int i = 0; i < 4; ++i) {
  150. SolveRTRWithSparseRHS<int>(cols().size() - 1,
  151. rows().data(),
  152. cols().data(),
  153. values().data(),
  154. i,
  155. solution);
  156. for (int j = 0; j < 4; ++j) {
  157. EXPECT_NEAR(solution[j], expected[4 * i + j], 1e-3) << i;
  158. }
  159. }
  160. }
  161. } // namespace ceres::internal