dynamic_compressed_row_sparse_matrix_test.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 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. #include "ceres/dynamic_compressed_row_sparse_matrix.h"
  31. #include <memory>
  32. #include <vector>
  33. #include "ceres/casts.h"
  34. #include "ceres/compressed_row_sparse_matrix.h"
  35. #include "ceres/internal/eigen.h"
  36. #include "ceres/linear_least_squares_problems.h"
  37. #include "ceres/triplet_sparse_matrix.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres {
  40. namespace internal {
  41. class DynamicCompressedRowSparseMatrixTest : public ::testing::Test {
  42. protected:
  43. void SetUp() final {
  44. num_rows = 7;
  45. num_cols = 4;
  46. // The number of additional elements reserved when `Finalize` is called
  47. // should have no effect on the number of rows, columns or nonzeros.
  48. // Set this to some nonzero value to be sure.
  49. num_additional_elements = 13;
  50. expected_num_nonzeros = num_rows * num_cols - std::min(num_rows, num_cols);
  51. InitialiseDenseReference();
  52. InitialiseSparseMatrixReferences();
  53. dcrsm = std::make_unique<DynamicCompressedRowSparseMatrix>(
  54. num_rows, num_cols, 0);
  55. }
  56. void Finalize() { dcrsm->Finalize(num_additional_elements); }
  57. void InitialiseDenseReference() {
  58. dense.resize(num_rows, num_cols);
  59. dense.setZero();
  60. int num_nonzeros = 0;
  61. for (int i = 0; i < (num_rows * num_cols); ++i) {
  62. const int r = i / num_cols, c = i % num_cols;
  63. if (r != c) {
  64. dense(r, c) = i + 1;
  65. ++num_nonzeros;
  66. }
  67. }
  68. ASSERT_EQ(num_nonzeros, expected_num_nonzeros);
  69. }
  70. void InitialiseSparseMatrixReferences() {
  71. std::vector<int> rows, cols;
  72. std::vector<double> values;
  73. for (int i = 0; i < (num_rows * num_cols); ++i) {
  74. const int r = i / num_cols, c = i % num_cols;
  75. if (r != c) {
  76. rows.push_back(r);
  77. cols.push_back(c);
  78. values.push_back(i + 1);
  79. }
  80. }
  81. ASSERT_EQ(values.size(), expected_num_nonzeros);
  82. tsm = std::make_unique<TripletSparseMatrix>(
  83. num_rows, num_cols, expected_num_nonzeros);
  84. std::copy(rows.begin(), rows.end(), tsm->mutable_rows());
  85. std::copy(cols.begin(), cols.end(), tsm->mutable_cols());
  86. std::copy(values.begin(), values.end(), tsm->mutable_values());
  87. tsm->set_num_nonzeros(values.size());
  88. Matrix dense_from_tsm;
  89. tsm->ToDenseMatrix(&dense_from_tsm);
  90. ASSERT_TRUE((dense.array() == dense_from_tsm.array()).all());
  91. crsm = CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm);
  92. Matrix dense_from_crsm;
  93. crsm->ToDenseMatrix(&dense_from_crsm);
  94. ASSERT_TRUE((dense.array() == dense_from_crsm.array()).all());
  95. }
  96. void InsertNonZeroEntriesFromDenseReference() {
  97. for (int r = 0; r < num_rows; ++r) {
  98. for (int c = 0; c < num_cols; ++c) {
  99. const double& v = dense(r, c);
  100. if (v != 0.0) {
  101. dcrsm->InsertEntry(r, c, v);
  102. }
  103. }
  104. }
  105. }
  106. void ExpectEmpty() {
  107. EXPECT_EQ(dcrsm->num_rows(), num_rows);
  108. EXPECT_EQ(dcrsm->num_cols(), num_cols);
  109. EXPECT_EQ(dcrsm->num_nonzeros(), 0);
  110. Matrix dense_from_dcrsm;
  111. dcrsm->ToDenseMatrix(&dense_from_dcrsm);
  112. EXPECT_EQ(dense_from_dcrsm.rows(), num_rows);
  113. EXPECT_EQ(dense_from_dcrsm.cols(), num_cols);
  114. EXPECT_TRUE((dense_from_dcrsm.array() == 0.0).all());
  115. }
  116. void ExpectEqualToDenseReference() {
  117. Matrix dense_from_dcrsm;
  118. dcrsm->ToDenseMatrix(&dense_from_dcrsm);
  119. EXPECT_TRUE((dense.array() == dense_from_dcrsm.array()).all());
  120. }
  121. void ExpectEqualToCompressedRowSparseMatrixReference() {
  122. using ConstIntVectorRef = Eigen::Map<const Eigen::VectorXi>;
  123. ConstIntVectorRef crsm_rows(crsm->rows(), crsm->num_rows() + 1);
  124. ConstIntVectorRef dcrsm_rows(dcrsm->rows(), dcrsm->num_rows() + 1);
  125. EXPECT_TRUE((crsm_rows.array() == dcrsm_rows.array()).all());
  126. ConstIntVectorRef crsm_cols(crsm->cols(), crsm->num_nonzeros());
  127. ConstIntVectorRef dcrsm_cols(dcrsm->cols(), dcrsm->num_nonzeros());
  128. EXPECT_TRUE((crsm_cols.array() == dcrsm_cols.array()).all());
  129. ConstVectorRef crsm_values(crsm->values(), crsm->num_nonzeros());
  130. ConstVectorRef dcrsm_values(dcrsm->values(), dcrsm->num_nonzeros());
  131. EXPECT_TRUE((crsm_values.array() == dcrsm_values.array()).all());
  132. }
  133. int num_rows;
  134. int num_cols;
  135. int num_additional_elements;
  136. int expected_num_nonzeros;
  137. Matrix dense;
  138. std::unique_ptr<TripletSparseMatrix> tsm;
  139. std::unique_ptr<CompressedRowSparseMatrix> crsm;
  140. std::unique_ptr<DynamicCompressedRowSparseMatrix> dcrsm;
  141. };
  142. TEST_F(DynamicCompressedRowSparseMatrixTest, Initialization) {
  143. ExpectEmpty();
  144. Finalize();
  145. ExpectEmpty();
  146. }
  147. TEST_F(DynamicCompressedRowSparseMatrixTest, InsertEntryAndFinalize) {
  148. InsertNonZeroEntriesFromDenseReference();
  149. ExpectEmpty();
  150. Finalize();
  151. ExpectEqualToDenseReference();
  152. ExpectEqualToCompressedRowSparseMatrixReference();
  153. }
  154. TEST_F(DynamicCompressedRowSparseMatrixTest, ClearRows) {
  155. InsertNonZeroEntriesFromDenseReference();
  156. Finalize();
  157. ExpectEqualToDenseReference();
  158. ExpectEqualToCompressedRowSparseMatrixReference();
  159. dcrsm->ClearRows(0, 0);
  160. Finalize();
  161. ExpectEqualToDenseReference();
  162. ExpectEqualToCompressedRowSparseMatrixReference();
  163. dcrsm->ClearRows(0, num_rows);
  164. ExpectEqualToCompressedRowSparseMatrixReference();
  165. Finalize();
  166. ExpectEmpty();
  167. InsertNonZeroEntriesFromDenseReference();
  168. dcrsm->ClearRows(1, 2);
  169. Finalize();
  170. dense.block(1, 0, 2, num_cols).setZero();
  171. ExpectEqualToDenseReference();
  172. InitialiseDenseReference();
  173. }
  174. } // namespace internal
  175. } // namespace ceres