RobWorkProject  23.9.11-
Public Member Functions | Friends | Related Functions | List of all members
Polynomial< T > Class Template Reference

Representation of an ordinary polynomial with scalar coefficients (that can be both real and complex). More...

#include <Polynomial.hpp>

Inherits PolynomialND< double, double >.

Public Member Functions

 Polynomial (std::size_t order)
 Create polynomial with coefficients initialized to zero. More...
 
 Polynomial (const std::vector< T > &coefficients)
 Create polynomial from vector. More...
 
 Polynomial (const Polynomial< T > &p)
 Create polynomial from other polynomial. More...
 
 Polynomial (const PolynomialND< T, T > &p)
 Create polynomial from other polynomial. More...
 
virtual ~Polynomial ()
 Destructor.
 
evaluate (const T &x) const
 Evaluate the polynomial using Horner's Method. More...
 
template<typename ErrT = T>
evaluate (const T &x, ErrT &err) const
 Evaluate the polynomial using Horner's Method. More...
 
std::vector< T > evaluateDerivatives (const T &x, std::size_t n=1) const
 Evaluate the first n derivatives of the polynomial using Horner's Method. More...
 
template<typename ErrT = T>
std::vector< T > evaluateDerivatives (const T &x, std::vector< ErrT > &err, std::size_t n=1) const
 Evaluate the first n derivatives of the polynomial using Horner's Method. More...
 
Polynomial< T > deflate (const T &x) const
 Perform deflation of polynomial. More...
 
Polynomial< T > derivative (std::size_t n=1) const
 Get the derivative polynomial. More...
 
const Polynomial< T > operator- () const
 Negate coefficients. More...
 
bool operator== (const Polynomial< T > &b) const
 Check if polynomials are equal. More...
 
template<class Q >
const Polynomial< Qcast ()
 Cast to other type. More...
 
Arithmetic operators between polynomials.

Operators used to do arithmetic between two polynomials.

const Polynomial< T > operator- (const Polynomial< T > &b) const
 Polynomial subtraction. More...
 
Polynomial< T > & operator-= (const Polynomial< T > &b)
 Polynomial subtraction. More...
 
const Polynomial< T > operator+ (const Polynomial< T > &b) const
 Polynomial addition. More...
 
Polynomial< T > & operator+= (const Polynomial< T > &b)
 Polynomial addition. More...
 
void operator= (const Polynomial< T > &b)
 Assignment. More...
 
- Public Member Functions inherited from PolynomialND< double, double >
 PolynomialND (std::size_t order)
 Create polynomial with uninitialized coefficients. More...
 
 PolynomialND (const std::vector< double > &coefficients)
 Create polynomial from vector. More...
 
 PolynomialND (const PolynomialND< double, double > &p)
 Create polynomial from other polynomial. More...
 
virtual ~PolynomialND ()
 Destructor.
 
std::size_t order () const
 Get the order of the polynomial (the highest power). More...
 
void increaseOrder (std::size_t increase, const double &value)
 Increase the order of this polynomial. More...
 
void increaseOrder (std::size_t increase=1)
 Increase the order of this polynomial. More...
 
double evaluate (const double &x) const
 Evaluate the polynomial using Horner's Method. More...
 
std::vector< double > evaluateDerivatives (const double &x, std::size_t n=1) const
 Evaluate the first n derivatives of the polynomial using Horner's Method. More...
 
PolynomialND< double, double > deflate (const double &x) const
 Perform deflation of polynomial. More...
 
PolynomialND< double, double > derivative (std::size_t n=1) const
 Get the derivative polynomial. More...
 
const PolynomialND< double, double > operator- () const
 Negate coefficients. More...
 
bool operator== (const PolynomialND< double, double > &b) const
 Check if polynomials are equal. More...
 
const double & operator() (std::size_t i) const
 Get specific coefficient. More...
 
double & operator() (size_t i)
 Get specific coefficient. More...
 
