Public Types | Public Member Functions | Public Attributes | Protected Types | Friends | Related Functions

TRTK::GenericPolynomial< T > Class Template Reference

Multivariate multidimensional polynomial. More...

#include <GenericPolynomial.hpp>

Inheritance diagram for TRTK::GenericPolynomial< T >:
Collaboration diagram for TRTK::GenericPolynomial< T >:

List of all members.

Public Types

typedef super::MatrixXT MatrixXT
typedef super::Point Point
enum  Error {
  DIVISION_BY_ZERO, INVALID_ARGUMENT, NOT_ENOUGH_POINTS, UNEQUAL_NUMBER_OF_POINTS,
  UNKNOWN_ERROR, WRONG_COORDINATE_SIZE
}

Public Member Functions

 GenericPolynomial (unsigned n, unsigned m, unsigned r)
 Constructs an instance of GenericPolynomial.
 GenericPolynomial (const MatrixXT &, unsigned n, unsigned m, unsigned r)
 Constructs an instance of GenericPolynomial.
 GenericPolynomial (const GenericPolynomial &)
 Copy constructor.
template<class U >
 GenericPolynomial (const GenericPolynomial< U > &, unsigned n, unsigned m, unsigned r)
 Copy constructor.
virtual ~GenericPolynomial ()
 Destroys the instance of GenericPolynomial.
MatrixXT & getCoefficients ()
 Returns the internal coefficients.
const MatrixXT & getCoefficients () const
 Returns the internal coefficients.
estimate (Range< Point > source_points, Range< Point > target_points, Range< T > weights=Range< T >())
 Estimates the polynomial function from two point sets.
estimate (Iterator< Point > source_points_first, Iterator< Point > source_points_last, Iterator< Point > target_points_first, Iterator< Point > target_points_last, Iterator< T > weights_first=Iterator< T >(), Iterator< T > weights_last=Iterator< T >())
 Estimates the polynomial function from two point sets.
Point operator* (const Point &) const
 Transforms a point with the internally saved transformation.
Point transform (const Point &) const
 Transforms a point with the internally saved transformation.
Polynomial< T > & reset ()
 Resets the transformation.
void setDegree (unsigned degree)

Public Attributes

EIGEN_MAKE_ALIGNED_OPERATOR_NEW
typedef T 
value_type

Protected Types

typedef super::VectorXT VectorXT
typedef super::Vector3T Vector3T
typedef super::coordinate_type coordinate_type
typedef Eigen::Matrix< T, 2, 1 > Vector2T

Friends

void computeMonomials (unsigned, unsigned, unsigned, Iterator< Point >, std::vector< VectorXT > &)

Related Functions

(Note that these are not member functions.)


unsigned numberMonomials (unsigned n, unsigned r)
template<class T >
void computeMonomials (unsigned n, unsigned r, unsigned number_points, Iterator< typename GenericPolynomial< T >::Point > it_source_points, std::vector< Eigen::Matrix< T, Eigen::Dynamic, 1 > > &monomials)

Detailed Description

template<class T>
class TRTK::GenericPolynomial< T >

Multivariate multidimensional polynomial.

This class provides means to estimate and compute a multivariate multidimensional polynomial of arbitrary degree.

The transformation is computed as follows (here, shown for a polynomial of degree two in three variables):

