dense_linear_solver_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <memory>
  31. #include "ceres/casts.h"
  32. #include "ceres/context_impl.h"
  33. #include "ceres/internal/config.h"
  34. #include "ceres/linear_least_squares_problems.h"
  35. #include "ceres/linear_solver.h"
  36. #include "ceres/triplet_sparse_matrix.h"
  37. #include "ceres/types.h"
  38. #include "glog/logging.h"
  39. #include "gtest/gtest.h"
  40. namespace ceres::internal {
  41. using Param = ::testing::
  42. tuple<LinearSolverType, DenseLinearAlgebraLibraryType, bool, int>;
  43. static std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
  44. Param param = info.param;
  45. std::stringstream ss;
  46. ss << LinearSolverTypeToString(::testing::get<0>(param)) << "_"
  47. << DenseLinearAlgebraLibraryTypeToString(::testing::get<1>(param)) << "_"
  48. << (::testing::get<2>(param) ? "Regularized" : "Unregularized") << "_"
  49. << ::testing::get<3>(param);
  50. return ss.str();
  51. }
  52. class DenseLinearSolverTest : public ::testing::TestWithParam<Param> {};
  53. TEST_P(DenseLinearSolverTest, _) {
  54. Param param = GetParam();
  55. const bool regularized = testing::get<2>(param);
  56. std::unique_ptr<LinearLeastSquaresProblem> problem =
  57. CreateLinearLeastSquaresProblemFromId(testing::get<3>(param));
  58. DenseSparseMatrix lhs(*down_cast<TripletSparseMatrix*>(problem->A.get()));
  59. const int num_cols = lhs.num_cols();
  60. const int num_rows = lhs.num_rows();
  61. Vector rhs = Vector::Zero(num_rows + num_cols);
  62. rhs.head(num_rows) = ConstVectorRef(problem->b.get(), num_rows);
  63. LinearSolver::Options options;
  64. options.type = ::testing::get<0>(param);
  65. options.dense_linear_algebra_library_type = ::testing::get<1>(param);
  66. ContextImpl context;
  67. options.context = &context;
  68. std::unique_ptr<LinearSolver> solver(LinearSolver::Create(options));
  69. LinearSolver::PerSolveOptions per_solve_options;
  70. if (regularized) {
  71. per_solve_options.D = problem->D.get();
  72. }
  73. Vector solution(num_cols);
  74. LinearSolver::Summary summary =
  75. solver->Solve(&lhs, rhs.data(), per_solve_options, solution.data());
  76. EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
  77. Vector normal_rhs = lhs.matrix().transpose() * rhs.head(num_rows);
  78. Matrix normal_lhs = lhs.matrix().transpose() * lhs.matrix();
  79. if (regularized) {
  80. ConstVectorRef diagonal(problem->D.get(), num_cols);
  81. normal_lhs += diagonal.array().square().matrix().asDiagonal();
  82. }
  83. Vector actual_normal_rhs = normal_lhs * solution;
  84. const double normalized_residual =
  85. (normal_rhs - actual_normal_rhs).norm() / normal_rhs.norm();
  86. EXPECT_NEAR(
  87. normalized_residual, 0.0, 10 * std::numeric_limits<double>::epsilon())
  88. << "\nexpected: " << normal_rhs.transpose()
  89. << "\nactual: " << actual_normal_rhs.transpose();
  90. }
  91. namespace {
  92. // TODO(sameeragarwal): Should we move away from hard coded linear
  93. // least squares problem to randomly generated ones?
  94. #ifndef CERES_NO_LAPACK
  95. INSTANTIATE_TEST_SUITE_P(
  96. DenseLinearSolver,
  97. DenseLinearSolverTest,
  98. ::testing::Combine(::testing::Values(DENSE_QR, DENSE_NORMAL_CHOLESKY),
  99. ::testing::Values(EIGEN, LAPACK),
  100. ::testing::Values(true, false),
  101. ::testing::Values(0, 1)),
  102. ParamInfoToString);
  103. #else
  104. INSTANTIATE_TEST_SUITE_P(
  105. DenseLinearSolver,
  106. DenseLinearSolverTest,
  107. ::testing::Combine(::testing::Values(DENSE_QR, DENSE_NORMAL_CHOLESKY),
  108. ::testing::Values(EIGEN),
  109. ::testing::Values(true, false),
  110. ::testing::Values(0, 1)),
  111. ParamInfoToString);
  112. #endif
  113. } // namespace
  114. } // namespace ceres::internal