const double & operator[] (size_t i) const
 Get specific coefficient. More...
 
double & operator[] (size_t i)
 Get specific coefficient. More...
 
const PolynomialND< double, double > operator- (const PolynomialND< double, double > &b) const
 Polynomial subtraction. More...
 
PolynomialND< double, double > & operator-= (const PolynomialND< double, double > &b)
 Polynomial subtraction. More...
 
const PolynomialND< double, double > operator+ (const PolynomialND< double, double > &b) const
 Polynomial addition. More...
 
PolynomialND< double, double > & operator+= (const PolynomialND< double, double > &b)
 Polynomial addition. More...
 
PolynomialND< OutCoef, double > multiply (const PolynomialND< Coef2, double > &b) const
 Polynomial multiplication. More...
 
PolynomialND< OutCoef, double > multiply (const Coef2 &b) const
 Multiply with a coefficient. More...
 
void operator= (const PolynomialND< double, double > &b)
 Assignment. More...
 
const PolynomialND< double, double > operator* (double s) const
 Scalar multiplication. More...
 
const PolynomialND< double, double > operator/ (double s) const
 Scalar division. More...
 
PolynomialND< double, double > & operator*= (double s)
 Scalar multiplication. More...
 
PolynomialND< double, double > & operator/= (double s)
 Scalar division. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const Polynomial< T > &p)
 Printing polynomial to stream. More...
 

Related Functions

(Note that these are not member functions.)

template<>
void write (const rw::math::Polynomial< double > &sobject, rw::common::OutputArchive &oarchive, const std::string &id)
 
template<>
void write (const rw::math::Polynomial< float > &sobject, rw::common::OutputArchive &oarchive, const std::string &id)
 
template<>
void read (rw::math::Polynomial< double > &sobject, rw::common::InputArchive &iarchive, const std::string &id)
 
template<>
void read (rw::math::Polynomial< float > &sobject, rw::common::InputArchive &iarchive, const std::string &id)
 

Arithmetic operators between polynomial and scalars.

Operators used to do arithmetic with scalars.

const Polynomial< T > operator+ (T s) const
 Scalar addition. More...
 
const Polynomial< T > operator- (T s) const
 Scalar subtraction. More...
 
const Polynomial< T > operator* (T s) const
 Scalar multiplication. More...
 
const Polynomial< T > operator* (const Polynomial< T > &polynomial) const
 Polynomial multiplication. More...
 
PolynomialND< Eigen::Matrix< T, 3, 1 >, T > operator* (const PolynomialND< Eigen::Matrix< T, 3, 1 >, T > &polynomial) const
 Multiply polynomial with scalar coefficients with a 3D polynomial vector. More...
 
PolynomialND< Eigen::Matrix< T, 1, 3 >, T > operator* (const PolynomialND< Eigen::Matrix< T, 1, 3 >, T > &polynomial) const
 Multiply polynomial with scalar coefficients with a 3D polynomial vector. More...
 
PolynomialND< Eigen::Matrix< T, 3, 3 >, T > operator* (const PolynomialND< Eigen::Matrix< T, 3, 3 >, T > &polynomial) const
 Multiply polynomial with scalar coefficients with a 3D polynomial matrix. More...
 
PolynomialND< Eigen::Matrix< T, 3, 1 >, T > operator* (const Eigen::Matrix< T, 3, 1 > &a) const
 Multiply polynomial with scalar coefficients with a vector. More...
 
PolynomialND< Eigen::Matrix< T, 1, 3 >, T > operator* (const Eigen::Matrix< T, 1, 3 > &a) const
 Multiply polynomial with scalar coefficients with a vector. More...
 
PolynomialND< Eigen::Matrix< T, 3, 3 >, T > operator* (const Eigen::Matrix< T, 3, 3 > &A) const
 Multiply polynomial with scalar coefficients with a matrix. More...
 
const Polynomial< T > operator/ (T s) const
 Scalar division. More...
 
Polynomial< T > & operator+= (T s)
 Scalar addition. More...
 
Polynomial< T > & operator-= (T s)
 Scalar subtraction. More...
 
Polynomial< T > & operator*= (T s)
 Scalar multiplication. More...
 
Polynomial< T > & operator/= (T s)
 Scalar division. More...
 
const Polynomial< T > operator* (T s, const Polynomial< T > &p)
 Scalar multiplication. More...
 

Additional Inherited Members

- Protected Attributes inherited from PolynomialND< double, double >
std::vector< double > _coef
 The coefficient vector.
 

Detailed Description

template<typename T = double>
class rw::math::Polynomial< T >

Representation of an ordinary polynomial with scalar coefficients (that can be both real and complex).

Representation of a polynomial of the following form:

\( f(x) = c_n x^n + c_(n-1) x^(n-1) + c_2 x^2 + c_1 x + c_0 \)

The polynomial is represented as a list of coefficients ordered from lowest-order term to highest-order term, \({c_0,c_1,...,c_n}\).

Constructor & Destructor Documentation

◆ Polynomial() [1/4]

Polynomial ( std::size_t  order)
inlineexplicit

Create polynomial with coefficients initialized to zero.

Parameters
order[in] the order of the polynomial.

◆ Polynomial() [2/4]

Polynomial ( const std::vector< T > &  coefficients)
inline

Create polynomial from vector.

Parameters
coefficients[in] the coefficients ordered from lowest-order term to highest-order term.

◆ Polynomial() [3/4]

Polynomial ( const Polynomial< T > &  p)
inline

Create polynomial from other polynomial.

Parameters
p[in] the polynomial to copy.

◆ Polynomial() [4/4]

Polynomial ( const PolynomialND< T, T > &  p)
inline

Create polynomial from other polynomial.

Parameters
p[in] the polynomial to copy.

Member Function Documentation

◆ cast()

const Polynomial<Q> cast ( )
inline

Cast to other type.

Returns
a new polynomial after cast to new type.

◆ deflate()

Polynomial<T> deflate ( const T &  x) const
inline

Perform deflation of polynomial.

Parameters
x[in] a root of the polynomial.
Returns
a new polynomial of same order minus one.
Note
There is no check that the given root is in fact a root of the polynomial.

◆ derivative()

Polynomial<T> derivative ( std::size_t  n = 1) const
inline

Get the derivative polynomial.

Parameters
n[in] gives the n'th derivative (default is n=1).
Returns
a new polynomial of same order minus one.
Note
To evaluate derivatives use the evaluate derivative method which is more precise.

◆ evaluate() [1/2]

T evaluate ( const T &  x) const
inline

Evaluate the polynomial using Horner's Method.

Parameters
x[in] the input parameter.
Returns
the value \( f(x)\).

◆ evaluate() [2/2]

T evaluate ( const T &  x,
ErrT &  err 
) const
inline

Evaluate the polynomial using Horner's Method.

This function estimates the error of the result. For float or double types, the error type, ErrT, should be the same as the type T. For std::complex<float> or std::complex<double> types, the error type, ErrT, should be either float or double.

Parameters
x[in] the input parameter.
err[out] estimate of the absolute error.
Returns
the value \(f(x)\).
Note
Error is the absolute size of the interval where \(f(x)\) can be, assuming coefficients are exact.

◆ evaluateDerivatives() [1/2]

std::vector<T> evaluateDerivatives ( const T &  x,
std::size_t  n = 1 
) const
inline

Evaluate the first n derivatives of the polynomial using Horner's Method.

Parameters
x[in] the input parameter.
n[in] the number of derivatives to find (default is the first derivative only)
Returns
a vector of values \( {f(x),\dot{f}(x),\ddot{f}(x),\cdots}\).

◆ evaluateDerivatives() [2/2]

std::vector<T> evaluateDerivatives ( const T &  x,
std::vector< ErrT > &  err,
std::size_t  n = 1 
) const
inline

