UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils::Poly< Real > Class Template Reference

Specializes the Eigen::Matrix class to represent and manipulate polynomials. More...

#include <Utils_Poly.hh>

Inheritance diagram for Utils::Poly< Real >:

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_tset_scalar (Real a)
 
Poly_tset_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_toperator= (Poly_t const &c)
 
Poly_t operator- ()
 
Poly_toperator+= (Poly_t const &q)
 
Poly_toperator-= (Poly_t const &q)
 
Poly_toperator*= (Poly_t const &q)
 
Poly_toperator+= (Real a)
 
Poly_toperator-= (Real a)
 
Poly_toperator*= (Real a)
 

Detailed Description

template<typename Real>
class Utils::Poly< Real >

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

  • Operator Overloading: Supports the +, -, and * operators for polynomial addition, subtraction, and multiplication, respectively.
  • Flexible Degree Management: Allows setting and adjusting the polynomial degree.
  • Integration with Eigen: Leverages the Eigen::Matrix class for efficient vector and matrix operations.

Usage

#include <Poly.h> // Ensure you include the Poly class header
Poly<double> P, Q, R;
// Initialize P(x) = 1 + x + 2*x^2
P << 1, 1, 2;
// Initialize Q(x) = 1 - 3*x^4
Q << 1, 0, 0, 0, -3;
// Compute R(x) = P(x) + 3 * Q(x)
R = P + 3 * Q;
// Compute R(x) = P(x) * Q(x)
R.set_degree(P.degree() + Q.degree());
R = P * Q;
void set_degree(Integer degree)
Definition Utils_Poly.hh:233
Poly()
Definition Utils_Poly.hh:119
Integer degree() const
Definition Utils_Poly.hh:325

Additional Information

For detailed descriptions of the available methods and further functionality, refer to the member function documentation.

Member Typedef Documentation

◆ dvec_t

template<typename Real>
using Utils::Poly< Real >::dvec_t = Eigen::Matrix<Real,Eigen::Dynamic,1>

◆ Integer

template<typename Real>
using Utils::Poly< Real >::Integer = int

◆ Poly_t

template<typename Real>
using Utils::Poly< Real >::Poly_t = Poly<Real>

Constructor & Destructor Documentation

◆ Poly() [1/4]

template<typename Real>
Utils::Poly< Real >::Poly ( )
inline

◆ Poly() [2/4]

template<typename Real>
Utils::Poly< Real >::Poly ( int order)
inlineexplicit

Initializes the polynomial with a specified maximum degree.

Parameters
orderThe highest degree of the polynomial that can be stored.

This defines the size of the coefficient vector.

◆ Poly() [3/4]

template<typename Real>
Utils::Poly< Real >::Poly ( Poly_t const & c)
inline

Copy constructor for the polynomial. Initializes the polynomial by creating a copy of the given polynomial \(c(x)\).

Parameters
cThe polynomial to be copied.

◆ Poly() [4/4]

template<typename Real>
Utils::Poly< Real >::Poly ( dvec_t const & c)
inlineexplicit

Constructs a polynomial by copying the given vector of coefficients \(c(x)\).

Parameters
cA 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.

Member Function Documentation

◆ adjust_degree()

template<typename Real>
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

P << 0, 0, 3, 4, 0; // P(x) = 3x^2 + 4x^3
P.adjust_degree(); // Adjusts degree, ensuring a_n is non-zero
// Resulting coefficients will reflect the highest degree with non-zero coefficient
void adjust_degree()

◆ coeffs()

template<typename Real>
dvec_t Utils::Poly< Real >::coeffs ( ) const
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\).

Returns
A copy of the Eigen vector containing the polynomial coefficients.

Example

P.set_monomial(3); // P(x) = x + 3
dvec_t coefficients = P.coeffs(); // coefficients will contain [3, 1]
Poly_t & set_monomial(Real a)
dvec_t coeffs() const
Definition Utils_Poly.hh:306
Eigen::Matrix< Real, Eigen::Dynamic, 1 > dvec_t
Definition Utils_Poly.hh:110

◆ degree()

template<typename Real>
Integer Utils::Poly< Real >::degree ( ) const
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.

Returns
The degree of the polynomial as an integer value.

Example

P.set_monomial(3); // P(x) = x + 3
Integer deg = P.degree(); // deg will be 1
int Integer
Definition Utils_Poly.hh:109

◆ derivative()

template<typename Real>
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.

Parameters
resultThe polynomial object where the derivative will be stored.

Example

P.set_degree(3); // P(x) = c_0 + c_1 x + c_2 x^2 + c_3 x^3
P << 1, 2, 3, 4; // Coefficients: c_0 = 1, c_1 = 2, c_2 = 3, c_3 = 4
Poly<double> derivativePoly;
P.derivative(derivativePoly); // derivativePoly now contains the derivative
void derivative(Poly_t &result) const

