dense_qr_test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/dense_qr.h"
  31. #include <memory>
  32. #include <numeric>
  33. #include <string>
  34. #include <tuple>
  35. #include <vector>
  36. #include "Eigen/Dense"
  37. #include "ceres/internal/eigen.h"
  38. #include "ceres/linear_solver.h"
  39. #include "glog/logging.h"
  40. #include "gmock/gmock.h"
  41. #include "gtest/gtest.h"
  42. namespace ceres::internal {
  43. using Param = DenseLinearAlgebraLibraryType;
  44. namespace {
  45. std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
  46. return DenseLinearAlgebraLibraryTypeToString(info.param);
  47. }
  48. } // namespace
  49. class DenseQRTest : public ::testing::TestWithParam<Param> {};
  50. TEST_P(DenseQRTest, FactorAndSolve) {
  51. // TODO(sameeragarwal): Convert these tests into type parameterized tests so
  52. // that we can test the single and double precision solvers.
  53. using Scalar = double;
  54. using MatrixType = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
  55. using VectorType = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
  56. LinearSolver::Options options;
  57. ContextImpl context;
  58. #ifndef CERES_NO_CUDA
  59. options.context = &context;
  60. std::string error;
  61. CHECK(context.InitCuda(&error)) << error;
  62. #endif // CERES_NO_CUDA
  63. options.dense_linear_algebra_library_type = GetParam();
  64. const double kEpsilon = std::numeric_limits<double>::epsilon() * 1.5e4;
  65. std::unique_ptr<DenseQR> dense_qr = DenseQR::Create(options);
  66. const int kNumTrials = 10;
  67. const int kMinNumCols = 1;
  68. const int kMaxNumCols = 10;
  69. const int kMinRowsFactor = 1;
  70. const int kMaxRowsFactor = 3;
  71. for (int num_cols = kMinNumCols; num_cols < kMaxNumCols; ++num_cols) {
  72. for (int num_rows = kMinRowsFactor * num_cols;
  73. num_rows < kMaxRowsFactor * num_cols;
  74. ++num_rows) {
  75. for (int trial = 0; trial < kNumTrials; ++trial) {
  76. MatrixType lhs = MatrixType::Random(num_rows, num_cols);
  77. Vector x = VectorType::Random(num_cols);
  78. Vector rhs = lhs * x;
  79. Vector actual = Vector::Random(num_cols);
  80. LinearSolver::Summary summary;
  81. summary.termination_type = dense_qr->FactorAndSolve(num_rows,
  82. num_cols,
  83. lhs.data(),
  84. rhs.data(),
  85. actual.data(),
  86. &summary.message);
  87. ASSERT_EQ(summary.termination_type,
  88. LinearSolverTerminationType::SUCCESS);
  89. ASSERT_NEAR((x - actual).norm() / x.norm(), 0.0, kEpsilon)
  90. << "\nexpected: " << x.transpose()
  91. << "\nactual : " << actual.transpose();
  92. }
  93. }
  94. }
  95. }
  96. namespace {
  97. // NOTE: preprocessor directives in a macro are not standard conforming
  98. decltype(auto) MakeValues() {
  99. return ::testing::Values(EIGEN
  100. #ifndef CERES_NO_LAPACK
  101. ,
  102. LAPACK
  103. #endif
  104. #ifndef CERES_NO_CUDA
  105. ,
  106. CUDA
  107. #endif
  108. );
  109. }
  110. } // namespace
  111. INSTANTIATE_TEST_SUITE_P(_, DenseQRTest, MakeValues(), ParamInfoToString);
  112. } // namespace ceres::internal