blob: 4795579b78a2f4c03c3486567c45cd67140e5ced [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
31#include "ceres/autodiff_cost_function.h"
32
33#include <cstddef>
34#include <memory>
35
36#include "gtest/gtest.h"
37#include "ceres/cost_function.h"
38#include "ceres/array_utils.h"
39
40namespace ceres {
41namespace internal {
42
43class BinaryScalarCost {
44 public:
45 explicit BinaryScalarCost(double a): a_(a) {}
46 template <typename T>
47 bool operator()(const T* const x, const T* const y,
48 T* cost) const {
49 cost[0] = x[0] * y[0] + x[1] * y[1] - T(a_);
50 return true;
51 }
52 private:
53 double a_;
54};
55
56TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
57 CostFunction* cost_function =
58 new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
59 new BinaryScalarCost(1.0));
60
61 double** parameters = new double*[2];
62 parameters[0] = new double[2];
63 parameters[1] = new double[2];
64
65 parameters[0][0] = 1;
66 parameters[0][1] = 2;
67
68 parameters[1][0] = 3;
69 parameters[1][1] = 4;
70
71 double** jacobians = new double*[2];
72 jacobians[0] = new double[2];
73 jacobians[1] = new double[2];
74
75 double residuals = 0.0;
76
77 cost_function->Evaluate(parameters, &residuals, NULL);
78 EXPECT_EQ(10.0, residuals);
79
80 cost_function->Evaluate(parameters, &residuals, jacobians);
81 EXPECT_EQ(10.0, residuals);
82
83 EXPECT_EQ(3, jacobians[0][0]);
84 EXPECT_EQ(4, jacobians[0][1]);
85 EXPECT_EQ(1, jacobians[1][0]);
86 EXPECT_EQ(2, jacobians[1][1]);
87
88 delete[] jacobians[0];
89 delete[] jacobians[1];
90 delete[] parameters[0];
91 delete[] parameters[1];
92 delete[] jacobians;
93 delete[] parameters;
94 delete cost_function;
95}
96
97struct TenParameterCost {
98 template <typename T>
99 bool operator()(const T* const x0,
100 const T* const x1,
101 const T* const x2,
102 const T* const x3,
103 const T* const x4,
104 const T* const x5,
105 const T* const x6,
106 const T* const x7,
107 const T* const x8,
108 const T* const x9,
109 T* cost) const {
110 cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
111 return true;
112 }
113};
114
115TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) {
116 CostFunction* cost_function =
117 new AutoDiffCostFunction<
118 TenParameterCost, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>(
119 new TenParameterCost);
120
121 double** parameters = new double*[10];
122 double** jacobians = new double*[10];
123 for (int i = 0; i < 10; ++i) {
124 parameters[i] = new double[1];
125 parameters[i][0] = i;
126 jacobians[i] = new double[1];
127 }
128
129 double residuals = 0.0;
130
131 cost_function->Evaluate(parameters, &residuals, NULL);
132 EXPECT_EQ(45.0, residuals);
133
134 cost_function->Evaluate(parameters, &residuals, jacobians);
135 EXPECT_EQ(residuals, 45.0);
136 for (int i = 0; i < 10; ++i) {
137 EXPECT_EQ(1.0, jacobians[i][0]);
138 }
139
140 for (int i = 0; i < 10; ++i) {
141 delete[] jacobians[i];
142 delete[] parameters[i];
143 }
144 delete[] jacobians;
145 delete[] parameters;
146 delete cost_function;
147}
148
149struct OnlyFillsOneOutputFunctor {
150 template <typename T>
151 bool operator()(const T* x, T* output) const {
152 output[0] = x[0];
153 return true;
154 }
155};
156
157TEST(AutoDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) {
158 double parameter = 1.0;
159 double jacobian[2];
160 double residuals[2];
161 double* parameters[] = {&parameter};
162 double* jacobians[] = {jacobian};
163
164 std::unique_ptr<CostFunction> cost_function(
165 new AutoDiffCostFunction<OnlyFillsOneOutputFunctor, 2, 1>(
166 new OnlyFillsOneOutputFunctor));
167 InvalidateArray(2, jacobian);
168 InvalidateArray(2, residuals);
169 EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, jacobians));
170 EXPECT_FALSE(IsArrayValid(2, jacobian));
171 EXPECT_FALSE(IsArrayValid(2, residuals));
172}
173
174} // namespace internal
175} // namespace ceres