numeric_diff_test_utils.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
  31. #define CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
  32. #include <random>
  33. #include "ceres/cost_function.h"
  34. #include "ceres/internal/export.h"
  35. #include "ceres/sized_cost_function.h"
  36. #include "ceres/types.h"
  37. namespace ceres::internal {
  38. // Noise factor for randomized cost function.
  39. static constexpr double kNoiseFactor = 0.01;
  40. // Default random seed for randomized cost function.
  41. static constexpr unsigned int kRandomSeed = 1234;
  42. // y1 = x1'x2 -> dy1/dx1 = x2, dy1/dx2 = x1
  43. // y2 = (x1'x2)^2 -> dy2/dx1 = 2 * x2 * (x1'x2), dy2/dx2 = 2 * x1 * (x1'x2)
  44. // y3 = x2'x2 -> dy3/dx1 = 0, dy3/dx2 = 2 * x2
  45. class CERES_NO_EXPORT EasyFunctor {
  46. public:
  47. bool operator()(const double* x1, const double* x2, double* residuals) const;
  48. void ExpectCostFunctionEvaluationIsNearlyCorrect(
  49. const CostFunction& cost_function, NumericDiffMethodType method) const;
  50. };
  51. class EasyCostFunction : public SizedCostFunction<3, 5, 5> {
  52. public:
  53. bool Evaluate(double const* const* parameters,
  54. double* residuals,
  55. double** /* not used */) const final {
  56. return functor_(parameters[0], parameters[1], residuals);
  57. }
  58. private:
  59. EasyFunctor functor_;
  60. };
  61. // y1 = sin(x1'x2)
  62. // y2 = exp(-x1'x2 / 10)
  63. //
  64. // dy1/dx1 = x2 * cos(x1'x2), dy1/dx2 = x1 * cos(x1'x2)
  65. // dy2/dx1 = -x2 * exp(-x1'x2 / 10) / 10, dy2/dx2 = -x2 * exp(-x1'x2 / 10) / 10
  66. class CERES_NO_EXPORT TranscendentalFunctor {
  67. public:
  68. bool operator()(const double* x1, const double* x2, double* residuals) const;
  69. void ExpectCostFunctionEvaluationIsNearlyCorrect(
  70. const CostFunction& cost_function, NumericDiffMethodType method) const;
  71. };
  72. class CERES_NO_EXPORT TranscendentalCostFunction
  73. : public SizedCostFunction<2, 5, 5> {
  74. public:
  75. bool Evaluate(double const* const* parameters,
  76. double* residuals,
  77. double** /* not used */) const final {
  78. return functor_(parameters[0], parameters[1], residuals);
  79. }
  80. private:
  81. TranscendentalFunctor functor_;
  82. };
  83. // y = exp(x), dy/dx = exp(x)
  84. class CERES_NO_EXPORT ExponentialFunctor {
  85. public:
  86. bool operator()(const double* x1, double* residuals) const;
  87. void ExpectCostFunctionEvaluationIsNearlyCorrect(
  88. const CostFunction& cost_function) const;
  89. };
  90. class ExponentialCostFunction : public SizedCostFunction<1, 1> {
  91. public:
  92. bool Evaluate(double const* const* parameters,
  93. double* residuals,
  94. double** /* not used */) const final {
  95. return functor_(parameters[0], residuals);
  96. }
  97. private:
  98. ExponentialFunctor functor_;
  99. };
  100. // Test adaptive numeric differentiation by synthetically adding random noise
  101. // to a functor.
  102. // y = x^2 + [random noise], dy/dx ~ 2x
  103. class CERES_NO_EXPORT RandomizedFunctor {
  104. public:
  105. RandomizedFunctor(double noise_factor, std::mt19937& prng)
  106. : noise_factor_(noise_factor),
  107. prng_(&prng),
  108. uniform_distribution_{-noise_factor, noise_factor} {}
  109. bool operator()(const double* x1, double* residuals) const;
  110. void ExpectCostFunctionEvaluationIsNearlyCorrect(
  111. const CostFunction& cost_function) const;
  112. private:
  113. double noise_factor_;
  114. // Store the generator as a pointer to be able to modify the instance the
  115. // pointer is pointing to.
  116. std::mt19937* prng_;
  117. mutable std::uniform_real_distribution<> uniform_distribution_;
  118. };
  119. class CERES_NO_EXPORT RandomizedCostFunction : public SizedCostFunction<1, 1> {
  120. public:
  121. RandomizedCostFunction(double noise_factor, std::mt19937& prng)
  122. : functor_(noise_factor, prng) {}
  123. bool Evaluate(double const* const* parameters,
  124. double* residuals,
  125. double** /* not used */) const final {
  126. return functor_(parameters[0], residuals);
  127. }
  128. private:
  129. RandomizedFunctor functor_;
  130. };
  131. } // namespace ceres::internal
  132. #endif // CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_