![]() |
UtilsLite
Utilities for C++ programming
|
Specializes the Eigen::Matrix
class to represent and manipulate polynomials.
More...
#include <Utils_Poly.hh>
Public Types | |
using | Integer = int |
using | dvec_t = Eigen::Matrix<Real,Eigen::Dynamic,1> |
using | Poly_t = Poly<Real> |
Public Member Functions | |
Poly () | |
Poly (int order) | |
Poly (Poly_t const &c) | |
Poly (dvec_t const &c) | |
dvec_t const & | to_eigen () const |
void | set_order (Integer order) |
void | set_degree (Integer degree) |
Poly_t & | set_scalar (Real a) |
Poly_t & | set_monomial (Real a) |
dvec_t | coeffs () const |
Integer | degree () const |
Integer | order () const |
string | to_string () const |
Real | eval (Real x) const |
Real | eval_D (Real x) const |
void | eval (Real x, Real &p, Real &Dp) const |
Real | leading_coeff () const |
void | derivative (Poly_t &result) const |
void | integral (Poly_t &result) const |
Real | normalize () |
void | purge (Real epsi) |
void | adjust_degree () |
Integer | sign_variations () const |
void | make_monic () |
Poly_t & | operator= (Poly_t const &c) |
Poly_t | operator- () |
Poly_t & | operator+= (Poly_t const &q) |
Poly_t & | operator-= (Poly_t const &q) |
Poly_t & | operator*= (Poly_t const &q) |
Poly_t & | operator+= (Real a) |
Poly_t & | operator-= (Real a) |
Poly_t & | operator*= (Real a) |
Specializes the Eigen::Matrix
class to represent and manipulate polynomials.
This class provides a convenient way to handle polynomials using the Eigen::Matrix
class, where a polynomial is represented as a vector of coefficients.
Given a coefficient vector:
\[ \boldsymbol{v} = ( v_0, v_1, v_2, \ldots, v_m ) \]
It corresponds to the polynomial:
\[ p(x) = v_0 + v_1\,x + v_2\,x^2 + \cdots + v_m\,x^m \]
The class overloads common operators (+
, -
, *
) to support polynomial arithmetic, making it simple to add, subtract, and multiply polynomials.
Features
+
, -
, and *
operators for polynomial addition, subtraction, and multiplication, respectively.Eigen::Matrix
class for efficient vector and matrix operations.Usage
Additional Information
For detailed descriptions of the available methods and further functionality, refer to the member function documentation.
using Utils::Poly< Real >::dvec_t = Eigen::Matrix<Real,Eigen::Dynamic,1> |
using Utils::Poly< Real >::Integer = int |
using Utils::Poly< Real >::Poly_t = Poly<Real> |
|
inline |
|
inlineexplicit |
Initializes the polynomial with a specified maximum degree.
order | The highest degree of the polynomial that can be stored. |
This defines the size of the coefficient vector.
|
inline |
Copy constructor for the polynomial. Initializes the polynomial by creating a copy of the given polynomial \(c(x)\).
c | The polynomial to be copied. |
|
inlineexplicit |
Constructs a polynomial by copying the given vector of coefficients \(c(x)\).
c | A vector of coefficients representing the polynomial, where each element corresponds to the coefficient of increasing powers of \(x\) (i.e., c[i] is the coefficient of \(x^i\)). |
The size of the vector determines the polynomial's degree. The copy operation explicitly invokes the base class assignment operator to avoid recursion.
void Utils::Poly< Real >::adjust_degree | ( | ) |
Adjusts the polynomial order of \( p(x) = \sum_{i=0}^n a_i x^i \) such that the leading coefficient \( a_n \) is non-zero. This method ensures that the polynomial's degree accurately reflects its meaningful terms by removing any trailing zero coefficients.
Specifically, the method checks for any leading zeros in the coefficient array and modifies the polynomial's order to the highest degree where the coefficient is non-zero. This is crucial for maintaining the integrity of the polynomial representation and ensuring that operations involving the polynomial (such as evaluation or differentiation) yield correct results.
If all coefficients are zero, the polynomial order will be adjusted to reflect this.
Example
|
inline |
Returns a copy of the coefficients of the polynomial as an Eigen
vector.
This function provides a copy of the underlying Eigen
vector that contains the polynomial's coefficients. The coefficients correspond to increasing powers of \(x\), with the element at index \(i\) being the coefficient of \(x^i\).
Eigen
vector containing the polynomial coefficients.Example
|
inline |
Returns the degree of the polynomial.
This function calculates and returns the degree of the polynomial, which is defined as the highest exponent of \(x\) in the polynomial. The degree is equal to \( m_order - 1 \), reflecting the number of terms in the polynomial minus one.
Example
void Utils::Poly< Real >::derivative | ( | Poly_t & | result | ) | const |
Computes the derivative of the polynomial and stores the result in the provided polynomial object.
This method calculates the derivative of the polynomial using the power rule. The derivative of a polynomial \( p(x) = c_0 + c_1 x + c_2 x^2 + \ldots + c_n x^n \) is given by:
\[ p'(x) = c_1 + 2c_2 x + 3c_3 x^2 + \ldots + n c_n x^{n-1} \]
The result is stored in the polynomial object passed as an argument.
result | The polynomial object where the derivative will be stored. |
Example
Real Utils::Poly< Real >::eval | ( | Real | x | ) | const |
Evaluates the polynomial at a given point using Horner's method.
This method computes the value of the polynomial \(p(x)\) at a specified point \( x \) using Horner's method, which is an efficient algorithm for polynomial evaluation. The polynomial is represented in the form:
\[ p(x) = c_0 + c_1 x + c_2 x^2 + \ldots + c_n x^n \]
where \( c_i \) are the coefficients of the polynomial. Horner's method reduces the number of multiplications required to evaluate the polynomial, providing a more efficient computation, especially for polynomials of high degree.
x | The point at which the polynomial is to be evaluated. |
Example
void Utils::Poly< Real >::eval | ( | Real | x, |
Real & | p, | ||
Real & | Dp ) const |
Evaluates the polynomial and its derivative at a given point using Horner's method.
This method computes both the value of the polynomial \( p(x) \) and its derivative \( p'(x) \) at a specified point \( x \). The calculations are performed using Horner's method, which is an efficient algorithm for polynomial evaluation and its derivatives.
The polynomial is represented in the form:
\[ p(x) = c_0 + c_1 x + c_2 x^2 + \ldots + c_n x^n \]
The derivative is represented as:
\[ p'(x) = c_1 + 2c_2 x + 3c_3 x^2 + \ldots + n c_n x^{n-1} \]
The results are stored in the provided reference parameters, where:
x | The point at which the polynomial and its derivative are to be evaluated. |
p | Reference to a variable where the computed value of the polynomial at \( x \) will be stored. This variable is modified to hold the result of the evaluation. |
Dp | Reference to a variable where the computed value of the derivative at \( x \) will be stored. This variable is modified to hold the result of the derivative evaluation. |
Example
Real Utils::Poly< Real >::eval_D | ( | Real | x | ) | const |
Evaluates the derivative of the polynomial at a given point using Horner's method.
This method computes the value of the derivative \( p'(x) \) of the polynomial \( p(x) \) at a specified point \( x \). The derivative is calculated using Horner's method, which is an efficient technique for evaluating polynomials and their derivatives.
The polynomial is represented in the form:
\[ p(x) = c_0 + c_1 x + c_2 x^2 + \ldots + c_n x^n \]
The derivative is given by:
\[ p'(x) = c_1 + 2c_2 x + 3c_3 x^2 + \ldots + n c_n x^{n-1} \]
where \( c_i \) are the coefficients of the polynomial. This implementation efficiently computes the derivative without explicitly forming it, thus optimizing performance.
x | The point at which the derivative of the polynomial is to be evaluated. |
Example
void Utils::Poly< Real >::integral | ( | Poly_t & | result | ) | const |
Computes the integral of the polynomial and stores the result in the provided polynomial object.
This method calculates the indefinite integral of the polynomial using the power rule. The integral of a polynomial \(p(x) = c_0 + c_1 x + c_2 x^2 + \ldots + c_n x^n \) is given by:
\[ \int p(x) \,dx = c_0 x + \frac{c_1}{2} x^2 + \frac{c_2}{3} x^3 + \ldots + \frac{c_n}{n+1} x^{n+1} + C \]
The constant of integration \( C \) is not included in the result. The result is stored in the polynomial object passed as an argument.
result | The polynomial object where the integral will be stored. |
Example
|
inline |
Returns the leading coefficient of the polynomial.
The leading coefficient is the coefficient of the term with the highest degree in the polynomial. This method retrieves the coefficient corresponding to the term \( c_n x^n \), where \( c_n \) is the coefficient stored at the last index of the coefficient vector.
Example
|
inline |
Transforms the polynomial \( p(x) = \sum_{i=0}^{n} a_i x^i \) into a monic polynomial, which is defined as a polynomial where the leading coefficient is equal to 1. The method modifies the polynomial such that it takes the form:
\[ p(x) = x^n + \sum_{i=0}^{n-1} a_i x^i \]
Specifically, this method performs the following operations:
Example
Real Utils::Poly< Real >::normalize | ( | ) |
Normalizes the polynomial \( p(x) = \sum_{i=0}^n a_i x^i \) by scaling its coefficients such that the maximum absolute value of the coefficients is equal to 1. The scaling factor \( S \) is calculated to achieve this normalization.
Specifically, the normalization ensures that:
\[ \max_{i=0}^n \left(\frac{|a_i|}{S}\right) = 1 \]
where \( S \) is the maximum absolute value of the coefficients of the polynomial:
\[ S = \max_{i=0}^n |a_i| \]
If the maximum absolute value of the coefficients is zero (i.e., the polynomial is zero), the method will return zero without altering the coefficients.
Example
Poly_t & Utils::Poly< Real >::operator*= | ( | Poly_t const & | q | ) |
Multiplication assignment operator for the polynomial. This method multiplies the current polynomial \( p(x) \) by another polynomial \( q(x) \). The operation modifies the current polynomial as follows:
\[ p(x) \leftarrow p(x) \cdot q(x) \]
q | The polynomial to be multiplied. |
Example
Poly_t & Utils::Poly< Real >::operator*= | ( | Real | a | ) |
Multiplication assignment operator for the polynomial with a scalar. This method multiplies the current polynomial \( p(x) \) by a scalar value \( a \). The operation modifies the current polynomial as follows:
\[ p(x) \leftarrow p(x) \cdot a \]
a | The scalar value to be multiplied. |
Example
Poly_t & Utils::Poly< Real >::operator+= | ( | Poly_t const & | q | ) |
Addition assignment operator for the polynomial. This method adds another polynomial \( q(x) \) to the current polynomial \( p(x) \). The operation modifies the current polynomial as follows:
\[ p(x) \leftarrow p(x) + q(x) \]
q | The polynomial to be added. |
Example
Poly_t & Utils::Poly< Real >::operator+= | ( | Real | a | ) |
Addition assignment operator for the polynomial with a scalar. This method adds a scalar value \( a \) to the current polynomial \( p(x) \). The operation modifies the current polynomial as follows:
\[ p(x) \leftarrow p(x) + a \]
a | The scalar value to be added. |
Example
|
inline |
Unary negation operator for the polynomial. This method returns a new polynomial that represents the negation of the current polynomial \( p(x) \). The resulting polynomial is given by:
\[ -p(x) = -\sum_{i=0}^{n} a_i x^i \]
Example
Poly_t & Utils::Poly< Real >::operator-= | ( | Poly_t const & | q | ) |
Subtraction assignment operator for the polynomial. This method subtracts another polynomial \( q(x) \) from the current polynomial \( p(x) \). The operation modifies the current polynomial as follows:
\[ p(x) \leftarrow p(x) - q(x) \]
q | The polynomial to be subtracted. |
Example
Poly_t & Utils::Poly< Real >::operator-= | ( | Real | a | ) |
Subtraction assignment operator for the polynomial with a scalar. This method subtracts a scalar value \( a \) from the current polynomial \( p(x) \). The operation modifies the current polynomial as follows:
\[ p(x) \leftarrow p(x) - a \]
a | The scalar value to be subtracted. |
Example
Poly_t & Utils::Poly< Real >::operator= | ( | Poly_t const & | c | ) |
Assignment operator for the polynomial class. This method copies the values from another polynomial \( p(x) \) into the current instance, ensuring that the internal state is updated to match the source polynomial.
c | The polynomial to be copied. |
Example
|
inline |
Returns the order of the polynomial.
This function returns the order (size) of the polynomial, which is defined as the total number of coefficients stored in the polynomial, including the constant term. The order is equal to \( m_order \).
Example
void Utils::Poly< Real >::purge | ( | Real | epsi | ) |
Purges (sets to zero) the coefficients of the polynomial \( p(x) = \sum_{i=0}^n a_i x^i \) for which the absolute value is less than or equal to a specified threshold \( \epsilon \).
This method modifies the polynomial by removing insignificant coefficients, which can be useful for simplifying the polynomial and improving numerical stability. Specifically, it sets:
\[ a_i = 0 \quad \text{if} \quad |a_i| \leq \epsilon \]
This operation can be particularly beneficial when dealing with polynomials that have been generated through numerical methods, where small coefficients may result from rounding errors or other numerical inaccuracies.
epsi | The threshold value below which coefficients will be purged. |
Example
|
inline |
Sets the degree of the polynomial and resizes the coefficient vector accordingly.
This function adjusts the polynomial to the specified degree by resizing the underlying coefficient vector to have \(degree + 1\) terms. All coefficients are initialized to zero.
degree | The highest exponent of \(x\) in the polynomial. The polynomial will have \(degree + 1\) terms (from the constant term up to the \(x^{\text{degree}}\) term). |
Example
Poly_t & Utils::Poly< Real >::set_monomial | ( | Real | a | ) |
Initializes the polynomial as \(x + a\) and returns a reference to the internal polynomial.
This function sets the polynomial to a linear form, representing the polynomial
\[ p(x) = x + a \]
where \(a\) is the provided constant.
a | The constant value to be added to the polynomial. |
Example
|
inline |
Sets the order (degree+1) of the polynomial and resizes the coefficient vector accordingly.
This function adjusts the polynomial to the specified degree by resizing the underlying coefficient vector to match the new order. All coefficients are initialized to zero.
order | The new order of the polynomial. The polynomial will have \(order\) terms (from the constant term up to the \(x^{\text{order-1}}\) term). |
Example
Poly_t & Utils::Poly< Real >::set_scalar | ( | Real | a | ) |
Initializes the polynomial as a scalar and returns a reference to the internal polynomial.
This function sets the polynomial to a constant value, effectively representing the polynomial \(p(x) = a\), where \(a\) is the provided scalar value.
a | The scalar value to initialize the polynomial. |
Example
Integer Utils::Poly< Real >::sign_variations | ( | ) | const |
Counts the number of sign variations in the coefficients of the polynomial \( p(x) = \sum_{i=0}^n a_i x^i \). This method computes the number of sign changes in the sequence of coefficients \( [a_0, a_1, \ldots, a_n] \).
This method returns the total count of sign changes in the coefficient sequence, which can be useful for root-finding algorithms and analyzing the behavior of the polynomial.
Example
|
inline |
Returns the polynomial as an Eigen
vector. This function provides access to the underlying Eigen
vector representation of the polynomial's coefficients.
Eigen
vector that stores the polynomial's coefficients. The vector elements correspond to the coefficients of increasing powers of \(x\) (i.e., the element at index \(i\) is the coefficient of \(x^i\)).
|
inline |
Converts the polynomial to a human-readable string representation.
This method generates a string that represents the polynomial in the standard mathematical notation. The output format will display the polynomial's terms with appropriate signs and powers of \(x\).
The polynomial is represented in the form:
\[ p(x) = c_0 + c_1 x + c_2 x^2 + \ldots + c_n x^n \]
where \( c_i \) are the coefficients of the polynomial. The function handles formatting for positive and negative coefficients, omitting terms with a coefficient of zero, and does not display a coefficient of one when writing the variable term.
Example