Logo ROOT   6.10/09
Reference Guide
TMatrixTBase.h
Go to the documentation of this file.
1 // @(#)root/matrix:$Id$
2 // Authors: Fons Rademakers, Eddy Offermann Nov 2003
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TMatrixTBase
13 #define ROOT_TMatrixTBase
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TMatrixTBase //
18 // //
19 // Template of base class in the linear algebra package //
20 // //
21 // matrix properties are stored here, however the data storage is part //
22 // of the derived classes //
23 // //
24 //////////////////////////////////////////////////////////////////////////
25 
26 //======================================================================//
27 // Summary of the streamer version history //
28 //======================================================================//
29 // 3.10/02 4.00/a 4.00/b 4.00/c 4.00-08 5.05-1 //
30 // TMatrixFBase - 2 2 2 4 5 //
31 // TMatrix 2 3 3 3 3 4 //
32 // TMatrixF - 3 3 3 3 4 //
33 // TMatrixFSym - 1 1 1 1 2 //
34 // TMatrixDSparse - - - - - 2 //
35 // //
36 // TMatrixDBase - 2 3 3 4 5 //
37 // TMatrixD 2 3 3 3 3 4 //
38 // TMatrixDSym - 1 1 1 1 2 //
39 // TMatrixDSparse - - 1 1 1 2 //
40 // //
41 // TVector 2 3 3 3 3 4 //
42 // TVectorF - 2 2 2 3 4 //
43 // //
44 // TVectorD 2 2 2 2 3 4 //
45 //======================================================================//
46 // //
47 // 4.00/a : (Jan 25 2004) introduced new classes/inheritance scheme, //
48 // TMatrix now inherits from TMatrixF //
49 // //
50 // TMatrixF::TMatrixFBase //
51 // TMatrixFSym::TMatrixFBase //
52 // TMatrixD::TMatrixDBase //
53 // TMatrixDSym::TMatrixDBase //
54 // //
55 // 4.00/b : (May 12 2004) introduced TMatrixDSparse and added new //
56 // element fNRowIndex to TMatrixFBase and TMatrixDBase //
57 // TMatrixDSparse::TMatrixDBase //
58 // //
59 // 4.00/c : (May 27 2004) Used the TObject::fBits to store validity //
60 // state for vectors and matrices //
61 // //
62 // 5.05-1 : templates TMatrixTBase,TMatrixT,TMatrixTSym and //
63 // TMatrixTSparse were introduced, all versions were //
64 // increased by 1 . //
65 // //
66 //======================================================================//
67 
68 #include <limits>
69 
70 #include "TNamed.h"
71 #include "TMatrixFBasefwd.h"
72 #include "TMatrixDBasefwd.h"
73 #include "TVectorFfwd.h"
74 #include "TVectorDfwd.h"
75 #include "TError.h"
76 
77 template<class Element> class TVectorT;
78 template<class Element> class TElementActionT;
79 template<class Element> class TElementPosActionT;
80 
82 
83 template<class Element> class TMatrixTBase : public TObject {
84 
85 private:
86  Element *GetElements(); // This function is now obsolete (and is not implemented) you should use TMatrix::GetMatrixArray().
87 
88 protected:
89  Int_t fNrows; // number of rows
90  Int_t fNcols; // number of columns
91  Int_t fRowLwb; // lower bound of the row index
92  Int_t fColLwb; // lower bound of the col index
93  Int_t fNelems; // number of elements in matrix
94  Int_t fNrowIndex; // length of row index array (= fNrows+1) wich is only used for sparse matrices
95 
96  Element fTol; // sqrt(epsilon); epsilon is smallest number number so that 1+epsilon > 1
97  // fTol is used in matrix decomposition (like in inversion)
98 
99  Bool_t fIsOwner; //!default kTRUE, when Use array kFALSE
100 
101  static void DoubleLexSort (Int_t n,Int_t *first,Int_t *second,Element *data);
102  static void IndexedLexSort(Int_t n,Int_t *first,Int_t swapFirst,
103  Int_t *second,Int_t swapSecond,Int_t *index);
104 
105  enum {kSizeMax = 25}; // size data container on stack, see New_m(),Delete_m()
106  enum {kWorkMax = 100}; // size of work array's in several routines
107 
109  kStatus = BIT(14) // set if matrix object is valid
110  };
111 
112 public:
113 
115  fNrows(0), fNcols(0), fRowLwb(0), fColLwb(0), fNelems(0), fNrowIndex(0),
116  fTol(0), fIsOwner(kTRUE) { }
117 
118  virtual ~TMatrixTBase() {}
119 
120  inline Int_t GetRowLwb () const { return fRowLwb; }
121  inline Int_t GetRowUpb () const { return fNrows+fRowLwb-1; }
122  inline Int_t GetNrows () const { return fNrows; }
123  inline Int_t GetColLwb () const { return fColLwb; }
124  inline Int_t GetColUpb () const { return fNcols+fColLwb-1; }
125  inline Int_t GetNcols () const { return fNcols; }
126  inline Int_t GetNoElements () const { return fNelems; }
127  inline Element GetTol () const { return fTol; }
128 
129  virtual const Element *GetMatrixArray () const = 0;
130  virtual Element *GetMatrixArray () = 0;
131  virtual const Int_t *GetRowIndexArray() const = 0;
132  virtual Int_t *GetRowIndexArray() = 0;
133  virtual const Int_t *GetColIndexArray() const = 0;
134  virtual Int_t *GetColIndexArray() = 0;
135 
137  virtual TMatrixTBase<Element> &SetColIndexArray(Int_t *data) = 0;
138  virtual TMatrixTBase<Element> &SetMatrixArray (const Element *data,Option_t *option="");
139  inline Element SetTol (Element tol);
140 
141  virtual void Clear (Option_t *option="") = 0;
142 
143  inline void Invalidate () { SetBit(kStatus); }
144  inline void MakeValid () { ResetBit(kStatus); }
145  inline Bool_t IsValid () const { return !TestBit(kStatus); }
146  inline Bool_t IsOwner () const { return fIsOwner; }
147  virtual Bool_t IsSymmetric() const;
148 
149  virtual TMatrixTBase<Element> &GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
150  TMatrixTBase<Element> &target,Option_t *option="S") const = 0;
151  virtual TMatrixTBase<Element> &SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source) = 0;
152 
153  virtual void GetMatrix2Array(Element *data,Option_t *option="") const;
154  virtual TMatrixTBase<Element> &InsertRow (Int_t row,Int_t col,const Element *v,Int_t n = -1);
155  virtual void ExtractRow (Int_t row,Int_t col, Element *v,Int_t n = -1) const;
156 
157  virtual TMatrixTBase<Element> &Shift (Int_t row_shift,Int_t col_shift);
158  virtual TMatrixTBase<Element> &ResizeTo (Int_t nrows,Int_t ncols,Int_t nr_nonzeros=-1) = 0;
159  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;
160 
161  virtual Double_t Determinant() const { AbstractMethod("Determinant()"); return 0.; }
162  virtual void Determinant(Double_t &d1,Double_t &d2) const { AbstractMethod("Determinant()"); d1 = 0.; d2 = 0.; }
163 
164  virtual TMatrixTBase<Element> &Zero ();
165  virtual TMatrixTBase<Element> &Abs ();
166  virtual TMatrixTBase<Element> &Sqr ();
167  virtual TMatrixTBase<Element> &Sqrt ();
168  virtual TMatrixTBase<Element> &UnitMatrix ();
169 
170  virtual TMatrixTBase<Element> &NormByDiag (const TVectorT<Element> &v,Option_t *option="D");
171 
172  virtual Element RowNorm () const;
173  virtual Element ColNorm () const;
174  virtual Element E2Norm () const;
175  inline Element NormInf () const { return RowNorm(); }
176  inline Element Norm1 () const { return ColNorm(); }
177  virtual Int_t NonZeros () const;
178  virtual Element Sum () const;
179  virtual Element Min () const;
180  virtual Element Max () const;
181 
182  void Draw (Option_t *option=""); // *MENU*
183  void Print(Option_t *name ="") const; // *MENU*
184 
185  virtual Element operator()(Int_t rown,Int_t coln) const = 0;
186  virtual Element &operator()(Int_t rown,Int_t coln) = 0;
187 
188  Bool_t operator==(Element val) const;
189  Bool_t operator!=(Element val) const;
190  Bool_t operator< (Element val) const;
191  Bool_t operator<=(Element val) const;
192  Bool_t operator> (Element val) const;
193  Bool_t operator>=(Element val) const;
194 
195  virtual TMatrixTBase<Element> &Apply(const TElementActionT<Element> &action);
197 
198  virtual TMatrixTBase<Element> &Randomize(Element alpha,Element beta,Double_t &seed);
199 
200  // make it public since it can be called by TMatrixTRow
201  static Element & NaNValue();
202 
203  ClassDef(TMatrixTBase,5) // Matrix base class (template)
204 };
205 
206 #ifndef __CINT__
207 // When building with -fmodules, it instantiates all pending instantiations,
208 // instead of delaying them until the end of the translation unit.
209 // We 'got away with' probably because the use and the definition of the
210 // explicit specialization do not occur in the same TU.
211 //
212 // In case we are building with -fmodules, we need to forward declare the
213 // specialization in order to compile the dictionary G__Matrix.cxx.
214 template <> TClass *TMatrixTBase<double>::Class();
215 #endif // __CINT__
216 
217 
218 template<class Element> Element TMatrixTBase<Element>::SetTol(Element newTol)
219 {
220  const Element oldTol = fTol;
221  if (newTol >= 0.0)
222  fTol = newTol;
223  return oldTol;
224 }
225 
226 template<class Element> Bool_t operator== (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
227 template<class Element> Element E2Norm (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
228 template<class Element1,class Element2>
230 template<class Element> void Compare (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
231 
232 // Service functions (useful in the verification code).
233 // They print some detail info if the validation condition fails
234 
235 template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,
236  Int_t verbose,Element maxDevAllow);
237 template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,Int_t verbose)
238  { return VerifyMatrixValue(m,val,verbose,Element(0.)); }
239 template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val)
240  { return VerifyMatrixValue(m,val,1,Element(0.)); }
241 template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2,
242  Int_t verbose,Element maxDevAllow);
244  { return VerifyMatrixIdentity(m1,m2,verbose,Element(0.)); }
245 template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2)
246  { return VerifyMatrixIdentity(m1,m2,1,Element(0.)); }
247 
248 #endif
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
virtual const Element * GetMatrixArray() const =0
virtual TMatrixTBase< Element > & Sqrt()
Take square root of all elements.
virtual void Clear(Option_t *option="")=0
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
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...
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
Int_t GetRowLwb() const
Definition: TMatrixTBase.h:120
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *option="")
Copy array data to matrix .
Element fTol
Definition: TMatrixTBase.h:96
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
Bool_t operator!=(Element val) const
Are all matrix elements not equal to val?
const char Option_t
Definition: RtypesCore.h:62
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:159
#define BIT(n)
Definition: Rtypes.h:75
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *data)=0
TVectorT.
Definition: TMatrixTBase.h:77
Bool_t VerifyMatrixValue(const TMatrixTBase< Element > &m, Element val, Int_t verbose, Element maxDevAllow)
Validate that all elements of matrix have value val within maxDevAllow.
virtual TMatrixTBase< Element > & Sqr()
Square each element of the matrix.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Bool_t operator>(Element val) const
Are all matrix elements > val?
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros=-1)=0
virtual TMatrixTBase< Element > & SetRowIndexArray(Int_t *data)=0
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:687
double beta(double x, double y)
Calculates the beta function.
Element NormInf() const
Definition: TMatrixTBase.h:175
void Invalidate()
Definition: TMatrixTBase.h:143
Int_t GetNoElements() const
Definition: TMatrixTBase.h:126
Bool_t operator<(Element val) const
Are all matrix elements < val?
Bool_t VerifyMatrixIdentity(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2, Int_t verbose, Element maxDevAllow)
Verify that elements of the two matrices are equal within MaxDevAllow .
static void DoubleLexSort(Int_t n, Int_t *first, Int_t *second, Element *data)
default kTRUE, when Use array kFALSE
virtual Double_t Determinant() const
Definition: TMatrixTBase.h:161
#define ClassDef(name, id)
Definition: Rtypes.h:297
Int_t GetColLwb() const
Definition: TMatrixTBase.h:123
Bool_t IsOwner() const
Definition: TMatrixTBase.h:146
Bool_t operator>=(Element val) const
Are all matrix elements >= val?
Bool_t AreCompatible(const TMatrixTBase< Element1 > &m1, const TMatrixTBase< Element2 > &m2, Int_t verbose=0)
Check that matrice sm1 and m2 areboth valid and have identical shapes .
Int_t fNrowIndex
Definition: TMatrixTBase.h:94
Element Norm1() const
Definition: TMatrixTBase.h:176
virtual Element operator()(Int_t rown, Int_t coln) const =0
virtual TMatrixTBase< Element > & Apply(const TElementActionT< Element > &action)
Apply action to each matrix element.
Element GetTol() const
Definition: TMatrixTBase.h:127
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &v, Option_t *option="D")
option:
const double tol
virtual void Determinant(Double_t &d1, Double_t &d2) const
Definition: TMatrixTBase.h:162
SVector< double, 2 > v
Definition: Dict.h:5
virtual TMatrixTBase< Element > & SetSub(Int_t row_lwb, Int_t col_lwb, const TMatrixTBase< Element > &source)=0
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.
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.
TMarker * m
Definition: textangle.C:8
bool verbose
virtual void GetMatrix2Array(Element *data, Option_t *option="") const
Copy matrix data to array .
virtual Element Min() const
return minimum matrix element value
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
Randomize matrix element values.
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
Bool_t operator<=(Element val) const
Are all matrix elements <= val?
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition: TObject.cxx:166
R__EXTERN Int_t gMatrixCheck
Definition: TMatrixTBase.h:79
Int_t GetColUpb() const
Definition: TMatrixTBase.h:124
virtual ~TMatrixTBase()
Definition: TMatrixTBase.h:118
Linear Algebra Package.
Element * GetElements()
Int_t GetNrows() const
Definition: TMatrixTBase.h:122
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
virtual Element E2Norm() const
Square of the Euclidian norm, SUM{ m(i,j)^2 }.
virtual Element Sum() const
Compute sum of elements.
double Double_t
Definition: RtypesCore.h:55
void MakeValid()
Definition: TMatrixTBase.h:144
Bool_t fIsOwner
Definition: TMatrixTBase.h:99
void Print(Option_t *name="") const
Print the matrix as a table of elements.
void Draw(Option_t *option="")
Draw this matrix The histogram is named "TMatrixT" by default and no title.
Mother of all ROOT objects.
Definition: TObject.h:37
#define R__EXTERN
Definition: DllImport.h:27
static void 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.
Bool_t operator==(Element val) const
Are all matrix elements equal to val?
static Element & NaNValue()
Int_t GetRowUpb() const
Definition: TMatrixTBase.h:121
void ResetBit(UInt_t f)
Definition: TObject.h:158
Definition: first.py:1
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
TClass * Class()
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
const Bool_t kTRUE
Definition: RtypesCore.h:91
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don&#39;t want to leave purely abstract...
Definition: TObject.cxx:915
virtual Element Max() const
return maximum vector element value
const Int_t n
Definition: legend1.C:16
Element SetTol(Element tol)
Definition: TMatrixTBase.h:218
Bool_t IsValid() const
Definition: TMatrixTBase.h:145
virtual const Int_t * GetColIndexArray() const =0
virtual const Int_t * GetRowIndexArray() const =0
virtual TMatrixTBase< Element > & Abs()
Take an absolute value of a matrix, i.e. apply Abs() to each element.