Evaluate the first n derivatives of the polynomial using Horner's Method.

This function estimates the error of the result. For float or double types, the error type, ErrT, should be the same as the type T. For std::complex<float> or std::complex<double> types, the error type, ErrT, should be either float or double.

Parameters
x[in] the input parameter.
err[out] estimate of the absolute errors.
n[in] the number of derivatives to find (default is the first derivative only)
Returns
the value \(\dot{f}(x)\).
Note
Error is the absolute size of the interval where \(f(x)\) can be, assuming coefficients are exact.

◆ operator*() [1/8]

PolynomialND<Eigen::Matrix<T, 1, 3>, T> operator* ( const Eigen::Matrix< T, 1, 3 > &  a) const
inline

Multiply polynomial with scalar coefficients with a vector.

Parameters
a[in] vector to multiply with.
Returns
a 3D polynomial vector.

◆ operator*() [2/8]

PolynomialND<Eigen::Matrix<T, 3, 1>, T> operator* ( const Eigen::Matrix< T, 3, 1 > &  a) const
inline

Multiply polynomial with scalar coefficients with a vector.

Parameters
a[in] vector to multiply with.
Returns
a 3D polynomial vector.

◆ operator*() [3/8]

PolynomialND<Eigen::Matrix<T, 3, 3>, T> operator* ( const Eigen::Matrix< T, 3, 3 > &  A) const
inline

Multiply polynomial with scalar coefficients with a matrix.

Parameters
A[in] matrix to multiply with.
Returns
a 3D polynomial matrix.

◆ operator*() [4/8]

const Polynomial<T> operator* ( const Polynomial< T > &  polynomial) const
inline

Polynomial multiplication.

This multiplication functions uses a convolution of the coefficients. More efficient implementations are possible.

Parameters
polynomial[in] polynomial to multiply with.
Returns
new polynomial after multiplication.

◆ operator*() [5/8]

PolynomialND<Eigen::Matrix<T, 1, 3>, T> operator* ( const PolynomialND< Eigen::Matrix< T, 1, 3 >, T > &  polynomial) const
inline

Multiply polynomial with scalar coefficients with a 3D polynomial vector.

Parameters
polynomial[in] polynomial vector.
Returns
a 3D polynomial vector.

◆ operator*() [6/8]

PolynomialND<Eigen::Matrix<T, 3, 1>, T> operator* ( const PolynomialND< Eigen::Matrix< T, 3, 1 >, T > &  polynomial) const
inline

Multiply polynomial with scalar coefficients with a 3D polynomial vector.

Parameters
polynomial[in] polynomial vector.
Returns
a 3D polynomial vector.

◆ operator*() [7/8]

PolynomialND<Eigen::Matrix<T, 3, 3>, T> operator* ( const PolynomialND< Eigen::Matrix< T, 3, 3 >, T > &  polynomial) const
inline

Multiply polynomial with scalar coefficients with a 3D polynomial matrix.

Parameters
polynomial[in] polynomial matrix.
Returns
a 3D polynomial matrix.

◆ operator*() [8/8]

const Polynomial<T> operator* ( s) const
inline

Scalar multiplication.

Parameters
s[in] scalar to multiply with.
Returns
new polynomial after multiplication.

◆ operator*=()

Polynomial<T>& operator*= ( s)
inline

Scalar multiplication.

Parameters
s[in] the scalar to multiply with.
Returns
reference to same polynomial with changed coefficients.

◆ operator+() [1/2]

const Polynomial<T> operator+ ( const Polynomial< T > &  b) const
inline

Polynomial addition.

Parameters
b[in] polynomial to add.
Returns
new polynomial after addition.

◆ operator+() [2/2]

const Polynomial<T> operator+ ( s) const
inline

Scalar addition.

Parameters
s[in] scalar to add.
Returns
new polynomial after addition.

◆ operator+=() [1/2]

Polynomial<T>& operator+= ( const Polynomial< T > &  b)
inline

Polynomial addition.

Parameters
b[in] polynomial to add.
Returns
same polynomial with different coefficients after addition.

◆ operator+=() [2/2]

Polynomial<T>& operator+= ( s)
inline

Scalar addition.

Parameters
s[in] scalar to add.
Returns
same polynomial with coefficients changed.

◆ operator-() [1/3]

const Polynomial<T> operator- ( ) const
inline

Negate coefficients.

Returns
new polynomial with coefficients negated.

◆ operator-() [2/3]

const Polynomial<T> operator- ( const Polynomial< T > &  b) const
inline

Polynomial subtraction.

Parameters
b[in] polynomial of to subtract.
Returns
new polynomial after subtraction.

◆ operator-() [3/3]

const Polynomial<T> operator- ( s) const
inline

Scalar subtraction.

Parameters
s[in] scalar to subtract.
Returns
new polynomial after subtraction.

◆ operator-=() [1/2]

Polynomial<T>& operator-= ( const Polynomial< T > &  b)
inline

Polynomial subtraction.

Parameters
b[in] polynomial to subtract.
Returns
same polynomial with different coefficients after subtraction.

◆ operator-=() [2/2]

Polynomial<T>& operator-= ( s)
inline

Scalar subtraction.

Parameters
s[in] scalar to subtract.
Returns
same polynomial with coefficients changed.

◆ operator/()

const Polynomial<T> operator/ ( s) const
inline

Scalar division.

Parameters
s[in] scalar to divide with.
Returns
new polynomial after division.

◆ operator/=()

Polynomial<T>& operator/= ( s)
inline

Scalar division.

Parameters
s[in] the scalar to divide with.
Returns
reference to same polynomial with changed coefficients.

◆ operator=()

void operator= ( const Polynomial< T > &  b)
inline

Assignment.

Parameters
b[in] the polynomial to take coefficients from.
Returns
true if equal, false if not.

◆ operator==()

bool operator== ( const Polynomial< T > &  b) const
inline

Check if polynomials are equal.

Parameters
b[in] the polynomial to compare with.
Returns
true if equal, false if not.

Friends And Related Function Documentation

◆ operator*

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

Scalar multiplication.

Parameters
s[in] scalar to multiply with.
p[in] polynomial to multiply with.
Returns
new polynomial after multiplication.

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const Polynomial< T > &  p 
)
friend

Printing polynomial to stream.

Parameters
out[in/out] the stream to write to.
p[in] the polynomail to print.
Returns
the same ostream as out parameter.

◆ read() [1/2]

void read ( rw::math::Polynomial< double > &  sobject,
rw::common::InputArchive iarchive,
const std::string &  id 
)
related

Enable read-serialization of class T by overloading this method. Data is read from iarchive and filled into sobject.

Parameters
sobject[out] the object in which the data should be streamed into
iarchive[in] the InputArchive from which to read data.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

◆ read() [2/2]

void read ( rw::math::Polynomial< float > &  sobject,
rw::common::InputArchive iarchive,
const std::string &  id 
)
related

Enable read-serialization of class T by overloading this method. Data is read from iarchive and filled into sobject.

Parameters
sobject[out] the object in which the data should be streamed into
iarchive[in] the InputArchive from which to read data.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

◆ write() [1/2]

void write ( const rw::math::Polynomial< double > &  sobject,
rw::common::OutputArchive oarchive,
const std::string &  id 
)
related

Enable write-serialization of class T by overloading this method. Data is written to oarchive from the sobject.

Parameters
sobject[in] the object from which the data should be streamed.
oarchive[out] the OutputArchive in which data should be written.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

◆ write() [2/2]

void write ( const rw::math::Polynomial< float > &  sobject,
rw::common::OutputArchive oarchive,
const std::string &  id 
)
related

Enable write-serialization of class T by overloading this method. Data is written to oarchive from the sobject.

Parameters
sobject[in] the object from which the data should be streamed.
oarchive[out] the OutputArchive in which data should be written.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

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