polynomial.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  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: moll.markus@arcor.de (Markus Moll)
  30. // sameeragarwal@google.com (Sameer Agarwal)
  31. #ifndef CERES_INTERNAL_POLYNOMIAL_SOLVER_H_
  32. #define CERES_INTERNAL_POLYNOMIAL_SOLVER_H_
  33. #include <vector>
  34. #include "ceres/internal/disable_warnings.h"
  35. #include "ceres/internal/eigen.h"
  36. #include "ceres/internal/export.h"
  37. namespace ceres::internal {
  38. struct FunctionSample;
  39. // All polynomials are assumed to be the form
  40. //
  41. // sum_{i=0}^N polynomial(i) x^{N-i}.
  42. //
  43. // and are given by a vector of coefficients of size N + 1.
  44. // Evaluate the polynomial at x using the Horner scheme.
  45. CERES_NO_EXPORT
  46. inline double EvaluatePolynomial(const Vector& polynomial, double x) {
  47. double v = 0.0;
  48. for (int i = 0; i < polynomial.size(); ++i) {
  49. v = v * x + polynomial(i);
  50. }
  51. return v;
  52. }
  53. // Use the companion matrix eigenvalues to determine the roots of the
  54. // polynomial.
  55. //
  56. // This function returns true on success, false otherwise.
  57. // Failure indicates that the polynomial is invalid (of size 0) or
  58. // that the eigenvalues of the companion matrix could not be computed.
  59. // On failure, a more detailed message will be written to LOG(ERROR).
  60. // If real is not nullptr, the real parts of the roots will be returned in it.
  61. // Likewise, if imaginary is not nullptr, imaginary parts will be returned in
  62. // it.
  63. CERES_NO_EXPORT bool FindPolynomialRoots(const Vector& polynomial,
  64. Vector* real,
  65. Vector* imaginary);
  66. // Return the derivative of the given polynomial. It is assumed that
  67. // the input polynomial is at least of degree zero.
  68. CERES_NO_EXPORT Vector DifferentiatePolynomial(const Vector& polynomial);
  69. // Find the minimum value of the polynomial in the interval [x_min,
  70. // x_max]. The minimum is obtained by computing all the roots of the
  71. // derivative of the input polynomial. All real roots within the
  72. // interval [x_min, x_max] are considered as well as the end points
  73. // x_min and x_max. Since polynomials are differentiable functions,
  74. // this ensures that the true minimum is found.
  75. CERES_NO_EXPORT void MinimizePolynomial(const Vector& polynomial,
  76. double x_min,
  77. double x_max,
  78. double* optimal_x,
  79. double* optimal_value);
  80. // Given a set of function value and/or gradient samples, find a
  81. // polynomial whose value and gradients are exactly equal to the ones
  82. // in samples.
  83. //
  84. // Generally speaking,
  85. //
  86. // degree = # values + # gradients - 1
  87. //
  88. // Of course its possible to sample a polynomial any number of times,
  89. // in which case, generally speaking the spurious higher order
  90. // coefficients will be zero.
  91. CERES_NO_EXPORT Vector
  92. FindInterpolatingPolynomial(const std::vector<FunctionSample>& samples);
  93. // Interpolate the function described by samples with a polynomial,
  94. // and minimize it on the interval [x_min, x_max]. Depending on the
  95. // input samples, it is possible that the interpolation or the root
  96. // finding algorithms may fail due to numerical difficulties. But the
  97. // function is guaranteed to return its best guess of an answer, by
  98. // considering the samples and the end points as possible solutions.
  99. CERES_NO_EXPORT void MinimizeInterpolatingPolynomial(
  100. const std::vector<FunctionSample>& samples,
  101. double x_min,
  102. double x_max,
  103. double* optimal_x,
  104. double* optimal_value);
  105. } // namespace ceres::internal
  106. #include "ceres/internal/reenable_warnings.h"
  107. #endif // CERES_INTERNAL_POLYNOMIAL_SOLVER_H_