\[ \begin{pmatrix} x' \\ y' \\ z' \end{pmatrix} = f(x, y ,z) = \begin{pmatrix} a_{1,1} + a_{1,2} x + a_{1,3} y + \cdots + a_{1,8} y^2 + a_{1,9} yz + a_{1,10} z^2 \\ a_{2,1} + a_{2,2} x + a_{2,3} y + \cdots + a_{2,8} y^2 + a_{2,9} yz + a_{2,10} z^2 \\ a_{3,1} + a_{3,2} x + a_{3,3} y + \cdots + a_{3,8} y^2 + a_{3,9} yz + a_{3,10} z^2 \\ \end{pmatrix} \qquad f : \mathbb{R}^n \rightarrow \mathbb{R}^m \quad n = 3, \, m = 3, \, r = 2 \]

The number \( N \) of estimated coefficients is \( N = m \sum_{i=0}^{r} \binom{n + i - 1}{i} \) where \( n \) is the number of input variables, \( m \) is the dimension of the polynomial, and \( r \) is the degree of the polynomial. You will need at least as much as \( N / m \) point pairs to estimate the polynomial, otherwise the internal computation might suffer from a rank deficient matrix.

Most functions check for certain assertions (e.g., if the coordinate size is valid) and trigger an assertion failure if the assumption does not hold. This is meant for debugging purposes only and can be disabled by defining the macro NDEBUG.

Examples

Here is an example of how to use the GenericPolynomial class:

 #include <iomanip>
 #include <iostream>
 #include <vector>

 #include <TRTK/Tools.hpp>
 #include <TRTK/GenericPolynomial.hpp>

 using namespace std;
 using namespace TRTK;
 using namespace TRTK::Tools;

 typedef TRTK::Coordinate<double> Point;

 Point f(const Point & point)
 {
     double x = point.x();
     double y = point.y();
     double z = point.z();

     return Point(-x, x + 20 * y, z + 5 * x * z);
 };

 int main()
 {
     // Generate two point sets that relate to each other via the
     // trivariate quadratic polynomial function f.

     vector<Point> source_points;
     vector<Point> target_points;

     for (unsigned i = 0; i < 100; ++i)
     {
         Point source_point(0, 0, 0);

         source_point.x() = rand(-10.0, 10.0);
         source_point.y() = rand(-10.0, 10.0);
         source_point.z() = rand(-10.0, 10.0);

         Point target_point = f(source_point);

         // Add noise.

         target_point.x() += 0.1 * randn();
         target_point.y() += 0.1 * randn();
         target_point.z() += 0.1 * randn();

         source_points.push_back(source_point);
         target_points.push_back(target_point);
     }

     // Estimate the polynomial function f from the above two sets.

     GenericPolynomial<double> polynomial(3, 3, 2);

     // double rmse = polynomial.estimate(make_iterator(source_points.begin()),
     //                                   make_iterator(source_points.end()),
     //                                   make_iterator(target_points.begin()),
     //                                   make_iterator(target_points.end()));
     //
     // or simply:

     double rmse = polynomial.estimate(make_range(source_points),
                                       make_range(target_points));

     // Print out the results.

     cout << "Root Mean Square Error: " << rmse << endl;
     cout << "Coefficients: " << endl << setprecision(4) << fixed
          << polynomial.getCoefficients().transpose() << endl;

     return 0;
 }

Output:

 Root Mean Square Error: 0.162183
 Coefficients:
        0.0409        0.0047        0.0017
       -0.9991        0.9957        0.0000
       -0.0019       19.9994        0.0009
       -0.0006       -0.0000        1.0001
       -0.0000        0.0002       -0.0002
       -0.0002       -0.0001       -0.0000
       -0.0005        0.0001        5.0002
       -0.0008       -0.0005        0.0002
        0.0001       -0.0001       -0.0004
        0.0003        0.0001        0.0002
Algorithm

This algorithm estimates a linear model

\[ y_i = x_i^T \beta + \epsilon_i \]

where the parameter vector \( \beta \) contains the sought coefficients and where \( \epsilon_i \) represents the unknown error. The parameter vector is estimated using weighted least squares

\[ \begin{align} \hat\beta &= \arg_{\beta}\min \sum_i w_i (x_i^T \beta - y_i)^2 \\ &= \arg_{\beta}\min || W^{1/2} (A \beta - y) ||^2 \\[0.75em] &= [A^T W A]^{-1} A^T W y \end{align} \]

If no weights are given \( w_i \) is assumed to be equal to 1.

The algorithm returns the (weighted) root-mean-square error RMSE \( = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i (f(x_i) - y_i)^2} \) of the estimation.

Given constant weights \( w_i = w \) the RMSE is proportional to the RMSE of an ordinary least squares estimation ( \( w_i = 1 \)) by a factor of \( w^{-1/2} \). It follows, a smaller RMSE does not necessarily imply a better estimation result. You might want to compare results by scaling the RMSE with \( \bar{w}^{-1/2} \) where \( \bar{w} \) is the mean of all weights.

See also:
Coordinate, Iterator
Author:
Christoph Haenisch
Version:
0.1.0
Date:
last changed on 2013-06-06

Definition at line 426 of file GenericPolynomial.hpp.


Constructor & Destructor Documentation

template<class T >
TRTK::GenericPolynomial< T >::GenericPolynomial ( unsigned  n,
unsigned  m,
unsigned  r 
)

Constructs an instance of GenericPolynomial.

Template Parameters:
Tscalar type
Parameters:
[in]nNumber of input variables (dimension of the domain).
[in]mDimension of the output vector (dimension of the codomain).
[in]rDegree of the polynomial.

The transformation is initialized to be the identity function.

See also:
reset()

Definition at line 512 of file GenericPolynomial.hpp.

template<class T >
TRTK::GenericPolynomial< T >::GenericPolynomial ( const MatrixXT &  matrix,
unsigned  n,
unsigned  m,
unsigned  r 
)

Constructs an instance of GenericPolynomial.

Template Parameters:
Tscalar type
Parameters:
[in]matrixA matrix containing the coefficients.
[in]nNumber of input variables (dimension of the domain).
[in]mDimension of the output vector (dimension of the codomain).
[in]rDegree of the polynomial.

Sets the internal coefficients to the given matrix. The matrix must have m rows and \( \sum_{i=0}^{r} \binom{n + i - 1}{i} \) columns.

See also:
reset()

Definition at line 540 of file GenericPolynomial.hpp.

template<class T >
TRTK::GenericPolynomial< T >::GenericPolynomial ( const GenericPolynomial< T > &  other )

Copy constructor.

Template Parameters:
Tscalar type of the newly created instance
See also:
reset()

Definition at line 560 of file GenericPolynomial.hpp.

