blob: cc340f6d93626c579d227144a3143a61b57ed5a1 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08002// Copyright 2019 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
31#include "ceres/autodiff_cost_function.h"
32
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include <memory>
34
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include "ceres/array_utils.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036#include "ceres/cost_function.h"
37#include "gtest/gtest.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038
39namespace ceres {
40namespace internal {
41
42class BinaryScalarCost {
43 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080044 explicit BinaryScalarCost(double a) : a_(a) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080045 template <typename T>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080046 bool operator()(const T* const x, const T* const y, T* cost) const {
47 cost[0] = x[0] * y[0] + x[1] * y[1] - T(a_);
Austin Schuh70cc9552019-01-21 19:46:48 -080048 return true;
49 }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080050
Austin Schuh70cc9552019-01-21 19:46:48 -080051 private:
52 double a_;
53};
54
55TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080056 CostFunction* cost_function =
57 new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
58 new BinaryScalarCost(1.0));
Austin Schuh70cc9552019-01-21 19:46:48 -080059
60 double** parameters = new double*[2];
61 parameters[0] = new double[2];
62 parameters[1] = new double[2];
63
64 parameters[0][0] = 1;
65 parameters[0][1] = 2;
66
67 parameters[1][0] = 3;
68 parameters[1][1] = 4;
69
70 double** jacobians = new double*[2];
71 jacobians[0] = new double[2];
72 jacobians[1] = new double[2];
73
74 double residuals = 0.0;
75
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080076 cost_function->Evaluate(parameters, &residuals, nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -080077 EXPECT_EQ(10.0, residuals);
78
79 cost_function->Evaluate(parameters, &residuals, jacobians);
80 EXPECT_EQ(10.0, residuals);
81
82 EXPECT_EQ(3, jacobians[0][0]);
83 EXPECT_EQ(4, jacobians[0][1]);
84 EXPECT_EQ(1, jacobians[1][0]);
85 EXPECT_EQ(2, jacobians[1][1]);
86
87 delete[] jacobians[0];
88 delete[] jacobians[1];
89 delete[] parameters[0];
90 delete[] parameters[1];
91 delete[] jacobians;
92 delete[] parameters;
93 delete cost_function;
94}
95
96struct TenParameterCost {
97 template <typename T>
98 bool operator()(const T* const x0,
99 const T* const x1,
100 const T* const x2,
101 const T* const x3,
102 const T* const x4,
103 const T* const x5,
104 const T* const x6,
105 const T* const x7,
106 const T* const x8,
107 const T* const x9,
108 T* cost) const {
109 cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
110 return true;
111 }
112};
113
114TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800115 CostFunction* cost_function =
116 new AutoDiffCostFunction<TenParameterCost,
117 1,
118 1,
119 1,
120 1,
121 1,
122 1,
123 1,
124 1,
125 1,
126 1,
127 1>(new TenParameterCost);
Austin Schuh70cc9552019-01-21 19:46:48 -0800128
129 double** parameters = new double*[10];
130 double** jacobians = new double*[10];
131 for (int i = 0; i < 10; ++i) {
132 parameters[i] = new double[1];
133 parameters[i][0] = i;
134 jacobians[i] = new double[1];
135 }
136
137 double residuals = 0.0;
138
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800139 cost_function->Evaluate(parameters, &residuals, nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -0800140 EXPECT_EQ(45.0, residuals);
141
142 cost_function->Evaluate(parameters, &residuals, jacobians);
143 EXPECT_EQ(residuals, 45.0);
144 for (int i = 0; i < 10; ++i) {
145 EXPECT_EQ(1.0, jacobians[i][0]);
146 }
147
148 for (int i = 0; i < 10; ++i) {
149 delete[] jacobians[i];
150 delete[] parameters[i];
151 }
152 delete[] jacobians;
153 delete[] parameters;
154 delete cost_function;
155}
156
157struct OnlyFillsOneOutputFunctor {
158 template <typename T>
159 bool operator()(const T* x, T* output) const {
160 output[0] = x[0];
161 return true;
162 }
163};
164
165TEST(AutoDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) {
166 double parameter = 1.0;
167 double jacobian[2];
168 double residuals[2];
169 double* parameters[] = {&parameter};
170 double* jacobians[] = {jacobian};
171
172 std::unique_ptr<CostFunction> cost_function(
173 new AutoDiffCostFunction<OnlyFillsOneOutputFunctor, 2, 1>(
174 new OnlyFillsOneOutputFunctor));
175 InvalidateArray(2, jacobian);
176 InvalidateArray(2, residuals);
177 EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, jacobians));
178 EXPECT_FALSE(IsArrayValid(2, jacobian));
179 EXPECT_FALSE(IsArrayValid(2, residuals));
180}
181
182} // namespace internal
183} // namespace ceres