Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "TError.h"
69 #include "TObject.h"
70 #include "TMathBase.h"
71 #include "TMatrixFBasefwd.h"
72 #include "TMatrixDBasefwd.h"
73 #include "TString.h"
74 #include "TVectorFfwd.h"
75 #include "TVectorDfwd.h"
76 
77 #include <limits>
78 
79 template<class Element> class TVectorT;
80 template<class Element> class TElementActionT;
81 template<class Element> class TElementPosActionT;
82 
83 R__EXTERN Int_t gMatrixCheck;
84 
85 template<class Element> class TMatrixTBase : public TObject {
86 
87 private:
88  Element *GetElements(); // This function is now obsolete (and is not implemented) you should use TMatrix::GetMatrixArray().
89 
90 protected:
91  Int_t fNrows; // number of rows
92  Int_t fNcols; // number of columns
93  Int_t fRowLwb; // lower bound of the row index
94  Int_t fColLwb; // lower bound of the col index
95  Int_t fNelems; // number of elements in matrix
96  Int_t fNrowIndex; // length of row index array (= fNrows+1) wich is only used for sparse matrices
97 
98  Element fTol; // sqrt(epsilon); epsilon is smallest number number so that 1+epsilon > 1
99  // fTol is used in matrix decomposition (like in inversion)
100 
101  Bool_t fIsOwner; //!default kTRUE, when Use array kFALSE
102 
103  static void DoubleLexSort (Int_t n,Int_t *first,Int_t *second,Element *data);
104  static void IndexedLexSort(Int_t n,Int_t *first,Int_t swapFirst,
105  Int_t *second,Int_t swapSecond,Int_t *index);
106 
107  enum {kSizeMax = 25}; // size data container on stack, see New_m(),Delete_m()
108  enum {kWorkMax = 100}; // size of work array's in several routines
109 
110  enum EMatrixStatusBits {
111  kStatus = BIT(14) // set if matrix object is valid
112  };
113 
114 public:
115 
116  TMatrixTBase():
117  fNrows(0), fNcols(0), fRowLwb(0), fColLwb(0), fNelems(0), fNrowIndex(0),
118  fTol(0), fIsOwner(kTRUE) { }
119 
120  virtual ~TMatrixTBase() {}
121 
122  inline Int_t GetRowLwb () const { return fRowLwb; }
123  inline Int_t GetRowUpb () const { return fNrows+fRowLwb-1; }
124  inline Int_t GetNrows () const { return fNrows; }
125  inline Int_t GetColLwb () const { return fColLwb; }
126  inline Int_t GetColUpb () const { return fNcols+fColLwb-1; }
127  inline Int_t GetNcols () const { return fNcols; }
128  inline Int_t GetNoElements () const { return fNelems; }
129  inline Element GetTol () const { return fTol; }
130 
131  virtual const Element *GetMatrixArray () const = 0;
132  virtual Element *GetMatrixArray () = 0;
133  virtual const Int_t *GetRowIndexArray() const = 0;
134  virtual Int_t *GetRowIndexArray() = 0;
135  virtual const Int_t *GetColIndexArray() const = 0;
136  virtual Int_t *GetColIndexArray() = 0;
137 
138  virtual TMatrixTBase<Element> &SetRowIndexArray(Int_t *data) = 0;
139  virtual TMatrixTBase<Element> &SetColIndexArray(Int_t *data) = 0;
140  virtual TMatrixTBase<Element> &SetMatrixArray (const Element *data,Option_t *option="");
141  inline Element SetTol (Element tol);
142 
143  virtual void Clear (Option_t *option="") = 0;
144 
145  inline void Invalidate () { SetBit(kStatus); }
146  inline void MakeValid () { ResetBit(kStatus); }
147  inline Bool_t IsValid () const { return !TestBit(kStatus); }
148  inline Bool_t IsOwner () const { return fIsOwner; }
149  virtual Bool_t IsSymmetric() const;
150 
151  virtual TMatrixTBase<Element> &GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
152  TMatrixTBase<Element> &target,Option_t *option="S") const = 0;
153  virtual TMatrixTBase<Element> &SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source) = 0;
154 
155  virtual void GetMatrix2Array(Element *data,Option_t *option="") const;
156  virtual TMatrixTBase<Element> &InsertRow (Int_t row,Int_t col,const Element *v,Int_t n = -1);
157  virtual void ExtractRow (Int_t row,Int_t col, Element *v,Int_t n = -1) const;
158 
159  virtual TMatrixTBase<Element> &Shift (Int_t row_shift,Int_t col_shift);
160  virtual TMatrixTBase<Element> &ResizeTo (Int_t nrows,Int_t ncols,Int_t nr_nonzeros=-1) = 0;
161  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;
162 
163  virtual Double_t Determinant() const { AbstractMethod("Determinant()"); return 0.; }
164  virtual void Determinant(Double_t &d1,Double_t &d2) const { AbstractMethod("Determinant()"); d1 = 0.; d2 = 0.; }
165 
166  virtual TMatrixTBase<Element> &Zero ();
167  virtual TMatrixTBase<Element> &Abs ();
168  virtual TMatrixTBase<Element> &Sqr ();
169  virtual TMatrixTBase<Element> &Sqrt ();
170  virtual TMatrixTBase<Element> &UnitMatrix ();
171 
172  virtual TMatrixTBase<Element> &NormByDiag (const TVectorT<Element> &v,Option_t *option="D");
173 
174  virtual Element RowNorm () const;
175  virtual Element ColNorm () const;
176  virtual Element E2Norm () const;
177  inline Element NormInf () const { return RowNorm(); }
178  inline Element Norm1 () const { return ColNorm(); }
179  virtual Int_t NonZeros () const;
180  virtual Element Sum () const;
181  virtual Element Min () const;
182  virtual Element Max () const;
183 
184  void Draw (Option_t *option=""); // *MENU*
185  void Print(Option_t *name ="") const; // *MENU*
186 
187  virtual Element operator()(Int_t rown,Int_t coln) const = 0;
188  virtual Element &operator()(Int_t rown,Int_t coln) = 0;
189 
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  Bool_t operator> (Element val) const;
195  Bool_t operator>=(Element val) const;
196 
197  virtual TMatrixTBase<Element> &Apply(const TElementActionT<Element> &action);
198  virtual TMatrixTBase<Element> &Apply(const TElementPosActionT<Element> &action);
199 
200  virtual TMatrixTBase<Element> &Randomize(Element alpha,Element beta,Double_t &seed);
201 
202  // make it public since it can be called by TMatrixTRow
203  static Element & NaNValue();
204 
205  ClassDef(TMatrixTBase,5) // Matrix base class (template)
206 };
207 
208 #ifndef __CLING__
209 // When building with -fmodules, it instantiates all pending instantiations,
210 // instead of delaying them until the end of the translation unit.
211 // We 'got away with' probably because the use and the definition of the
212 // explicit specialization do not occur in the same TU.
213 //
214 // In case we are building with -fmodules, we need to forward declare the
215 // specialization in order to compile the dictionary G__Matrix.cxx.
216 template <> TClass *TMatrixTBase<double>::Class();
217 #endif // __CLING__
218 
219 
220 template<class Element> Element TMatrixTBase<Element>::SetTol(Element newTol)
221 {
222  const Element oldTol = fTol;
223  if (newTol >= 0.0)
224  fTol = newTol;
225  return oldTol;
226 }
227 
228 template<class Element> Bool_t operator== (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
229 template<class Element> Element E2Norm (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
230 template<class Element1,class Element2>
231  Bool_t AreCompatible(const TMatrixTBase<Element1> &m1,const TMatrixTBase<Element2> &m2,Int_t verbose=0);
232 template<class Element> void Compare (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
233 
234 // Service functions (useful in the verification code).
235 // They print some detail info if the validation condition fails
236 
237 template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,
238  Int_t verbose,Element maxDevAllow);
239 template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,Int_t verbose)
240  { return VerifyMatrixValue(m,val,verbose,Element(0.)); }
241 template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val)
242  { return VerifyMatrixValue(m,val,1,Element(0.)); }
243 template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2,
244  Int_t verbose,Element maxDevAllow);
245 template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2,Int_t verbose)
246  { return VerifyMatrixIdentity(m1,m2,verbose,Element(0.)); }
247 template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2)
248  { return VerifyMatrixIdentity(m1,m2,1,Element(0.)); }
249 
250 #endif