◆ eval() [1/2]

template<typename Real>
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.

Parameters
xThe point at which the polynomial is to be evaluated.
Returns
The computed value of the polynomial at \( x \).

Example

P.set_monomial(2); // P(x) = x + 2
Real value = P.eval(3); // value will be 5 = 3 + 2
Real eval(Real x) const

◆ eval() [2/2]

template<typename Real>
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:

  • \( p \) will contain the value of the polynomial at \( x \).
  • \( Dp \) will contain the value of the derivative at \( x \).
Parameters
xThe point at which the polynomial and its derivative are to be evaluated.
pReference 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.
DpReference 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

P.set_monomial(2); // P(x) = x + 2
Real value, derivative;
P.eval(3, value, derivative); // value will be 5 and derivative will be 1

◆ eval_D()

template<typename Real>
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.

Parameters
xThe point at which the derivative of the polynomial is to be evaluated.
Returns
The computed value of the derivative at \( x \).

Example

P.set_monomial(2); // P(x) = x + 2
Real derivativeValue = P.eval_D(3); // derivativeValue will be 1 (the derivative is constant)
Real eval_D(Real x) const

◆ integral()

template<typename Real>
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.

Parameters
resultThe polynomial object where the integral will be stored.

Example

P.set_degree(3); // P(x) = c_0 + c_1 x + c_2 x^2 + c_3 x^3
P << 1, 2, 3, 4; // Coefficients: c_0 = 1, c_1 = 2, c_2 = 3, c_3 = 4
Poly<double> integralPoly;
P.integral(integralPoly); // integralPoly now contains the integral
void integral(Poly_t &result) const

◆ leading_coeff()

template<typename Real>
Real Utils::Poly< Real >::leading_coeff ( ) const
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.

Returns
The leading coefficient of the polynomial.

Example

P.set_degree(3); // P(x) = c_0 + c_1 x + c_2 x^2 + c_3 x^3
P << 1, 2, 3, 4; // Coefficients: c_0 = 1, c_1 = 2, c_2 = 3, c_3 = 4
Real leadCoeff = P.leading_coeff(); // leadCoeff will be 4
Real leading_coeff() const
Definition Utils_Poly.hh:513

◆ make_monic()

template<typename Real>
void Utils::Poly< Real >::make_monic ( )
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:

  1. Divides all coefficients of the polynomial by the current leading coefficient (i.e., \( a_n \)).
  2. Sets the leading coefficient (i.e., the coefficient of \( x^n \)) to 1.

Example

P << 4, -2, 1; // P(x) = 4 + (-2)x + 1x^2
P.make_monic(); // Transforms P into monic form
// Resulting coefficients will be: [1, -0.5, 0.25] corresponding to x^2 - 0.5x + 0.25
void make_monic()
Definition Utils_Poly.hh:702

◆ normalize()

template<typename Real>
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.

Returns
The scaling factor \( S \), which is used to normalize the polynomial coefficients.

Example

P << 2, -3, 5; // P(x) = 2 - 3x + 5x^2
Real scale = P.normalize(); // Scale will be 5, and coefficients will be normalized
// New coefficients will be: [0.4, -0.6, 1.0] (after normalization)
Real normalize()

◆ operator*=() [1/2]

template<typename Real>
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) \]

Parameters
qThe polynomial to be multiplied.
Returns
A reference to the current polynomial instance after the multiplication.

Example

P << 2, 1; // P(x) = 2 + x
Q << 1, -1; // Q(x) = 1 - x
P *= Q; // Multiplies P by Q
// Now P(x) = (2 + x)(1 - x) = 2 - 2*x + x^2

◆ operator*=() [2/2]

template<typename Real>
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 \]

Parameters
aThe scalar value to be multiplied.
Returns
A reference to the current polynomial instance after the multiplication.

Example

P << 1, 2; // P(x) = 1 + 2x
P *= 2; // Multiplies P by 2
// Now P(x) = 2 + 4x

◆ operator+=() [1/2]

template<typename Real>
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) \]

Parameters
qThe polynomial to be added.
Returns
A reference to the current polynomial instance after the addition.

Example

P << 1, 2; // P(x) = 1 + 2x
Q << 3, -4; // Q(x) = 3 - 4x
P += Q; // Adds Q to P
// Now P(x) = 4 - 2x

◆ operator+=() [2/2]

template<typename Real>
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 \]

Parameters
aThe scalar value to be added.
Returns
A reference to the current polynomial instance after the addition.

Example

P << 1, 2; // P(x) = 1 + 2x
P += 3; // Adds 3 to P
// Now P(x) = 4 + 2x

◆ operator-()

template<typename Real>
Poly_t Utils::Poly< Real >::operator- ( )
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 \]

Returns
A new polynomial that is the negation of the current polynomial.

Example

P << 3, -1, 2; // P(x) = 3 - x + 2x^2
Poly<double> NegP = -P; // Negates P
// NegP(x) = -3 + x - 2x^2

