Mathematical libraries
The ROOT mathematical libraries consist of the following components:
MathCore library
The MathCore library provides a collection of functions, C++ classes and ROOT classes for HEP numerical computing.
The MathCore
is a selfconsistent minimal set of tools required for the basic numerical computing. More advanced mathematical functionalities is provided by the MathMore library.
The following is included in the MathCore
library:

Special functions: Functions like the gamma, beta and error function that are used in HEP.

Statistical functions: Functions used in statistics, such as the probability density functions and the cumulative distributions functions for continuous and discrete distributions.

Function classes and interfaces: Interfaces (abstract classes) and base classes, including helper classes to wrap free (static) and nonstatic member functions.
 Numerical algorithms: User classes with basic implementations for:
 Fitting and parameter estimation: ROOT classes for fitting and parameter estimation from a given data set.
In addition, the MathCore library contains the following ROOT classes that were originally part of libCore:

the namespaces for TMath and ROOT::Math.

ROOT classes for pseudorandom number generators, TRandom and the derived classes TRandom1 , TRandom2 and TRandom3 .

ROOT class for complex numbers, TComplex .

other ROOT classes like:
 TKDTree: ROOT class implementing a kdtree.
 ROOT::Math::GoFTest: ROOT class for testing the for goodness of fit tests.
TMath
The TMath namespace provides a collection of free functions:
 numerical constants (like π, e, h, etc.)
 trigonometric and elementary mathematical functions
 functions to work with arrays and collections (e.g., functions to find the minimum and maximum of arrays)
 statistic functions to work on array of data (e.g., mean and RMS of arrays)
 algorithms for binary search/hashing sorting
 special mathematical functions like
Bessel
,Erf
,Gamma
, etc.  statistical functions, like common probability and cumulative (quantile) distributions
 geometrical functions
Elementary functions
Some of elementary mathematical 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 them also offer some security checks or a
better precision, like the trigonometrical functions ASin(x)
, ACos(x)
or ATan(x)
.
Examples
Statistic functions operating on arrays
TMath provides functions that process arrays for calculation:
 mean
 median
 geometrical mean
 sample standard deviation (RMS)
 the kth smallest element
Example
Special and statistical functions
TMath 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 statistical distributions are provided also as free functions in the ROOT::Math namespace.
Functions not present in the ROOT::Math name that are provided only by TMath are:
 Special functions:
 DiLogarithm
 Struve
 Statistical functions:
 KolmogorovProb
 Voigt function
 LaplaceDist
 Vavilov
ROOT::Math
The ROOT::Math namespace provides a set of function interfaces to define the basic behaviour of a mathematical function:
 Onedimensional function interfaces
 Multidimensional function interfaces
 Parametric function interfaces
In addition, helper classes, wrapping the user interfaces in the ROOT::Math function interfaces are provided. With wrapper functions you can insert your own type of function in the needed function interface.
To use the selfdefined functions, they must have inherited from one of the following classes:
Figure: ROOT::Math function interface structure.
Onedimensional function interfaces
This interface is used for numerical algorithms operating only on onedimensional functions. It cannot applied to multidimensional functions.
ROOT::Math::IBaseFunctionOneDim
This interface provides a method to evaluate the function given a value (simple double) by implementing double operator()
(const double
). The user class
defined only needs to reimplement the pure abstract method double DoEval(double x)
that will do the work of evaluating the function at point x.
Example
Example for the implementation of a class that represents a mathematical function.
ROOT::Math::IGradientFunctionOneDim
This interface is needed by some numerical algorithms to calculate the derivatives of the function. It introduces the method double Derivative(double x)
that will return
the derivative of the function at the point x. The class inherit by the user will have to implement the abstract
method double DoDerivative(double x)
, leaving the rest of the class untouched.
Example
Example for the implementation of a gradient onedimensional function.
Multidimensional function interfaces
This interface is used for numerical algorithms operating on multidimensional functions.
ROOT::Math::IBaseFunctionMultiDim
This interface provides the double operator()
(const double*
) that takes an array of doubles with all the values for the different dimensions. In this case, the user has to provide
the functionality for two different functions: double DoEval(const double*)
and unsigned int NDim()
. The first ones evaluates the function given the array that represents the multiple variables. The second returns the number of dimensions of the function.
Example
Example for the implementation of a basic multidimensional function.
ROOT::Math::IGradientFunctionMultiDim
This interface offers the same functionality as the base function and additionally the calculation of the derivative. It only adds the double Derivative(double* x, uint ivar)
method for
the user to implement. This method must implement the derivative of the function with respect to the variable indicated with the second parameter.
Example
Example for the implementation of a multidimensional gradient function.
Parametric function interfaces
This interface is used for fitting after evaluating multidimensional functions.
ROOT::Math::IParametricFunctionMultiDim
This interface describes a multidimensional parametric function. Similarly to the one dimensional version, the user needs to provide the void SetParameters(double* p)
method as well
as the getter methods const double * Parameters()
and uint NPar()
.
Example
Example for the implementation of a parametric function.
ROOT::Math::IParametricGradFunctionMultiDim
This interface provides an interface for parametric gradient multidimensional functions. In addition to function evaluation, it provides the gradient with respect to the parameters,
via the ParameterGradient()
method. This interface is only used in case of some dedicated fitting algorithms, when is required or more efficient to provide derivatives with respect to the parameters.
Example
Example for the implementation of a parametric gradient function.
Wrapper functions
To insert your own type of function in the needed function interface, helper classes, wrapping the user interface in the ROOT::Math function interfaces are provided.
There is one possible wrapper for every interface.
Interface  Wrapper  Description 

ROOT::Math::IBaseFunctionOneDim  ROOT::Math::Functor1D  See → Wrapping onedimensional functions 
ROOT::Math::IGradientFunctionOneDim  ROOT::Math::GradFunctor1D  See → Wrapping onedimensional gradient functions 
ROOT::Math::IBaseFunctionMultiDim  ROOT::Math::Functor  See → Wrapping multidimensional functions 
ROOT::Math::IGradientFunctionMultiDim  ROOT::Math::GradFunctor  See → Wrapping multidimensional gradient functions 
Note the special case when wrapping TF1 objects in parametric function interfaces.
Wrapping onedimensional functions
Use ROOT::Math::Functor1D to wrap onedimensional functions.
ROOT::Math::Functor1D can wrap the following types:
 A free C function of type
double ()(double )
.  Any C++ callable object implementation
double operator()( double)
.  A class member function with the correct signature like
double Foo::Eval(double )
. In this case one pass the object pointer and a pointer to the member function (&Foo::Eval
).
Example
Wrapping onedimensional gradient functions
Use ROOT::Math::GradFunctor1D to wrap onedimensional gradient functions.
It can be constructed in three different ways:
 Any object implementing both double
operator()( double)
for the function evaluation anddouble Derivative(double)
for the function derivative.  Any object implementing any member function like
Foo::XXX(double )
for the function evaluation and any other member function likeFoo::YYY(double)
for the derivative.  Any two function objects implementing
double operator()( double)
. One object provides the function evaluation, the other the derivative. One or both function object can be a free C function of typedouble ()(double)
.
Wrapping multidimensional functions
Use the ROOT::Math::Functor to wrap multidimensional function objects.
It can wrap all the following types:
 Any C++ callable object implementing double
operator()( const double * )
.  A free C function of type
double ()(const double *)
.  A member function with the correct signature like
Foo::Eval(const double *)
. In this case one pass the object pointer and a pointer to the member function(&Foo::Eval)
.
Example
Wrapping multidimensional gradient functions
Use ROOT::Math::GradFunctor to wrap C++ callable objects to make gradient functions.
It can be constructed in three different way:
 From an object implementing both
double operator()( const double*)
for the function evaluation anddouble Derivative(const double *, int icoord)
for the partial derivatives.  From an object implementing any member function like
Foo::XXX(const double *)
for the function evaluation and any member function likeFoo::XXX(const double *, int icoord)
for the partial derivatives.  From an function object implementing
double operator()( const double *)
for the function evaluation and another function object implementingdouble operator() (const double *, int icoord)
for the partial derivatives.
The function dimension is required when constructing the functor.
Wrapping TF1 objects in parametric function interfaces
Often the TF1
class is used.
Use the ROOT::Math::WrappedTF1 class, if the interface to be wrapped is onedimensional.
The default constructor takes a TF1 reference as argument, that will be wrapped with the interfaces of a ROOT::Math::IParametricGradFunctionOneDim.
Example
Use the ROOT::Math::WrappedMultiTF1 class, if the interface to be wrapped is multidimensional.
Following the usual procedure, setting the TF1 though the constructor, will wrap it into a ROOT::Math::IParametricGradFunctionMultiDim.
Example
Random numbers
The MathCore library provides the following classes for generating pseudorandom numbers:
 TRandom: Using a linear congruential random generator.
 TRandom1: Random number generator based on the Ranlux engine.
 TRandom2: Based on the maximally equidistributed combined Tausworthe generator by L’Ecuyer.
 TRandom3: Based on the Mersenne and Twister pseudorandom number generator.
Note
For generating nonuniform random numbers, the UNU.RAN package (see → UNU.RAN) is available.
You can work with the random number generators as follows:
Seeding the random number generators
 Use the SetSeed() method.
When no value is given, the generator default seed is used. In this case an identical sequence will be generated every time the application is run.
When the 0 value is used as seed, then a unique seed is generated using a TUUID, for TRandom
, TRandom1
and TRandom3
.
For TRandom
the seed is generated using only the machine clock, which has a resolution of about 1 s. Therefore, identical sequences will be generated if the elapsed time is less than a second.
Using the random number generators
 Use the Rndm() method for generating a pseudorandom number distributed between 0 and 1.
Example
Random number distributions
The TRandom class provides functions that can be used by all other derived classes to generate random variables according to predefined distributions. In the simplest cases, as in the exponential distribution, the nonuniform random number is obtained by suitable transformations. In the more complicated cases, the random variables are obtained by acceptancerejection methods that require several random numbers.
Example
The following table shows the various distributions that can be generated using methods of the TRandom
classes.
In addition, you can use TF1::GetRandom() or TH1::GetRandom() to generate random numbers distributed according to a user defined function, in a limited interval, or to a user defined histogram.
Distributions  Description 

Double_t Uniform(Double_t x1,Double_t x2)  Uniform random numbers between x1,x2. 
Double_t Gaus(Double_t mu,Double_t sigma)  Gaussian random numbers. Default values: mu=0, sigma=1. 
Double_t Exp(Double_t tau)  Exponential random numbers with mean tau. 
Double_t Landau(Double_t mean,Double_t sigma)  Landau distributed random numbers. Default values: mean=0, sigma=1. 
Double_t BreitWigner(Double_t mean,Double_t gamma)  BreitWigner distributed random numbers. Default values mean=0, gamma=1. 
Int_t Poisson(Double_t mean)  Poisson random numbers. 
Double_t PoissonD(Double_t mean)  Poisson random numbers. 
Int_t Binomial(Int_t ntot,Double_t prob)  Binomial Random numbers 
Circle(Double_t &x,Double_t &y,Double_t r)  Generate a random 2D point (x,y) in a circle of radius r. 
Sphere(Double_t &x,Double_t &y,Double_t &z,Double_t r)  Generate a random 3D point (x,y,z) in a sphere of radius r. 
Rannor(Double_t &a,Double_t &b)  Generate a pair of Gaussian random numbers with mu=0 and sigma=1. 
Complex numbers
The MathCore library provides with TComplex a class for complex numbers.
MathMore library
The MathMore library provides an advanced collection of functions and C++ classes for numerical computing. This is an extension of the functionality provided by the MathCore library. The MathMore library is implemented wrapping in C++ the GNU Scientific Library (GSL). The mathematical functions are implemented as a set of free functions in the namespace ROOT::Math.
The MathMore library includes classes and functions for:

Special functions
Containing all the major functions such as Bessel functions, Legendre polynomial, etc. 
Statistical functions
Contains mathematical functions used in statistics such as probability density functions, cumulative distributions functions and their inverse (quantiles).  Numerical algorithms:

Function approximation (ChebyshevApprox)
Based on Chebyshev polynomials.  Random classes
Linear algebra packages
The linear algebra packages provide a complete environment in ROOT to perform calculations like equation solving and eigenvalue decompositions.
There are the following linear algebra packages available:
Matrix package
The following topics are covered for the matrix package:
 matrix classes
 matrix properties
 creating and filling a matrix
 inverting a matrix
 matrix operators and methods
 matrix views
 matrix decompositions
 matrix Eigen analysis
Matrix classes
ROOT provides the following matrix classes, among others:

TMatrixDBase
: Base class for matrices. 
TMatrixF
: Matrix with single precision (float
). 
TMatrixFSym
: Symmetrical matrix with single precision (float
). 
TVectorF
: Vector with single precision (float
). 
TMatrixD
: Matrix with double precision (double
). 
TMatrixDSym
: Symmetrical matrix with double precision (double
). 
TMatrixDSparse
: Sparse matrix with double precision (double
). 
TDecompBase: Decomposition base class.

TDecompChol: Cholesky decomposition class.
Matrix properties
A matrix has five properties, which are all set in the constructor:

precision
If theprecision
is float (this is single precision), use theTMatrixF
class family. If the precision is double, use theTMatrixD
class family. 
type
Possible values are:general
(TMatrixD
),symmetric
(TMatrixDSym
) orsparse
(TMatrixDSparse
). 
size
Number of rows and columns. 
index
Range start of row and column index. By default these start at 0. 
sparse map
Only relevant for a sparse matrix. It indicates where elements are unequal 0.
You can:
Accessing matrix properties
Use one of the following methods to access the information about the relevant matrix property:

Int_t
GetRowLwb(): Row lowerbound index. 
Int_t
GetRowUpb(): Row upperbound index. 
Int_t
GetNrows(): Number of rows. 
Int_t
GetColLwb(): Column lowerbound index. 
Int_t
GetColUpb(): Column upperbound index. 
Int_t
GetNcols: Number of columns. 
Int_t
GetNoElements(): Number of elements, for a dense matrix this equals:fNrows x fNcols
. 
Double_t
GetTol(): Tolerance number that is used in decomposition operations. 
Int_t
*GetRowIndexArray(): For sparse matrices, access to the row index offNrows+1
entries. 
Int_t
*GetColIndexArray(): For sparse matrices, access to the column index offNelems
entries.
*GetRowIndexArray()
and *GetColIndexArray()
are specific to the sparse matrix, which is implemented according to the Harwell
Boeing format. Here, besides the usual shape/size descriptors of the matrix like fNrows
, fRowLwb
, fNcols
and fColLwb
,
also a row index fRowIndex
and a column index fColIndex
are stored:
fRowIndex[0,..,fNrows]
: Stores for each row the index range of the elements in the data and column array.fColIndex[0,..,fNelems1]
: Stores the column number for each data element != 0.
Setting matrix properties
Use one of the following methods to set a matrix property:

SetTol (Double_t tol)
Sets the tolerance number. 
ResizeTo (Int_t nrows,Int_t ncols, Int_t nr_nonzeros=1)
Changes the matrix shape tonrows x ncols
. Index will start at 0. 
ResizeTo(Int_t row_lwb,Int_t row_upb, Int_t col_lwb,Int_t col_upb, Int_t nr_nonzeros=1)
Changes the matrix shape torow_lwb:row_upb x col_lwb:col_upb
. 
SetRowIndexArray (Int_t *data)
For sparse matrices, it sets the row index. The array data should contain at leastfNrows+1
entries column lowerbound index. 
SetColIndexArray (Int_t *data)
For sparse matrices, it sets the column index. The array data should contain at leastfNelems
entries. 
SetSparseIndex (Int_t nelems new)
Allocates memory for a sparse map ofnelems_new
elements and copies (if exists) at mostnelems_new
matrix elements over to the new structure. 
SetSparseIndex (const TMatrixDBase &a)
Copies the sparse map from matrixa
. 
SetSparseIndexAB (const TMatrixDSparse &a, const TMatrixDSparse &b)
Sets the sparse map to the same map of matrixa
andb
.
Creating and filling a matrix
Use one of the following constructors to create a matrix:
TMatrixD(Int_t nrows,Int_t ncols)
TMatrixD(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb)
TMatrixD(Int_t nrows,Int_t ncols,const Double_t *data, Option_t option= "")
TMatrixD(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb, const Double_t *data,Option_t *option="")
TMatrixDSym(Int_t nrows)
TMatrixDSym(Int_t row_lwb,Int_t row_upb)
TMatrixDSym(Int_t nrows,const Double_t *data,Option_t *option="")
TMatrixDSym(Int_t row_lwb,Int_t row_upb, const Double_t *data, Option_t *opt="")
TMatrixDSparse(Int_t nrows,Int_t ncols)
TMatrixDSparse(Int_t row_lwb,Int_t row_upb,Int_t col_lwb, Int_t col_upb)
TMatrixDSparse(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb, Int_t nr_nonzeros,Int_t *row,Int_t *col,Double_t *data)
Use one of the following methods to fill a matrix:

SetMatrixArray(const Double_t*data,Option_t*option="")
Copies array data. Ifoption="F"
, the array fills the matrix columnwise else rowwise. This option is implemented forTMatrixD
andTMatrixDSym
. It is expected that the array data contains at leastfNelems
entries. 
SetMatrixArray(Int_t nr,Int_t *irow,Int_t *icol,Double_t *data)
Only available for sparse matrices. The three arrays should each containnr
entries with row index, column index and data entry. Only the entries with nonzero data value are inserted. 
operator()
,operator[]
These operators provide the easiest way to fill a matrix but are in particular for a sparse matrix expensive. If no entry for slot (i
,j
) is found in the sparse index table it will be entered, which involves some memory management. Therefore, before invoking this method in a loop set the index table first through a call to theSetSparseIndex()
method. 
SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixDBase &source)
The matrix to be inserted at position (row_lwb
,col_lwb
) can be both, dense or sparse. 
Use()
Allows inserting another matrix or data array without actually copying the data.
The following list shows the application of theUse()
method:Use(TMatrixD &a)
Use(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Double_t *data)
Use(Int_t nrows,Int_t ncols,Double_t *data)
Use(TMatrixDSym &a)
Use(Int_t nrows,Double_t *data)
Use(Int_t row_lwb,Int_t row_upb,Double_t *data)
Use(TMatrixDSparse &a)
Use(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_no nzeros, Int_t *pRowIndex,Int_t *pColIndex,Double_t *pData)
Use(Int_t nrows,Int_t ncols,Int_t nr_nonzeros,Int_t *pRowIndex,Int_t *pColIndex,Double_t *pData)
Example
A Hilbert matrix is created by copying an array.
You can also assign the data array to the matrix without actually copying it.
The array data now contains the inverted matrix.
Now a unit matrix in sparse format is created.
Inverting a matrix
 Use the
Invert(Double_t &det=0)
function to invert a matrix:
– or –
 Use the appropriate constructor to invert a matrix:
Both methods are available for general and symmetric matrices.
For matrices whose size is less than or equal to 6x6, the InvertFast(Double_t &det=0)
function is available. Here the Cramer algorithm will be applied, which is faster but less accurate.
Using decomposition classes for inverting
You can also use the following decomposition classes (see → Matrix decompositions) for inverting a matrix:
Name  Matrix type  Comment 

TDecompLU  General  
TDecompQRH  General  
TDecompSVD  General  Can manipulate singular matrix. 
TDecompBK  symmetric  
TDecompChol  Symmetric  Matrix should also be positive definite. 
TDecompSparse  Sparse 
If the required matrix type is general, you also can handle symmetric matrices.
Example
This example shows how to check whether the matrix is singular before attempting to invert it.
Matrix operators and methods
The matrix/vector operations are classified according to BLAS (basic linear algebra subroutines) levels.
The following operations and methods are available:
 arithmetic operations between matrices
 arithmetic operations between matrices and real numbers
 comparison between two matrices
 comparison between matrix and real number
Arithmetic operations between matrices
Description  Format  Comment 

Element  C=A+B  Overwrites A 
Wise sum  A+=B Add (A,alpha,B) TMatrixD(A,TMatrixD::kPlus,B) 
A = A + α B constructor 
Element wise subtraction  C=AB A=B TMatrixD(A,TMatrixD::kMinus,B) 
Overwrites A Constructor 
Matrix multiplication  C=A*B A*=B C.Mult(A,B) TMatrixD(A,TMatrixD::kMult,B) TMatrixD(A, TMatrixD(A, TMatrixD::kTransposeMult,B) TMatrixD(A, TMatrixD::kMultTranspose,B) 
Overwrites A Constructor of A.B Constructor of A^{T} .B Constructor of A.B^{T} 
Element wise multiplication  ElementMult(A,B)  A(i,j)*= B(i,j) 
Element wise division  ElementDiv(A,B)  A(i,j)/= B(i,j) 
Arithmetic operations between matrices and real numbers
Description  Format  Comment 

Element wise sum  C=r+A C=A+r A+=r  overwrites A 
Element wise subtraction  C=rA C=Ar A=r  overwrites A 
Matrix multiplication  C=r*A C=A*r A*=r  overwrites A 
Comparison between two matrices
Format  Output  Description 

A == B  Bool_t  Equal to 
A != B  matrix  Not equal 
A > B  matrix  Greater than 
A >= B  matrix  Greater than or equal to 
A < B  matrix  Smaller than 
A <= B  matrix  Smaller than or equal to 
AreCompatible(A,B)  Bool_t  Compare matrix properties 
Compare(A,B)  Bool_t  Return summary of comparison 
VerifyMatrixIdentity(A,B,verb, maxDev)  Check matrix identity within maxDev tolerance 
Comparison between matrix and real number
Format  Output  Description 

A == r  Bool_t  Equal to 
A != r  Bool_t  Not equal 
A > r  Bool_t  Greater than 
A >= r  Bool_t  Greater than or equal to 
A < r  Bool_t  Smaller than 
A <= r  Bool_t  Smaller than or equal to 
VerifyMatrixValue(A,r,verb, maxDev)  Bool_t  Compare matrix value with r within maxDev tolerance 
A.RowNorm()  Double_t  Norm induced by the infinity vector norm 
A.NormInf()  Double_t  
A.ColNorm()  Double_t  Norm induced by the 1 vector norm 
A.Norm1()  Double_t  
A.E2Norm()  Double_t  Square of the Euclidean norm 
A.NonZeros()  Int_t  
A.Sum()  Double_t  Number of elements unequal zero 
A.Min()  Double_t  
A.Max()  Double_t  
A.NormByColumn (v,"D")  TMatrixD  
A.NormByRow (v,"D")  TMatrixD 
Matrix views
With the following matrix view classes, you can access the matrix elements:
TMatrixDRow
TMatrixDColumn
TMatrixDDiag
TMatrixDSub
Matrix view operators
For the matrix view classes TMatrixDRow
, TMatrixDColumn
and TMatrixDDiag
, the necessary assignment operators are available to interact with the vector class TVectorD
.
The sub matrix view classes TMatrixDSub
has links to the matrix classes TMatrixD
and TMatrixDSym.
The next table summarizes how to access the individual matrix elements in the matrix view classes.
Format  Comment 

TMatrixDRow(A,i)(j) TMatrixDRow(A,i)[j]  Element A_{ij} 
TMatrixDColumn(A,j)(i) TMatrixDColumn(A,j)[i]  Element A_{ij} 
TMatrixDDiag(A(i) TMatrixDDiag(A[i]  Element A_{ij} 
TMatrixDSub(A(i) TMatrixDSub(A,rl,rh,cl,ch)(i,j)  Element A_{ij} Element A_{rl+i,cl+j} 
Matrix decompositions
There are the following classes available for matrix decompositions:
 TDecompLU: Decomposes a general
n x n
matrixA
intoP A = L U
.  TDecompBK: The BunchKaufman diagonal pivoting method decomposes a real symmetric matrix
A
.  TDecompChol : The Cholesky decomposition class, which decomposes a symmetric, positive definite matrix
A = U^T * U
whereU
is a upper triangular matrix.  TDecompQRH: QR decomposition class.
 TDecompSVD: Single value decomposition class.
 TDecompSparse: Sparse symmetric decomposition class.
Matrix Eigen analysis
With the TMatrixDEigen and TMatrixDSymEigen classes, you can compute eigenvalues and eigenvectors for general dense and symmetric real matrices.
The following table lists the methods of the TMatrixDEigen
and the TMatrixDSymEigen
to obtain the eigenvalues and eigenvectors. TMatrixDSymEigen
constructors can only be called with TMatrixDSym
:
Format  Output  Description 

eig.GetEigenVectors()  TMatrixD  Eigenvectors for both TMatrixDEigen and TMatrixDSymEigen. 
eig.GetEigenValues()  TVectorD  Eigenvalues vector for TMatrixDSymEigen. 
eig.GetEigenValues()  TMatrixD  Eigenvalues matrix for TMatrixDEigen. 
eig.GetEigenValuesRe()  TVectorD  Real part of eigenvalues for TMatrixDEigen. 
eig.GetEigenValuesIm()  TVectorD  Imaginary part of eigenvalues for TMatrixDEigen. 
Example
The usage of the eigenvalue class is shown in this example where it is checked that the square of the singular values of
a matrix c
are identical to the eigenvalues of c<sup>T</sup>.c
:
SMatrix
SMatrix is a C++ package for high performance vector and matrix computations. It can be used only in problems when the size of the matrices is known at compile time, like in the tracking reconstruction of HEP experiments. It is based on a C++ technique, called expression templates, to achieve an high level optimization. The C++ templates can be used to implement vector and matrix expressions such that these expressions can be transformed at compile time to code which is equivalent to hand optimized code in a lowlevel language like FORTRAN or C.
The SMatrix has been developed initially by T. Glebe of the MaxPlanckInstitut, Heidelberg, as part of the HeraB analysis framework. A subset of the original package has been now incorporated in the ROOT distribution, with the aim to provide to the LHC experiments a standalone and high performance matrix package for reconstruction. The API of the current package differs from the original one, in order to be compliant to the ROOT coding conventions.
SMatrix contains the following generic classes for describing matrices and vectors of arbitrary dimensions and of arbitrary type:
SVector
The template class ROOT::Math::SVector represents ndimensional vectors for objects of arbitrary type. The class has two template parameters that define their properties at compile time:
 Type of the contained elements (for example
float
ordouble
).  Size of the vector.
Creating a vector
Use one of the following constructors to create a vector:
 Default constructor for a zero vector (all elements equal to zero).
 Constructor (and assignment) from a vector expression, like
v=p*q+w
. Due to the expression template technique, no temporary objects are created in this operation.  Constructor by passing directly the elements. This is possible only for vectors up to size 10.
 Constructor from an iterator copying the data referred by the iterator. It is possible to specify the begin and end of the iterator or the begin and the size. Note that for
Example The namespace ROOT::Math is used.
SMatrix
The template class ROOT::Math::SMatrix represents a matrix of arbitrary type with nrows x ncol
dimension. The class has four template parameters that define their properties at compile time:
 type of the contained elements (for example
float
ordouble
)  number of rows
 number of columns
 representation type
Creating a matrix
Use one of the following constructors to create a matrix:
 Default constructor for a zero matrix (all elements equal to zero).
 Constructor of an identity matrix.
 Copy constructor (and assignment) for a matrix with the same representation, or from a different one when possible, for example from a symmetric to a general matrix.
 Constructor (and assignment) from a matrix expression, like
D=A*B+C
. Due to the expression template technique, no temporary objects are created in this operation. In the case of an operation likeA=A*B+C
, a temporary object is needed and it is created automatically to store the intermediary result in order to preserve the validity of this operation.  Constructor from a generic STLlike iterator copying the data referred by the iterator, following its order. It is both possible, to specify the begin and end of the iterator or the begin and the size. In case of a symmetric matrix, it is required only the triangular block and the user can specify whether giving a block representing the lower (default case) or the upper diagonal part.
Example Typedef’s are used in this example to avoid the full C++ names for the matrix classes. For a general matrix the representation has the default value ROOT::Math::MatRepStd. For a general square matrix, the number of columns can be omitted.
Example
A symmetric matrix is filled from a std::vector
.
Minimization libraries and classes
ROOT provides several minimization libraries and classes:
TMinuit
The Minuit minimization package was originally written in Fortran by Fred James and part of PACKLIB (patch D506). It has been converted to the C++ class TMinuit , by R.Brun.
Topical manual
For TMinuit, a topical manual it available at Topical Manual  TMinuit.
It contains indepth information about TMinuit.
Minuit2 library
The Minuit2 library is a new objectoriented implementation, written in C++, of the popular MINUIT minimization package. These new version provides basically all the functionality present in the old Fortran version, with almost equivalent numerical accuracy and computational performances.
Furthermore, it contains new functionality, like the possibility to set single side parameter limits or the FUMILI algorithm (see → FUMILI minimization package), which is an optimized method for least square and log likelihood minimizations. The package has been originally developed by M. Winkler and F. James.
Topical manuals
For Minuit2, topical manuals are available at Topical Manuals  Minuit2.
They contain indepth information about Minuit2.
FUMILI minimization package
FUMILI is used to minimize Chisquare function or to search maximum of likelihood function.
FUMILI is based on ideas, proposed by I.N. Silin. It was converted from FORTRAN to C by Sergey Yaschenko s.yaschenko@fzjuelich.de.
For detailed information on the FUMILI minimization package, see → TFumili class reference.
UNU.RAN
UNU.RAN (Universal Non Uniform RAndom Number generator for generating nonuniform pseudorandom numbers) contains universal (also called automatic or blackbox) algorithms that can generate random numbers from large classes of continuous (in one or multidimensions), discrete distributions, empirical distributions (like histograms) and also from practically all standard distributions.
UNU.RAN is an ANSI C library licensed under GPL.
The TUnuran class is used to interface the UNURAN package.
→ Unuran tutorialsTutorials
Initializing TUnuran with string API
You can initialize UNU.RAN with the string API via TUnuran::Init(), passing the distribution type and the method.
Example
Using TUnuranContDist for a onedimensional distribution
 Use TUnuranContDist
for creating a continuous 1D distribution object (for example from a TF1
object providing the PDF (probability density function).
You can provide additional information via TUnuranContDist::SetDomain(min,max) like thedomain()
for generating numbers in a restricted region.
Example
Using TUnuranMultiContDist for a multidimensional distribution
 Use TUnuranMultiContDist to create a multidimensional distribution, which can be created from a multidimensional PDF (probability density function).
Example
Using TUnuranDiscrDist for a discrete onedimensional distribution
 Use TUnuranDiscrDist to create a discrete onedimensional distribution, which can be initialized from a TF1 object or from a vector of probabilities.
Example
Using TUnuranEmpDist an empirical distribution
Use TUnuranEmpDist for creating an empirical distribution, which can be initialized from a TH1 object (using the bins or from its buffer for unbinned data) or from a vector of data.
Example