ROOT » MATH » MATRIX » TMatrixTBase<double>

class TMatrixTBase<double>: public TObject

Linear Algebra Package

The present package implements all the basic algorithms dealing
with vectors, matrices, matrix columns, rows, diagonals, etc.
In addition eigen-Vector analysis and several matrix decomposition
have been added (LU,QRH,Cholesky,Bunch-Kaufman and SVD) .
The decompositions are used in matrix inversion, equation solving.

For a dense matrix, elements are arranged in memory in a ROW-wise
fashion . For (n x m) matrices where n*m <=kSizeMax (=25 currently)
storage space is available on the stack, thus avoiding expensive
allocation/deallocation of heap space . However, this introduces of
course kSizeMax overhead for each matrix object . If this is an
issue recompile with a new appropriate value (>=0) for kSizeMax

Sparse matrices are also stored in row-wise fashion but additional
row/column information is stored, see TMatrixTSparse source for
additional details .

Another way to assign and store matrix data is through Use
see for instance stressLinear.cxx file .

Unless otherwise specified, matrix and vector indices always start
with 0, spanning up to the specified limit-1. However, there are
constructors to which one can specify aribtrary lower and upper
bounds, e.g. TMatrixD m(1,10,1,5) defines a matrix that ranges
from 1..10, 1..5 (a(1,1)..a(10,5)).

The present package provides all facilities to completely AVOID
returning matrices. Use "TMatrixD A(TMatrixD::kTransposed,B);"
 and other fancy constructors as much as possible. If one really needs
to return a matrix, return a TMatrixTLazy object instead. The
conversion is completely transparent to the end user, e.g.
"TMatrixT m = THaarMatrixT(5);" and _is_ efficient.

Since TMatrixT et al. are fully integrated in ROOT, they of course
can be stored in a ROOT database.

For usage examples see $ROOTSYS/test/stressLinear.cxx


1. Oleg E. Kiselyov
First implementations were based on the his code . We have diverged
quite a bit since then but the ideas/code for lazy matrix and
"nested function" are 100% his .
You can see him and his code in action at
2. Chris R. Birchenhall,
We adapted his idea of the implementation for the decomposition
classes instead of our messy installation of matrix inversion
His installation of matrix condition number, using an iterative
scheme using the Hage algorithm is worth looking at !
Chris has a nice writeup ( on his matrix classes at
3. Mark Fischler and Steven Haywood of CLHEP
They did the slave labor of spelling out all sub-determinants
for Cramer inversion  of (4x4),(5x5) and (6x6) matrices
The stack storage for small matrices was also taken from them
4. Roldan Pozo of TNT (
He converted the EISPACK routines for the eigen-vector analysis to
C++ . We started with his implementation
5. Siegmund Brandt (
We adapted his (very-well) documented SVD routines

How to efficiently use this package

1. Never return complex objects (matrices or vectors)
Danger: For example, when the following snippet:
TMatrixD foo(int n)
TMatrixD foom(n,n); fill_in(foom); return foom;
TMatrixD m = foo(5);
runs, it constructs matrix foo:foom, copies it onto stack as a
return value and destroys foo:foom. Return value (a matrix)
from foo() is then copied over to m (via a copy constructor),
and the return value is destroyed. So, the matrix constructor is
called 3 times and the destructor 2 times. For big matrices,
the cost of multiple constructing/copying/destroying of objects
may be very large. *Some* optimized compilers can cut down on 1
copying/destroying, but still it leaves at least two calls to
the constructor. Note, TMatrixDLazy (see below) can construct
TMatrixD m "inplace", with only a _single_ call to the

2. Use "two-address instructions"
"void TMatrixD::operator += (const TMatrixD &B);"
as much as possible.
That is, to add two matrices, it's much more efficient to write
A += B;
TMatrixD C = A + B;
(if both operand should be preserved,
TMatrixD C = A; C += B;
is still better).

3. Use glorified constructors when returning of an object seems
"TMatrixD A(TMatrixD::kTransposed,B);"
"TMatrixD C(A,TMatrixD::kTransposeMult,B);"

like in the following snippet (from $ROOTSYS/test/vmatrix.cxx)
that verifies that for an orthogonal matrix T, T'T = TT' = E.

TMatrixD haar = THaarMatrixD(5);
TMatrixD unit(TMatrixD::kUnit,haar);
TMatrixD haar_t(TMatrixD::kTransposed,haar);
TMatrixD hth(haar,TMatrixD::kTransposeMult,haar);
TMatrixD hht(haar,TMatrixD::kMult,haar_t);
TMatrixD hht1 = haar; hht1 *= haar_t;

4. Accessing row/col/diagonal of a matrix without much fuss
(and without moving a lot of stuff around):

TMatrixD m(n,n); TVectorD v(n); TMatrixDDiag(m) += 4;
v = TMatrixDRow(m,0);
TMatrixDColumn m1(m,1); m1(2) = 3; // the same as m(2,1)=3;
Note, constructing of, say, TMatrixDDiag does *not* involve any
copying of any elements of the source matrix.

5. It's possible (and encouraged) to use "nested" functions
For example, creating of a Hilbert matrix can be done as follows:

void foo(const TMatrixD &m)
TMatrixD m1(TMatrixD::kZero,m);
struct MakeHilbert : public TElementPosActionD {
void Operation(Double_t &element)
{ element = 1./(fI+fJ-1); }

of course, using a special method THilbertMatrixD() is
still more optimal, but not by a whole lot. And that's right,
class MakeHilbert is declared *within* a function and local to
that function. It means one can define another MakeHilbert class
(within another function or outside of any function, that is, in
the global scope), and it still will be OK. Note, this currently
is not yet supported by the interpreter CINT.

Another example is applying of a simple function to each matrix

void foo(TMatrixD &m,TMatrixD &m1)
typedef  double (*dfunc_t)(double);
class ApplyFunction : public TElementActionD {
dfunc_t fFunc;
void Operation(Double_t &element)
{ element=fFunc(element); }
ApplyFunction(dfunc_t func):fFunc(func) {}
ApplyFunction x(TMath::Sin);

Validation code $ROOTSYS/test/vmatrix.cxx and vvector.cxx contain
a few more examples of that kind.

6. Lazy matrices: instead of returning an object return a "recipe"
how to make it. The full matrix would be rolled out only when
and where it's needed:
TMatrixD haar = THaarMatrixD(5);
THaarMatrixD() is a *class*, not a simple function. However
similar this looks to a returning of an object (see note #1
above), it's dramatically different. THaarMatrixD() constructs a
TMatrixDLazy, an object of just a few bytes long. A special
"TMatrixD(const TMatrixDLazy &recipe)" constructor follows the
recipe and makes the matrix haar() right in place. No matrix
element is moved whatsoever!

Function Members (Methods)

    This is an abstract class, constructors will not be documented.
    Look at the header to check for available constructors.

virtual TMatrixTBase<double>&Abs()
voidTObject::AbstractMethod(const char* method) const
virtual voidTObject::AppendPad(Option_t* option = "")
virtual TMatrixTBase<double>&Apply(const TElementActionT<double>& action)
virtual TMatrixTBase<double>&Apply(const TElementPosActionT<double>& action)
virtual voidTObject::Browse(TBrowser* b)
static TClass*Class()
virtual const char*TObject::ClassName() const
virtual voidClear(Option_t* option = "")
virtual TObject*TObject::Clone(const char* newname = "") const
virtual doubleColNorm() const
virtual Int_tTObject::Compare(const TObject* obj) const
virtual voidTObject::Copy(TObject& object) const
virtual voidTObject::Delete(Option_t* option = "")MENU
virtual Double_tDeterminant() const
virtual voidDeterminant(Double_t& d1, Double_t& d2) const
virtual Int_tTObject::DistancetoPrimitive(Int_t px, Int_t py)
virtual voidDraw(Option_t* option = "")MENU
virtual voidTObject::DrawClass() constMENU
virtual TObject*TObject::DrawClone(Option_t* option = "") constMENU
virtual voidTObject::Dump() constMENU
virtual doubleE2Norm() const
virtual voidTObject::Error(const char* method, const char* msgfmt) const
virtual voidTObject::Execute(const char* method, const char* params, Int_t* error = 0)
virtual voidTObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0)
virtual voidTObject::ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual voidExtractRow(Int_t row, Int_t col, double* v, Int_t n = -1) const
virtual voidTObject::Fatal(const char* method, const char* msgfmt) const
virtual TObject*TObject::FindObject(const char* name) const
virtual TObject*TObject::FindObject(const TObject* obj) const
virtual const Int_t*GetColIndexArray() const
virtual Int_t*GetColIndexArray()
Int_tGetColLwb() const
Int_tGetColUpb() const
virtual Option_t*TObject::GetDrawOption() const
static Long_tTObject::GetDtorOnly()
virtual const char*TObject::GetIconName() const
virtual voidGetMatrix2Array(double* data, Option_t* option = "") const
virtual const double*GetMatrixArray() const
virtual double*GetMatrixArray()
virtual const char*TObject::GetName() const
Int_tGetNcols() const
Int_tGetNoElements() const
Int_tGetNrows() const
virtual char*TObject::GetObjectInfo(Int_t px, Int_t py) const
static Bool_tTObject::GetObjectStat()
virtual Option_t*TObject::GetOption() const
virtual const Int_t*GetRowIndexArray() const
virtual Int_t*GetRowIndexArray()
Int_tGetRowLwb() const
Int_tGetRowUpb() const
virtual TMatrixTBase<double>&GetSub(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, TMatrixTBase<double>& target, Option_t* option = "S") const
virtual const char*TObject::GetTitle() const
doubleGetTol() const
virtual UInt_tTObject::GetUniqueID() const
virtual Bool_tTObject::HandleTimer(TTimer* timer)
virtual ULong_tTObject::Hash() const
virtual voidTObject::Info(const char* method, const char* msgfmt) const
virtual Bool_tTObject::InheritsFrom(const char* classname) const
virtual Bool_tTObject::InheritsFrom(const TClass* cl) const
virtual TMatrixTBase<double>&InsertRow(Int_t row, Int_t col, const double* v, Int_t n = -1)
virtual voidTObject::Inspect() constMENU
voidTObject::InvertBit(UInt_t f)
virtual TClass*IsA() const
virtual Bool_tTObject::IsEqual(const TObject* obj) const
virtual Bool_tTObject::IsFolder() const
Bool_tTObject::IsOnHeap() const
Bool_tIsOwner() const
virtual Bool_tTObject::IsSortable() const
virtual Bool_tIsSymmetric() const
Bool_tIsValid() const
Bool_tTObject::IsZombie() const
virtual voidTObject::ls(Option_t* option = "") const
virtual doubleMax() const
voidTObject::MayNotUse(const char* method) const
virtual doubleMin() const
static double&NaNValue()
virtual Int_tNonZeros() const
doubleNorm1() const
virtual TMatrixTBase<double>&NormByDiag(const TVectorT<double>& v, Option_t* option = "D")
doubleNormInf() const
virtual Bool_tTObject::Notify()
voidTObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const
voidTObject::operator delete(void* ptr)
voidTObject::operator delete(void* ptr, void* vp)
voidTObject::operator delete[](void* ptr)
voidTObject::operator delete[](void* ptr, void* vp)
void*TObject::operator new(size_t sz)
void*TObject::operator new(size_t sz, void* vp)
void*TObject::operator new[](size_t sz)
void*TObject::operator new[](size_t sz, void* vp)
Bool_toperator!=(double val) const
virtual doubleoperator()(Int_t rown, Int_t coln) const
virtual double&operator()(Int_t rown, Int_t coln)
Bool_toperator<(double val) const
Bool_toperator<=(double val) const
TMatrixTBase<double>&operator=(const TMatrixTBase<double>&)
Bool_toperator==(double val) const
Bool_toperator>(double val) const
Bool_toperator>=(double val) const
virtual voidTObject::Paint(Option_t* option = "")
virtual voidTObject::Pop()
virtual voidPrint(Option_t* name = "") constMENU
virtual TMatrixTBase<double>&Randomize(double alpha, double beta, Double_t& seed)
virtual Int_tTObject::Read(const char* name)
virtual voidTObject::RecursiveRemove(TObject* obj)
voidTObject::ResetBit(UInt_t f)
virtual TMatrixTBase<double>&ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros = -1)
virtual TMatrixTBase<double>&ResizeTo(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Int_t nr_nonzeros = -1)
virtual doubleRowNorm() const
virtual voidTObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU
virtual voidTObject::SavePrimitive(ostream& out, Option_t* option = "")
voidTObject::SetBit(UInt_t f)
voidTObject::SetBit(UInt_t f, Bool_t set)
virtual TMatrixTBase<double>&SetColIndexArray(Int_t* data)
virtual voidTObject::SetDrawOption(Option_t* option = "")MENU
static voidTObject::SetDtorOnly(void* obj)
virtual TMatrixTBase<double>&SetMatrixArray(const double* data, Option_t* option = "")
static voidTObject::SetObjectStat(Bool_t stat)
virtual TMatrixTBase<double>&SetRowIndexArray(Int_t* data)
virtual TMatrixTBase<double>&SetSub(Int_t row_lwb, Int_t col_lwb, const TMatrixTBase<double>& source)
doubleSetTol(double tol)
virtual voidTObject::SetUniqueID(UInt_t uid)
virtual TMatrixTBase<double>&Shift(Int_t row_shift, Int_t col_shift)
virtual voidShowMembers(TMemberInspector& insp) const
virtual TMatrixTBase<double>&Sqr()
virtual TMatrixTBase<double>&Sqrt()
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
virtual doubleSum() const
virtual voidTObject::SysError(const char* method, const char* msgfmt) const
Bool_tTObject::TestBit(UInt_t f) const
Int_tTObject::TestBits(UInt_t f) const
TMatrixTBase<double>(const TMatrixTBase<double>&)
virtual TMatrixTBase<double>&UnitMatrix()
virtual voidTObject::UseCurrentStyle()
virtual voidTObject::Warning(const char* method, const char* msgfmt) const
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0)
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const
virtual TMatrixTBase<double>&Zero()
virtual voidTObject::DoError(int level, const char* location, const char* fmt, va_list va) const
static voidDoubleLexSort(Int_t n, Int_t* first, Int_t* second, double* data)
static voidIndexedLexSort(Int_t n, Int_t* first, Int_t swapFirst, Int_t* second, Int_t swapSecond, Int_t* index)

Data Members

static TObject::(anonymous)TObject::kBitMask
static TObject::EStatusBitsTObject::kCanDelete
static TObject::EStatusBitsTObject::kCannotPick
static TObject::EStatusBitsTObject::kHasUUID
static TObject::EStatusBitsTObject::kInvalidObject
static TObject::(anonymous)TObject::kIsOnHeap
static TObject::EStatusBitsTObject::kIsReferenced
static TObject::EStatusBitsTObject::kMustCleanup
static TObject::EStatusBitsTObject::kNoContextMenu
static TObject::(anonymous)TObject::kNotDeleted
static TObject::EStatusBitsTObject::kObjInCanvas
static TObject::(anonymous)TObject::kOverwrite
static TObject::(anonymous)TObject::kSingleKey
static TObject::(anonymous)TObject::kWriteDelete
static TObject::(anonymous)TObject::kZombie
Int_tfColLwblower bound of the col index
Bool_tfIsOwner!default kTRUE, when Use array kFALSE
Int_tfNcolsnumber of columns
Int_tfNelemsnumber of elements in matrix
Int_tfNrowIndexlength of row index array (= fNrows+1) wich is only used for sparse matrices
Int_tfNrowsnumber of rows
Int_tfRowLwblower bound of the row index
doublefTolsqrt(epsilon); epsilon is smallest number number so that 1+epsilon > 1
static TMatrixTBase<double>::(anonymous)kSizeMax
static TMatrixTBase<double>::EMatrixStatusBitskStatus
static TMatrixTBase<double>::(anonymous)kWorkMax

Class Charts

Inheritance Inherited Members Includes Libraries
Class Charts

Function documentation

void TMatrixTBase<Element> DoubleLexSort(Int_t n, Int_t* first, Int_t* second, double* data)
 Lexical sort on array data using indices first and second
void TMatrixTBase<Element> IndexedLexSort(Int_t n, Int_t* first, Int_t swapFirst, Int_t* second, Int_t swapSecond, Int_t* index)
 Lexical sort on array data using indices first and second
TMatrixTBase<Element> &TMatrixTBase<Element> SetMatrixArray(const double* data, Option_t* option = "")
 Copy array data to matrix . It is assumed that array is of size >= fNelems
 (=)))) fNrows*fNcols
 option indicates how the data is stored in the array:
 option =
          'F'   : column major (Fortran) m[i][j] = array[i+j*fNrows]
          else  : row major    (C)       m[i][j] = array[i*fNcols+j] (default)
Bool_t TMatrixTBase<Element> IsSymmetric() const
 Check whether matrix is symmetric
void TMatrixTBase<Element> GetMatrix2Array(double* data, Option_t* option = "") const
 Copy matrix data to array . It is assumed that array is of size >= fNelems
 (=)))) fNrows*fNcols
 option indicates how the data is stored in the array:
 option =
          'F'   : column major (Fortran) array[i+j*fNrows] = m[i][j]
          else  : row major    (C)       array[i*fNcols+j] = m[i][j] (default)
TMatrixTBase<Element> &TMatrixTBase<Element> InsertRow(Int_t row, Int_t col, const double* v, Int_t n = -1)
 Copy n elements from array v to row rown starting at column coln
void TMatrixTBase<Element> ExtractRow(Int_t row, Int_t col, double* v, Int_t n = -1) const
 Store in array v, n matrix elements of row rown starting at column coln
TMatrixTBase<Element> &TMatrixTBase<Element> Shift(Int_t row_shift, Int_t col_shift)
 Shift the row index by adding row_shift and the column index by adding
 col_shift, respectively. So [rowLwb..rowUpb][colLwb..colUpb] becomes
TMatrixTBase<Element> &TMatrixTBase<Element> Zero()
 Set matrix elements to zero
TMatrixTBase<Element> &TMatrixTBase<Element> Abs()
 Take an absolute value of a matrix, i.e. apply Abs() to each element.
TMatrixTBase<Element> &TMatrixTBase<Element> Sqr()
 Square each element of the matrix.
TMatrixTBase<Element> &TMatrixTBase<Element> Sqrt()
 Take square root of all elements.
TMatrixTBase<Element> &TMatrixTBase<Element> UnitMatrix()
 Make a unit matrix (matrix need not be a square one).
TMatrixTBase<Element> &TMatrixTBase<Element> NormByDiag(const TVectorT<double>& v, Option_t* option = "D")
 "D"   :  b(i,j) = a(i,j)/sqrt(abs*(v(i)*v(j)))  (default)
 else  :  b(i,j) = a(i,j)*sqrt(abs*(v(i)*v(j)))  (default)
Element TMatrixTBase<Element> RowNorm() const
 Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
 The norm is induced by the infinity vector norm.
Element TMatrixTBase<Element> ColNorm() const
 Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
 The norm is induced by the 1 vector norm.
Element TMatrixTBase<Element> E2Norm() const
 Square of the Euclidian norm, SUM{ m(i,j)^2 }.
Int_t TMatrixTBase<Element> NonZeros() const
 Compute the number of elements != 0.0
Element TMatrixTBase<Element> Sum() const
 Compute sum of elements
Element TMatrixTBase<Element> Min() const
 return minimum matrix element value
Element TMatrixTBase<Element> Max() const
 return maximum vector element value
void TMatrixTBase<Element> Draw(Option_t* option = "")
 Draw this matrix
 The histogram is named "TMatrixT" by default and no title
void TMatrixTBase<Element> Print(Option_t* name = "") const
 Print the matrix as a table of elements.
 By default the format "%11.4g" is used to print one element.
 One can specify an alternative format with eg
  option ="f=  %6.2f  "
Bool_t TMatrixTBase<Element> operator==(double val) const
 Are all matrix elements equal to val?
Bool_t TMatrixTBase<Element> operator!=(double val) const
 Are all matrix elements not equal to val?
Bool_t TMatrixTBase<Element> operator<(double val) const
 Are all matrix elements < val?
Bool_t TMatrixTBase<Element> operator<=(double val) const
 Are all matrix elements <= val?
Bool_t TMatrixTBase<Element> operator>(double val) const
 Are all matrix elements > val?
Bool_t TMatrixTBase<Element> operator>=(double val) const
 Are all matrix elements >= val?
TMatrixTBase<Element> &TMatrixTBase<Element> Apply(const TElementActionT<Element> &action)
 Apply action to each matrix element
TMatrixTBase<Element> &TMatrixTBase<Element> Apply(const TElementPosActionT<Element> &action)
 Apply action to each element of the matrix. To action the location
 of the current element is passed.
TMatrixTBase<Element> &TMatrixTBase<Element> Randomize(double alpha, double beta, Double_t& seed)
 Randomize matrix element values
void TMatrixTBase<Element> Streamer(TBuffer& )
 Stream an object of class TMatrixTBase<Element>.
Element & TMatrixTBase<Element> NaNValue()
template<class Element> Element TMatrixTBase<Element> SetTol(double tol)
Element * GetElements()
Int_t GetRowLwb() const
{ return fRowLwb; }
Int_t GetRowUpb() const
{ return fNrows+fRowLwb-1; }
Int_t GetNrows() const
{ return fNrows; }
Int_t GetColLwb() const
{ return fColLwb; }
Int_t GetColUpb() const
{ return fNcols+fColLwb-1; }
Int_t GetNcols() const
{ return fNcols; }
Int_t GetNoElements() const
{ return fNelems; }
Element GetTol() const
{ return fTol; }
const Element * GetMatrixArray() const
Element * GetMatrixArray()
const Int_t * GetRowIndexArray() const
Int_t * GetRowIndexArray()
const Int_t * GetColIndexArray() const
Int_t * GetColIndexArray()
TMatrixTBase<Element> & SetRowIndexArray(Int_t* data)
TMatrixTBase<Element> & SetColIndexArray(Int_t* data)
void Clear(Option_t* option = "")
void Invalidate()
void MakeValid()
Bool_t IsValid() const
{ return !TestBit(kStatus); }
Bool_t IsOwner() const
{ return fIsOwner; }
TMatrixTBase<Element> & GetSub(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, TMatrixTBase<double>& target, Option_t* option = "S") const
TMatrixTBase<Element> & ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros = -1)
TMatrixTBase<Element> & ResizeTo(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Int_t nr_nonzeros = -1)
Double_t Determinant() const
{ AbstractMethod("Determinant()"); return 0.; }
void Determinant(Double_t& d1, Double_t& d2) const
{ AbstractMethod("Determinant()"); d1 = 0.; d2 = 0.; }
Element NormInf() const
{ return RowNorm(); }
Element Norm1() const
{ return ColNorm(); }
Element operator()(Int_t rown, Int_t coln) const
Element & operator()(Int_t rown, Int_t coln)