Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 | #ifndef CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ |
| 32 | #define CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ |
| 33 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 34 | #include <random> |
| 35 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "ceres/cost_function.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 37 | #include "ceres/internal/export.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/sized_cost_function.h" |
| 39 | #include "ceres/types.h" |
| 40 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 41 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | |
| 43 | // Noise factor for randomized cost function. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 44 | static constexpr double kNoiseFactor = 0.01; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 45 | |
| 46 | // Default random seed for randomized cost function. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 47 | static constexpr unsigned int kRandomSeed = 1234; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 48 | |
| 49 | // y1 = x1'x2 -> dy1/dx1 = x2, dy1/dx2 = x1 |
| 50 | // y2 = (x1'x2)^2 -> dy2/dx1 = 2 * x2 * (x1'x2), dy2/dx2 = 2 * x1 * (x1'x2) |
| 51 | // y3 = x2'x2 -> dy3/dx1 = 0, dy3/dx2 = 2 * x2 |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 52 | class CERES_NO_EXPORT EasyFunctor { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 53 | public: |
| 54 | bool operator()(const double* x1, const double* x2, double* residuals) const; |
| 55 | void ExpectCostFunctionEvaluationIsNearlyCorrect( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 56 | const CostFunction& cost_function, NumericDiffMethodType method) const; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | class EasyCostFunction : public SizedCostFunction<3, 5, 5> { |
| 60 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 61 | bool Evaluate(double const* const* parameters, |
| 62 | double* residuals, |
| 63 | double** /* not used */) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 64 | return functor_(parameters[0], parameters[1], residuals); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | EasyFunctor functor_; |
| 69 | }; |
| 70 | |
| 71 | // y1 = sin(x1'x2) |
| 72 | // y2 = exp(-x1'x2 / 10) |
| 73 | // |
| 74 | // dy1/dx1 = x2 * cos(x1'x2), dy1/dx2 = x1 * cos(x1'x2) |
| 75 | // dy2/dx1 = -x2 * exp(-x1'x2 / 10) / 10, dy2/dx2 = -x2 * exp(-x1'x2 / 10) / 10 |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 76 | class CERES_NO_EXPORT TranscendentalFunctor { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 77 | public: |
| 78 | bool operator()(const double* x1, const double* x2, double* residuals) const; |
| 79 | void ExpectCostFunctionEvaluationIsNearlyCorrect( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 80 | const CostFunction& cost_function, NumericDiffMethodType method) const; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 83 | class CERES_NO_EXPORT TranscendentalCostFunction |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 84 | : public SizedCostFunction<2, 5, 5> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 85 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 86 | bool Evaluate(double const* const* parameters, |
| 87 | double* residuals, |
| 88 | double** /* not used */) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 89 | return functor_(parameters[0], parameters[1], residuals); |
| 90 | } |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 91 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 92 | private: |
| 93 | TranscendentalFunctor functor_; |
| 94 | }; |
| 95 | |
| 96 | // y = exp(x), dy/dx = exp(x) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 97 | class CERES_NO_EXPORT ExponentialFunctor { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 98 | public: |
| 99 | bool operator()(const double* x1, double* residuals) const; |
| 100 | void ExpectCostFunctionEvaluationIsNearlyCorrect( |
| 101 | const CostFunction& cost_function) const; |
| 102 | }; |
| 103 | |
| 104 | class ExponentialCostFunction : public SizedCostFunction<1, 1> { |
| 105 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 106 | bool Evaluate(double const* const* parameters, |
| 107 | double* residuals, |
| 108 | double** /* not used */) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 109 | return functor_(parameters[0], residuals); |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | ExponentialFunctor functor_; |
| 114 | }; |
| 115 | |
| 116 | // Test adaptive numeric differentiation by synthetically adding random noise |
| 117 | // to a functor. |
| 118 | // y = x^2 + [random noise], dy/dx ~ 2x |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 119 | class CERES_NO_EXPORT RandomizedFunctor { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 120 | public: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 121 | RandomizedFunctor(double noise_factor, std::mt19937& prng) |
| 122 | : noise_factor_(noise_factor), |
| 123 | prng_(&prng), |
| 124 | uniform_distribution_{-noise_factor, noise_factor} {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 125 | |
| 126 | bool operator()(const double* x1, double* residuals) const; |
| 127 | void ExpectCostFunctionEvaluationIsNearlyCorrect( |
| 128 | const CostFunction& cost_function) const; |
| 129 | |
| 130 | private: |
| 131 | double noise_factor_; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 132 | // Store the generator as a pointer to be able to modify the instance the |
| 133 | // pointer is pointing to. |
| 134 | std::mt19937* prng_; |
| 135 | mutable std::uniform_real_distribution<> uniform_distribution_; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 138 | class CERES_NO_EXPORT RandomizedCostFunction : public SizedCostFunction<1, 1> { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 139 | public: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 140 | RandomizedCostFunction(double noise_factor, std::mt19937& prng) |
| 141 | : functor_(noise_factor, prng) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 142 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 143 | bool Evaluate(double const* const* parameters, |
| 144 | double* residuals, |
| 145 | double** /* not used */) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 146 | return functor_(parameters[0], residuals); |
| 147 | } |
| 148 | |
| 149 | private: |
| 150 | RandomizedFunctor functor_; |
| 151 | }; |
| 152 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 153 | } // namespace ceres::internal |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 154 | |
| 155 | #endif // CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_ |