2013-02-20 01:39:03 -08:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2022-08-08 21:06:22 +02:00
|
|
|
// Copyright 2022 Google Inc. All rights reserved.
|
2015-03-17 22:30:16 -07:00
|
|
|
// http://ceres-solver.org/
|
2013-02-20 01:39:03 -08:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
|
|
|
// used to endorse or promote products derived from this software without
|
|
|
|
|
// specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
|
|
|
|
|
|
|
|
#ifndef CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
|
|
|
|
|
#define CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
|
|
|
|
|
|
2022-08-08 21:06:22 +02:00
|
|
|
#include <random>
|
|
|
|
|
|
2013-01-18 13:11:32 -08:00
|
|
|
#include "ceres/cost_function.h"
|
2022-02-07 23:43:19 +01:00
|
|
|
#include "ceres/internal/export.h"
|
2013-01-18 13:11:32 -08:00
|
|
|
#include "ceres/sized_cost_function.h"
|
|
|
|
|
#include "ceres/types.h"
|
|
|
|
|
|
2022-04-21 17:41:10 -07:00
|
|
|
namespace ceres::internal {
|
2013-01-18 13:11:32 -08:00
|
|
|
|
2015-05-13 15:43:51 +03:00
|
|
|
// Noise factor for randomized cost function.
|
2020-04-21 10:10:01 -07:00
|
|
|
static constexpr double kNoiseFactor = 0.01;
|
2015-05-13 15:43:51 +03:00
|
|
|
|
|
|
|
|
// Default random seed for randomized cost function.
|
2020-04-21 10:10:01 -07:00
|
|
|
static constexpr unsigned int kRandomSeed = 1234;
|
2015-05-13 15:43:51 +03:00
|
|
|
|
2013-01-18 13:11:32 -08:00
|
|
|
// y1 = x1'x2 -> dy1/dx1 = x2, dy1/dx2 = x1
|
|
|
|
|
// y2 = (x1'x2)^2 -> dy2/dx1 = 2 * x2 * (x1'x2), dy2/dx2 = 2 * x1 * (x1'x2)
|
|
|
|
|
// y3 = x2'x2 -> dy3/dx1 = 0, dy3/dx2 = 2 * x2
|
2022-02-07 23:43:19 +01:00
|
|
|
class CERES_NO_EXPORT EasyFunctor {
|
2013-01-18 13:11:32 -08:00
|
|
|
public:
|
|
|
|
|
bool operator()(const double* x1, const double* x2, double* residuals) const;
|
|
|
|
|
void ExpectCostFunctionEvaluationIsNearlyCorrect(
|
2020-09-20 21:45:24 +02:00
|
|
|
const CostFunction& cost_function, NumericDiffMethodType method) const;
|
2013-01-18 13:11:32 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class EasyCostFunction : public SizedCostFunction<3, 5, 5> {
|
|
|
|
|
public:
|
2019-07-14 00:16:13 +02:00
|
|
|
bool Evaluate(double const* const* parameters,
|
|
|
|
|
double* residuals,
|
|
|
|
|
double** /* not used */) const final {
|
2013-01-18 13:11:32 -08:00
|
|
|
return functor_(parameters[0], parameters[1], residuals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
EasyFunctor functor_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// y1 = sin(x1'x2)
|
|
|
|
|
// y2 = exp(-x1'x2 / 10)
|
|
|
|
|
//
|
|
|
|
|
// dy1/dx1 = x2 * cos(x1'x2), dy1/dx2 = x1 * cos(x1'x2)
|
|
|
|
|
// dy2/dx1 = -x2 * exp(-x1'x2 / 10) / 10, dy2/dx2 = -x2 * exp(-x1'x2 / 10) / 10
|
2022-02-07 23:43:19 +01:00
|
|
|
class CERES_NO_EXPORT TranscendentalFunctor {
|
2013-01-18 13:11:32 -08:00
|
|
|
public:
|
|
|
|
|
bool operator()(const double* x1, const double* x2, double* residuals) const;
|
|
|
|
|
void ExpectCostFunctionEvaluationIsNearlyCorrect(
|
2020-09-20 21:45:24 +02:00
|
|
|
const CostFunction& cost_function, NumericDiffMethodType method) const;
|
2013-01-18 13:11:32 -08:00
|
|
|
};
|
|
|
|
|
|
2022-03-03 18:44:07 +01:00
|
|
|
class CERES_NO_EXPORT TranscendentalCostFunction
|
2020-01-28 12:09:30 -05:00
|
|
|
: public SizedCostFunction<2, 5, 5> {
|
2013-01-18 13:11:32 -08:00
|
|
|
public:
|
2019-07-14 00:16:13 +02:00
|
|
|
bool Evaluate(double const* const* parameters,
|
|
|
|
|
double* residuals,
|
|
|
|
|
double** /* not used */) const final {
|
2013-01-18 13:11:32 -08:00
|
|
|
return functor_(parameters[0], parameters[1], residuals);
|
|
|
|
|
}
|
2020-09-20 21:45:24 +02:00
|
|
|
|
2013-01-18 13:11:32 -08:00
|
|
|
private:
|
|
|
|
|
TranscendentalFunctor functor_;
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-13 15:43:51 +03:00
|
|
|
// y = exp(x), dy/dx = exp(x)
|
2022-02-07 23:43:19 +01:00
|
|
|
class CERES_NO_EXPORT ExponentialFunctor {
|
2015-05-13 15:43:51 +03:00
|
|
|
public:
|
|
|
|
|
bool operator()(const double* x1, double* residuals) const;
|
|
|
|
|
void ExpectCostFunctionEvaluationIsNearlyCorrect(
|
|
|
|
|
const CostFunction& cost_function) const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ExponentialCostFunction : public SizedCostFunction<1, 1> {
|
|
|
|
|
public:
|
2019-07-14 00:16:13 +02:00
|
|
|
bool Evaluate(double const* const* parameters,
|
|
|
|
|
double* residuals,
|
|
|
|
|
double** /* not used */) const final {
|
2015-05-13 15:43:51 +03:00
|
|
|
return functor_(parameters[0], residuals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ExponentialFunctor functor_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Test adaptive numeric differentiation by synthetically adding random noise
|
|
|
|
|
// to a functor.
|
|
|
|
|
// y = x^2 + [random noise], dy/dx ~ 2x
|
2022-02-07 23:43:19 +01:00
|
|
|
class CERES_NO_EXPORT RandomizedFunctor {
|
2015-05-13 15:43:51 +03:00
|
|
|
public:
|
2022-08-08 21:06:22 +02:00
|
|
|
RandomizedFunctor(double noise_factor, std::mt19937& prng)
|
|
|
|
|
: noise_factor_(noise_factor),
|
|
|
|
|
prng_(&prng),
|
|
|
|
|
uniform_distribution_{-noise_factor, noise_factor} {}
|
2015-05-13 15:43:51 +03:00
|
|
|
|
|
|
|
|
bool operator()(const double* x1, double* residuals) const;
|
|
|
|
|
void ExpectCostFunctionEvaluationIsNearlyCorrect(
|
|
|
|
|
const CostFunction& cost_function) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
double noise_factor_;
|
2022-08-08 21:06:22 +02:00
|
|
|
// Store the generator as a pointer to be able to modify the instance the
|
|
|
|
|
// pointer is pointing to.
|
|
|
|
|
std::mt19937* prng_;
|
|
|
|
|
mutable std::uniform_real_distribution<> uniform_distribution_;
|
2015-05-13 15:43:51 +03:00
|
|
|
};
|
|
|
|
|
|
2022-03-03 18:44:07 +01:00
|
|
|
class CERES_NO_EXPORT RandomizedCostFunction : public SizedCostFunction<1, 1> {
|
2015-05-13 15:43:51 +03:00
|
|
|
public:
|
2022-08-08 21:06:22 +02:00
|
|
|
RandomizedCostFunction(double noise_factor, std::mt19937& prng)
|
|
|
|
|
: functor_(noise_factor, prng) {}
|
2015-05-13 15:43:51 +03:00
|
|
|
|
2019-07-14 00:16:13 +02:00
|
|
|
bool Evaluate(double const* const* parameters,
|
|
|
|
|
double* residuals,
|
|
|
|
|
double** /* not used */) const final {
|
2015-05-13 15:43:51 +03:00
|
|
|
return functor_(parameters[0], residuals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
RandomizedFunctor functor_;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-21 17:41:10 -07:00
|
|
|
} // namespace ceres::internal
|
2013-02-20 01:39:03 -08:00
|
|
|
|
|
|
|
|
#endif // CERES_INTERNAL_NUMERIC_DIFF_TEST_UTILS_H_
|