# TMath

In the namespace, TMath is a collection of free functions is provided for the following functionality:

• Numerical constants.
• Trigonometric and elementary mathematical functions.
• Functions to work with arrays and collections.
• Statistic functions.
• Polynomial and Geometrical functions.
• Special mathematical functions.

For more details, see the reference documentation of TMath.

#### Numerical Constants

TMath offers a wide range of constants in the form of inline functions. Notice that they are not defined as C/C++ preprocessor macros. This set of functions includes one or more definitions for the following constants:

• Pi.
• Base of natural logarithm.
• Velocity of light.
• Gravitational constant (G).
• Standard acceleration of gravity (g).
• Standard acceleration of Gravity.
• Plank's contant.
• Boltzmann's and Steffan-Boltzmann's constants.
• Universal gas constant.
• Molecular weight of dry air.
• Dry air gas constant.
• Euler-Mascheroni Constant.
• Elementary charge.

#### Elementary Functions

A set of miscellaneous elementary mathematical functions is provided along with a set of basic trigonometrical functions. Some of this functions refer to basic mathematical functions like the square root, the power to a number of the calculus of a logarithm, while others are used for number treatment, like rounding.

Although there are some functions that are not in the standard C math library (like `Factorial`), most of the functionality offered here is just a wrapper of the first ones. Nevertheless, some of the them also offer some security checks or a better precision, like the trigonometrical functions `ASin(x)`, `ACos(x)` or `ATan(x)`.

``` // Generate a vector with 10 random numbers
vector<double> v(10);
std::generate(v.begin(), v.end(), rand);

// Find the minumum value of the vector (iterator version)
vector<double>::iterator it;
it = TMath::LocMin(v.begin(), v.end());
std::cout << *it << std::endl;

// The same with the old-style version
int i;
i = TMath::LocMin(10, &v);
std::cout << v[i] << std::endl;```

Another example of these functions can be found in `\$ROOTSYS/tutorials/permute.C`.

#### Statistic Functions Operating on Arrays.

This set process arrays to calculate:

• Mean.
• Median.
• Geometrical mean.
• RMS.
• The kth smallest element.

These functions, as the array algorithms, have two different interfaces. An old-style one where the size of the array is passed as a first argument followed by a pointer to the array itself and a modern interface that receives two iterators to it.

``` // Size of the array
const int n = 100;

// Vector v with random values
vector<double> v(n);
std::generate(v.begin(), v.end(), rand);

// Weight vector w
vector<double> w(n);
std::fill(w.begin(), w.end, 1);

double mean;

// Calculate the mean of the vector
// with iterators
mean = TMath::Mean(v.begin(), v.end());

// old-style
mean = TMath::Mean(n, &v);

// Calculate the mean with a weight vector
// with iterators
mean = TMath::Mean(v.begin(), v.end(), w.begin());

// old-style
mean = TMath::Mean(n, &v, &w);```

#### Special and Statistical Functions.

TMath also provides special functions like Bessel, Error functions, Gamma or similar plus statistical mathematical functions, including probability density functions, cumulative distribution and their inverse.

The majority of the special functions and the statitical distributions are provided also as free functions in the ROOT::Math namespace. The user is encourage to use those versions of the algorithms rather than the ones in TMath.

Functions not present in ROOT::Math and provided only by TMath are:

Special functions:

• DiLogarithm
• Struve

Statistical functions:

• KolmogorovProb
• Voigt
• LaplaceDist
• Vavilov

`GammaFun.C` and `mathBeta.C` in `\$ROOTSYS/tutorials` shows an example of use of the ROOT::Math special functions

#### Geometrical Functions.

Functions to work with vectors and polinomials. Most of them are already implemented as methods in related classes. The user is encourage to use those versions of the algorithms rather than the ones in TMath.