MRPT logo

utils.h File Reference

#include <mrpt/utils/utils_defs.h>
#include <mrpt/math/CMatrixTemplateNumeric.h>
#include <mrpt/math/CMatrixFixedNumeric.h>
#include <mrpt/math/CVectorTemplate.h>
#include <mrpt/math/vector_ops.h>
#include <mrpt/math/CHistogram.h>
#include <numeric>
#include <cmath>

Go to the source code of this file.

Namespaces

namespace  mrpt
 

This is the global namespace for all Mobile Robot Porgramming Toolkit (MRPT) libraries.


namespace  mrpt::math
 

This base provides a set of functions for maths stuff.


Functions

bool MRPTDLLIMPEXP mrpt::math::loadVector (utils::CFileStream &f, std::vector< int > &d)
 Loads one row of a text file as a numerical std::vector.
bool MRPTDLLIMPEXP mrpt::math::loadVector (utils::CFileStream &f, std::vector< double > &d)
 Loads one row of a text file as a numerical std::vector.
bool MRPTDLLIMPEXP mrpt::math::isNan (float v)
 Returns true if value is Not-a-number (NAN).
bool MRPTDLLIMPEXP mrpt::math::isNan (double v)
 Returns true if value is Not-a-number (NAN).
bool MRPTDLLIMPEXP mrpt::math::isFinite (float v)
 Returns true if value is finite.
bool MRPTDLLIMPEXP mrpt::math::isFinite (double v)
 Returns true if value is finite.
template<class T >
size_t mrpt::math::countNonZero (const std::vector< T > &a)
 A template for counting how many elements in an array are non-Zero.
template<class T >
mrpt::math::maximum (const std::vector< T > &v, unsigned int *maxIndex=NULL)
 Finds the maximum value (and the corresponding zero-based index) from a given vector.
template<class T >
mrpt::math::norm_inf (const std::vector< T > &v, unsigned int *maxIndex=NULL)
 Compute the norm-infinite of a vector ($f[ ||{v}||_ $f]), ie the maximum absolute value of the elements.
template<class T >
mrpt::math::norm (const std::vector< T > &v)
 Compute the 2-norm of the vector (the Euclidean distance to the origin).
template<class T >
mrpt::math::minimum (const std::vector< T > &v, unsigned int *minIndex=NULL)
 Finds the maximum value (and the corresponding zero-based index) from a given vector.
template<class T >
void mrpt::math::minimum_maximum (const std::vector< T > &v, T &out_min, T &out_max, unsigned int *minIndex=NULL, unsigned int *maxIndex=NULL)
 Compute the minimum and maximum of a vector at once.
template<class T >
double mrpt::math::mean (const std::vector< T > &v)
 Computes the mean value of a vector.
template<class T >
mrpt::math::sum (const std::vector< T > &v)
 Computes the sum of all the elements of a vector.
template<typename T , typename K >
void mrpt::math::linspace (T first, T last, size_t count, std::vector< K > &out_vector)
 Generates an equidistant sequence of numbers given the first one, the last one and the desired number of points.
template<class T >
std::vector< T > mrpt::math::linspace (T first, T last, size_t count)
 Generates an equidistant sequence of numbers given the first one, the last one and the desired number of points.
template<class T >
std::vector< T > mrpt::math::ones (size_t count)
 Generates a vector of all ones of the given length.
template<class T >
std::vector< T > mrpt::math::zeros (size_t count)
 Generates a vector of all zeros of the given length.
template<class T >
void mrpt::math::normalize (const std::vector< T > &v, std::vector< T > &out_v)
 Normalize a vector, such as its norm is the unity.
template<class T >
std::vector< T > mrpt::math::cumsum (const std::vector< T > &v)
 Computes the cumulative sum of all the elements of a vector.
template<class T >
void mrpt::math::cumsum (const std::vector< T > &v, std::vector< T > &out_cumsum)
 Computes the cumulative sum of all the elements of a vector, saving the result in a given vector.
template<class T >
double mrpt::math::stddev (const std::vector< T > &v, bool unbiased=true)
 Computes the standard deviation of a vector.
template<class T >
void mrpt::math::meanAndCov (const std::vector< std::vector< T > > &v, vector_double &out_mean, CMatrixDouble &out_cov)
 Computes the mean vector and covariance from a list of values given as a vector of vectors, where each row is a sample.
template<class T >
CMatrixDouble mrpt::math::cov (const std::vector< std::vector< T > > &v)
 Computes the covariance matrix from a list of values given as a vector of vectors, where each row is a sample.
template<class T >
void mrpt::math::meanAndStd (const std::vector< T > &v, double &out_mean, double &out_std, bool unbiased=true)
 Computes the standard deviation of a vector.
template<class T >
void mrpt::math::weightedHistogram (const std::vector< T > &values, const std::vector< T > &weights, float binWidth, std::vector< float > &out_binCenters, std::vector< float > &out_binValues)
 Computes the weighted histogram for a vector of values and their corresponding weights.
uint64_t MRPTDLLIMPEXP mrpt::math::factorial64 (unsigned int n)
 Computes the factorial of an integer number and returns it as a 64-bit integer number.
double MRPTDLLIMPEXP mrpt::math::factorial (unsigned int n)
 Computes the factorial of an integer number and returns it as a double value (internally it uses logarithms for avoiding overflow).
template<class T >
void mrpt::math::wrapTo2PiInPlace (T &a)
 Modifies the given angle to translate it into the [0,2pi[ range.
template<class T >
mrpt::math::wrapTo2Pi (T a)
 Modifies the given angle to translate it into the [0,2pi[ range.
template<class T >
mrpt::math::wrapToPi (T a)
 Modifies the given angle to translate it into the ]-pi,pi] range.
template<class T >
void mrpt::math::wrapToPiInPlace (T &a)
 Modifies the given angle to translate it into the ]-pi,pi] range.
template<class T >
mrpt::math::round2up (T val)
 Round up to the nearest power of two of a given number.
template<class T >
mrpt::math::round_10power (T val, int power10)
 Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and also fractions) power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, .
template<class T >
void mrpt::math::chol (const CMatrixTemplateNumeric< T > &in, CMatrixTemplateNumeric< T > &out)
 Cholesky factorization: in = out' · out Given a positive-definite symmetric matrix, this routine constructs its Cholesky decomposition.
template<class T >
double mrpt::math::correlate_matrix (const CMatrixTemplateNumeric< T > &a1, const CMatrixTemplateNumeric< T > &a2)
 Calculate the correlation between two matrices (by AJOGD @ JAN-2007).
template<class T >
void MRPTDLLIMPEXP mrpt::math::qr_decomposition (CMatrixTemplateNumeric< T > &A, CMatrixTemplateNumeric< T > &R, CMatrixTemplateNumeric< T > &Q, CVectorTemplate< T > &c, int &sing)
 Matrix QR decomposition.
template<class T >
void MRPTDLLIMPEXP mrpt::math::UpdateCholesky (CMatrixTemplateNumeric< T > &chol, CVectorTemplate< T > &r1Modification)
 If R = CHOL(A) is the original Cholesky factorization of A, then R1 = CHOLUPDATE(R,X) returns the upper triangular Cholesky factor of A + X*X', where X is a column vector of appropriate length.
void MRPTDLLIMPEXP mrpt::math::computeEigenValues2x2 (const CMatrixFloat &in_matrix, float &min_eigenvalue, float &max_eigenvalue)
 Compute the two eigenvalues of a 2x2 matrix.
template<class T >
std::vector< T > mrpt::math::Exp (const std::vector< T > &v)
 Computes the 'exp' of all the elements of a vector.
template<class T >
std::vector< T > mrpt::math::Log (const std::vector< T > &v)
 Computes the 'log' of all the elements of a vector.
double MRPTDLLIMPEXP mrpt::math::averageLogLikelihood (const vector_double &logLikelihoods)
 A numerically-stable method to compute average likelihood values with strongly different ranges (unweighted likelihoods: compute the arithmetic mean).
double MRPTDLLIMPEXP mrpt::math::averageWrap2Pi (const vector_double &angles)
 Computes the average of a sequence of angles in radians taking into account the correct wrapping in the range $ ]-\pi,\pi [ $, for example, the mean of (2,-2) is $ \pi $, not 0.
double MRPTDLLIMPEXP mrpt::math::averageLogLikelihood (const vector_double &logWeights, const vector_double &logLikelihoods)
 A numerically-stable method to average likelihood values with strongly different ranges (weighted likelihoods).
std::string MRPTDLLIMPEXP mrpt::math::MATLAB_plotCovariance2D (const CMatrixFloat &cov22, const CVectorFloat &mean, const float &stdCount, const std::string &style=std::string("b"), const size_t &nEllipsePoints=30)
 Generates a string with the MATLAB commands required to plot an confidence interval (ellipse) for a 2D Gaussian ('float' version).
void MRPTDLLIMPEXP mrpt::math::homogeneousMatrixInverse (const CMatrixDouble &M, CMatrixDouble &out_inverse_M)
 Efficiently compute the inverse of a 4x4 homogeneous matrix by only transposing the rotation 3x3 part and solving the translation with dot products.
void MRPTDLLIMPEXP mrpt::math::homogeneousMatrixInverse (const CMatrixDouble44 &M, CMatrixDouble44 &out_inverse_M)
 Efficiently compute the inverse of a 4x4 homogeneous matrix by only transposing the rotation 3x3 part and solving the translation with dot products.
template<class T >
size_t mrpt::math::countCommonElements (const std::vector< T > &a, const std::vector< T > &b)
 Counts the number of elements that appear in both vectors (comparison through the == operator) It is assumed that no repeated elements appear within each of the vectors.
template<typename T , class USERPARAM >
void mrpt::math::estimateJacobian (const std::vector< T > &x, void(*functor)(const std::vector< T > &x, const USERPARAM &y, std::vector< T > &out), const std::vector< T > &increments, const USERPARAM &userParam, CMatrixTemplateNumeric< T > &out_Jacobian)
 Estimate the Jacobian of a multi-dimensional function around a point "x", using finite differences of a given size in each input dimension.
template<class T >
vector_double mrpt::math::histogram (const std::vector< T > &v, double limit_min, double limit_max, size_t number_bins, bool do_normalization=false)
 Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the limits.
template<typename T , typename At , size_t N>
std::vector< T > & mrpt::math::loadVector (std::vector< T > &v, At(&theArray)[N])
 Assignment operator for initializing a std::vector from a C array (The vector will be automatically set to the correct size).
template<class T >
std::vector< T > mrpt::math::Abs (const std::vector< T > &a)
 Absolute value of a vector.
void mrpt::math::unwrap2PiSequence (vector_double &x)
 Modify a sequence of angle values such as no consecutive values have a jump larger than PI in absolute value.
template<class T >
T MRPTDLLIMPEXP mrpt::math::mahalanobisDistance (const std::vector< T > &X, const std::vector< T > &MU, const CMatrixTemplateNumeric< T > &COV_inv)
 Computes the mahalanobis distance of a vector X given the mean MU and the covariance *inverse* COV_inv

\[ d = \sqrt{ (X-MU)^\top \Sigma^{-1} (X-MU) } \]

.

template<typename T , size_t N, typename U >
void mrpt::math::covariancesAndMean (const std::vector< T > &elements, CMatrixTemplateNumeric< U > &covariances, U(&means)[N])
 Computes covariances and mean of any vector of containers.
template<typename T , size_t N, typename U >
void mrpt::math::covariancesAndMean (const std::vector< T > &elements, CMatrixFixedNumeric< U, N, N > &covariances, U(&means)[N])
 Computes covariances and mean of any vector of containers.
template<typename T , size_t N, typename U >
void mrpt::math::covariancesAndMean (const std::vector< T > &elements, CMatrixFixedNumeric< U, N, N > &covariances, std::vector< U > &means)
 Computes covariances and mean of any vector of containers.
template<size_t N, class T , class U , class V >
mrpt::math::dotProduct (const U &v1, const V &v2)
 The dot product of two vectors v1·v2.
template<size_t N, class T , class U >
mrpt::math::squareNorm (const U &v)
 Square norm of a vector of the given length N.
Interpolation functions



template<class T >
mrpt::math::interpolate (const T &x, const std::vector< T > &ys, const T &x0, const T &x1)
 Interpolate a data sequence "ys" ranging from "x0" to "x1" (equally spaced), to obtain the approximation of the sequence at the point "x".
double MRPTDLLIMPEXP mrpt::math::interpolate2points (const double x, const double x0, const double y0, const double x1, const double y1, bool wrap2pi=false)
 Linear interpolation/extrapolation: evaluates at "x" the line (x0,y0)-(x1,y1).
double MRPTDLLIMPEXP mrpt::math::spline (const double t, const std::vector< double > &x, const std::vector< double > &y, bool wrap2pi=false)
 Interpolates the value of a function in a point "t" given 4 SORTED points where "t" is between the two middle points If wrap2pi is true, output "y" values are wrapped to ]-pi,pi] (It is assumed that input "y" values already are in the correct range).
double MRPTDLLIMPEXP mrpt::math::leastSquareLinearFit (const double t, const std::vector< double > &x, const std::vector< double > &y, bool wrap2pi=false)
 Interpolates or extrapolates using a least-square linear fit of the set of values "x" and "y", evaluated at a single point "t".
void MRPTDLLIMPEXP mrpt::math::leastSquareLinearFit (const std::vector< double > &ts, std::vector< double > &outs, const std::vector< double > &x, const std::vector< double > &y, bool wrap2pi=false)
 Interpolates or extrapolates using a least-square linear fit of the set of values "x" and "y", evaluated at a sequence of points "ts" and returned at "outs".



Page generated by Doxygen 1.6.1 for MRPT 0.7.1 SVN: at Tue Dec 22 08:29:35 CET 2009