123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- #include "ceres/polynomial.h"
- #include <cmath>
- #include <cstddef>
- #include <vector>
- #include "Eigen/Dense"
- #include "ceres/function_sample.h"
- #include "ceres/internal/export.h"
- #include "glog/logging.h"
- namespace ceres::internal {
- namespace {
- void BalanceCompanionMatrix(Matrix* companion_matrix_ptr) {
- CHECK(companion_matrix_ptr != nullptr);
- Matrix& companion_matrix = *companion_matrix_ptr;
- Matrix companion_matrix_offdiagonal = companion_matrix;
- companion_matrix_offdiagonal.diagonal().setZero();
- const int degree = companion_matrix.rows();
-
-
-
-
-
- const double gamma = 0.9;
-
- bool scaling_has_changed;
- do {
- scaling_has_changed = false;
- for (int i = 0; i < degree; ++i) {
- const double row_norm = companion_matrix_offdiagonal.row(i).lpNorm<1>();
- const double col_norm = companion_matrix_offdiagonal.col(i).lpNorm<1>();
-
-
-
- int exponent = 0;
- std::frexp(row_norm / col_norm, &exponent);
- exponent /= 2;
- if (exponent != 0) {
- const double scaled_col_norm = std::ldexp(col_norm, exponent);
- const double scaled_row_norm = std::ldexp(row_norm, -exponent);
- if (scaled_col_norm + scaled_row_norm < gamma * (col_norm + row_norm)) {
-
-
-
- scaling_has_changed = true;
- companion_matrix_offdiagonal.row(i) *= std::ldexp(1.0, -exponent);
- companion_matrix_offdiagonal.col(i) *= std::ldexp(1.0, exponent);
- }
- }
- }
- } while (scaling_has_changed);
- companion_matrix_offdiagonal.diagonal() = companion_matrix.diagonal();
- companion_matrix = companion_matrix_offdiagonal;
- VLOG(3) << "Balanced companion matrix is\n" << companion_matrix;
- }
- void BuildCompanionMatrix(const Vector& polynomial,
- Matrix* companion_matrix_ptr) {
- CHECK(companion_matrix_ptr != nullptr);
- Matrix& companion_matrix = *companion_matrix_ptr;
- const int degree = polynomial.size() - 1;
- companion_matrix.resize(degree, degree);
- companion_matrix.setZero();
- companion_matrix.diagonal(-1).setOnes();
- companion_matrix.col(degree - 1) = -polynomial.reverse().head(degree);
- }
- Vector RemoveLeadingZeros(const Vector& polynomial_in) {
- int i = 0;
- while (i < (polynomial_in.size() - 1) && polynomial_in(i) == 0.0) {
- ++i;
- }
- return polynomial_in.tail(polynomial_in.size() - i);
- }
- void FindLinearPolynomialRoots(const Vector& polynomial,
- Vector* real,
- Vector* imaginary) {
- CHECK_EQ(polynomial.size(), 2);
- if (real != nullptr) {
- real->resize(1);
- (*real)(0) = -polynomial(1) / polynomial(0);
- }
- if (imaginary != nullptr) {
- imaginary->setZero(1);
- }
- }
- void FindQuadraticPolynomialRoots(const Vector& polynomial,
- Vector* real,
- Vector* imaginary) {
- CHECK_EQ(polynomial.size(), 3);
- const double a = polynomial(0);
- const double b = polynomial(1);
- const double c = polynomial(2);
- const double D = b * b - 4 * a * c;
- const double sqrt_D = sqrt(fabs(D));
- if (real != nullptr) {
- real->setZero(2);
- }
- if (imaginary != nullptr) {
- imaginary->setZero(2);
- }
-
- if (D >= 0) {
- if (real != nullptr) {
-
-
- if (b >= 0) {
- (*real)(0) = (-b - sqrt_D) / (2.0 * a);
- (*real)(1) = (2.0 * c) / (-b - sqrt_D);
- } else {
- (*real)(0) = (2.0 * c) / (-b + sqrt_D);
- (*real)(1) = (-b + sqrt_D) / (2.0 * a);
- }
- }
- return;
- }
-
- if (real != nullptr) {
- (*real)(0) = -b / (2.0 * a);
- (*real)(1) = -b / (2.0 * a);
- }
- if (imaginary != nullptr) {
- (*imaginary)(0) = sqrt_D / (2.0 * a);
- (*imaginary)(1) = -sqrt_D / (2.0 * a);
- }
- }
- }
- bool FindPolynomialRoots(const Vector& polynomial_in,
- Vector* real,
- Vector* imaginary) {
- if (polynomial_in.size() == 0) {
- LOG(ERROR) << "Invalid polynomial of size 0 passed to FindPolynomialRoots";
- return false;
- }
- Vector polynomial = RemoveLeadingZeros(polynomial_in);
- const int degree = polynomial.size() - 1;
- VLOG(3) << "Input polynomial: " << polynomial_in.transpose();
- if (polynomial.size() != polynomial_in.size()) {
- VLOG(3) << "Trimmed polynomial: " << polynomial.transpose();
- }
-
- if (degree == 0) {
- LOG(WARNING) << "Trying to extract roots from a constant "
- << "polynomial in FindPolynomialRoots";
-
-
-
- return true;
- }
-
- if (degree == 1) {
- FindLinearPolynomialRoots(polynomial, real, imaginary);
- return true;
- }
-
- if (degree == 2) {
- FindQuadraticPolynomialRoots(polynomial, real, imaginary);
- return true;
- }
-
-
-
- const double leading_term = polynomial(0);
- polynomial /= leading_term;
-
- Matrix companion_matrix(degree, degree);
- BuildCompanionMatrix(polynomial, &companion_matrix);
- BalanceCompanionMatrix(&companion_matrix);
-
- Eigen::EigenSolver<Matrix> solver(companion_matrix, false);
- if (solver.info() != Eigen::Success) {
- LOG(ERROR) << "Failed to extract eigenvalues from companion matrix.";
- return false;
- }
-
- if (real != nullptr) {
- *real = solver.eigenvalues().real();
- } else {
- LOG(WARNING) << "nullptr pointer passed as real argument to "
- << "FindPolynomialRoots. Real parts of the roots will not "
- << "be returned.";
- }
- if (imaginary != nullptr) {
- *imaginary = solver.eigenvalues().imag();
- }
- return true;
- }
- Vector DifferentiatePolynomial(const Vector& polynomial) {
- const int degree = polynomial.rows() - 1;
- CHECK_GE(degree, 0);
-
-
-
- if (degree == 0) {
- return Eigen::VectorXd::Zero(1);
- }
- Vector derivative(degree);
- for (int i = 0; i < degree; ++i) {
- derivative(i) = (degree - i) * polynomial(i);
- }
- return derivative;
- }
- void MinimizePolynomial(const Vector& polynomial,
- const double x_min,
- const double x_max,
- double* optimal_x,
- double* optimal_value) {
-
-
-
-
-
- *optimal_x = (x_min + x_max) / 2.0;
- *optimal_value = EvaluatePolynomial(polynomial, *optimal_x);
- const double x_min_value = EvaluatePolynomial(polynomial, x_min);
- if (x_min_value < *optimal_value) {
- *optimal_value = x_min_value;
- *optimal_x = x_min;
- }
- const double x_max_value = EvaluatePolynomial(polynomial, x_max);
- if (x_max_value < *optimal_value) {
- *optimal_value = x_max_value;
- *optimal_x = x_max;
- }
-
- if (polynomial.rows() <= 2) {
- return;
- }
- const Vector derivative = DifferentiatePolynomial(polynomial);
- Vector roots_real;
- if (!FindPolynomialRoots(derivative, &roots_real, nullptr)) {
- LOG(WARNING) << "Unable to find the critical points of "
- << "the interpolating polynomial.";
- return;
- }
-
-
- for (int i = 0; i < roots_real.rows(); ++i) {
- const double root = roots_real(i);
- if ((root < x_min) || (root > x_max)) {
- continue;
- }
- const double value = EvaluatePolynomial(polynomial, root);
- if (value < *optimal_value) {
- *optimal_value = value;
- *optimal_x = root;
- }
- }
- }
- Vector FindInterpolatingPolynomial(const std::vector<FunctionSample>& samples) {
- const int num_samples = samples.size();
- int num_constraints = 0;
- for (int i = 0; i < num_samples; ++i) {
- if (samples[i].value_is_valid) {
- ++num_constraints;
- }
- if (samples[i].gradient_is_valid) {
- ++num_constraints;
- }
- }
- const int degree = num_constraints - 1;
- Matrix lhs = Matrix::Zero(num_constraints, num_constraints);
- Vector rhs = Vector::Zero(num_constraints);
- int row = 0;
- for (int i = 0; i < num_samples; ++i) {
- const FunctionSample& sample = samples[i];
- if (sample.value_is_valid) {
- for (int j = 0; j <= degree; ++j) {
- lhs(row, j) = pow(sample.x, degree - j);
- }
- rhs(row) = sample.value;
- ++row;
- }
- if (sample.gradient_is_valid) {
- for (int j = 0; j < degree; ++j) {
- lhs(row, j) = (degree - j) * pow(sample.x, degree - j - 1);
- }
- rhs(row) = sample.gradient;
- ++row;
- }
- }
-
-
- Eigen::FullPivLU<Matrix> lu(lhs);
- return lu.setThreshold(0.0).solve(rhs);
- }
- void MinimizeInterpolatingPolynomial(const std::vector<FunctionSample>& samples,
- double x_min,
- double x_max,
- double* optimal_x,
- double* optimal_value) {
- const Vector polynomial = FindInterpolatingPolynomial(samples);
- MinimizePolynomial(polynomial, x_min, x_max, optimal_x, optimal_value);
- for (const auto& sample : samples) {
- if ((sample.x < x_min) || (sample.x > x_max)) {
- continue;
- }
- const double value = EvaluatePolynomial(polynomial, sample.x);
- if (value < *optimal_value) {
- *optimal_x = sample.x;
- *optimal_value = value;
- }
- }
- }
- }
|