Functions

TRTK::Tools Namespace Reference

This namespace contains various helper functions. More...

Functions

bool fileExists (const char *file_name)
 Checks whether a certain file exists.
unsigned long long fileLength (const char *file_name)
 Returns the size of a certain file.
unsigned long long fileLength (ifstream &file_stream)
string getCurrentDate ()
 Returns the current date in ISO 8601 format (YYYY-MM-DD).
string getCurrentTime ()
 Returns the current time in format hh:mm:ss.
double randn ()
 Random sample of the standard normal distribution.
template<class T >
bool isZero (const T value)
 Tests for equality with zero.
template<class T >
rand ()
 Random sample of the standard uniform distribution.
template<class T >
rand (T a, T b)
 Random sample of a uniform distribution.
template<class T >
randn (T mu, T sigma)
 Random sample of the normal distribution.
template<class EigenMatrix >
QTransform Eigen3x3_to_QTransfom (const EigenMatrix &eigenMatrix)
 Converts an Eigen 3x3 matrix into a QTransform.
template<class T >
std::pair< Eigen::Matrix< T, 3, 1 >
, T > 
axisAngleFromRotationMatrix (const Eigen::Matrix< T, 3, 3 > &matrix)
template<class T >
std::tuple< T, T, T > cartesian2Spherical (const T &x, const T &y, const T &z)
template<class T >
Eigen::Matrix< T, 3, 1 > cartesian2Spherical (const Eigen::Matrix< T, 3, 1 > &point)
template<class T >
Coordinate< T > cartesian2Spherical (const Coordinate< T > &point)
unsigned long long fileLength (std::ifstream &file_stream)
 Returns the size of a certain file stream.
template<class Derived , class Base >
bool isClass (Base *base_ptr)
 Checks whether an object is of a certain class.
template<class Derived , class Base >
bool isClass (Base &base)
 Checks whether an object is of a certain class.
template<typename Base >
bool isDerivedFrom (const Base *base_ptr)
 Checks whether an object is derived from a certain class.
template<typename Base >
bool isDerivedFrom (const void *base_ptr)
 Checks whether an object is derived from a certain class.
template<class T >
bool isEqual (const T x, const T y)
 Tests for equality of two scalars.
template<>
bool isEqual< int > (const int x, const int y)
template<class T >
std::vector< T > listToVector (const std::list< T > &lst)
 Converts an STL list into an STL vector.
template<class Container , class ValueType >
ValueType mean (const Container &container, ValueType null_value=ValueType())
 Returns the mean of all container elements.
template<class WeightsContainer , class ValuesContainer , class ValueType = typename ValuesContainer::value_type>
ValueType weightedMean (const WeightsContainer &weights, const ValuesContainer &values, ValueType null_value=ValueType(0))
 Returns the weighted mean of all container elements.
template<class Container >
auto median (const Container &container)-> std
 Returns the median of all container elements.
template<class T >
TRTK::Coordinate< T > orthogonalMatrixToQuaternion (const Eigen::Matrix< T, 3, 3 > &matrix)
 Converts an orthogonal matrix to a unit quaternion.
template<class T >
TRTK::Coordinate< T > orthogonalMatrixToQuaternion2 (const Eigen::Matrix< T, 3, 3 > &matrix)
 Converts an orthogonal matrix to a unit quaternion.
template<class T >
Eigen::Matrix< T, 3, 3 > quaternionToOrthogonalMatrix (T q0, T q1, T q2, T q3)
 Converts a unit quaternion to an orthogonal matrix.
template<class T >
Eigen::Matrix< T, 3, 3 > quaternionToOrthogonalMatrix (const TRTK::Coordinate< T > &quaternion)
 Converts a unit quaternion to an orthogonal matrix.
template<>
int rand< int > (int a, int b)
template<>
unsigned rand< unsigned > (unsigned a, unsigned b)
template<>
int randn (int mu, int sigma)
template<class T >
int sign (T value)
 Extracts the sign of a floating point number.
