1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
31 #include "ceres/autodiff_cost_function.h"
35 #include "gtest/gtest.h"
36 #include "ceres/cost_function.h"
37 #include "ceres/array_utils.h"
42 class BinaryScalarCost {
44 explicit BinaryScalarCost(double a): a_(a) {}
46 bool operator()(const T* const x, const T* const y,
48 cost[0] = x[0] * y[0] + x[1] * y[1] - T(a_);
55 TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
56 CostFunction* cost_function =
57 new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
58 new BinaryScalarCost(1.0));
60 double** parameters = new double*[2];
61 parameters[0] = new double[2];
62 parameters[1] = new double[2];
70 double** jacobians = new double*[2];
71 jacobians[0] = new double[2];
72 jacobians[1] = new double[2];
74 double residuals = 0.0;
76 cost_function->Evaluate(parameters, &residuals, NULL);
77 EXPECT_EQ(10.0, residuals);
78 cost_function->Evaluate(parameters, &residuals, jacobians);
80 EXPECT_EQ(3, jacobians[0][0]);
81 EXPECT_EQ(4, jacobians[0][1]);
82 EXPECT_EQ(1, jacobians[1][0]);
83 EXPECT_EQ(2, jacobians[1][1]);
85 delete[] jacobians[0];
86 delete[] jacobians[1];
87 delete[] parameters[0];
88 delete[] parameters[1];
94 struct TenParameterCost {
96 bool operator()(const T* const x0,
107 cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
112 TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) {
113 CostFunction* cost_function =
114 new AutoDiffCostFunction<
115 TenParameterCost, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>(
116 new TenParameterCost);
118 double** parameters = new double*[10];
119 double** jacobians = new double*[10];
120 for (int i = 0; i < 10; ++i) {
121 parameters[i] = new double[1];
122 parameters[i][0] = i;
123 jacobians[i] = new double[1];
126 double residuals = 0.0;
128 cost_function->Evaluate(parameters, &residuals, NULL);
129 EXPECT_EQ(45.0, residuals);
131 cost_function->Evaluate(parameters, &residuals, jacobians);
132 EXPECT_EQ(residuals, 45.0);
133 for (int i = 0; i < 10; ++i) {
134 EXPECT_EQ(1.0, jacobians[i][0]);
137 for (int i = 0; i < 10; ++i) {
138 delete[] jacobians[i];
139 delete[] parameters[i];
143 delete cost_function;
146 struct OnlyFillsOneOutputFunctor {
147 template <typename T>
148 bool operator()(const T* x, T* output) const {
154 TEST(AutoDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) {
155 double parameter = 1.0;
158 double* parameters[] = {¶meter};
159 double* jacobians[] = {jacobian};
161 scoped_ptr<CostFunction> cost_function(
162 new AutoDiffCostFunction<OnlyFillsOneOutputFunctor, 2, 1>(
163 new OnlyFillsOneOutputFunctor));
164 InvalidateArray(2, jacobian);
165 InvalidateArray(2, residuals);
166 EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, jacobians));
167 EXPECT_FALSE(IsArrayValid(2, jacobian));
168 EXPECT_FALSE(IsArrayValid(2, residuals));
171 } // namespace internal