numeric_diff_test_utils.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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. // tbennun@gmail.com (Tal Ben-Nun)
  31. #include "ceres/numeric_diff_test_utils.h"
  32. #include <algorithm>
  33. #include <cmath>
  34. #include "ceres/cost_function.h"
  35. #include "ceres/test_util.h"
  36. #include "ceres/types.h"
  37. #include "gtest/gtest.h"
  38. namespace ceres::internal {
  39. bool EasyFunctor::operator()(const double* x1,
  40. const double* x2,
  41. double* residuals) const {
  42. residuals[0] = residuals[1] = residuals[2] = 0;
  43. for (int i = 0; i < 5; ++i) {
  44. residuals[0] += x1[i] * x2[i];
  45. residuals[2] += x2[i] * x2[i];
  46. }
  47. residuals[1] = residuals[0] * residuals[0];
  48. return true;
  49. }
  50. void EasyFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
  51. const CostFunction& cost_function, NumericDiffMethodType method) const {
  52. // The x1[0] is made deliberately small to test the performance near zero.
  53. // clang-format off
  54. double x1[] = { 1e-64, 2.0, 3.0, 4.0, 5.0 };
  55. double x2[] = { 9.0, 9.0, 5.0, 5.0, 1.0 };
  56. double *parameters[] = { &x1[0], &x2[0] };
  57. // clang-format on
  58. double dydx1[15]; // 3 x 5, row major.
  59. double dydx2[15]; // 3 x 5, row major.
  60. double* jacobians[2] = {&dydx1[0], &dydx2[0]};
  61. double residuals[3] = {-1e-100, -2e-100, -3e-100};
  62. ASSERT_TRUE(
  63. cost_function.Evaluate(&parameters[0], &residuals[0], &jacobians[0]));
  64. double expected_residuals[3];
  65. EasyFunctor functor;
  66. functor(x1, x2, expected_residuals);
  67. EXPECT_EQ(expected_residuals[0], residuals[0]);
  68. EXPECT_EQ(expected_residuals[1], residuals[1]);
  69. EXPECT_EQ(expected_residuals[2], residuals[2]);
  70. double tolerance = 0.0;
  71. switch (method) {
  72. default:
  73. case CENTRAL:
  74. tolerance = 3e-9;
  75. break;
  76. case FORWARD:
  77. tolerance = 2e-5;
  78. break;
  79. case RIDDERS:
  80. tolerance = 1e-13;
  81. break;
  82. }
  83. for (int i = 0; i < 5; ++i) {
  84. // clang-format off
  85. ExpectClose(x2[i], dydx1[5 * 0 + i], tolerance); // y1
  86. ExpectClose(x1[i], dydx2[5 * 0 + i], tolerance);
  87. ExpectClose(2 * x2[i] * residuals[0], dydx1[5 * 1 + i], tolerance); // y2
  88. ExpectClose(2 * x1[i] * residuals[0], dydx2[5 * 1 + i], tolerance);
  89. ExpectClose(0.0, dydx1[5 * 2 + i], tolerance); // y3
  90. ExpectClose(2 * x2[i], dydx2[5 * 2 + i], tolerance);
  91. // clang-format on
  92. }
  93. }
  94. bool TranscendentalFunctor::operator()(const double* x1,
  95. const double* x2,
  96. double* residuals) const {
  97. double x1x2 = 0;
  98. for (int i = 0; i < 5; ++i) {
  99. x1x2 += x1[i] * x2[i];
  100. }
  101. residuals[0] = sin(x1x2);
  102. residuals[1] = exp(-x1x2 / 10);
  103. return true;
  104. }
  105. void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
  106. const CostFunction& cost_function, NumericDiffMethodType method) const {
  107. struct TestParameterBlocks {
  108. double x1[5];
  109. double x2[5];
  110. };
  111. // clang-format off
  112. std::vector<TestParameterBlocks> kTests = {
  113. { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // No zeros.
  114. { 9.0, 9.0, 5.0, 5.0, 1.0 },
  115. },
  116. { { 0.0, 2.0, 3.0, 0.0, 5.0 }, // Some zeros x1.
  117. { 9.0, 9.0, 5.0, 5.0, 1.0 },
  118. },
  119. { { 1.0, 2.0, 3.0, 1.0, 5.0 }, // Some zeros x2.
  120. { 0.0, 9.0, 0.0, 5.0, 0.0 },
  121. },
  122. { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros x1.
  123. { 9.0, 9.0, 5.0, 5.0, 1.0 },
  124. },
  125. { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // All zeros x2.
  126. { 0.0, 0.0, 0.0, 0.0, 0.0 },
  127. },
  128. { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros.
  129. { 0.0, 0.0, 0.0, 0.0, 0.0 },
  130. },
  131. };
  132. // clang-format on
  133. for (auto& test : kTests) {
  134. double* x1 = &(test.x1[0]);
  135. double* x2 = &(test.x2[0]);
  136. double* parameters[] = {x1, x2};
  137. double dydx1[10];
  138. double dydx2[10];
  139. double* jacobians[2] = {&dydx1[0], &dydx2[0]};
  140. double residuals[2];
  141. ASSERT_TRUE(
  142. cost_function.Evaluate(&parameters[0], &residuals[0], &jacobians[0]));
  143. double x1x2 = 0;
  144. for (int i = 0; i < 5; ++i) {
  145. x1x2 += x1[i] * x2[i];
  146. }
  147. double tolerance = 0.0;
  148. switch (method) {
  149. default:
  150. case CENTRAL:
  151. tolerance = 2e-7;
  152. break;
  153. case FORWARD:
  154. tolerance = 2e-5;
  155. break;
  156. case RIDDERS:
  157. tolerance = 3e-12;
  158. break;
  159. }
  160. for (int i = 0; i < 5; ++i) {
  161. // clang-format off
  162. ExpectClose( x2[i] * cos(x1x2), dydx1[5 * 0 + i], tolerance);
  163. ExpectClose( x1[i] * cos(x1x2), dydx2[5 * 0 + i], tolerance);
  164. ExpectClose(-x2[i] * exp(-x1x2 / 10.) / 10., dydx1[5 * 1 + i], tolerance);
  165. ExpectClose(-x1[i] * exp(-x1x2 / 10.) / 10., dydx2[5 * 1 + i], tolerance);
  166. // clang-format on
  167. }
  168. }
  169. }
  170. bool ExponentialFunctor::operator()(const double* x1, double* residuals) const {
  171. residuals[0] = exp(x1[0]);
  172. return true;
  173. }
  174. void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
  175. const CostFunction& cost_function) const {
  176. // Evaluating the functor at specific points for testing.
  177. std::vector<double> kTests = {1.0, 2.0, 3.0, 4.0, 5.0};
  178. // Minimal tolerance w.r.t. the cost function and the tests.
  179. const double kTolerance = 2e-14;
  180. for (double& test : kTests) {
  181. double* parameters[] = {&test};
  182. double dydx;
  183. double* jacobians[1] = {&dydx};
  184. double residual;
  185. ASSERT_TRUE(
  186. cost_function.Evaluate(&parameters[0], &residual, &jacobians[0]));
  187. double expected_result = exp(test);
  188. // Expect residual to be close to exp(x).
  189. ExpectClose(residual, expected_result, kTolerance);
  190. // Check evaluated differences. dydx should also be close to exp(x).
  191. ExpectClose(dydx, expected_result, kTolerance);
  192. }
  193. }
  194. bool RandomizedFunctor::operator()(const double* x1, double* residuals) const {
  195. double random_value = uniform_distribution_(*prng_);
  196. residuals[0] = x1[0] * x1[0] + random_value;
  197. return true;
  198. }
  199. void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
  200. const CostFunction& cost_function) const {
  201. std::vector<double> kTests = {0.0, 1.0, 3.0, 4.0, 50.0};
  202. const double kTolerance = 2e-4;
  203. for (double& test : kTests) {
  204. double* parameters[] = {&test};
  205. double dydx;
  206. double* jacobians[1] = {&dydx};
  207. double residual;
  208. ASSERT_TRUE(
  209. cost_function.Evaluate(&parameters[0], &residual, &jacobians[0]));
  210. // Expect residual to be close to x^2 w.r.t. noise factor.
  211. ExpectClose(residual, test * test, noise_factor_);
  212. // Check evaluated differences. (dy/dx = ~2x)
  213. ExpectClose(dydx, 2 * test, kTolerance);
  214. }
  215. }
  216. } // namespace ceres::internal