Drake
|
A scalar multi-variate polynomial, modeled after the msspoly in spotless. More...
#include <drake/util/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 _CoefficientType | CoefficientType |
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< CoefficientType >::Real | RealScalar |
typedef std::complex< RealScalar > | RootType |
typedef Eigen::Matrix< RootType, Eigen::Dynamic, 1 > | RootsType |
Public Member Functions | |
Polynomial (void) | |
Construct the vacuous polynomial, "0". More... | |
Polynomial (const CoefficientType &scalar) | |
Construct a Polynomial of a single constant. e.g. "5". More... | |
Polynomial (const CoefficientType 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... | |
Polynomial (const std::string varname, const unsigned int num=1) | |
Construct a polynomial consisting of a single Monomial of the variable named varname + num. More... | |
Polynomial (const CoefficientType &coeff, const VarType &v) | |
Construct a single Monomial of the given coefficient and variable. More... | |
template<typename Derived > | |
Polynomial (Eigen::MatrixBase< Derived > const &coefficients) | |
A legacy constructor for univariate polynomials: Takes a vector of coefficients for the constant, x, 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 |
Eigen::Matrix< CoefficientType, Eigen::Dynamic, 1 > | getCoefficients () const |
std::set< VarType > | getVariables () const |
Returns a set of all of the variables present in this Polynomial. More... | |
template<typename T > | |
Product< CoefficientType, T >::type | evaluateUnivariate (const T &x) const |
Evaluate a univariate Polynomial at a specific point. More... | |
template<typename T > | |
Product< CoefficientType, T >::type | evaluateMultivariate (const std::map< VarType, T > &var_values) const |
Evaluate a multivariate Polynomial at a specific point. More... | |
Drake::TaylorVarXd | evaluateMultivariate (const std::map< VarType, Drake::TaylorVarXd > &var_values) const |
Specialization of evaluateMultivariate on TaylorVarXd. More... | |
Polynomial | evaluatePartial (const std::map< VarType, CoefficientType > &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 | derivative (unsigned int derivative_order=1) const |
Takes the derivative of this (univariate) Polynomial. More... | |
Polynomial | integral (const CoefficientType &integration_constant=0.0) const |
Takes the integral of this (univariate, non-constant) Polynomial. More... | |
bool | operator== (const Polynomial &other) const |
Polynomial & | operator+= (const Polynomial &other) |
Polynomial & | operator-= (const Polynomial &other) |
Polynomial & | operator*= (const Polynomial &other) |
Polynomial & | operator+= (const CoefficientType &scalar) |
Polynomial & | operator-= (const CoefficientType &scalar) |
Polynomial & | operator*= (const CoefficientType &scalar) |
Polynomial & | operator/= (const CoefficientType &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 CoefficientType &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... | |
bool | isApprox (const Polynomial &other, const RealScalar &tol) const |
Checks if a (univariate) Polynomial is approximately equal to this one. More... | |
Static Public Member Functions | |
static Eigen::Matrix< Polynomial, Eigen::Dynamic, Eigen::Dynamic > | randomPolynomialMatrix (Eigen::Index num_coefficients_per_polynomial, Eigen::Index rows, Eigen::Index cols) |
Obtains a matrix of random unvariate Polynomials of the specified size. More... | |
Friends | |
const Polynomial | operator+ (const Polynomial &p, const CoefficientType &scalar) |
const Polynomial | operator+ (const CoefficientType &scalar, const Polynomial &p) |
const Polynomial | operator- (const Polynomial &p, const CoefficientType &scalar) |
const Polynomial | operator- (const CoefficientType &scalar, const Polynomial &p) |
const Polynomial | operator* (const Polynomial &p, const CoefficientType &scalar) |
const Polynomial | operator* (const CoefficientType &scalar, const Polynomial &p) |
std::ostream & | operator<< (std::ostream &os, const Monomial &m) |
std::ostream & | operator<< (std::ostream &os, const Polynomial &poly) |
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 _CoefficientType, 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 covert 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 _CoefficientType) because Polynomials are not closed under division.
typedef _CoefficientType CoefficientType |
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.
typedef Eigen::NumTraits<CoefficientType>::Real RealScalar |
typedef std::complex<RealScalar> RootType |
typedef unsigned int VarType |
|
inline |
Construct the vacuous polynomial, "0".
Polynomial | ( | const CoefficientType & | scalar | ) |
Construct a Polynomial of a single constant. e.g. "5".
Polynomial | ( | const CoefficientType | coeff, |
const std::vector< Term > & | terms | ||
) |
Construct a Polynomial consisting of a single Monomial, e.g. "5xy**3".
Polynomial | ( | typename std::vector< Monomial >::const_iterator | start, |
typename std::vector< Monomial >::const_iterator | finish | ||
) |
Construct a Polynomial from a sequence of Monomials.
|
explicit |
Construct a polynomial consisting of a single Monomial of the variable named varname + num.
Polynomial | ( | const CoefficientType & | coeff, |
const VarType & | v | ||
) |
Construct a single Monomial of the given coefficient and variable.
|
inlineexplicit |
A legacy constructor for univariate polynomials: Takes a vector of coefficients for the constant, x, x**2, x**3...
Monomials.
Polynomial derivative | ( | unsigned 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. Throws an exception of this Polynomial is not univariate.
If derivative_order is given, takes the nth derivative of this Polynomial.
|
inline |
Evaluate a multivariate Polynomial at a specific point.
Evaluates a Polynomial with the given values for each variable. Throws std::out_of_range if 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)
|
inline |
Specialization of evaluateMultivariate on TaylorVarXd.
Specialize evaluateMultivariate on TaylorVarXd because Eigen autodiffs implement a confusing subset of operators and conversions that makes a strictly generic approach too confusing and unreadable.
Note that it is up to the caller to ensure that all of the TaylorVarXds in var_values correctly correspond to one another, because Polynomial has no knowledge of what partial derivative terms the indices of a given TaylorVarXd correspond to.
Polynomial evaluatePartial | ( | const std::map< VarType, CoefficientType > & | var_values | ) | const |
Substitute values for some but not necessarily all variables of a polynomial.
Analogous to evaluateMultivariate, but: (1) Restricted to CoefficientType, 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.
|
inline |
Evaluate a univariate Polynomial at a specific point.
Evaluates a univariate Polynomial at the given x. Throws an exception of this Polynomial is not univariate.
x may be of any type supporting the ** and + operations (which can be different from both CoefficientsType and RealScalar)
Eigen::Matrix<CoefficientType, Eigen::Dynamic, 1> getCoefficients | ( | ) | const |
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.
const std::vector<Monomial>& getMonomials | ( | ) | const |
int getNumberOfCoefficients | ( | ) | const |
Returns the number of unique Monomials (and thus the number of coefficients) in this Polynomial.
VarType getSimpleVariable | ( | ) | const |
If the polynomial is "simple" – e.g.
just a single term with coefficient 1 – then returns that variable; otherwise returns 0.
std::set<VarType> getVariables | ( | ) | const |
Returns a set of all of the variables present in this Polynomial.
Polynomial integral | ( | const CoefficientType & | 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. Throws an exception of 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.
bool isAffine | ( | ) | const |
Returns true iff this is a sum of terms of degree 1, plus a constant.
bool isApprox | ( | const Polynomial< _CoefficientType > & | other, |
const RealScalar & | tol | ||
) | const |
Checks if a (univariate) Polynomial is approximately equal to this one.
Checks that every coefficient of other is within tol of the corresponding coefficient of this Polynomial. Throws an exception if either Polynomial is not univariate.
const Polynomial operator* | ( | const Polynomial< _CoefficientType > & | other | ) | const |
Polynomial& operator*= | ( | const Polynomial< _CoefficientType > & | other | ) |
Polynomial& operator*= | ( | const CoefficientType & | scalar | ) |
const Polynomial operator+ | ( | const Polynomial< _CoefficientType > & | other | ) | const |
Polynomial& operator+= | ( | const Polynomial< _CoefficientType > & | other | ) |
Polynomial& operator+= | ( | const CoefficientType & | scalar | ) |
const Polynomial operator- | ( | const Polynomial< _CoefficientType > & | other | ) | const |
const Polynomial operator- | ( | ) | const |
Polynomial& operator-= | ( | const Polynomial< _CoefficientType > & | other | ) |
Polynomial& operator-= | ( | const CoefficientType & | scalar | ) |
const Polynomial operator/ | ( | const CoefficientType & | scalar | ) | const |
Polynomial& operator/= | ( | const CoefficientType & | scalar | ) |
|
inline |
A comparison to allow std::lexicographical_compare on this class; does not reflect any sort of mathematical total order.
bool operator== | ( | const Polynomial< _CoefficientType > & | other | ) | const |
|
inlinestatic |
Obtains a matrix of random unvariate Polynomials of the specified size.
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. Throws an exception of this Polynomial is not univariate.
Replaces all instances of variable orig with replacement.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |