Trivariate quadratic polynomial. More...
#include <TrivariateQuadraticPolynomial.hpp>
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. | |
T | estimate (Range< Point > source_points, Range< Point > target_points, Range< T > weights=Range< T >()) |
Estimates a trivariate quadratic function from two point sets. | |
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 >()) |
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 |
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
.
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
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.
Definition at line 210 of file TrivariateQuadraticPolynomial.hpp.
TRTK::TrivariateQuadraticPolynomial< T >::TrivariateQuadraticPolynomial | ( | ) |
Constructs an instance of TrivariateQuadraticPolynomial.
T | scalar type |
The transformation is initialized to be the identity function.
Definition at line 283 of file TrivariateQuadraticPolynomial.hpp.
TRTK::TrivariateQuadraticPolynomial< T >::TrivariateQuadraticPolynomial | ( | const MatrixXT & | matrix ) |
Constructs an instance of TrivariateQuadraticPolynomial.
T | scalar type |
[in] | matrix | A 3x10 matrix containing the coefficients. |
Sets the internal coefficients to the given matrix
.
Definition at line 304 of file TrivariateQuadraticPolynomial.hpp.
TRTK::TrivariateQuadraticPolynomial< T >::TrivariateQuadraticPolynomial | ( | const TrivariateQuadraticPolynomial< T > & | other ) |
Copy constructor.
T | scalar type of the newly created instance |
Definition at line 319 of file TrivariateQuadraticPolynomial.hpp.
TRTK::TrivariateQuadraticPolynomial< T >::TrivariateQuadraticPolynomial | ( | const TrivariateQuadraticPolynomial< U > & | other ) |
Copy constructor.
T | scalar type of the newly created instance |
U | scalar type of the copied instance |
Definition at line 335 of file TrivariateQuadraticPolynomial.hpp.
TRTK::TrivariateQuadraticPolynomial< T >::~TrivariateQuadraticPolynomial | ( | ) | [virtual] |
Destroys the instance of TrivariateQuadraticPolynomial.
T | scalar type |
Definition at line 347 of file TrivariateQuadraticPolynomial.hpp.
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.
T | scalar 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.
ErrorObj | An error object is thrown if the point sets differ in their sizes. |
ErrorObj | An 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.
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.
T | scalar 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.
ErrorObj | An error object is thrown if the point sets differ in their sizes. |
ErrorObj | An 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.
const TrivariateQuadraticPolynomial< T >::MatrixXT & TRTK::TrivariateQuadraticPolynomial< T >::getCoefficients | ( | ) | const [virtual] |
Returns the internal coefficients.
T | scalar type |
Implements TRTK::Polynomial< T >.
Definition at line 374 of file TrivariateQuadraticPolynomial.hpp.
TrivariateQuadraticPolynomial< T >::MatrixXT & TRTK::TrivariateQuadraticPolynomial< T >::getCoefficients | ( | ) | [virtual] |
Returns the internal coefficients.
T | scalar type |
Implements TRTK::Polynomial< T >.
Definition at line 360 of file TrivariateQuadraticPolynomial.hpp.
TrivariateQuadraticPolynomial< T >::Point TRTK::TrivariateQuadraticPolynomial< T >::operator* | ( | const Point & | point ) | const [virtual] |
Transforms a point with the internally saved transformation.
T | scalar type |
[in] | point | Point to be transformed. |
The given point
must be of size three.
Implements TRTK::Polynomial< T >.
Definition at line 631 of file TrivariateQuadraticPolynomial.hpp.
Polynomial< T > & TRTK::TrivariateQuadraticPolynomial< T >::reset | ( | ) | [virtual] |
Resets the transformation.
T | scalar type |
Sets the internal coefficients such that the transformation is the identity function.
*this
Implements TRTK::Polynomial< T >.
Definition at line 650 of file TrivariateQuadraticPolynomial.hpp.
TrivariateQuadraticPolynomial< T >::Point TRTK::TrivariateQuadraticPolynomial< T >::transform | ( | const Point & | point ) | const [virtual] |
Transforms a point with the internally saved transformation.
T | scalar type |
[in] | point | Point to be transformed. |
The given point
must be of size two.
Implements TRTK::Polynomial< T >.
Definition at line 672 of file TrivariateQuadraticPolynomial.hpp.
Documentation generated by Doxygen