1// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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
38namespace ceres {
39namespace internal {
40
41class BinaryScalarCost {
42 public:
43  explicit BinaryScalarCost(double a): a_(a) {}
44  template <typename T>
45  bool operator()(const T* const x, const T* const y,
46                  T* cost) const {
47    cost[0] = x[0] * y[0] + x[1] * y[1]  - T(a_);
48    return true;
49  }
50 private:
51  double a_;
52};
53
54TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
55  CostFunction* cost_function  =
56    new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
57        new BinaryScalarCost(1.0));
58
59  double** parameters = new double*[2];
60  parameters[0] = new double[2];
61  parameters[1] = new double[2];
62
63  parameters[0][0] = 1;
64  parameters[0][1] = 2;
65
66  parameters[1][0] = 3;
67  parameters[1][1] = 4;
68
69  double** jacobians = new double*[2];
70  jacobians[0] = new double[2];
71  jacobians[1] = new double[2];
72
73  double residuals = 0.0;
74
75  cost_function->Evaluate(parameters, &residuals, NULL);
76  EXPECT_EQ(10.0, residuals);
77  cost_function->Evaluate(parameters, &residuals, jacobians);
78
79  EXPECT_EQ(3, jacobians[0][0]);
80  EXPECT_EQ(4, jacobians[0][1]);
81  EXPECT_EQ(1, jacobians[1][0]);
82  EXPECT_EQ(2, jacobians[1][1]);
83
84  delete[] jacobians[0];
85  delete[] jacobians[1];
86  delete[] parameters[0];
87  delete[] parameters[1];
88  delete[] jacobians;
89  delete[] parameters;
90  delete cost_function;
91}
92
93struct TenParameterCost {
94  template <typename T>
95  bool operator()(const T* const x0,
96                  const T* const x1,
97                  const T* const x2,
98                  const T* const x3,
99                  const T* const x4,
100                  const T* const x5,
101                  const T* const x6,
102                  const T* const x7,
103                  const T* const x8,
104                  const T* const x9,
105                  T* cost) const {
106    cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
107    return true;
108  }
109};
110
111TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) {
112  CostFunction* cost_function  =
113      new AutoDiffCostFunction<
114          TenParameterCost, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>(
115              new TenParameterCost);
116
117  double** parameters = new double*[10];
118  double** jacobians = new double*[10];
119  for (int i = 0; i < 10; ++i) {
120    parameters[i] = new double[1];
121    parameters[i][0] = i;
122    jacobians[i] = new double[1];
123  }
124
125  double residuals = 0.0;
126
127  cost_function->Evaluate(parameters, &residuals, NULL);
128  EXPECT_EQ(45.0, residuals);
129
130  cost_function->Evaluate(parameters, &residuals, jacobians);
131  EXPECT_EQ(residuals, 45.0);
132  for (int i = 0; i < 10; ++i) {
133    EXPECT_EQ(1.0, jacobians[i][0]);
134  }
135
136  for (int i = 0; i < 10; ++i) {
137    delete[] jacobians[i];
138    delete[] parameters[i];
139  }
140  delete[] jacobians;
141  delete[] parameters;
142  delete cost_function;
143}
144
145}  // namespace internal
146}  // namespace ceres
147