ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
polynomial.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 
12 namespace colmap {
13 
14 // All polynomials are assumed to be the form:
15 //
16 // sum_{i=0}^N polynomial(i) x^{N-i}.
17 //
18 // and are given by a vector of coefficients of size N + 1.
19 //
20 // The implementation is based on COLMAP's old polynomial functionality and is
21 // inspired by Ceres-Solver's/Theia's implementation to support complex
22 // polynomials. The companion matrix implementation is based on NumPy.
23 
24 // Evaluate the polynomial for the given coefficients at x using the Horner
25 // scheme. This function is templated such that the polynomial may be evaluated
26 // at real and/or imaginary points.
27 template <typename T>
28 T EvaluatePolynomial(const Eigen::VectorXd& coeffs, const T& x);
29 
30 // Find the root of polynomials of the form: a * x + b = 0.
31 // The real and/or imaginary variable may be NULL if the output is not needed.
32 bool FindLinearPolynomialRoots(const Eigen::VectorXd& coeffs,
33  Eigen::VectorXd* real,
34  Eigen::VectorXd* imag);
35 
36 // Find the roots of polynomials of the form: a * x^2 + b * x + c = 0.
37 // The real and/or imaginary variable may be NULL if the output is not needed.
38 bool FindQuadraticPolynomialRoots(const Eigen::VectorXd& coeffs,
39  Eigen::VectorXd* real,
40  Eigen::VectorXd* imag);
41 
42 // Find the roots of a polynomial using the Durand-Kerner method, based on:
43 //
44 // https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method
45 //
46 // The Durand-Kerner is comparatively fast but often unstable/inaccurate.
47 // The real and/or imaginary variable may be NULL if the output is not needed.
48 bool FindPolynomialRootsDurandKerner(const Eigen::VectorXd& coeffs,
49  Eigen::VectorXd* real,
50  Eigen::VectorXd* imag);
51 
52 // Find the roots of a polynomial using the companion matrix method, based on:
53 //
54 // R. A. Horn & C. R. Johnson, Matrix Analysis. Cambridge,
55 // UK: Cambridge University Press, 1999, pp. 146-7.
56 //
57 // Compared to Durand-Kerner, this method is slower but more stable/accurate.
58 // The real and/or imaginary variable may be NULL if the output is not needed.
59 bool FindPolynomialRootsCompanionMatrix(const Eigen::VectorXd& coeffs,
60  Eigen::VectorXd* real,
61  Eigen::VectorXd* imag);
62 
64 // Implementation
66 
67 template <typename T>
68 T EvaluatePolynomial(const Eigen::VectorXd& coeffs, const T& x) {
69  T value = 0.0;
70  for (Eigen::VectorXd::Index i = 0; i < coeffs.size(); ++i) {
71  value = value * x + coeffs(i);
72  }
73  return value;
74 }
75 
76 } // namespace colmap
coeffs(0)
normal_z x
bool FindPolynomialRootsCompanionMatrix(const Eigen::VectorXd &coeffs_all, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:208
bool FindLinearPolynomialRoots(const Eigen::VectorXd &coeffs, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:65
bool FindQuadraticPolynomialRoots(const Eigen::VectorXd &coeffs, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:86
bool FindPolynomialRootsDurandKerner(const Eigen::VectorXd &coeffs_all, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:143
T EvaluatePolynomial(const Eigen::VectorXd &coeffs, const T &x)
Definition: polynomial.h:68
Eigen::MatrixXd::Index Index
Definition: knncpp.h:26