Drake
Drake C++ Documentation
Polynomial< T > Class Template Reference

Detailed Description

template<typename T = double>
class drake::Polynomial< T >

A scalar multi-variate polynomial, modeled after the msspoly in spotless.

Polynomial represents a list of additive Monomials, each one of which is a product of a constant coefficient (of T, which by default is double) and any number of distinct Terms (variables raised to positive integer powers).

Variables are identified by integer indices rather than symbolic names, but an automatic facility is provided to convert variable names up to four characters into unique integers, provided those variables are named using only lowercase letters and the "@#_." characters followed by a number. For example, valid names include "dx4" and "m_x".

Monomials which have the same variables and powers may be constructed but will be automatically combined: (3 * a * b * a) + (1.5 * b * a**2) will be reduced to (4.5 * b * a**2) internally after construction.

Polynomials can be added, subtracted, and multiplied. They may only be divided by scalars (of T) because Polynomials are not closed under division.

Template Parameters
TThe scalar type, which must be one of the default scalars.

#include <drake/common/polynomial.h>

Classes

class  Monomial
 An additive atom of a Polynomial: The product of any number of Terms and a coefficient. More...
 
struct  Product
 
class  Term
 An individual variable raised to an integer power; e.g. x**2. More...
 

Public Types

typedef unsigned int VarType
 
typedef int PowerType
 This should be 'unsigned int' but MSVC considers a call to std::pow(..., unsigned int) ambiguous because it won't cast unsigned int to int. More...
 
typedef Eigen::NumTraits< T >::Real RealScalar
 
typedef std::complex< RealScalarRootType
 
typedef Eigen::Matrix< RootType, Eigen::Dynamic, 1 > RootsType
 

Public Member Functions

 Polynomial (void)
 Construct the vacuous polynomial, "0". More...
 
 Polynomial (const T &scalar)
 Construct a Polynomial of a single constant. e.g. "5". More...
 
 Polynomial (const T coeff, const std::vector< Term > &terms)
 Construct a Polynomial consisting of a single Monomial, e.g. "5xy**3". More...
 
 Polynomial (typename std::vector< Monomial >::const_iterator start, typename std::vector< Monomial >::const_iterator finish)
 Construct a Polynomial from a sequence of Monomials. More...
 
template<typename U = T>
 Polynomial (const std::enable_if_t< std::is_same_v< U, double >, std::string > &varname)
 Constructs a polynomial consisting of a single Monomial of the variable named varname1. More...
 
 Polynomial (const std::string &varname, unsigned int num)
 Construct a polynomial consisting of a single Monomial of the variable named varname + num. More...
 
 Polynomial (const T &coeff, const VarType &v)
 Construct a single Monomial of the given coefficient and variable. More...
 
template<typename Derived >
 Polynomial (const Eigen::MatrixBase< Derived > &coefficients)
 A constructor for univariate polynomials: takes a vector of coefficients for the x**0, x**1, x**2, x**3... More...
 
int GetNumberOfCoefficients () const
 Returns the number of unique Monomials (and thus the number of coefficients) in this Polynomial. More...
 
int GetDegree () const
 Returns the highest degree of any Monomial in this Polynomial. More...
 
bool IsAffine () const
 Returns true iff this is a sum of terms of degree 1, plus a constant. More...
 
VarType GetSimpleVariable () const
 If the polynomial is "simple" – e.g. More...
 
const std::vector< Monomial > & GetMonomials () const
 
VectorX< T > GetCoefficients () const
 Returns the vector of the coefficients of the polynomial in the order of powers of the variable - the [i]th element is the coefficient of the variable raised to the ith power. More...
 
std::set< VarTypeGetVariables () const
 Returns a set of all of the variables present in this Polynomial. More...
 
template<typename U >
Product< T, U >::type EvaluateUnivariate (const U &x, int derivative_order=0) const
 Evaluate a univariate Polynomial at a specific point. More...
 
template<typename U >
Product< T, U >::type EvaluateMultivariate (const std::map< VarType, U > &var_values) const
 Evaluate a multivariate Polynomial at a specific point. More...
 
Polynomial EvaluatePartial (const std::map< VarType, T > &var_values) const
 Substitute values for some but not necessarily all variables of a Polynomial. More...
 
void Subs (const VarType &orig, const VarType &replacement)
 Replaces all instances of variable orig with replacement. More...
 
Polynomial Substitute (const VarType &orig, const Polynomial &replacement) const
 Replaces all instances of variable orig with replacement. More...
 
Polynomial Derivative (int derivative_order=1) const
 Takes the derivative of this (univariate) Polynomial. More...
 
Polynomial Integral (const T &integration_constant=0.0) const
 Takes the integral of this (univariate, non-constant) Polynomial. More...
 
bool is_univariate () const
 Returns true if this is a univariate polynomial. More...
 
bool operator== (const Polynomial &other) const
 
Polynomialoperator+= (const Polynomial &other)
 
Polynomialoperator-= (const Polynomial &other)
 
Polynomialoperator *= (const Polynomial &other)
 
Polynomialoperator+= (const T &scalar)
 
Polynomialoperator-= (const T &scalar)
 
Polynomialoperator *= (const T &scalar)
 
Polynomialoperator/= (const T &scalar)
 
const Polynomial operator+ (const Polynomial &other) const
 
const Polynomial operator- (const Polynomial &other) const
 
const Polynomial operator- () const
 
const Polynomial operator * (const Polynomial &other) const
 
const Polynomial operator/ (const T &scalar) const
 
bool operator< (const Polynomial &other) const
 A comparison to allow std::lexicographical_compare on this class; does not reflect any sort of mathematical total order. More...
 
RootsType Roots () const
 Returns the roots of this (univariate) Polynomial. More...
 
boolean< T > CoefficientsAlmostEqual (const Polynomial< T > &other, const RealScalar &tol=0.0, const ToleranceType &tol_type=ToleranceType::kAbsolute) const
 Checks if a Polynomial is approximately equal to this one. More...
 
Implements CopyConstructible, CopyAssignable, MoveConstructible, MoveAssignable
 Polynomial (const Polynomial &)=default
 
Polynomialoperator= (const Polynomial &)=default
 
 Polynomial (Polynomial &&)=default
 
Polynomialoperator= (Polynomial &&)=default
 

Static Public Member Functions

static Polynomial< T > FromExpression (const drake::symbolic::Expression &e)
 Constructs a Polynomial representing the symbolic expression e. More...
 
static bool IsValidVariableName (const std::string name)
 Variable name/ID conversion facility. More...
 
static VarType VariableNameToId (const std::string name, unsigned int m=1)
 
static std::string IdToVariableName (const VarType id)
 

Friends

const Polynomial operator+ (const Polynomial &p, const T &scalar)
 
const Polynomial operator+ (const T &scalar, const Polynomial &p)
 
const Polynomial operator- (const Polynomial &p, const T &scalar)
 
const Polynomial operator- (const T &scalar, const Polynomial &p)
 
const Polynomial operator * (const Polynomial &p, const T &scalar)
 
const Polynomial operator * (const T &scalar, const Polynomial &p)
 
std::ostream & operator<< (std::ostream &os, const Monomial &m)
 
std::ostream & operator<< (std::ostream &os, const Polynomial &poly)
 
template<typename U >
Polynomial< U > pow (const Polynomial< U > &p, typename Polynomial< U >::PowerType n)
 

Member Typedef Documentation

◆ PowerType

typedef int PowerType

This should be 'unsigned int' but MSVC considers a call to std::pow(..., unsigned int) ambiguous because it won't cast unsigned int to int.

◆ RealScalar

typedef Eigen::NumTraits<T>::Real RealScalar

◆ RootsType

typedef Eigen::Matrix<RootType, Eigen::Dynamic, 1> RootsType

◆ RootType

typedef std::complex<RealScalar> RootType

◆ VarType

typedef unsigned int VarType

Constructor & Destructor Documentation

◆ Polynomial() [1/10]

Polynomial ( const Polynomial< T > &  )
default

◆ Polynomial() [2/10]

Polynomial ( Polynomial< T > &&  )
default

◆ Polynomial() [3/10]

Polynomial ( void  )

Construct the vacuous polynomial, "0".

◆ Polynomial() [4/10]

Polynomial ( const T &  scalar)

Construct a Polynomial of a single constant. e.g. "5".

◆ Polynomial() [5/10]

Polynomial ( const T  coeff,
const std::vector< Term > &  terms 
)

Construct a Polynomial consisting of a single Monomial, e.g. "5xy**3".

◆ Polynomial() [6/10]

Polynomial ( typename std::vector< Monomial >::const_iterator  start,
typename std::vector< Monomial >::const_iterator  finish 
)

Construct a Polynomial from a sequence of Monomials.

◆ Polynomial() [7/10]

Polynomial ( const std::enable_if_t< std::is_same_v< U, double >, std::string > &  varname)
explicit

Constructs a polynomial consisting of a single Monomial of the variable named varname1.

