Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / autodiff_cost_function_test.cc
1 // 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
35 #include "gtest/gtest.h"
36 #include "ceres/cost_function.h"
37 #include "ceres/array_utils.h"
38
39 namespace ceres {
40 namespace internal {
41
42 class BinaryScalarCost {
43  public:
44   explicit BinaryScalarCost(double a): a_(a) {}
45   template <typename T>
46   bool operator()(const T* const x, const T* const y,
47                   T* cost) const {
48     cost[0] = x[0] * y[0] + x[1] * y[1]  - T(a_);
49     return true;
50   }
51  private:
52   double a_;
53 };
54
55 TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
56   CostFunction* cost_function  =
57     new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
58         new BinaryScalarCost(1.0));
59
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
76   cost_function->Evaluate(parameters, &residuals, NULL);
77   EXPECT_EQ(10.0, residuals);
78   cost_function->Evaluate(parameters, &residuals, jacobians);
79
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]);
84
85   delete[] jacobians[0];
86   delete[] jacobians[1];
87   delete[] parameters[0];
88   delete[] parameters[1];
89   delete[] jacobians;
90   delete[] parameters;
91   delete cost_function;
92 }
93
94 struct TenParameterCost {
95   template <typename T>
96   bool operator()(const T* const x0,
97                   const T* const x1,
98                   const T* const x2,
99                   const T* const x3,
100                   const T* const x4,
101                   const T* const x5,
102                   const T* const x6,
103                   const T* const x7,
104                   const T* const x8,
105                   const T* const x9,
106                   T* cost) const {
107     cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
108     return true;
109   }
110 };
111
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);
117
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];
124   }
125
126   double residuals = 0.0;
127
128   cost_function->Evaluate(parameters, &residuals, NULL);
129   EXPECT_EQ(45.0, residuals);
130
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]);
135   }
136
137   for (int i = 0; i < 10; ++i) {
138     delete[] jacobians[i];
139     delete[] parameters[i];
140   }
141   delete[] jacobians;
142   delete[] parameters;
143   delete cost_function;
144 }
145
146 struct OnlyFillsOneOutputFunctor {
147   template <typename T>
148   bool operator()(const T* x, T* output) const {
149     output[0] = x[0];
150     return true;
151   }
152 };
153
154 TEST(AutoDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) {
155   double parameter = 1.0;
156   double jacobian[2];
157   double residuals[2];
158   double* parameters[] = {&parameter};
159   double* jacobians[] = {jacobian};
160
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));
169 }
170
171 }  // namespace internal
172 }  // namespace ceres