TMatrix
class description - source file - inheritance tree
protected:
void Allocate(Int_t nrows, Int_t ncols, Int_t row_lwb = 0, Int_t col_lwb = 0)
void AMultB(const TMatrix& a, const TMatrix& b)
void AtMultB(const TMatrix& a, const TMatrix& b)
void Invalidate()
void Invert(const TMatrix& m)
void Transpose(const TMatrix& m)
public:
TMatrix TMatrix(Int_t nrows, Int_t ncols)
TMatrix TMatrix(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb)
TMatrix TMatrix(const TMatrix& another)
TMatrix TMatrix(TMatrix::EMatrixCreatorsOp1 op, const TMatrix& prototype)
TMatrix TMatrix(const TMatrix& a, TMatrix::EMatrixCreatorsOp2 op, const TMatrix& b)
TMatrix TMatrix()
TMatrix TMatrix(const TLazyMatrix& lazy_constructor)
virtual void ~TMatrix()
TMatrix& Abs()
TMatrix& Apply(TElementPosAction& action)
TMatrix& Apply(TElementAction& action)
TClass* Class()
Double_t ColNorm() const
Double_t Determinant() const
Double_t E2Norm() const
Int_t GetColLwb() const
Int_t GetColUpb() const
Int_t GetNcols() const
Int_t GetNoElements() const
Int_t GetNrows() const
Int_t GetRowLwb() const
Int_t GetRowUpb() const
TMatrix& HilbertMatrix()
TMatrix& Invert(Double_t* determ_ptr = 0)
virtual TClass* IsA() const
Bool_t IsValid() const
void Mult(const TMatrix& a, const TMatrix& b)
Double_t Norm1() const
Double_t NormInf() const
Bool_t operator!=(Real_t val) const
const Real_t& operator()(Int_t rown, Int_t coln) const
TMatrix& operator*=(const TMatrixDiag& diag)
TMatrix& operator*=(const TMatrix& source)
TMatrix& operator*=(Double_t val)
TMatrix& operator+=(Double_t val)
TMatrix& operator-=(Double_t val)
Bool_t operator<(Real_t val) const
Bool_t operator<=(Real_t val) const
TMatrix& operator=(const TMatrix& source)
TMatrix& operator=(const TLazyMatrix& source)
TMatrix& operator=(Real_t val)
Bool_t operator==(Real_t val) const
Bool_t operator>(Real_t val) const
Bool_t operator>=(Real_t val) const
virtual void Print(Option_t* option)
void ResizeTo(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb)
void ResizeTo(const TMatrix& m)
void ResizeTo(Int_t nrows, Int_t ncols)
Double_t RowNorm() const
virtual void ShowMembers(TMemberInspector& insp, char* parent)
TMatrix& Sqr()
TMatrix& Sqrt()
virtual void Streamer(TBuffer& b)
TMatrix& UnitMatrix()
TMatrix& Zero()
protected:
Int_t fNrows number of rows
Int_t fNcols number of columns
Int_t fNelems number of elements in matrix
Int_t fRowLwb lower bound of the row index
Int_t fColLwb lower bound of the col index
Real_t* fElements elements themselves
Real_t** fIndex index[i] = &matrix(0,i) (col index)
public:
static const TMatrix::EMatrixCreatorsOp1 kZero
static const TMatrix::EMatrixCreatorsOp1 kUnit
static const TMatrix::EMatrixCreatorsOp1 kTransposed
static const TMatrix::EMatrixCreatorsOp1 kInverted
static const TMatrix::EMatrixCreatorsOp2 kMult
static const TMatrix::EMatrixCreatorsOp2 kTransposeMult
static const TMatrix::EMatrixCreatorsOp2 kInvMult
static const TMatrix::EMatrixCreatorsOp2 kAtBA
Linear Algebra Package
The present package implements all the basic algorithms dealing
with vectors, matrices, matrix columns, rows, diagonals, etc.
Matrix elements are arranged in memory in a COLUMN-wise
fashion (in FORTRAN's spirit). In fact, it makes it very easy to
feed the matrices to FORTRAN procedures, which implement more
elaborate algorithms.
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. TMatrix m(1,10,1,5) defines a matrix that ranges, and
that can be addresses, from 1..10, 1..5 (a(1,1)..a(10,5)).
The present package provides all facilities to completely AVOID
returning matrices. Use "TMatrix A(TMatrix::kTransposed,B);" and
other fancy constructors as much as possible. If one really needs
to return a matrix, return a TLazyMatrix object instead. The
conversion is completely transparent to the end user, e.g.
"TMatrix m = THaarMatrix(5);" and _is_ efficient.
Since TMatrix et al. are fully integrated in ROOT they of course
can be stored in a ROOT database.
How to efficiently use this package
-----------------------------------
1. Never return complex objects (matrices or vectors)
Danger: For example, when the following snippet:
TMatrix foo(int n)
{
TMatrix foom(n,n); fill_in(foom); return foom;
}
TMatrix 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, TLazyMatrices (see below) can construct
TMatrix m "inplace", with only a _single_ call to the
constructor.
2. Use "two-address instructions"
"void TMatrix::operator += (const TMatrix &B);"
as much as possible.
That is, to add two matrices, it's much more efficient to write
A += B;
than
TMatrix C = A + B;
(if both operand should be preserved,
TMatrix C = A; C += B;
is still better).
3. Use glorified constructors when returning of an object seems
inevitable:
"TMatrix A(TMatrix::kTransposed,B);"
"TMatrix C(A,TMatrix::kTransposeMult,B);"
like in the following snippet (from $ROOTSYS/test/vmatrix.cxx)
that verifies that for an orthogonal matrix T, T'T = TT' = E.
TMatrix haar = THaarMatrix(5);
TMatrix unit(TMatrix::kUnit,haar);
TMatrix haar_t(TMatrix::kTransposed,haar);
TMatrix hth(haar,TMatrix::kTransposeMult,haar);
TMatrix hht(haar,TMatrix::kMult,haar_t);
TMatrix hht1 = haar; hht1 *= haar_t;
VerifyMatrixIdentity(unit,hth);
VerifyMatrixIdentity(unit,hht);
VerifyMatrixIdentity(unit,hht1);
4. Accessing row/col/diagonal of a matrix without much fuss
(and without moving a lot of stuff around):
TMatrix m(n,n); TVector v(n); TMatrixDiag(m) += 4;
v = TMatrixRow(m,0);
TMatrixColumn m1(m,1); m1(2) = 3; // the same as m(2,1)=3;
Note, constructing of, say, TMatrixDiag 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 TMatrix &m)
{
TMatrix m1(TMatrix::kZero,m);
struct MakeHilbert : public TElementPosAction {
void Operation(Real_t &element) { element = 1./(fI+fJ-1); }
};
m1.Apply(MakeHilbert());
}
of course, using a special method TMatrix::HilbertMatrix() 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:
void foo(TMatrix &m, TMatrix &m1)
{
typedef double (*dfunc_t)(double);
class ApplyFunction : public TElementAction {
dfunc_t *fFunc;
void Operation(Real_t &element) { element=fFunc(element); }
public:
ApplyFunction(dfunc_t func):fFunc(func) {}
};
m.Apply(ApplyFunction(TMath::Sin));
m1.Apply(ApplyFunction(TMath::Cos));
}
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:
TMatrix haar = THaarMatrix(5);
THaarMatrix() 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. THaarMatrix() constructs a
TLazyMatrix, an object of just a few bytes long. A
"TMatrix(const TLazyMatrix &recipe)" constructor follows the
recipe and makes the matrix haar() right in place. No matrix
element is moved whatsoever!
The implementation is based on original code by
Oleg E. Kiselyov (oleg@pobox.com).
void Allocate(Int_t no_rows, Int_t no_cols, Int_t row_lwb, Int_t col_lwb)
Allocate new matrix. Arguments are number of rows, columns, row
lowerbound (0 default) and column lowerbound (0 default).
~TMatrix()
TMatrix destructor.
void ResizeTo(Int_t nrows, Int_t ncols)
Erase the old matrix and create a new one according to new boundaries
with indexation starting at 0.
void ResizeTo(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb)
Erase the old matrix and create a new one according to new boudaries.
TMatrix(EMatrixCreatorsOp1 op, const TMatrix &prototype)
Create a matrix applying a specific operation to the prototype.
Example: TMatrix a(10,12); ...; TMatrix b(TMatrix::kTransposed, a);
Supported operations are: kZero, kUnit, kTransposed and kInverted.
TMatrix(const TMatrix &a, EMatrixCreatorsOp2 op, const TMatrix &b)
Create a matrix applying a specific operation to two prototypes.
Example: TMatrix a(10,12), b(12,5); ...; TMatrix c(a, TMatrix::kMult, b);
Supported operations are: kMult (a*b), kTransposeMult (a'*b),
kInvMult (a^(-1)*b) and kAtBA (a'*b*a).
Double_t RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
The norm is induced by the infinity vector norm.
Double_t ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
The norm is induced by the 1 vector norm.
Double_t E2Norm() const
Square of the Euclidian norm, SUM{ m(i,j)^2 }.
void Print(Option_t *)
Print the matrix as a table of elements (zeros are printed as dots).
void Transpose(const TMatrix &prototype)
Transpose a matrix.
void Invert(const TMatrix &m)
Allocate new matrix and set it to inv(m).
void AMultB(const TMatrix &a, const TMatrix &b)
General matrix multiplication. Create a matrix C such that C = A * B.
Note, matrix C needs to be allocated.
void Mult(const TMatrix &a, const TMatrix &b)
Compute C = A*B. The same as AMultB(), only matrix C is already
allocated, and it is *this.
void AtMultB(const TMatrix &a, const TMatrix &b)
Create a matrix C such that C = A' * B. In other words,
c[i,j] = SUM{ a[k,i] * b[k,j] }. Note, matrix C needs to be allocated.
Double_t Determinant() const
Compute the determinant of a general square matrix.
Example: Matrix A; Double_t A.Determinant();
Gauss-Jordan transformations of the matrix with a slight
modification to take advantage of the *column*-wise arrangement
of Matrix elements. Thus we eliminate matrix's columns rather than
rows in the Gauss-Jordan transformations. Note that determinant
is invariant to matrix transpositions.
The matrix is copied to a special object of type TMatrixPivoting,
where all Gauss-Jordan eliminations with full pivoting are to
take place.
void Streamer(TBuffer &R__b)
Stream an object of class TMatrix.
TMatrix(Int_t no_rows, Int_t no_cols)
TMatrix(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb)
TMatrix(const TLazyMatrix &lazy_constructor)
Bool_t IsValid() const
TMatrix(const TMatrix &another)
void ResizeTo(const TMatrix &m)
Inline Functions
void Invalidate()
TMatrix TMatrix(const TLazyMatrix& lazy_constructor)
Int_t GetRowLwb() const
Int_t GetRowUpb() const
Int_t GetNrows() const
Int_t GetColLwb() const
Int_t GetColUpb() const
Int_t GetNcols() const
Int_t GetNoElements() const
const Real_t& operator()(Int_t rown, Int_t coln) const
TMatrix& operator=(const TMatrix& source)
TMatrix& operator=(const TLazyMatrix& source)
TMatrix& operator=(Real_t val)
TMatrix& operator-=(Double_t val)
TMatrix& operator+=(Double_t val)
TMatrix& operator*=(Double_t val)
Bool_t operator==(Real_t val) const
Bool_t operator!=(Real_t val) const
Bool_t operator<(Real_t val) const
Bool_t operator<=(Real_t val) const
Bool_t operator>(Real_t val) const
Bool_t operator>=(Real_t val) const
TMatrix& Zero()
TMatrix& Abs()
TMatrix& Sqr()
TMatrix& Sqrt()
TMatrix& Apply(TElementAction& action)
TMatrix& Apply(TElementPosAction& action)
TMatrix& Invert(Double_t* determ_ptr = 0)
TMatrix& UnitMatrix()
TMatrix& HilbertMatrix()
TMatrix& operator*=(const TMatrix& source)
TMatrix& operator*=(const TMatrixDiag& diag)
Double_t NormInf() const
Double_t Norm1() const
TClass* Class()
TClass* IsA() const
void ShowMembers(TMemberInspector& insp, char* parent)
Author: Fons Rademakers 03/11/97
Last update: 2.22/01 20/05/99 16.31.50 by Rene Brun
Copyright (c) 1995-1999, The ROOT System, All rights reserved. *
ROOT page - Class index - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.