template<class T >
Eigen::Matrix< T, 3, 3 > rotationMatrix (const Eigen::Matrix< T, 3, 1 > &axis_, double angle)
template<class T >
round (const T number)
 Rounds towards the next (signed) integer value.
template<>
int round< int > (const int number)
template<class T >
std::tuple< T, T, T > spherical2Cartesian (const T &r, const T &theta, const T &phi)
template<class T >
Coordinate< T > spherical2Cartesian (const Coordinate< T > &point)
template<class T >
Eigen::Matrix< T, 3, 1 > spherical2Cartesian (const Eigen::Matrix< T, 3, 1 > &point)
template<class Container , class ValueType >
ValueType standardDeviation (const Container &container, ValueType null_value=ValueType())
 Computes the corrected sample standard deviation of all container elements.
template<class T >
std::string toString (const T &value)
 Converts the input argument (int, double etc.) into a std::string.
template<class Container , class ValueType >
ValueType variance (const Container &container, ValueType null_value=ValueType())
 Computes the sample variance of all container elements.
template<class T >
std::list< T > vectorToList (const std::vector< T > &vec)
 Converts an STL vector into an STL list.
template<class T1 , class T2 >
std::vector< std::pair< T1, T2 > > zip (const std::vector< T1 > &v1, const std::vector< T2 > &v2)
 Zips two STL vectors into a single one.

Detailed Description

This namespace contains various helper functions.

Helper functions sorted by topic:


Function Documentation

template<class T >
std::pair<Eigen::Matrix<T, 3, 1>, T> TRTK::Tools::axisAngleFromRotationMatrix ( const Eigen::Matrix< T, 3, 3 > &  matrix )
Template Parameters:
Tscalar (floating point) type

This function decomposes a given 3-by-3 rotation matrix into the normalized rotation axis and the positive rotation angle where the rotation angle \( \alpha \in [0, \pi] \). The decomposition is unique. The rotation matrix does not need to describe a pure rotation; an additional scaling may be implied but is neglected. The matrix is assumed to be left multiplied to column vectors.

Example:

 Matrix3d R = rotationMatrix(Vector3d(1, 1, 1), 0.3);
 Vector3d axis;
 double angle;
 tie(axis, angle) = axisAngleFromRotationMatrix(R);

Then, axis = (0.5774, 0.5774, 0.5774) and angle = 0.3.

Returns:
(axis, angle) where axis is a 3d vector
See also:
rotationMatrix
Author:
Christoph Hänisch
Version:
0.2.0
Date:
last changed on 2019-07-01

Definition at line 155 of file Tools.hpp.

template<class T >
std::tuple<T, T, T> TRTK::Tools::cartesian2Spherical ( const T &  x,
const T &  y,
const T &  z 
)
Template Parameters:
Tscalar (floating point) type

The Cartesian coordinate is converted to spherical coordinates using the ISO 80000-2 convention (i.e. as in physics: radius r, inclination theta, azimuth phi).

Returns:
(r, theta, phi)
See also:
spherical2Cartesian
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-17

Definition at line 226 of file Tools.hpp.

template<class T >
Eigen::Matrix<T, 3, 1> TRTK::Tools::cartesian2Spherical ( const Eigen::Matrix< T, 3, 1 > &  point )
Template Parameters:
Tscalar (floating point) type

The Cartesian coordinate is converted to spherical coordinates using the ISO 80000-2 convention (i.e. as in physics: radius r, inclination theta, azimuth phi).

Returns:
3d vector (r, theta, phi)
See also:
spherical2Cartesian
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-17

Definition at line 263 of file Tools.hpp.

template<class T >
Coordinate<T> TRTK::Tools::cartesian2Spherical ( const Coordinate< T > &  point )
Template Parameters:
Tscalar (floating point) type

The Cartesian coordinate is converted to spherical coordinates using the ISO 80000-2 convention (i.e. as in physics: radius r, inclination theta, azimuth phi).

See also:
spherical2Cartesian
Returns:
3d coordinate (r, theta, phi)
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-17

Definition at line 288 of file Tools.hpp.

template<class EigenMatrix >
QTransform TRTK::Tools::Eigen3x3_to_QTransfom ( const EigenMatrix &  eigenMatrix )

Converts an Eigen 3x3 matrix into a QTransform.

Note:
This function is only available, if Eigen and Qt were included before.
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2011-10-10

Definition at line 110 of file Tools.hpp.

bool TRTK::Tools::fileExists ( const char *  file_name )

Checks whether a certain file exists.

Parameters:
[in]file_nameName of the file to check for.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-11-29

Definition at line 45 of file Tools.cpp.

unsigned long long TRTK::Tools::fileLength ( const char *  file_name )

Returns the size of a certain file.

If a file does not exist zero is returned.

  unsigned long long length = fileLength("file name");
See also:
fileLength(std::ifstream & file_stream)
Author:
Christoph Hänisch
Version:
0.2.1
Date:
last changed on 2013-03-19

Definition at line 73 of file Tools.cpp.

unsigned long long TRTK::Tools::fileLength ( ifstream &  file_stream )

file_stream must be a valid stream, otherwise the result is undefined.

See also:
fileLength(const char * file_name)
  std::ifstream file("file name");

  if (file)
  {
      unsigned long long length = fileLength(file);
  }
Author:
Christoph Hänisch
Version:
0.2.0
Date:
last changed on 2012-08-03

Definition at line 111 of file Tools.cpp.

std::string TRTK::Tools::getCurrentDate (  )

Returns the current date in ISO 8601 format (YYYY-MM-DD).

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2013-08-19

Definition at line 129 of file Tools.cpp.

std::string TRTK::Tools::getCurrentTime (  )

Returns the current time in format hh:mm:ss.

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2013-08-19

Definition at line 150 of file Tools.cpp.

template<class Derived , class Base >
bool TRTK::Tools::isClass ( Base *  base_ptr ) [inline]

Checks whether an object is of a certain class.

An object is also classified as being of type Derived, if Derived is one of its base classes.

Here are some examples also incorporating isClass(Base & base):

 class A
 {
 public:
     virtual ~A() {};
 };

 class B : public A
 {
 public:
     virtual ~B() {};
 };

 class C
 {
 public:
     virtual ~C() {};
 };

 int main()
 {
     A a1;
     A * a2 = new A;
     A * a3 = new B;
     B b1;
     B * b2 = new B;
     C c1;
     C * c2 = new C;

     cout << isClass<B>(a1) << endl;    // output: 0
     cout << isClass<B>(a2) << endl;    // output: 0
     cout << isClass<B>(a3) << endl;    // output: 1
     cout << isClass<B>(b1) << endl;    // output: 1
     cout << isClass<B>(b2) << endl;    // output: 1
     cout << isClass<B>(c1) << endl;    // output: 0
     cout << isClass<B>(c2) << endl;    // output: 0

     return 0;
 }
Note:
  • The involved classes must be polymorphic, that is, they must at least contain one virtual function (e.g. a virtual destructor)
  • The compiler must support RTTI.
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2011-08-18

Definition at line 380 of file Tools.hpp.

template<class Derived , class Base >
bool TRTK::Tools::isClass ( Base &  base ) [inline]

Checks whether an object is of a certain class.

An object is also classified as being of type Derived, if Derived is one of its base classes.

Please have a look at isClass(Base * base_ptr) to see some example code.

Note:
  • The involved classes must be polymorphic, that is, they must at least contain one virtual function (e.g. a virtual destructor)
  • The compiler must support RTTI.
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2011-08-18

Definition at line 406 of file Tools.hpp.

