blob: 20071f2c693dfeca741337201f7be3c7a7138ddc [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 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
32#ifndef CERES_INTERNAL_POLYNOMIAL_SOLVER_H_
33#define CERES_INTERNAL_POLYNOMIAL_SOLVER_H_
34
35#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/internal/eigen.h"
38#include "ceres/internal/port.h"
39
40namespace ceres {
41namespace internal {
42
43struct FunctionSample;
44
45// All polynomials are assumed to be the form
46//
47// sum_{i=0}^N polynomial(i) x^{N-i}.
48//
49// and are given by a vector of coefficients of size N + 1.
50
51// Evaluate the polynomial at x using the Horner scheme.
52inline double EvaluatePolynomial(const Vector& polynomial, double x) {
53 double v = 0.0;
54 for (int i = 0; i < polynomial.size(); ++i) {
55 v = v * x + polynomial(i);
56 }
57 return v;
58}
59
60// Use the companion matrix eigenvalues to determine the roots of the
61// polynomial.
62//
63// This function returns true on success, false otherwise.
64// Failure indicates that the polynomial is invalid (of size 0) or
65// that the eigenvalues of the companion matrix could not be computed.
66// On failure, a more detailed message will be written to LOG(ERROR).
67// If real is not NULL, the real parts of the roots will be returned in it.
68// Likewise, if imaginary is not NULL, imaginary parts will be returned in it.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080069CERES_EXPORT_INTERNAL bool FindPolynomialRoots(const Vector& polynomial,
70 Vector* real,
71 Vector* imaginary);
Austin Schuh70cc9552019-01-21 19:46:48 -080072
73// Return the derivative of the given polynomial. It is assumed that
74// the input polynomial is at least of degree zero.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080075CERES_EXPORT_INTERNAL Vector DifferentiatePolynomial(const Vector& polynomial);
Austin Schuh70cc9552019-01-21 19:46:48 -080076
77// Find the minimum value of the polynomial in the interval [x_min,
78// x_max]. The minimum is obtained by computing all the roots of the
79// derivative of the input polynomial. All real roots within the
80// interval [x_min, x_max] are considered as well as the end points
81// x_min and x_max. Since polynomials are differentiable functions,
82// this ensures that the true minimum is found.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080083CERES_EXPORT_INTERNAL void MinimizePolynomial(const Vector& polynomial,
84 double x_min,
85 double x_max,
86 double* optimal_x,
87 double* optimal_value);
Austin Schuh70cc9552019-01-21 19:46:48 -080088
89// Given a set of function value and/or gradient samples, find a
90// polynomial whose value and gradients are exactly equal to the ones
91// in samples.
92//
93// Generally speaking,
94//
95// degree = # values + # gradients - 1
96//
97// Of course its possible to sample a polynomial any number of times,
98// in which case, generally speaking the spurious higher order
99// coefficients will be zero.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100CERES_EXPORT_INTERNAL Vector
101FindInterpolatingPolynomial(const std::vector<FunctionSample>& samples);
Austin Schuh70cc9552019-01-21 19:46:48 -0800102
103// Interpolate the function described by samples with a polynomial,
104// and minimize it on the interval [x_min, x_max]. Depending on the
105// input samples, it is possible that the interpolation or the root
106// finding algorithms may fail due to numerical difficulties. But the
107// function is guaranteed to return its best guess of an answer, by
108// considering the samples and the end points as possible solutions.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800109CERES_EXPORT_INTERNAL void MinimizeInterpolatingPolynomial(
110 const std::vector<FunctionSample>& samples,
111 double x_min,
112 double x_max,
113 double* optimal_x,
114 double* optimal_value);
Austin Schuh70cc9552019-01-21 19:46:48 -0800115
116} // namespace internal
117} // namespace ceres
118
119#endif // CERES_INTERNAL_POLYNOMIAL_SOLVER_H_