test_util.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Utility functions useful for testing.
  32. #include "ceres/test_util.h"
  33. #include <algorithm>
  34. #include <cmath>
  35. #include "ceres/file.h"
  36. #include "ceres/internal/port.h"
  37. #include "ceres/stringprintf.h"
  38. #include "ceres/types.h"
  39. #include "gflags/gflags.h"
  40. #include "glog/logging.h"
  41. #include "gtest/gtest.h"
  42. DECLARE_string(test_srcdir);
  43. // This macro is used to inject additional path information specific
  44. // to the build system.
  45. #ifndef CERES_TEST_SRCDIR_SUFFIX
  46. #define CERES_TEST_SRCDIR_SUFFIX ""
  47. #endif
  48. namespace ceres {
  49. namespace internal {
  50. bool ExpectClose(double x, double y, double max_abs_relative_difference) {
  51. if (std::isinf(x) && std::isinf(y)) {
  52. EXPECT_EQ(std::signbit(x), std::signbit(y));
  53. return true;
  54. }
  55. if (std::isnan(x) && std::isnan(y)) {
  56. return true;
  57. }
  58. double absolute_difference = fabs(x - y);
  59. double relative_difference = absolute_difference / std::max(fabs(x), fabs(y));
  60. if (x == 0 || y == 0) {
  61. // If x or y is exactly zero, then relative difference doesn't have any
  62. // meaning. Take the absolute difference instead.
  63. relative_difference = absolute_difference;
  64. }
  65. if (relative_difference > max_abs_relative_difference) {
  66. VLOG(1) << StringPrintf("x=%17g y=%17g abs=%17g rel=%17g",
  67. x,
  68. y,
  69. absolute_difference,
  70. relative_difference);
  71. }
  72. EXPECT_NEAR(relative_difference, 0.0, max_abs_relative_difference);
  73. return relative_difference <= max_abs_relative_difference;
  74. }
  75. void ExpectArraysCloseUptoScale(int n,
  76. const double* p,
  77. const double* q,
  78. double tol) {
  79. CHECK_GT(n, 0);
  80. CHECK(p);
  81. CHECK(q);
  82. double p_max = 0;
  83. double q_max = 0;
  84. int p_i = 0;
  85. int q_i = 0;
  86. for (int i = 0; i < n; ++i) {
  87. if (std::abs(p[i]) > p_max) {
  88. p_max = std::abs(p[i]);
  89. p_i = i;
  90. }
  91. if (std::abs(q[i]) > q_max) {
  92. q_max = std::abs(q[i]);
  93. q_i = i;
  94. }
  95. }
  96. // If both arrays are all zeros, they are equal up to scale, but
  97. // for testing purposes, that's more likely to be an error than
  98. // a desired result.
  99. CHECK_NE(p_max, 0.0);
  100. CHECK_NE(q_max, 0.0);
  101. for (int i = 0; i < n; ++i) {
  102. double p_norm = p[i] / p[p_i];
  103. double q_norm = q[i] / q[q_i];
  104. EXPECT_NEAR(p_norm, q_norm, tol) << "i=" << i;
  105. }
  106. }
  107. void ExpectArraysClose(int n, const double* p, const double* q, double tol) {
  108. CHECK_GT(n, 0);
  109. CHECK(p);
  110. CHECK(q);
  111. for (int i = 0; i < n; ++i) {
  112. EXPECT_TRUE(ExpectClose(p[i], q[i], tol)) << "p[" << i << "]" << p[i] << " "
  113. << "q[" << i << "]" << q[i] << " "
  114. << "tol: " << tol;
  115. }
  116. }
  117. std::string TestFileAbsolutePath(const std::string& filename) {
  118. return JoinPath(CERES_GET_FLAG(FLAGS_test_srcdir) + CERES_TEST_SRCDIR_SUFFIX,
  119. filename);
  120. }
  121. std::string ToString(const Solver::Options& options) {
  122. return StringPrintf("(%s, %s, %s, %s, %d)",
  123. LinearSolverTypeToString(options.linear_solver_type),
  124. SparseLinearAlgebraLibraryTypeToString(
  125. options.sparse_linear_algebra_library_type),
  126. options.linear_solver_ordering ? "USER" : "AUTOMATIC",
  127. PreconditionerTypeToString(options.preconditioner_type),
  128. options.num_threads);
  129. }
  130. } // namespace internal
  131. } // namespace ceres