Note
: This constructor is only provided for T = double. For the other cases, a user should use the constructor with two arguments below (taking std::string and unsigned int). If we provided this constructor for T = AutoDiffXd and T = symbolic::Expression, there would be compiler errors for Polynomial<T>(0) as the following candidates are ambiguous:

◆ Polynomial() [8/10]

Polynomial ( const std::string &  varname,
unsigned int  num 
)

Construct a polynomial consisting of a single Monomial of the variable named varname + num.

◆ Polynomial() [9/10]

Polynomial ( const T &  coeff,
const VarType v 
)

Construct a single Monomial of the given coefficient and variable.

◆ Polynomial() [10/10]

Polynomial ( const Eigen::MatrixBase< Derived > &  coefficients)
explicit

A constructor for univariate polynomials: takes a vector of coefficients for the x**0, x**1, x**2, x**3...

Monomials. All terms are always added, even if a coefficient is zero.

Member Function Documentation

◆ CoefficientsAlmostEqual()

boolean<T> CoefficientsAlmostEqual ( const Polynomial< T > &  other,
const RealScalar tol = 0.0,
const ToleranceType tol_type = ToleranceType::kAbsolute 
) const

Checks if a Polynomial is approximately equal to this one.

Checks that every coefficient of other is within tol of the corresponding coefficient of this Polynomial.

Note: When tol_type is kRelative, if any monomials appear in this or other but not both, then the method returns false (since the comparison is relative to a missing zero coefficient). Use kAbsolute if you want to ignore non-matching monomials with coefficients less than tol.

◆ Derivative()

Polynomial Derivative ( int  derivative_order = 1) const

Takes the derivative of this (univariate) Polynomial.

Returns a new Polynomial that is the derivative of this one in its sole variable.

Exceptions
std::exceptionif this Polynomial is not univariate.

If derivative_order is given, takes the nth derivative of this Polynomial.

◆ EvaluateMultivariate()

Product<T, U>::type EvaluateMultivariate ( const std::map< VarType, U > &  var_values) const

Evaluate a multivariate Polynomial at a specific point.

Evaluates a Polynomial with the given values for each variable.

Exceptions
std::exceptionif the Polynomial contains variables for which values were not provided.

The provided values may be of any type which is std::is_arithmetic (supporting the std::pow, *, and + operations) and need not be CoefficientsType or RealScalar)

◆ EvaluatePartial()

Polynomial EvaluatePartial ( const std::map< VarType, T > &  var_values) const

Substitute values for some but not necessarily all variables of a Polynomial.

Analogous to EvaluateMultivariate, but: (1) Restricted to T, and (2) Need not map every variable in var_values.

Returns a Polynomial in which each variable in var_values has been replaced with its value and constants appropriately combined.

◆ EvaluateUnivariate()

Product<T, U>::type EvaluateUnivariate ( const U &  x,
int  derivative_order = 0 
) const

Evaluate a univariate Polynomial at a specific point.

Evaluates a univariate Polynomial at the given x.

Exceptions
std::exceptionif this Polynomial is not univariate.

x may be of any type supporting the ** and + operations (which can be different from both CoefficientsType and RealScalar).

This method may also be used for efficient evaluation of the derivatives of the univariate polynomial, evaluated at x. derivative_order = 0 (the default) returns the polynomial value without differentiation. derivative_order = 1 returns the first derivative, etc.

Precondition
derivative_order must be non-negative.

◆ FromExpression()

static Polynomial<T> FromExpression ( const drake::symbolic::Expression e)
static

Constructs a Polynomial representing the symbolic expression e.

Note that the ID of a variable is preserved in this translation.

Exceptions
std::exceptionif e is not polynomial-convertible.
Precondition
e.is_polynomial() is true.

◆ GetCoefficients()

VectorX<T> GetCoefficients ( ) const

Returns the vector of the coefficients of the polynomial in the order of powers of the variable - the [i]th element is the coefficient of the variable raised to the ith power.

Exceptions
std::runtime_errorif this Polynomial is not univariate.

◆ GetDegree()

int GetDegree ( ) const

Returns the highest degree of any Monomial in this Polynomial.

The degree of a multivariate Monomial is the product of the degrees of each of its terms.

◆ GetMonomials()

const std::vector<Monomial>& GetMonomials ( ) const

◆ GetNumberOfCoefficients()

int GetNumberOfCoefficients ( ) const

Returns the number of unique Monomials (and thus the number of coefficients) in this Polynomial.

◆ GetSimpleVariable()

VarType GetSimpleVariable ( ) const

If the polynomial is "simple" – e.g.

just a single term with coefficient 1 – then returns that variable; otherwise returns 0.

◆ GetVariables()

std::set<VarType> GetVariables ( ) const

Returns a set of all of the variables present in this Polynomial.

◆ IdToVariableName()

static std::string IdToVariableName ( const VarType  id)
static

◆ Integral()

Polynomial Integral ( const T &  integration_constant = 0.0) const

Takes the integral of this (univariate, non-constant) Polynomial.

Returns a new Polynomial that is the indefinite integral of this one in its sole variable.

Exceptions
std::exceptionif this Polynomial is not univariate, or if it has no variables.

If integration_constant is given, adds that constant as the constant term (zeroth-order coefficient) of the resulting Polynomial.

◆ is_univariate()

bool is_univariate ( ) const

Returns true if this is a univariate polynomial.

◆ IsAffine()

bool IsAffine ( ) const

Returns true iff this is a sum of terms of degree 1, plus a constant.

◆ IsValidVariableName()

static bool IsValidVariableName ( const std::string  name)
static

Variable name/ID conversion facility.

◆ operator *()

const Polynomial operator * ( const Polynomial< T > &  other) const

◆ operator *=() [1/2]

Polynomial& operator *= ( const Polynomial< T > &  other)

◆ operator *=() [2/2]

Polynomial& operator *= ( const T &  scalar)

◆ operator+()

const Polynomial operator+ ( const Polynomial< T > &  other) const

◆ operator+=() [1/2]

Polynomial& operator+= ( const Polynomial< T > &  other)

◆ operator+=() [2/2]

Polynomial& operator+= ( const T &  scalar)

◆ operator-() [1/2]

const Polynomial operator- ( const Polynomial< T > &  other) const

◆ operator-() [2/2]

const Polynomial operator- ( ) const

◆ operator-=() [1/2]

Polynomial& operator-= ( const Polynomial< T > &  other)

◆ operator-=() [2/2]

Polynomial& operator-= ( const T &  scalar)

◆ operator/()

const Polynomial operator/ ( const T &  scalar) const

◆ operator/=()

Polynomial& operator/= ( const T &  scalar)

◆ operator<()

bool operator< ( const Polynomial< T > &  other) const

A comparison to allow std::lexicographical_compare on this class; does not reflect any sort of mathematical total order.

◆ operator=() [1/2]

Polynomial& operator= ( Polynomial< T > &&  )
default

◆ operator=() [2/2]

Polynomial& operator= ( const Polynomial< T > &  )
default

◆ operator==()

bool operator== ( const Polynomial< T > &  other) const

◆ Roots()

RootsType Roots ( ) const

Returns the roots of this (univariate) Polynomial.

Returns the roots of a univariate Polynomial as an Eigen column vector of complex numbers whose components are of the RealScalar type.

Exceptions
std::exceptionof this Polynomial is not univariate.

◆ Subs()

void Subs ( const VarType orig,
const VarType replacement 
)

Replaces all instances of variable orig with replacement.

◆ Substitute()

Polynomial Substitute ( const VarType orig,
const Polynomial< T > &  replacement 
) const

Replaces all instances of variable orig with replacement.

◆ VariableNameToId()

static VarType VariableNameToId ( const std::string  name,
unsigned int  m = 1 
)
static

Friends And Related Function Documentation

◆ operator * [1/2]

const Polynomial operator * ( const Polynomial< T > &  p,
const T &  scalar 
)
friend

◆ operator * [2/2]

const Polynomial operator * ( const T &  scalar,
const Polynomial< T > &  p 
)
friend

◆ operator+ [1/2]

const Polynomial operator+ ( const Polynomial< T > &  p,
const T &  scalar 
)
friend

◆ operator+ [2/2]

const Polynomial operator+ ( const T &  scalar,
const Polynomial< T > &  p 
)
friend

◆ operator- [1/2]

const Polynomial operator- ( const Polynomial< T > &  p,
const T &  scalar 
)
friend

◆ operator- [2/2]

const Polynomial operator- ( const T &  scalar,
const Polynomial< T > &  p 
)
friend

◆ operator<< [1/2]

std::ostream& operator<< ( std::ostream &  os,
const Monomial m 
)
friend

◆ operator<< [2/2]

std::ostream& operator<< ( std::ostream &  os,
const Polynomial< T > &  poly 
)
friend

◆ pow

Polynomial<U> pow ( const Polynomial< U > &  p,
typename Polynomial< U >::PowerType  n 
)
friend

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