13f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// Ceres Solver - A fast non-linear least squares minimizer 23f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// Copyright 2013 Google Inc. All rights reserved. 33f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// http://code.google.com/p/ceres-solver/ 43f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// 53f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// Redistribution and use in source and binary forms, with or without 63f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// modification, are permitted provided that the following conditions are met: 73f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// 83f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// * Redistributions of source code must retain the above copyright notice, 93f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// this list of conditions and the following disclaimer. 103f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// * Redistributions in binary form must reproduce the above copyright notice, 113f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// this list of conditions and the following disclaimer in the documentation 123f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// and/or other materials provided with the distribution. 133f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// * Neither the name of Google Inc. nor the names of its contributors may be 143f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// used to endorse or promote products derived from this software without 153f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// specific prior written permission. 163f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// 173f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 183f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 193f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 203f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 213f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 223f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 233f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 243f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 253f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 263f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 273f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// POSSIBILITY OF SUCH DAMAGE. 283f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// 293f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// Author: sameeragarwal@google.com (Sameer Agarwal) 303f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 313f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com#ifndef CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ 323f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com#define CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ 333f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 343f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com#include "ceres/cost_function.h" 353f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com#include "ceres/sized_cost_function.h" 363f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com#include "ceres/types.h" 373f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 383f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.comnamespace ceres { 393f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.comnamespace internal { 403f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 413f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// y1 = x1'x2 -> dy1/dx1 = x2, dy1/dx2 = x1 423f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// y2 = (x1'x2)^2 -> dy2/dx1 = 2 * x2 * (x1'x2), dy2/dx2 = 2 * x1 * (x1'x2) 433f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// y3 = x2'x2 -> dy3/dx1 = 0, dy3/dx2 = 2 * x2 443f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.comclass EasyFunctor { 453f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com public: 463f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com bool operator()(const double* x1, const double* x2, double* residuals) const; 473f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com void ExpectCostFunctionEvaluationIsNearlyCorrect( 483f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com const CostFunction& cost_function, 493f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com NumericDiffMethod method) const; 503f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com}; 513f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 523f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.comclass EasyCostFunction : public SizedCostFunction<3, 5, 5> { 533f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com public: 543f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com virtual bool Evaluate(double const* const* parameters, 553f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com double* residuals, 563f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com double** /* not used */) const { 573f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com return functor_(parameters[0], parameters[1], residuals); 583f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com } 593f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 603f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com private: 613f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com EasyFunctor functor_; 623f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com}; 633f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 643f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// y1 = sin(x1'x2) 653f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// y2 = exp(-x1'x2 / 10) 663f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// 673f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// dy1/dx1 = x2 * cos(x1'x2), dy1/dx2 = x1 * cos(x1'x2) 683f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com// dy2/dx1 = -x2 * exp(-x1'x2 / 10) / 10, dy2/dx2 = -x2 * exp(-x1'x2 / 10) / 10 693f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.comclass TranscendentalFunctor { 703f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com public: 713f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com bool operator()(const double* x1, const double* x2, double* residuals) const; 723f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com void ExpectCostFunctionEvaluationIsNearlyCorrect( 733f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com const CostFunction& cost_function, 743f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com NumericDiffMethod method) const; 753f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com}; 763f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 773f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.comclass TranscendentalCostFunction : public SizedCostFunction<2, 5, 5> { 783f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com public: 793f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com virtual bool Evaluate(double const* const* parameters, 803f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com double* residuals, 813f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com double** /* not used */) const { 823f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com return functor_(parameters[0], parameters[1], residuals); 833f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com } 843f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com private: 853f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com TranscendentalFunctor functor_; 863f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com}; 873f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 883f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com} // namespace internal 893f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com} // namespace ceres 903f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com 913f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com#endif // CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ 923f30e3a41fc16816105e28e0d4c241eff6d391b4bsalomon@google.com