TMath User's Manual

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

For more details, see the reference documentation of TMath at http://root.cern.ch/root/htmldoc/TMath.html.

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:

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).

Algorithms Operating on Arrays.

ROOT also provides some basic algorithms to work with arrays of elements. The functionality provided included:

Most of these methods work with the same interface. They all received as a first argument the size of the array followed by a pointer to the data. In recent versions of the library, some methods also offer an interface that works with iterators. In this case they will received an iterator to the beginning of the array and another one to the end as their arguments. This approach has to main advantages over the old-style interface, first it is more in the C++ style and the second is that it will allow to use many of these methods with different data structures, as long as they support the iterators interface.

An example of how to use these methods follows. Notice how the access to the data would be different depending on what interface is used with the method:

 // 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[0]);
 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:

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[0]);

 // 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[0], &w[0]);
    

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:

Statistical functions:

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.


David Gonzalez Maline
Last modified: Tue Apr 22 16:16:32 CEST 2008