template<typename Base >
bool TRTK::Tools::isDerivedFrom ( const Base *  base_ptr ) [inline]

Checks whether an object is derived from a certain class.

Example:

 class A
 {
 public:
     virtual ~A() {};
 };

 class B : public A
 {
 public:
     virtual ~B() {};
 };

 class C
 {
 public:
     virtual ~C() {};
 };

 int main()
 {
     A * a = new A;
     B * b = new B;
     C * c = new C;

     cout << isDerivedFrom<A>(a) << endl;    // output: 1
     cout << isDerivedFrom<A>(b) << endl;    // output: 1
     cout << isDerivedFrom<A>(c) << endl;    // output: 0

     return 0;
 }
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2011-08-18

Definition at line 472 of file Tools.hpp.

template<typename Base >
bool TRTK::Tools::isDerivedFrom ( const void *  base_ptr ) [inline]

Checks whether an object is derived from a certain class.

Please have a look at isDerivedFrom(Base * base_ptr) to see some example code.

Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2011-08-18

Definition at line 489 of file Tools.hpp.

template<class T >
bool TRTK::Tools::isEqual ( const T  x,
const T  y 
) [inline]

Tests for equality of two scalars.

Template Parameters:
Tscalar type

This function tests whether the absolute difference between the two input arguments is below a certain threshold. The default threshold is \( 1^{-13} \) or 10 numeric_limits<T>::epsilon() in case the numeric limit of T is greater than the default value.

Example:

 double value = 1.0;

 isEqual(value, 1.0);        // twice the same argument type
 isEqual<double>(value, 1);  // different argument types
Note:
The functions abs() as well as numeric_limits<T>::epsilon() must be defined for the given scalar type T. (A template function specialization for integers is implemented.)
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2019-07-17

Definition at line 523 of file Tools.hpp.

template<class T >
bool TRTK::Tools::isZero ( const T  value ) [inline]

Tests for equality with zero.

Template Parameters:
Tscalar type

Actually, this function is only a short form of isEqual<T>(value, T(0)), hence testing the absolute value against a certain threshold. See isEqual(...) for more details.

Note:
The template parameter can be omitted since it can be deduced from the argument type.
The type T must provide a single parameter constructor allowing an initialization of the form T(0).

Example:

 double value = 1.0;
 isZero(value); // returns false
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-04-21

Definition at line 568 of file Tools.hpp.

template<class T >
std::vector<T> TRTK::Tools::listToVector ( const std::list< T > &  lst )

Converts an STL list into an STL vector.

The vector's class type is the same as the one of the list.

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-04-21

Definition at line 584 of file Tools.hpp.

template<class Container , class ValueType >
ValueType TRTK::Tools::mean ( const Container &  container,
ValueType  null_value = ValueType() 
) [inline]

Returns the mean of all container elements.

Note:
The container must provide an STL interface.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2013-07-30

Definition at line 609 of file Tools.hpp.

template<class Container >
auto TRTK::Tools::median ( const Container &  container ) [inline]

Returns the median of all container elements.

Note:
The container must provide an STL interface.
If the container is empty a signaling NaN is returned.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-07-17

Definition at line 686 of file Tools.hpp.

template<class T >
TRTK::Coordinate<T> TRTK::Tools::orthogonalMatrixToQuaternion ( const Eigen::Matrix< T, 3, 3 > &  matrix )

Converts an orthogonal matrix to a unit quaternion.

Note:
This function needs the following header to be included!
 #include <Eigen/Core> 
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2013-08-12

Definition at line 732 of file Tools.hpp.

template<class T >
TRTK::Coordinate<T> TRTK::Tools::orthogonalMatrixToQuaternion2 ( const Eigen::Matrix< T, 3, 3 > &  matrix )

Converts an orthogonal matrix to a unit quaternion.

The given matrix can be nonorthogonal. Then this algorithm computes a quaternion that yields the closest orthogonal matrix to the given matrix.

Note:
This function needs the following headers to be included!
 #include <Eigen/Core>
 #include <Eigen/Eigenvalues>
References:

[1] Bar-Itzhack, "New Method for Extracting the Quaternion from a Rotation Matrix", J. Guidance, Vol. 23, No. 6: Engineering Notes, 2000

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2013-08-12

Definition at line 806 of file Tools.hpp.

template<class T >
Eigen::Matrix<T, 3, 3> TRTK::Tools::quaternionToOrthogonalMatrix ( q0,
q1,
q2,
q3 
)

Converts a unit quaternion to an orthogonal matrix.

Note:
This function needs the following header to be included!
 #include <Eigen/Core> 
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2016-04-29

Definition at line 851 of file Tools.hpp.

template<class T >
Eigen::Matrix<T, 3, 3> TRTK::Tools::quaternionToOrthogonalMatrix ( const TRTK::Coordinate< T > &  quaternion ) [inline]

Converts a unit quaternion to an orthogonal matrix.

Note:
This function needs the following header to be included!
 #include <Eigen/Core> 
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2013-08-06

Definition at line 890 of file Tools.hpp.

template<class T >
T TRTK::Tools::rand (  ) [inline]

Random sample of the standard uniform distribution.

Template Parameters:
Tfloating point type

Properties of the standard uniform distribution \( \mathcal{U}(0, 1) \):

  • pdf: \( f(x) = \begin{cases} 1 & \text{for } 0 \leq x \leq 1 \\ 0 & \text{otherwise} \end{cases} \)
  • support: \( x \in [0; 1] \)
  • mean value: \( 0.5 \)
  • variance: \( \frac{1}{12} \)
Returns:
Returns a pseudo-random sample of the standard uniform distribution, i.e. a number in the range \( [0; 1] \).
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-11-07

Definition at line 919 of file Tools.hpp.

template<class T >
T TRTK::Tools::rand ( a,
b 
)

Random sample of a uniform distribution.

Template Parameters:
Tfloating point type
Parameters:
[in]aminimum value of the distribution
[in]bmaximum value of the distribution

Properties of the uniform distribution \( \mathcal{U}(a, b) \):

  • pdf: \( f(x) = \begin{cases} \frac{1}{b - a} & \text{for } a \leq x \leq b \\ 0 & \text{otherwise} \end{cases} \)
  • support: \( x \in [a; b] \)
  • mean value: \( 0.5 (a + b) \)
  • variance: \( \frac{1}{12} (b - a)^2 \)
Returns:
Returns a pseudo-random number in the range \( [a; b] \).
Author:
Christoph Hänisch
Version:
0.1.2
Date:
last changed on 2016-07-13

Definition at line 949 of file Tools.hpp.

template<class T >
T TRTK::Tools::randn ( mu,
sigma 
) [inline]

Random sample of the normal distribution.

Template Parameters:
Tfloating point type
Parameters:
[in]mumean of the distribution
[in]sigmastandard deviation of the distribution

Properties of the normal distribution \( \mathcal{N}(\mu, \sigma^2) \):

  • pdf: \( f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}} \)
  • support: \( x \in R \)
  • mean value: \( \mu \)
  • variance: \( \sigma^2 \)
Returns:
Returns a pseudo-random number.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-11-07

Definition at line 1062 of file Tools.hpp.

double TRTK::Tools::randn (  )

Random sample of the standard normal distribution.

Convenience function for randn<double>().

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2012-03-15
Template Parameters:
Tfloating point type

Properties of the standard normal distribution \( \mathcal{N}(0, 1) \):

  • pdf: \( f(x) = \frac{1}{\sqrt{2\pi}} e^{-\frac{1}{2}x^2} \)
  • support: \( x \in R \)
  • mean value: \( 0 \)
  • variance: \( 1 \)
