11d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// Ceres Solver - A fast non-linear least squares minimizer
21d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// Copyright 2013 Google Inc. All rights reserved.
31d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// http://code.google.com/p/ceres-solver/
41d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//
51d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// Redistribution and use in source and binary forms, with or without
61d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// modification, are permitted provided that the following conditions are met:
71d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//
81d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// * Redistributions of source code must retain the above copyright notice,
91d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//   this list of conditions and the following disclaimer.
101d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// * Redistributions in binary form must reproduce the above copyright notice,
111d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//   this list of conditions and the following disclaimer in the documentation
121d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//   and/or other materials provided with the distribution.
131d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// * Neither the name of Google Inc. nor the names of its contributors may be
141d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//   used to endorse or promote products derived from this software without
151d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//   specific prior written permission.
161d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//
171d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
181d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
211d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
221d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
231d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
241d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
251d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
261d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
271d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// POSSIBILITY OF SUCH DAMAGE.
281d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//
291d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// Author: sameeragarwal@google.com (Sameer Agarwal)
301d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
311d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#ifndef CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
321d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#define CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
331d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
341d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "ceres/cost_function.h"
351d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "ceres/sized_cost_function.h"
361d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "ceres/types.h"
371d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
381d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingnamespace ceres {
391d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingnamespace internal {
401d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
411d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// y1 = x1'x2      -> dy1/dx1 = x2,               dy1/dx2 = x1
421d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// y2 = (x1'x2)^2  -> dy2/dx1 = 2 * x2 * (x1'x2), dy2/dx2 = 2 * x1 * (x1'x2)
431d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// y3 = x2'x2      -> dy3/dx1 = 0,                dy3/dx2 = 2 * x2
441d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingclass EasyFunctor {
451d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling public:
461d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  bool operator()(const double* x1, const double* x2, double* residuals) const;
471d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  void ExpectCostFunctionEvaluationIsNearlyCorrect(
481d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      const CostFunction& cost_function,
491d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      NumericDiffMethod method) const;
501d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling};
511d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
521d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingclass EasyCostFunction : public SizedCostFunction<3, 5, 5> {
531d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling public:
541d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  virtual bool Evaluate(double const* const* parameters,
551d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                        double* residuals,
561d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                        double** /* not used */) const {
571d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    return functor_(parameters[0], parameters[1], residuals);
581d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  }
591d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
601d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling private:
611d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  EasyFunctor functor_;
621d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling};
631d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
641d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// y1 = sin(x1'x2)
651d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// y2 = exp(-x1'x2 / 10)
661d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling//
671d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// dy1/dx1 =  x2 * cos(x1'x2),            dy1/dx2 =  x1 * cos(x1'x2)
681d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// dy2/dx1 = -x2 * exp(-x1'x2 / 10) / 10, dy2/dx2 = -x2 * exp(-x1'x2 / 10) / 10
691d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingclass TranscendentalFunctor {
701d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling public:
711d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  bool operator()(const double* x1, const double* x2, double* residuals) const;
721d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  void ExpectCostFunctionEvaluationIsNearlyCorrect(
731d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      const CostFunction& cost_function,
741d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      NumericDiffMethod method) const;
751d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling};
761d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
771d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingclass TranscendentalCostFunction : public SizedCostFunction<2, 5, 5> {
781d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling public:
791d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  virtual bool Evaluate(double const* const* parameters,
801d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                        double* residuals,
811d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                        double** /* not used */) const {
821d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    return functor_(parameters[0], parameters[1], residuals);
831d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  }
841d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling private:
851d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  TranscendentalFunctor functor_;
861d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling};
871d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
881d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling}  // namespace internal
891d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling}  // namespace ceres
901d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
911d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#endif  // CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
92