Public Types | Public Member Functions | Public Attributes | Protected Types

TRTK::TrivariateQuadraticPolynomial< T > Class Template Reference

Trivariate quadratic polynomial. More...

#include <TrivariateQuadraticPolynomial.hpp>

Inheritance diagram for TRTK::TrivariateQuadraticPolynomial< T >:
Collaboration diagram for TRTK::TrivariateQuadraticPolynomial< 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

 TrivariateQuadraticPolynomial ()
 Constructs an instance of TrivariateQuadraticPolynomial.
 TrivariateQuadraticPolynomial (const MatrixXT &)
 Constructs an instance of TrivariateQuadraticPolynomial.
 TrivariateQuadraticPolynomial (const TrivariateQuadraticPolynomial &)
 Copy constructor.
template<class U >
 TrivariateQuadraticPolynomial (const TrivariateQuadraticPolynomial< U > &)
 Copy constructor.
virtual ~TrivariateQuadraticPolynomial ()
 Destroys the instance of TrivariateQuadraticPolynomial.
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 a trivariate quadratic 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 a trivariate quadratic 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.

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

Detailed Description

template<class T>
class TRTK::TrivariateQuadraticPolynomial< T >

Trivariate quadratic polynomial.

This class provides means to estimate and compute a trivariate polynomial of degree 2.

The transformation is computed as follows:

\[ \begin{pmatrix} x' \\ y' \\ z' \end{pmatrix} = f(x, y ,z) = \begin{pmatrix} a_{11} + a_{12} x + a_{13} y + \cdots + a_{18} y^2 + a_{19} yz + a_{1,10} z^2 \\ a_{21} + a_{22} x + a_{23} y + \cdots + a_{28} y^2 + a_{29} yz + a_{2,10} z^2 \\ a_{31} + a_{32} x + a_{33} y + \cdots + a_{38} y^2 + a_{39} yz + a_{3,10} z^2 \\ \end{pmatrix} \]

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 TrivariateQuadraticPolynomial class:

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

 #include <TRTK/Tools.hpp>
 #include <TRTK/TrivariateQuadraticPolynomial.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.

     TrivariateQuadraticPolynomial<double> polynomial;

     // 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.2.0
Date:
last changed on 2013-03-28

Definition at line 210 of file TrivariateQuadraticPolynomial.hpp.


Constructor & Destructor Documentation

Constructs an instance of TrivariateQuadraticPolynomial.

Template Parameters:
Tscalar type

The transformation is initialized to be the identity function.

See also:
reset()

Definition at line 283 of file TrivariateQuadraticPolynomial.hpp.

template<class T >
TRTK::TrivariateQuadraticPolynomial< T >::TrivariateQuadraticPolynomial ( const MatrixXT &  matrix )

Constructs an instance of TrivariateQuadraticPolynomial.

Template Parameters:
Tscalar type
Parameters:
[in]matrixA 3x10 matrix containing the coefficients.

Sets the internal coefficients to the given matrix.

See also:
reset()

Definition at line 304 of file TrivariateQuadraticPolynomial.hpp.

Copy constructor.

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

Definition at line 319 of file TrivariateQuadraticPolynomial.hpp.

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

Copy constructor.

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

Definition at line 335 of file TrivariateQuadraticPolynomial.hpp.

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

Destroys the instance of TrivariateQuadraticPolynomial.

Template Parameters:
Tscalar type

Definition at line 347 of file TrivariateQuadraticPolynomial.hpp.


Member Function Documentation

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

Estimates a trivariate quadratic 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 401 of file TrivariateQuadraticPolynomial.hpp.

template<class T>
T TRTK::TrivariateQuadraticPolynomial< 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 a trivariate quadratic 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 433 of file TrivariateQuadraticPolynomial.hpp.

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

Returns the internal coefficients.

Template Parameters:
Tscalar type
Returns:
Returns the internal coefficients in form of a 3x10 matrix representation.

Implements TRTK::Polynomial< T >.

Definition at line 374 of file TrivariateQuadraticPolynomial.hpp.

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

Returns the internal coefficients.

Template Parameters:
Tscalar type
Returns:
Returns the internal coefficients in form of a 3x10 matrix representation.

Implements TRTK::Polynomial< T >.

Definition at line 360 of file TrivariateQuadraticPolynomial.hpp.

template<class T >
TrivariateQuadraticPolynomial< T >::Point TRTK::TrivariateQuadraticPolynomial< 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.

The given point must be of size three.

Implements TRTK::Polynomial< T >.

Definition at line 631 of file TrivariateQuadraticPolynomial.hpp.

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

Resets the transformation.

Template Parameters:
Tscalar type

Sets the internal coefficients such that the transformation is the identity function.

Returns:
*this

Implements TRTK::Polynomial< T >.

Definition at line 650 of file TrivariateQuadraticPolynomial.hpp.

template<class T >
TrivariateQuadraticPolynomial< T >::Point TRTK::TrivariateQuadraticPolynomial< 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.

The given point must be of size two.

Implements TRTK::Polynomial< T >.

Definition at line 672 of file TrivariateQuadraticPolynomial.hpp.


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