Returns:
Returns a pseudo-random number.
Note:
For reasons of convenience, there is also a non-templated randn() function which uses the double type.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-11-08

Definition at line 173 of file Tools.cpp.

template<class T >
Eigen::Matrix<T, 3, 3> TRTK::Tools::rotationMatrix ( const Eigen::Matrix< T, 3, 1 > &  axis_,
double  angle 
)
Template Parameters:
Tscalar (floating point) type

Constructs a 3d rotation matrix from a rotation vector and a rotation angle.

Note, negating the rotation vector or the rotation angle yields the inverse of the rotation. Consequently, if the rotation vector as well as the rotation angle are negated the same rotation is obtained.

Returns:
3-by-3 matrix
See also:
axisAngleFromRotationMatrix
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-24

Definition at line 1116 of file Tools.hpp.

template<class T >
T TRTK::Tools::round ( const T  number )

Rounds towards the next (signed) integer value.

Template Parameters:
Tscalar type
Returns:
Rounded number of the same type as the scalar type.

Here is an example of how to use this function:

 using TRTK::Tools::round;

 double a = round(-1.7);   // a == -2.0
 double b = round(-1.3);   // b == -1.0
 double c = round(1.3);    // c ==  1.0
 double d = round(1.7);    // d ==  2.0
Note:
The functions floor() and ceil() must be defined for T.
Author:
Christoph Hänisch
Version:
0.1.1
Date:
last changed on 2011-07-01

Definition at line 1195 of file Tools.hpp.

template<class T >
int TRTK::Tools::sign ( value )

Extracts the sign of a floating point number.

It returns

\[ sgn(x) = \begin{cases} -1 & \text{if } x < 0 \\ 0 & \text{if } x = 0 \\ 1 & \text{if } x > 0 \end{cases} \]

Note:
The type T must be constructable from 0 as well as provide some comparison operators.

Definition at line 1091 of file Tools.hpp.

template<class T >
Eigen::Matrix<T, 3, 1> TRTK::Tools::spherical2Cartesian ( const Eigen::Matrix< T, 3, 1 > &  point )
Template Parameters:
Tscalar (floating point) type
Parameters:
[in]point(r, theta, phi)

The spherical coordinate is converted to a Cartesian coordinate using the ISO 80000-2 convention (i.e., as in physics a coordinate is given as a triple with radius r, inclination theta, and azimuth phi).

Returns:
3d vector (x, y, z)
See also:
cartesian2Spherical
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-25

Definition at line 1288 of file Tools.hpp.

template<class T >
std::tuple<T, T, T> TRTK::Tools::spherical2Cartesian ( const T &  r,
const T &  theta,
const T &  phi 
)
Template Parameters:
Tscalar (floating point) type
Parameters:
[in]r
[in]theta
[in]phiThe spherical coordinate is converted to a Cartesian coordinate using the ISO 80000-2 convention (i.e., as in physics a coordinate is given as a triple with radius r, inclination theta, and azimuth phi).
Returns:
triple (x, y, z)
See also:
cartesian2Spherical
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-25

Definition at line 1231 of file Tools.hpp.

template<class T >
Coordinate<T> TRTK::Tools::spherical2Cartesian ( const Coordinate< T > &  point )
Template Parameters:
Tscalar (floating point) type
Parameters:
[in]point(r, theta, phi)

The spherical coordinate is converted to a Cartesian coordinate using the ISO 80000-2 convention (i.e., as in physics a coordinate is given as a triple with radius r, inclination theta, and azimuth phi).

Returns:
3d coordinate (x, y, z)
See also:
cartesian2Spherical
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-06-25

Definition at line 1261 of file Tools.hpp.

template<class Container , class ValueType >
ValueType TRTK::Tools::standardDeviation ( const Container &  container,
ValueType  null_value = ValueType() 
) [inline]

Computes the corrected sample standard deviation of all container elements.

While \( s^2 = \frac{1}{n - 1} \sum_{i = 1}^{n} (x_i - \bar x)^2 \) is an unbiased estimator for the population variance, s is a biased estimator for the population standard deviation [1].

However, for normally and independently distributed random variables an unbiased estimator is given by [2, 3, 4]

\[ s = \frac{1}{c_4(n)} \sqrt{ \frac{1}{n - 1} \sum_{i = 1}^{n} (x_i - \bar x)^2 } \]

where

\[ c_4(n) = \sqrt{\frac{2}{n - 1}} \frac{\Gamma(\frac{n}{2})}{\Gamma(\frac{n-1}{2})} = 1 - \frac{1}{4n} - \frac{7}{32n^2} - \frac{19}{128n^3} + O(n^{-4}) \]

This function implements the above described estimator.

References:

[1] http://en.wikipedia.org/wiki/Sample_standard_deviation

[2] http://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation

[3] Richard M. Brugger, "A Note on Unbiased Estimation of the Standard Deviation", The American Statistician (23) 4 p. 32 (1969)

[4] J. Gurland and R. C. Tripathi, Richard M. Brugger, "A Simple Approximation for Unbiased Estimation of the Standard Deviation", The American Statistician (25) 4 p. 30 (1971)

Note:
The container must provide an STL interface.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-04-21

Definition at line 1336 of file Tools.hpp.

template<class T >
std::string TRTK::Tools::toString ( const T &  value ) [inline]

Converts the input argument (int, double etc.) into a std::string.

Here is an example of how to use this function:

 using std::string;
 using TRTK::Tools::toString;

 double d = -1.7;
 string str = "The value of d is " + toString(d);
Author:
Christoph Hänisch
Version:
0.2.0
Date:
last changed on 2011-08-12

Definition at line 1377 of file Tools.hpp.

template<class Container , class ValueType >
ValueType TRTK::Tools::variance ( const Container &  container,
ValueType  null_value = ValueType() 
) [inline]

Computes the sample variance of all container elements.

The variance is computed using Bessel's correction:

\[ s^2 = \frac{1}{n - 1} \sum_{i = 1}^{n} (x_i - \bar x)^2 \]

Note:
The container must provide an STL interface.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-04-21

Definition at line 1401 of file Tools.hpp.

template<class T >
std::list<T> TRTK::Tools::vectorToList ( const std::vector< T > &  vec )

Converts an STL vector into an STL list.

The list's class type is the same as the one of the vector.

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2011-04-21

Definition at line 1429 of file Tools.hpp.

template<class WeightsContainer , class ValuesContainer , class ValueType = typename ValuesContainer::value_type>
ValueType TRTK::Tools::weightedMean ( const WeightsContainer &  weights,
const ValuesContainer &  values,
ValueType  null_value = ValueType(0) 
) [inline]

Returns the weighted mean of all container elements.

This function computes

\[ \bar{x}_{mean} = \frac{\sum_{i=1}^n w_i x_i}{\sum_{i=1}^n w_i} \]

It is assumed that there is an associated weight for each value and that the ordering in both containers is the same. Each containers must provide the STL interface. The weights are assumed to be scalars and its underlying type must be initializable with 0. Also, the container elements must support the addition operation and values must support the multiplication with a scalar.

Note:
The computation of the weighted mean stops as soon as the end of one of the containers is reached. In all cases a valid weighted mean is computed for the subset of weight-value pairs.
Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2019-09-12

Definition at line 647 of file Tools.hpp.

template<class T1 , class T2 >
std::vector<std::pair<T1, T2> > TRTK::Tools::zip ( const std::vector< T1 > &  v1,
const std::vector< T2 > &  v2 
)

Zips two STL vectors into a single one.

The elements are stored in pairs. Both vectors must have the same size.

Author:
Christoph Hänisch
Version:
0.1.0
Date:
last changed on 2016-07-13

Definition at line 1452 of file Tools.hpp.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines