template<class Element>
class TMatrixTBase< Element >
Linear Algebra Package.
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
Acknowledgements
- 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 http://okmij.org/ftp
- 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 (matdoc.ps) on his matrix classes at ftp://ftp.mcc.ac.uk/pub/matclass/
- 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
- Roldan Pozo of TNT (http://math.nist.gov/tnt/) He converted the EISPACK routines for the eigen-vector analysis to C++ . We started with his implementation
- Siegmund Brandt (http://siux00.physik.uni-siegen.de/~brandt/datan 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 foom(n,n); fill_in(foom);
return foom;
}
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 constructor.
#### 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
than
(if both operand should be preserved, TMatrixD C = A; C += B; is still better).
#### 3. Use glorified constructors when returning of an object seems inevitable:
"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.
4. Accessing row/col/diagonal of a matrix without much fuss
(and without moving a lot of stuff around):
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 Operation(Double_t &element)
{ element = 1./(fI+fJ-1); }
};
m1.Apply(MakeHilbert());
}
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 element:
{
typedef double (*dfunc_t)(double);
dfunc_t fFunc;
void Operation(Double_t &element)
{ element=fFunc(element); }
public:
ApplyFunction(dfunc_t func):fFunc(func) {}
};
}
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:
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!
Definition at line 25 of file TMatrixDBasefwd.h.
|
| TMatrixTBase () |
|
virtual | ~TMatrixTBase () |
|
virtual TMatrixTBase< Element > & | Abs () |
| Take an absolute value of a matrix, i.e. apply Abs() to each element. More...
|
|
virtual TMatrixTBase< Element > & | Apply (const TElementActionT< Element > &action) |
| Apply action to each matrix element. More...
|
|
virtual TMatrixTBase< Element > & | Apply (const TElementPosActionT< Element > &action) |
| Apply action to each element of the matrix. More...
|
|
virtual void | Clear (Option_t *option="")=0 |
|
virtual Element | ColNorm () const |
| Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}. More...
|
|
virtual Double_t | Determinant () const |
|
virtual void | Determinant (Double_t &d1, Double_t &d2) const |
|
void | Draw (Option_t *option="") |
| Draw this matrix The histogram is named "TMatrixT" by default and no title. More...
|
|
virtual Element | E2Norm () const |
| Square of the Euclidian norm, SUM{ m(i,j)^2 }. More...
|
|
virtual void | ExtractRow (Int_t row, Int_t col, Element *v, Int_t n=-1) const |
| Store in array v, n matrix elements of row rown starting at column coln. More...
|
|
virtual const Int_t * | GetColIndexArray () const =0 |
|
virtual Int_t * | GetColIndexArray ()=0 |
|
Int_t | GetColLwb () const |
|
Int_t | GetColUpb () const |
|
virtual void | GetMatrix2Array (Element *data, Option_t *option="") const |
| Copy matrix data to array . More...
|
|
virtual const Element * | GetMatrixArray () const =0 |
|
virtual Element * | GetMatrixArray ()=0 |
|
Int_t | GetNcols () const |
|
Int_t | GetNoElements () const |
|
Int_t | GetNrows () const |
|
virtual const Int_t * | GetRowIndexArray () const =0 |
|
virtual Int_t * | GetRowIndexArray ()=0 |
|
Int_t | GetRowLwb () const |
|
Int_t | GetRowUpb () const |
|
virtual TMatrixTBase< Element > & | GetSub (Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, TMatrixTBase< Element > &target, Option_t *option="S") const =0 |
|
Element | GetTol () const |
|
virtual TMatrixTBase< Element > & | InsertRow (Int_t row, Int_t col, const Element *v, Int_t n=-1) |
| Copy n elements from array v to row rown starting at column coln. More...
|
|
void | Invalidate () |
|
Bool_t | IsOwner () const |
|
virtual Bool_t | IsSymmetric () const |
| Check whether matrix is symmetric. More...
|
|
Bool_t | IsValid () const |
|
void | MakeValid () |
|
virtual Element | Max () const |
| return maximum vector element value More...
|
|
virtual Element | Min () const |
| return minimum matrix element value More...
|
|
virtual Int_t | NonZeros () const |
| Compute the number of elements != 0.0. More...
|
|
Element | Norm1 () const |
|
virtual TMatrixTBase< Element > & | NormByDiag (const TVectorT< Element > &v, Option_t *option="D") |
| option: More...
|
|
Element | NormInf () const |
|
Bool_t | operator!= (Element val) const |
| Are all matrix elements not equal to val? More...
|
|
virtual Element | operator() (Int_t rown, Int_t coln) const =0 |
|
virtual Element & | operator() (Int_t rown, Int_t coln)=0 |
|
Bool_t | operator< (Element val) const |
| Are all matrix elements < val? More...
|
|
Bool_t | operator<= (Element val) const |
| Are all matrix elements <= val? More...
|
|
Bool_t | operator== (Element val) const |
| Are all matrix elements equal to val? More...
|
|
Bool_t | operator> (Element val) const |
| Are all matrix elements > val? More...
|
|
Bool_t | operator>= (Element val) const |
| Are all matrix elements >= val? More...
|
|
void | Print (Option_t *name="") const |
| Print the matrix as a table of elements. More...
|
|
virtual TMatrixTBase< Element > & | Randomize (Element alpha, Element beta, Double_t &seed) |
| Randomize matrix element values. More...
|
|
virtual TMatrixTBase< Element > & | ResizeTo (Int_t nrows, Int_t ncols, Int_t nr_nonzeros=-1)=0 |
|
virtual TMatrixTBase< Element > & | ResizeTo (Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Int_t nr_nonzeros=-1)=0 |
|
virtual Element | RowNorm () const |
| Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}. More...
|
|
virtual TMatrixTBase< Element > & | SetColIndexArray (Int_t *data)=0 |
|
virtual TMatrixTBase< Element > & | SetMatrixArray (const Element *data, Option_t *option="") |
| Copy array data to matrix . More...
|
|
virtual TMatrixTBase< Element > & | SetRowIndexArray (Int_t *data)=0 |
|
virtual TMatrixTBase< Element > & | SetSub (Int_t row_lwb, Int_t col_lwb, const TMatrixTBase< Element > &source)=0 |
|
Element | SetTol (Element tol) |
|
virtual 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. More...
|
|
virtual TMatrixTBase< Element > & | Sqr () |
| Square each element of the matrix. More...
|
|
virtual TMatrixTBase< Element > & | Sqrt () |
| Take square root of all elements. More...
|
|
virtual Element | Sum () const |
| Compute sum of elements. More...
|
|
virtual TMatrixTBase< Element > & | UnitMatrix () |
| Make a unit matrix (matrix need not be a square one). More...
|
|
virtual TMatrixTBase< Element > & | Zero () |
| Set matrix elements to zero. More...
|
|