block_random_access_diagonal_matrix_test.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/block_random_access_diagonal_matrix.h"
  31. #include <limits>
  32. #include <memory>
  33. #include <vector>
  34. #include "Eigen/Cholesky"
  35. #include "ceres/internal/eigen.h"
  36. #include "glog/logging.h"
  37. #include "gtest/gtest.h"
  38. namespace ceres::internal {
  39. class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test {
  40. public:
  41. void SetUp() override {
  42. std::vector<Block> blocks;
  43. blocks.emplace_back(3, 0);
  44. blocks.emplace_back(4, 3);
  45. blocks.emplace_back(5, 7);
  46. const int num_rows = 3 + 4 + 5;
  47. num_nonzeros_ = 3 * 3 + 4 * 4 + 5 * 5;
  48. m_ =
  49. std::make_unique<BlockRandomAccessDiagonalMatrix>(blocks, &context_, 1);
  50. EXPECT_EQ(m_->num_rows(), num_rows);
  51. EXPECT_EQ(m_->num_cols(), num_rows);
  52. for (int i = 0; i < blocks.size(); ++i) {
  53. const int row_block_id = i;
  54. int col_block_id;
  55. int row;
  56. int col;
  57. int row_stride;
  58. int col_stride;
  59. for (int j = 0; j < blocks.size(); ++j) {
  60. col_block_id = j;
  61. CellInfo* cell = m_->GetCell(
  62. row_block_id, col_block_id, &row, &col, &row_stride, &col_stride);
  63. // Off diagonal entries are not present.
  64. if (i != j) {
  65. EXPECT_TRUE(cell == nullptr);
  66. continue;
  67. }
  68. EXPECT_TRUE(cell != nullptr);
  69. EXPECT_EQ(row, 0);
  70. EXPECT_EQ(col, 0);
  71. EXPECT_EQ(row_stride, blocks[row_block_id].size);
  72. EXPECT_EQ(col_stride, blocks[col_block_id].size);
  73. // Write into the block
  74. MatrixRef(cell->values, row_stride, col_stride)
  75. .block(row,
  76. col,
  77. blocks[row_block_id].size,
  78. blocks[col_block_id].size) =
  79. (row_block_id + 1) * (col_block_id + 1) *
  80. Matrix::Ones(blocks[row_block_id].size,
  81. blocks[col_block_id].size) +
  82. Matrix::Identity(blocks[row_block_id].size,
  83. blocks[row_block_id].size);
  84. }
  85. }
  86. }
  87. protected:
  88. ContextImpl context_;
  89. int num_nonzeros_;
  90. std::unique_ptr<BlockRandomAccessDiagonalMatrix> m_;
  91. };
  92. TEST_F(BlockRandomAccessDiagonalMatrixTest, MatrixContents) {
  93. auto* crsm = m_->matrix();
  94. EXPECT_EQ(crsm->num_nonzeros(), num_nonzeros_);
  95. Matrix dense;
  96. crsm->ToDenseMatrix(&dense);
  97. double kTolerance = 1e-14;
  98. // (0,0)
  99. EXPECT_NEAR(
  100. (dense.block(0, 0, 3, 3) - (Matrix::Ones(3, 3) + Matrix::Identity(3, 3)))
  101. .norm(),
  102. 0.0,
  103. kTolerance);
  104. // (1,1)
  105. EXPECT_NEAR((dense.block(3, 3, 4, 4) -
  106. (2 * 2 * Matrix::Ones(4, 4) + Matrix::Identity(4, 4)))
  107. .norm(),
  108. 0.0,
  109. kTolerance);
  110. // (1,1)
  111. EXPECT_NEAR((dense.block(7, 7, 5, 5) -
  112. (3 * 3 * Matrix::Ones(5, 5) + Matrix::Identity(5, 5)))
  113. .norm(),
  114. 0.0,
  115. kTolerance);
  116. // There is nothing else in the matrix besides these four blocks.
  117. EXPECT_NEAR(
  118. dense.norm(),
  119. sqrt(6 * 1.0 + 3 * 4.0 + 12 * 16.0 + 4 * 25.0 + 20 * 81.0 + 5 * 100.0),
  120. kTolerance);
  121. }
  122. TEST_F(BlockRandomAccessDiagonalMatrixTest, RightMultiplyAndAccumulate) {
  123. double kTolerance = 1e-14;
  124. auto* crsm = m_->matrix();
  125. Matrix dense;
  126. crsm->ToDenseMatrix(&dense);
  127. Vector x = Vector::Random(dense.rows());
  128. Vector expected_y = dense * x;
  129. Vector actual_y = Vector::Zero(dense.rows());
  130. m_->RightMultiplyAndAccumulate(x.data(), actual_y.data());
  131. EXPECT_NEAR((expected_y - actual_y).norm(), 0, kTolerance);
  132. }
  133. TEST_F(BlockRandomAccessDiagonalMatrixTest, Invert) {
  134. double kTolerance = 1e-14;
  135. auto* crsm = m_->matrix();
  136. Matrix dense;
  137. crsm->ToDenseMatrix(&dense);
  138. Matrix expected_inverse =
  139. dense.llt().solve(Matrix::Identity(dense.rows(), dense.rows()));
  140. m_->Invert();
  141. crsm->ToDenseMatrix(&dense);
  142. EXPECT_NEAR((expected_inverse - dense).norm(), 0.0, kTolerance);
  143. }
  144. } // namespace ceres::internal