dense_sparse_matrix_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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: keir@google.com (Keir Mierle)
  30. //
  31. // TODO(keir): Implement a generic "compare sparse matrix implementations" test
  32. // suite that can compare all the implementations. Then this file would shrink
  33. // in size.
  34. #include "ceres/dense_sparse_matrix.h"
  35. #include <memory>
  36. #include "ceres/casts.h"
  37. #include "ceres/internal/eigen.h"
  38. #include "ceres/linear_least_squares_problems.h"
  39. #include "ceres/triplet_sparse_matrix.h"
  40. #include "glog/logging.h"
  41. #include "gtest/gtest.h"
  42. namespace ceres::internal {
  43. static void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) {
  44. EXPECT_EQ(a->num_rows(), b->num_rows());
  45. EXPECT_EQ(a->num_cols(), b->num_cols());
  46. int num_rows = a->num_rows();
  47. int num_cols = a->num_cols();
  48. for (int i = 0; i < num_cols; ++i) {
  49. Vector x = Vector::Zero(num_cols);
  50. x(i) = 1.0;
  51. Vector y_a = Vector::Zero(num_rows);
  52. Vector y_b = Vector::Zero(num_rows);
  53. a->RightMultiplyAndAccumulate(x.data(), y_a.data());
  54. b->RightMultiplyAndAccumulate(x.data(), y_b.data());
  55. EXPECT_EQ((y_a - y_b).norm(), 0);
  56. }
  57. }
  58. class DenseSparseMatrixTest : public ::testing::Test {
  59. protected:
  60. void SetUp() final {
  61. std::unique_ptr<LinearLeastSquaresProblem> problem =
  62. CreateLinearLeastSquaresProblemFromId(1);
  63. CHECK(problem != nullptr);
  64. tsm.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
  65. dsm = std::make_unique<DenseSparseMatrix>(*tsm);
  66. num_rows = tsm->num_rows();
  67. num_cols = tsm->num_cols();
  68. }
  69. int num_rows;
  70. int num_cols;
  71. std::unique_ptr<TripletSparseMatrix> tsm;
  72. std::unique_ptr<DenseSparseMatrix> dsm;
  73. };
  74. TEST_F(DenseSparseMatrixTest, RightMultiplyAndAccumulate) {
  75. CompareMatrices(tsm.get(), dsm.get());
  76. // Try with a not entirely zero vector to verify column interactions, which
  77. // could be masked by a subtle bug when using the elementary vectors.
  78. Vector a(num_cols);
  79. for (int i = 0; i < num_cols; i++) {
  80. a(i) = i;
  81. }
  82. Vector b1 = Vector::Zero(num_rows);
  83. Vector b2 = Vector::Zero(num_rows);
  84. tsm->RightMultiplyAndAccumulate(a.data(), b1.data());
  85. dsm->RightMultiplyAndAccumulate(a.data(), b2.data());
  86. EXPECT_EQ((b1 - b2).norm(), 0);
  87. }
  88. TEST_F(DenseSparseMatrixTest, LeftMultiplyAndAccumulate) {
  89. for (int i = 0; i < num_rows; ++i) {
  90. Vector a = Vector::Zero(num_rows);
  91. a(i) = 1.0;
  92. Vector b1 = Vector::Zero(num_cols);
  93. Vector b2 = Vector::Zero(num_cols);
  94. tsm->LeftMultiplyAndAccumulate(a.data(), b1.data());
  95. dsm->LeftMultiplyAndAccumulate(a.data(), b2.data());
  96. EXPECT_EQ((b1 - b2).norm(), 0);
  97. }
  98. // Try with a not entirely zero vector to verify column interactions, which
  99. // could be masked by a subtle bug when using the elementary vectors.
  100. Vector a(num_rows);
  101. for (int i = 0; i < num_rows; i++) {
  102. a(i) = i;
  103. }
  104. Vector b1 = Vector::Zero(num_cols);
  105. Vector b2 = Vector::Zero(num_cols);
  106. tsm->LeftMultiplyAndAccumulate(a.data(), b1.data());
  107. dsm->LeftMultiplyAndAccumulate(a.data(), b2.data());
  108. EXPECT_EQ((b1 - b2).norm(), 0);
  109. }
  110. TEST_F(DenseSparseMatrixTest, ColumnNorm) {
  111. Vector b1 = Vector::Zero(num_cols);
  112. Vector b2 = Vector::Zero(num_cols);
  113. tsm->SquaredColumnNorm(b1.data());
  114. dsm->SquaredColumnNorm(b2.data());
  115. EXPECT_EQ((b1 - b2).norm(), 0);
  116. }
  117. TEST_F(DenseSparseMatrixTest, Scale) {
  118. Vector scale(num_cols);
  119. for (int i = 0; i < num_cols; ++i) {
  120. scale(i) = i + 1;
  121. }
  122. tsm->ScaleColumns(scale.data());
  123. dsm->ScaleColumns(scale.data());
  124. CompareMatrices(tsm.get(), dsm.get());
  125. }
  126. TEST_F(DenseSparseMatrixTest, ToDenseMatrix) {
  127. Matrix tsm_dense;
  128. Matrix dsm_dense;
  129. tsm->ToDenseMatrix(&tsm_dense);
  130. dsm->ToDenseMatrix(&dsm_dense);
  131. EXPECT_EQ((tsm_dense - dsm_dense).norm(), 0.0);
  132. }
  133. } // namespace ceres::internal