◆ operator-=() [1/2]

template<typename Real>
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) \]

Parameters
qThe polynomial to be subtracted.
Returns
A reference to the current polynomial instance after the subtraction.

Example

P << 5, 0, 2; // P(x) = 5 + 0*x + 2*x^2
Q << 1, 1; // Q(x) = 1 + x
P -= Q; // Subtracts Q from P
// Now P(x) = 4 - x + 2*x^2

◆ operator-=() [2/2]

template<typename Real>
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 \]

Parameters
aThe scalar value to be subtracted.
Returns
A reference to the current polynomial instance after the subtraction.

Example

P << 4, -1; // P(x) = 4 - x
P -= 2; // Subtracts 2 from P
// Now P(x) = 2 - x

◆ operator=()

template<typename Real>
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.

Parameters
cThe polynomial to be copied.
Returns
A reference to the current polynomial instance after the assignment.

Example

P << 1, -2, 3; // P(x) = 1 - 2x + 3x^2
Q = P; // Assigns P to Q
// Now Q(x) = 1 - 2x + 3x^2

◆ order()

template<typename Real>
Integer Utils::Poly< Real >::order ( ) const
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 \).

Returns
The order of the polynomial as an integer value, representing the total number of coefficients.

Example

P.set_monomial(3); // P(x) = x + 3
Integer ord = P.order(); // ord will be 2
Integer order() const
Definition Utils_Poly.hh:345

◆ purge()

template<typename Real>
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.

Parameters
epsiThe threshold value below which coefficients will be purged.

Example

P << 0.1, 0.0005, -0.2, 0.3, 0.0001; // P(x) = 0.1 + 0.0005x - 0.2x^2 + 0.3x^3 + 0.0001x^4
P.purge(0.001); // Coefficients less than or equal to 0.001 will be set to 0
// Resulting coefficients will be: [0.1, 0, -0.2, 0.3, 0]
void purge(Real epsi)

◆ set_degree()

template<typename Real>
void Utils::Poly< Real >::set_degree ( Integer degree)
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.

Parameters
degreeThe 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

// Set the polynomial degree to 3 (P(x) = 0 + 0*x + 0*x^2 + 0*x^3)
// After setting the degree, we can manually assign the coefficients
P << 1, -2, 3, 4; // P(x) = 1 - 2*x + 3*x^2 + 4*x^3

◆ set_monomial()

template<typename Real>
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.

Parameters
aThe constant value to be added to the polynomial.
Returns
A reference to the internal polynomial, allowing for method chaining.

Example

// Set the polynomial to a monomial of the form x + 3 (P(x) = x + 3)

◆ set_order()

template<typename Real>
void Utils::Poly< Real >::set_order ( Integer order)
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.

Parameters
orderThe new order of the polynomial. The polynomial will have \(order\) terms (from the constant term up to the \(x^{\text{order-1}}\) term).

Example

// Set the polynomial degree to 3 (P(x) = 0 + 0*x + 0*x^2 + 0*x^3)
P.set_order(4);
// After setting the degree, we can manually set the coefficients
P << 1, -2, 3, 4; // P(x) = 1 - 2*x + 3*x^2 + 4*x^3
void set_order(Integer order)
Definition Utils_Poly.hh:204

◆ set_scalar()

template<typename Real>
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.

Parameters
aThe scalar value to initialize the polynomial.
Returns
A reference to the internal polynomial, allowing for method chaining.

Example

// Set the polynomial to a scalar value of 5 (P(x) = 5)
Poly_t & set_scalar(Real a)

◆ sign_variations()

template<typename Real>
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.

Returns
The number of sign variations in the polynomial coefficients.

Example

P << 3, -1, 2, -4; // P(x) = 3 - x + 2x^2 - 4x^3
Integer variations = P.sign_variations(); // Counts the sign variations in [3, -1, 2, -4]
// variations will be 3, corresponding to the sign changes: + -> - -> + -> -
Integer sign_variations() const

◆ to_eigen()

template<typename Real>
dvec_t const & Utils::Poly< Real >::to_eigen ( ) const
inline

Returns the polynomial as an Eigen vector. This function provides access to the underlying Eigen vector representation of the polynomial's coefficients.

Returns
A constant reference to the 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\)).

◆ to_string()

template<typename Real>
string Utils::Poly< Real >::to_string ( ) const
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\).

  • If the polynomial is empty (order <= 0), it returns "EMPTY!".
  • If the polynomial consists of a single term (order == 1), it returns the coefficient of that term.
  • If all coefficients are zero, it returns "0".

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.

Returns
A string representation of the polynomial.

Example

P.set_monomial(3); // P(x) = x + 3
string str = P.to_string(); // str will be "x + 3"
string to_string() const
Definition Utils_Poly.hh:1284

The documentation for this class was generated from the following file: