1 // Ceres Solver - A fast non-linear least squares minimizer 2 // Copyright 2013 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 (at) google.com (Sameer Agarwal) 30 31 #include "ceres/numeric_diff_functor.h" 32 33 #include <algorithm> 34 #include <cmath> 35 #include <string> 36 #include <vector> 37 #include "ceres/autodiff_cost_function.h" 38 #include "ceres/internal/scoped_ptr.h" 39 #include "ceres/numeric_diff_test_utils.h" 40 #include "ceres/test_util.h" 41 #include "ceres/types.h" 42 #include "glog/logging.h" 43 #include "gtest/gtest.h" 44 45 namespace ceres { 46 namespace internal { 47 48 TEST(NumericDiffCostFunction, EasyCaseCentralDifferences) { 49 typedef NumericDiffFunctor<EasyFunctor, CENTRAL, 3, 5, 5> 50 NumericDiffEasyFunctor; 51 52 internal::scoped_ptr<CostFunction> cost_function; 53 EasyFunctor functor; 54 55 cost_function.reset( 56 new AutoDiffCostFunction<NumericDiffEasyFunctor, 3, 5, 5>( 57 new NumericDiffEasyFunctor)); 58 59 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); 60 61 cost_function.reset( 62 new AutoDiffCostFunction<NumericDiffEasyFunctor, 3, 5, 5>( 63 new NumericDiffEasyFunctor(new EasyFunctor))); 64 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); 65 } 66 67 TEST(NumericDiffCostFunction, EasyCaseForwardDifferences) { 68 typedef NumericDiffFunctor<EasyFunctor, FORWARD, 3, 5, 5> 69 NumericDiffEasyFunctor; 70 71 internal::scoped_ptr<CostFunction> cost_function; 72 EasyFunctor functor; 73 74 cost_function.reset( 75 new AutoDiffCostFunction<NumericDiffEasyFunctor, 3, 5, 5>( 76 new NumericDiffEasyFunctor)); 77 78 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); 79 80 cost_function.reset( 81 new AutoDiffCostFunction<NumericDiffEasyFunctor, 3, 5, 5>( 82 new NumericDiffEasyFunctor(new EasyFunctor))); 83 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); 84 } 85 86 TEST(NumericDiffCostFunction, TranscendentalCaseCentralDifferences) { 87 typedef NumericDiffFunctor<TranscendentalFunctor, CENTRAL, 2, 5, 5> 88 NumericDiffTranscendentalFunctor; 89 90 internal::scoped_ptr<CostFunction> cost_function; 91 TranscendentalFunctor functor; 92 93 cost_function.reset( 94 new AutoDiffCostFunction<NumericDiffTranscendentalFunctor, 2, 5, 5>( 95 new NumericDiffTranscendentalFunctor)); 96 97 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); 98 99 cost_function.reset( 100 new AutoDiffCostFunction<NumericDiffTranscendentalFunctor, 2, 5, 5>( 101 new NumericDiffTranscendentalFunctor(new TranscendentalFunctor))); 102 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); 103 } 104 105 TEST(NumericDiffCostFunction, TranscendentalCaseForwardDifferences) { 106 typedef NumericDiffFunctor<TranscendentalFunctor, FORWARD, 2, 5, 5> 107 NumericDiffTranscendentalFunctor; 108 109 internal::scoped_ptr<CostFunction> cost_function; 110 TranscendentalFunctor functor; 111 112 cost_function.reset( 113 new AutoDiffCostFunction<NumericDiffTranscendentalFunctor, 2, 5, 5>( 114 new NumericDiffTranscendentalFunctor)); 115 116 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); 117 118 cost_function.reset( 119 new AutoDiffCostFunction<NumericDiffTranscendentalFunctor, 2, 5, 5>( 120 new NumericDiffTranscendentalFunctor(new TranscendentalFunctor))); 121 functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); 122 } 123 124 } // namespace internal 125 } // namespace ceres 126