ROOT  6.06/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 #ifndef ROOT_TNamed
71 #include "TNamed.h"
72 #endif
73 #ifndef ROOT_TMatrixFBasefwd
74 #include "TMatrixFBasefwd.h"
75 #endif
76 #ifndef ROOT_TMatrixDBasefwd
77 #include "TMatrixDBasefwd.h"
78 #endif
79 #ifndef ROOT_TVectorFfwd
80 #include "TVectorFfwd.h"
81 #endif
82 #ifndef ROOT_TVectorDfwd
83 #include "TVectorDfwd.h"
84 #endif
85 #ifndef ROOT_TError
86 #include "TError.h"
87 #endif
88 
89 template<class Element> class TVectorT;
90 template<class Element> class TElementActionT;
91 template<class Element> class TElementPosActionT;
92 
94 
95 template<class Element> class TMatrixTBase : public TObject {
96 
97 private:
98  Element *GetElements(); // This function is now obsolete (and is not implemented) you should use TMatrix::GetMatrixArray().
99 
100 protected:
101  Int_t fNrows; // number of rows
102  Int_t fNcols; // number of columns
103  Int_t fRowLwb; // lower bound of the row index
104  Int_t fColLwb; // lower bound of the col index
105  Int_t fNelems; // number of elements in matrix
106  Int_t fNrowIndex; // length of row index array (= fNrows+1) wich is only used for sparse matrices
107 
108  Element fTol; // sqrt(epsilon); epsilon is smallest number number so that 1+epsilon > 1
109  // fTol is used in matrix decomposition (like in inversion)
110 
111  Bool_t fIsOwner; //!default kTRUE, when Use array kFALSE
112 
113  static void DoubleLexSort (Int_t n,Int_t *first,Int_t *second,Element *data);
114  static void IndexedLexSort(Int_t n,Int_t *first,Int_t swapFirst,
115  Int_t *second,Int_t swapSecond,Int_t *index);
116 
117  enum {kSizeMax = 25}; // size data container on stack, see New_m(),Delete_m()
118  enum {kWorkMax = 100}; // size of work array's in several routines
119 
121  kStatus = BIT(14) // set if matrix object is valid
122  };
123 
124 public:
125 
127  fNrows(0), fNcols(0), fRowLwb(0), fColLwb(0), fNelems(0), fNrowIndex(0),
128  fTol(0), fIsOwner(kTRUE) { }
129 
130  virtual ~TMatrixTBase() {}
131 
132  inline Int_t GetRowLwb () const { return fRowLwb; }
133  inline Int_t GetRowUpb () const { return fNrows+fRowLwb-1; }
134  inline Int_t GetNrows () const { return fNrows; }
135  inline Int_t GetColLwb () const { return fColLwb; }
136  inline Int_t GetColUpb () const { return fNcols+fColLwb-1; }
137  inline Int_t GetNcols () const { return fNcols; }
138  inline Int_t GetNoElements () const { return fNelems; }
139  inline Element GetTol () const { return fTol; }
140 
141  virtual const Element *GetMatrixArray () const = 0;
142  virtual Element *GetMatrixArray () = 0;
143  virtual const Int_t *GetRowIndexArray() const = 0;
144  virtual Int_t *GetRowIndexArray() = 0;
145  virtual const Int_t *GetColIndexArray() const = 0;
146  virtual Int_t *GetColIndexArray() = 0;
147 
148  virtual TMatrixTBase<Element> &SetRowIndexArray(Int_t *data) = 0;
149  virtual TMatrixTBase<Element> &SetColIndexArray(Int_t *data) = 0;
150  virtual TMatrixTBase<Element> &SetMatrixArray (const Element *data,Option_t *option="");
151  inline Element SetTol (Element tol);
152 
153  virtual void Clear (Option_t *option="") = 0;
154 
155  inline void Invalidate () { SetBit(kStatus); }
156  inline void MakeValid () { ResetBit(kStatus); }
157  inline Bool_t IsValid () const { return !TestBit(kStatus); }
158  inline Bool_t IsOwner () const { return fIsOwner; }
159  virtual Bool_t IsSymmetric() const;
160 
161  virtual TMatrixTBase<Element> &GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
162  TMatrixTBase<Element> &target,Option_t *option="S") const = 0;
163  virtual TMatrixTBase<Element> &SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source) = 0;
164 
165  virtual void GetMatrix2Array(Element *data,Option_t *option="") const;
166  virtual TMatrixTBase<Element> &InsertRow (Int_t row,Int_t col,const Element *v,Int_t n = -1);
167  virtual void ExtractRow (Int_t row,Int_t col, Element *v,Int_t n = -1) const;
168 
169  virtual TMatrixTBase<Element> &Shift (Int_t row_shift,Int_t col_shift);
170  virtual TMatrixTBase<Element> &ResizeTo (Int_t nrows,Int_t ncols,Int_t nr_nonzeros=-1) = 0;
171  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;
172 
173  virtual Double_t Determinant() const { AbstractMethod("Determinant()"); return 0.; }
174  virtual void Determinant(Double_t &d1,Double_t &d2) const { AbstractMethod("Determinant()"); d1 = 0.; d2 = 0.; }
175 
176  virtual TMatrixTBase<Element> &Zero ();
177  virtual TMatrixTBase<Element> &Abs ();
178  virtual TMatrixTBase<Element> &Sqr ();
179  virtual TMatrixTBase<Element> &Sqrt ();
180  virtual TMatrixTBase<Element> &UnitMatrix ();
181 
182  virtual TMatrixTBase<Element> &NormByDiag (const TVectorT<Element> &v,Option_t *option="D");
183 
184  virtual Element RowNorm () const;
185  virtual Element ColNorm () const;
186  virtual Element E2Norm () const;
187  inline Element NormInf () const { return RowNorm(); }
188  inline Element Norm1 () const { return ColNorm(); }
189  virtual Int_t NonZeros () const;
190  virtual Element Sum () const;
191  virtual Element Min () const;
192  virtual Element Max () const;
193 
194  void Draw (Option_t *option=""); // *MENU*
195  void Print(Option_t *name ="") const; // *MENU*
196 
197  virtual Element operator()(Int_t rown,Int_t coln) const = 0;
198  virtual Element &operator()(Int_t rown,Int_t coln) = 0;
199 
200  Bool_t operator==(Element val) const;
201  Bool_t operator!=(Element val) const;
202  Bool_t operator< (Element val) const;
203  Bool_t operator<=(Element val) const;
204  Bool_t operator> (Element val) const;
205  Bool_t operator>=(Element val) const;
206 
207  virtual TMatrixTBase<Element> &Apply(const TElementActionT<Element> &action);
209 
210  virtual TMatrixTBase<Element> &Randomize(Element alpha,Element beta,Double_t &seed);
211 
212  // make it public since it can be called by TMatrixTRow
213  static Element & NaNValue();
214 
215  ClassDef(TMatrixTBase,5) // Matrix base class (template)
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
Int_t GetRowLwb() const
Definition: TMatrixTBase.h:132
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 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 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 > & SetMatrixArray(const Element *data, Option_t *option="")
Copy array data to matrix .
Element fTol
Definition: TMatrixTBase.h:108
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
Int_t GetRowUpb() const
Definition: TMatrixTBase.h:133
const char Option_t
Definition: RtypesCore.h:62
virtual Element E2Norm() const
Square of the Euclidian norm, SUM{ m(i,j)^2 }.
#define BIT(n)
Definition: Rtypes.h:120
Bool_t operator<=(Element val) const
Are all matrix elements <= val?
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *data)=0
Int_t GetNoElements() const
Definition: TMatrixTBase.h:138
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
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
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:732
double beta(double x, double y)
Calculates the beta function.
void Invalidate()
Definition: TMatrixTBase.h:155
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
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 .
Element Norm1() const
Definition: TMatrixTBase.h:188
Bool_t IsValid() const
Definition: TMatrixTBase.h:157
Element GetTol() const
Definition: TMatrixTBase.h:139
Bool_t operator<(Element val) const
Are all matrix elements < val?
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual Element Sum() const
Compute sum of elements.
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:106
Int_t GetColUpb() const
Definition: TMatrixTBase.h:136
Bool_t operator==(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Check to see if two matrices are identical.
virtual Element operator()(Int_t rown, Int_t coln) const =0
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
virtual TMatrixTBase< Element > & Apply(const TElementActionT< Element > &action)
Apply action to each matrix element.
Bool_t IsOwner() const
Definition: TMatrixTBase.h:158
void Print(Option_t *name="") const
Print the matrix as a table of elements.
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &v, Option_t *option="D")
option: "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)
const double tol
static void DoubleLexSort(Int_t n, Int_t *first, Int_t *second, Element *data)
default kTRUE, when Use array kFALSE
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 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.
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
Bool_t operator==(Element val) const
Are all matrix elements equal to val?
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
TMarker * m
Definition: textangle.C:8
bool verbose
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
Randomize matrix element values.
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don't want to leave purely abstract...
Definition: TObject.cxx:960
virtual void Determinant(Double_t &d1, Double_t &d2) const
Definition: TMatrixTBase.h:174
R__EXTERN Int_t gMatrixCheck
Definition: TMatrixTBase.h:91
Int_t GetColLwb() const
Definition: TMatrixTBase.h:135
virtual ~TMatrixTBase()
Definition: TMatrixTBase.h:130
Element * GetElements()
virtual Element Min() const
return minimum matrix element value
double Double_t
Definition: RtypesCore.h:55
void MakeValid()
Definition: TMatrixTBase.h:156
void Compare(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Compare two matrices and print out the result of the comparison.
Bool_t operator>=(Element val) const
Are all matrix elements >= val?
virtual Element Max() const
return maximum vector element value
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
Bool_t fIsOwner
Definition: TMatrixTBase.h:111
#define name(a, b)
Definition: linkTestLib0.cpp:5
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:58
#define R__EXTERN
Definition: DllImport.h:27
virtual Double_t Determinant() const
Definition: TMatrixTBase.h:173
Bool_t operator>(Element val) const
Are all matrix elements > val?
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.
Element NormInf() const
Definition: TMatrixTBase.h:187
static Element & NaNValue()
void ResetBit(UInt_t f)
Definition: TObject.h:172
Bool_t operator!=(Element val) const
Are all matrix elements not equal to val?
const Bool_t kTRUE
Definition: Rtypes.h:91
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 Int_t n
Definition: legend1.C:16
Element SetTol(Element tol)
Definition: TMatrixTBase.h:218
Element E2Norm(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Square of the Euclidian norm of the difference between two matrices.
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.
virtual void GetMatrix2Array(Element *data, Option_t *option="") const
Copy matrix data to array .