blob: d833bbb881bb6a51c28dce3944f20e99c6d471f8 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 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
32#include "ceres/numeric_diff_test_utils.h"
33
34#include <algorithm>
35#include <cmath>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/cost_function.h"
38#include "ceres/test_util.h"
39#include "ceres/types.h"
40#include "gtest/gtest.h"
41
Austin Schuh70cc9552019-01-21 19:46:48 -080042namespace ceres {
43namespace internal {
44
45bool EasyFunctor::operator()(const double* x1,
46 const double* x2,
47 double* residuals) const {
48 residuals[0] = residuals[1] = residuals[2] = 0;
49 for (int i = 0; i < 5; ++i) {
50 residuals[0] += x1[i] * x2[i];
51 residuals[2] += x2[i] * x2[i];
52 }
53 residuals[1] = residuals[0] * residuals[0];
54 return true;
55}
56
57void EasyFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080058 const CostFunction& cost_function, NumericDiffMethodType method) const {
59 // The x1[0] is made deliberately small to test the performance near zero.
60 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080061 double x1[] = { 1e-64, 2.0, 3.0, 4.0, 5.0 };
62 double x2[] = { 9.0, 9.0, 5.0, 5.0, 1.0 };
63 double *parameters[] = { &x1[0], &x2[0] };
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080064 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -080065
66 double dydx1[15]; // 3 x 5, row major.
67 double dydx2[15]; // 3 x 5, row major.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080068 double* jacobians[2] = {&dydx1[0], &dydx2[0]};
Austin Schuh70cc9552019-01-21 19:46:48 -080069
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080070 double residuals[3] = {-1e-100, -2e-100, -3e-100};
Austin Schuh70cc9552019-01-21 19:46:48 -080071
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080072 ASSERT_TRUE(
73 cost_function.Evaluate(&parameters[0], &residuals[0], &jacobians[0]));
Austin Schuh70cc9552019-01-21 19:46:48 -080074
75 double expected_residuals[3];
76 EasyFunctor functor;
77 functor(x1, x2, expected_residuals);
78 EXPECT_EQ(expected_residuals[0], residuals[0]);
79 EXPECT_EQ(expected_residuals[1], residuals[1]);
80 EXPECT_EQ(expected_residuals[2], residuals[2]);
81
82 double tolerance = 0.0;
83 switch (method) {
84 default:
85 case CENTRAL:
86 tolerance = 3e-9;
87 break;
88
89 case FORWARD:
90 tolerance = 2e-5;
91 break;
92
93 case RIDDERS:
94 tolerance = 1e-13;
95 break;
96 }
97
98 for (int i = 0; i < 5; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080099 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800100 ExpectClose(x2[i], dydx1[5 * 0 + i], tolerance); // y1
101 ExpectClose(x1[i], dydx2[5 * 0 + i], tolerance);
102 ExpectClose(2 * x2[i] * residuals[0], dydx1[5 * 1 + i], tolerance); // y2
103 ExpectClose(2 * x1[i] * residuals[0], dydx2[5 * 1 + i], tolerance);
104 ExpectClose(0.0, dydx1[5 * 2 + i], tolerance); // y3
105 ExpectClose(2 * x2[i], dydx2[5 * 2 + i], tolerance);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800106 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800107 }
108}
109
110bool TranscendentalFunctor::operator()(const double* x1,
111 const double* x2,
112 double* residuals) const {
113 double x1x2 = 0;
114 for (int i = 0; i < 5; ++i) {
115 x1x2 += x1[i] * x2[i];
116 }
117 residuals[0] = sin(x1x2);
118 residuals[1] = exp(-x1x2 / 10);
119 return true;
120}
121
122void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123 const CostFunction& cost_function, NumericDiffMethodType method) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800124 struct TestParameterBlocks {
125 double x1[5];
126 double x2[5];
127 };
128
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800129 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800130 std::vector<TestParameterBlocks> kTests = {
131 { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // No zeros.
132 { 9.0, 9.0, 5.0, 5.0, 1.0 },
133 },
134 { { 0.0, 2.0, 3.0, 0.0, 5.0 }, // Some zeros x1.
135 { 9.0, 9.0, 5.0, 5.0, 1.0 },
136 },
137 { { 1.0, 2.0, 3.0, 1.0, 5.0 }, // Some zeros x2.
138 { 0.0, 9.0, 0.0, 5.0, 0.0 },
139 },
140 { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros x1.
141 { 9.0, 9.0, 5.0, 5.0, 1.0 },
142 },
143 { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // All zeros x2.
144 { 0.0, 0.0, 0.0, 0.0, 0.0 },
145 },
146 { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros.
147 { 0.0, 0.0, 0.0, 0.0, 0.0 },
148 },
149 };
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800150 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800151
152 for (int k = 0; k < kTests.size(); ++k) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800153 double* x1 = &(kTests[k].x1[0]);
154 double* x2 = &(kTests[k].x2[0]);
155 double* parameters[] = {x1, x2};
Austin Schuh70cc9552019-01-21 19:46:48 -0800156
157 double dydx1[10];
158 double dydx2[10];
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800159 double* jacobians[2] = {&dydx1[0], &dydx2[0]};
Austin Schuh70cc9552019-01-21 19:46:48 -0800160
161 double residuals[2];
162
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800163 ASSERT_TRUE(
164 cost_function.Evaluate(&parameters[0], &residuals[0], &jacobians[0]));
Austin Schuh70cc9552019-01-21 19:46:48 -0800165 double x1x2 = 0;
166 for (int i = 0; i < 5; ++i) {
167 x1x2 += x1[i] * x2[i];
168 }
169
170 double tolerance = 0.0;
171 switch (method) {
172 default:
173 case CENTRAL:
174 tolerance = 2e-7;
175 break;
176
177 case FORWARD:
178 tolerance = 2e-5;
179 break;
180
181 case RIDDERS:
182 tolerance = 3e-12;
183 break;
184 }
185
186 for (int i = 0; i < 5; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800187 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800188 ExpectClose( x2[i] * cos(x1x2), dydx1[5 * 0 + i], tolerance);
189 ExpectClose( x1[i] * cos(x1x2), dydx2[5 * 0 + i], tolerance);
190 ExpectClose(-x2[i] * exp(-x1x2 / 10.) / 10., dydx1[5 * 1 + i], tolerance);
191 ExpectClose(-x1[i] * exp(-x1x2 / 10.) / 10., dydx2[5 * 1 + i], tolerance);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800192 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800193 }
194 }
195}
196
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800197bool ExponentialFunctor::operator()(const double* x1, double* residuals) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800198 residuals[0] = exp(x1[0]);
199 return true;
200}
201
Austin Schuh70cc9552019-01-21 19:46:48 -0800202void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
203 const CostFunction& cost_function) const {
204 // Evaluating the functor at specific points for testing.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800205 std::vector<double> kTests = {1.0, 2.0, 3.0, 4.0, 5.0};
Austin Schuh70cc9552019-01-21 19:46:48 -0800206
207 // Minimal tolerance w.r.t. the cost function and the tests.
208 const double kTolerance = 2e-14;
209
210 for (int k = 0; k < kTests.size(); ++k) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800211 double* parameters[] = {&kTests[k]};
Austin Schuh70cc9552019-01-21 19:46:48 -0800212 double dydx;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800213 double* jacobians[1] = {&dydx};
Austin Schuh70cc9552019-01-21 19:46:48 -0800214 double residual;
215
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800216 ASSERT_TRUE(
217 cost_function.Evaluate(&parameters[0], &residual, &jacobians[0]));
Austin Schuh70cc9552019-01-21 19:46:48 -0800218
219 double expected_result = exp(kTests[k]);
220
221 // Expect residual to be close to exp(x).
222 ExpectClose(residual, expected_result, kTolerance);
223
224 // Check evaluated differences. dydx should also be close to exp(x).
225 ExpectClose(dydx, expected_result, kTolerance);
226 }
227}
228
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800229bool RandomizedFunctor::operator()(const double* x1, double* residuals) const {
230 double random_value =
231 static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
Austin Schuh70cc9552019-01-21 19:46:48 -0800232
233 // Normalize noise to [-factor, factor].
234 random_value *= 2.0;
235 random_value -= 1.0;
236 random_value *= noise_factor_;
237
238 residuals[0] = x1[0] * x1[0] + random_value;
239 return true;
240}
241
242void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
243 const CostFunction& cost_function) const {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800244 std::vector<double> kTests = {0.0, 1.0, 3.0, 4.0, 50.0};
Austin Schuh70cc9552019-01-21 19:46:48 -0800245
246 const double kTolerance = 2e-4;
247
248 // Initialize random number generator with given seed.
249 srand(random_seed_);
250
251 for (int k = 0; k < kTests.size(); ++k) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800252 double* parameters[] = {&kTests[k]};
Austin Schuh70cc9552019-01-21 19:46:48 -0800253 double dydx;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800254 double* jacobians[1] = {&dydx};
Austin Schuh70cc9552019-01-21 19:46:48 -0800255 double residual;
256
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800257 ASSERT_TRUE(
258 cost_function.Evaluate(&parameters[0], &residual, &jacobians[0]));
Austin Schuh70cc9552019-01-21 19:46:48 -0800259
260 // Expect residual to be close to x^2 w.r.t. noise factor.
261 ExpectClose(residual, kTests[k] * kTests[k], noise_factor_);
262
263 // Check evaluated differences. (dy/dx = ~2x)
264 ExpectClose(dydx, 2 * kTests[k], kTolerance);
265 }
266}
267
268} // namespace internal
269} // namespace ceres