Blame internal/ceres/polynomial.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: moll.markus@arcor.de (Markus Moll)
Packit ea1746
//         sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_POLYNOMIAL_SOLVER_H_
Packit ea1746
#define CERES_INTERNAL_POLYNOMIAL_SOLVER_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/internal/eigen.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
struct FunctionSample;
Packit ea1746
Packit ea1746
// All polynomials are assumed to be the form
Packit ea1746
//
Packit ea1746
//   sum_{i=0}^N polynomial(i) x^{N-i}.
Packit ea1746
//
Packit ea1746
// and are given by a vector of coefficients of size N + 1.
Packit ea1746
Packit ea1746
// Evaluate the polynomial at x using the Horner scheme.
Packit ea1746
inline double EvaluatePolynomial(const Vector& polynomial, double x) {
Packit ea1746
  double v = 0.0;
Packit ea1746
  for (int i = 0; i < polynomial.size(); ++i) {
Packit ea1746
    v = v * x + polynomial(i);
Packit ea1746
  }
Packit ea1746
  return v;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Use the companion matrix eigenvalues to determine the roots of the
Packit ea1746
// polynomial.
Packit ea1746
//
Packit ea1746
// This function returns true on success, false otherwise.
Packit ea1746
// Failure indicates that the polynomial is invalid (of size 0) or
Packit ea1746
// that the eigenvalues of the companion matrix could not be computed.
Packit ea1746
// On failure, a more detailed message will be written to LOG(ERROR).
Packit ea1746
// If real is not NULL, the real parts of the roots will be returned in it.
Packit ea1746
// Likewise, if imaginary is not NULL, imaginary parts will be returned in it.
Packit ea1746
bool FindPolynomialRoots(const Vector& polynomial,
Packit ea1746
                         Vector* real,
Packit ea1746
                         Vector* imaginary);
Packit ea1746
Packit ea1746
// Return the derivative of the given polynomial. It is assumed that
Packit ea1746
// the input polynomial is at least of degree zero.
Packit ea1746
Vector DifferentiatePolynomial(const Vector& polynomial);
Packit ea1746
Packit ea1746
// Find the minimum value of the polynomial in the interval [x_min,
Packit ea1746
// x_max]. The minimum is obtained by computing all the roots of the
Packit ea1746
// derivative of the input polynomial. All real roots within the
Packit ea1746
// interval [x_min, x_max] are considered as well as the end points
Packit ea1746
// x_min and x_max. Since polynomials are differentiable functions,
Packit ea1746
// this ensures that the true minimum is found.
Packit ea1746
void MinimizePolynomial(const Vector& polynomial,
Packit ea1746
                        double x_min,
Packit ea1746
                        double x_max,
Packit ea1746
                        double* optimal_x,
Packit ea1746
                        double* optimal_value);
Packit ea1746
Packit ea1746
// Given a set of function value and/or gradient samples, find a
Packit ea1746
// polynomial whose value and gradients are exactly equal to the ones
Packit ea1746
// in samples.
Packit ea1746
//
Packit ea1746
// Generally speaking,
Packit ea1746
//
Packit ea1746
// degree = # values + # gradients - 1
Packit ea1746
//
Packit ea1746
// Of course its possible to sample a polynomial any number of times,
Packit ea1746
// in which case, generally speaking the spurious higher order
Packit ea1746
// coefficients will be zero.
Packit ea1746
Vector FindInterpolatingPolynomial(const std::vector<FunctionSample>& samples);
Packit ea1746
Packit ea1746
// Interpolate the function described by samples with a polynomial,
Packit ea1746
// and minimize it on the interval [x_min, x_max]. Depending on the
Packit ea1746
// input samples, it is possible that the interpolation or the root
Packit ea1746
// finding algorithms may fail due to numerical difficulties. But the
Packit ea1746
// function is guaranteed to return its best guess of an answer, by
Packit ea1746
// considering the samples and the end points as possible solutions.
Packit ea1746
void MinimizeInterpolatingPolynomial(const std::vector<FunctionSample>& samples,
Packit ea1746
                                     double x_min,
Packit ea1746
                                     double x_max,
Packit ea1746
                                     double* optimal_x,
Packit ea1746
                                     double* optimal_value);
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_POLYNOMIAL_SOLVER_H_