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