template<class T >
template<class U >
TRTK::GenericPolynomial< T >::GenericPolynomial ( const GenericPolynomial< U > &  other,
unsigned  n,
unsigned  m,
unsigned  r 
)

Copy constructor.

Template Parameters:
Tscalar type of the newly created instance
Uscalar type of the copied instance
Parameters:
[in]otherAn instance of GenericPolynomial with differing template parameter.
[in]nNumber of input variables (dimension of the domain).
[in]mDimension of the output vector (dimension of the codomain).
[in]rDegree of the polynomial.
See also:
reset()

Definition at line 584 of file GenericPolynomial.hpp.

template<class T >
TRTK::GenericPolynomial< T >::~GenericPolynomial (  ) [virtual]

Destroys the instance of GenericPolynomial.

Template Parameters:
Tscalar type

Definition at line 599 of file GenericPolynomial.hpp.


Member Function Documentation

template<class T>
T TRTK::GenericPolynomial< T >::estimate ( Range< Point source_points,
Range< Point target_points,
Range< T >  weights = Range<T>() 
) [virtual]

Estimates the polynomial function from two point sets.

Template Parameters:
Tscalar type

The input parameters are ranges [first, last) of a sequence (e.g. a STL container like vector). The last elements are not included in the ranges which coincides with the convention in the STL.

Source and target points as well as weights must correspond to each other. If no weights are given, the weights are assumed to be equal to one.

Returns:
Returns the RMSE \( = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i (f(x_i) - y_i)^2} \) of the estimation.
Exceptions:
ErrorObjAn error object is thrown if the point sets differ in their sizes.
ErrorObjAn error object is thrown if not enough (less than ten) points are given.

Implements TRTK::Polynomial< T >.

Definition at line 649 of file GenericPolynomial.hpp.

template<class T>
T TRTK::GenericPolynomial< T >::estimate ( Iterator< Point source_points_first,
Iterator< Point source_points_last,
Iterator< Point target_points_first,
Iterator< Point target_points_last,
Iterator< T >  weights_first = Iterator<T>(),
Iterator< T >  weights_last = Iterator<T>() 
) [virtual]

Estimates the polynomial function from two point sets.

Template Parameters:
Tscalar type

The input parameters are ranges [first, last) of a sequence (e.g. a STL container like vector). The last elements are not included in the ranges which coincides with the convention in the STL.

Source and target points as well as weights must correspond to each other. If no weights are given, the weights are assumed to be equal to one.

Returns:
Returns the RMSE \( = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i (f(x_i) - y_i)^2} \) of the estimation.
Exceptions:
ErrorObjAn error object is thrown if the point sets differ in their sizes.
ErrorObjAn error object is thrown if not enough (less than ten) points are given.

Implements TRTK::Polynomial< T >.

Definition at line 681 of file GenericPolynomial.hpp.

template<class T >
const GenericPolynomial< T >::MatrixXT & TRTK::GenericPolynomial< T >::getCoefficients (  ) const [virtual]

Returns the internal coefficients.

Template Parameters:
Tscalar type

Implements TRTK::Polynomial< T >.

Definition at line 622 of file GenericPolynomial.hpp.

template<class T >
GenericPolynomial< T >::MatrixXT & TRTK::GenericPolynomial< T >::getCoefficients (  ) [virtual]

Returns the internal coefficients.

Template Parameters:
Tscalar type

Implements TRTK::Polynomial< T >.

Definition at line 610 of file GenericPolynomial.hpp.

template<class T >
GenericPolynomial< T >::Point TRTK::GenericPolynomial< T >::operator* ( const Point point ) const [virtual]

Transforms a point with the internally saved transformation.

Template Parameters:
Tscalar type
Parameters:
[in]pointPoint to be transformed.
Returns:
Transformed point.

Implements TRTK::Polynomial< T >.

Definition at line 864 of file GenericPolynomial.hpp.

template<class T >
Polynomial< T > & TRTK::GenericPolynomial< T >::reset (  ) [virtual]

Resets the transformation.

Template Parameters:
Tscalar type

Sets the internal coefficients such that the transformation is similar to the identity function. That is, the i-th component of the input vector is mapped to the i-th component of the output vector. If the dimension of the codomain is less than the dimension of the domain, the values are dropped. The other way round the output vector is padded with zeros.

Example:

    (1, 2, 3) maps to (1, 2)                (n = 3, m = 2)
    (1, 2, 3) maps to (1, 2, 3)             (n = 3, m = 3)
    (1, 2, 3) maps to (1, 2, 3, 0)          (n = 3, m = 4)
    
Returns:
*this

Implements TRTK::Polynomial< T >.

Definition at line 891 of file GenericPolynomial.hpp.

template<class T >
GenericPolynomial< T >::Point TRTK::GenericPolynomial< T >::transform ( const Point point ) const [virtual]

Transforms a point with the internally saved transformation.

Template Parameters:
Tscalar type
Parameters:
[in]pointPoint to be transformed.
Returns:
Transformed point.

Implements TRTK::Polynomial< T >.

Definition at line 914 of file GenericPolynomial.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines