Logo ROOT  
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 "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
79template<class Element> class TVectorT;
80template<class Element> class TElementActionT;
81template<class Element> class TElementPosActionT;
82
84
85template<class Element> class TMatrixTBase : public TObject {
86
87private:
88 Element *GetElements(); // This function is now obsolete (and is not implemented) you should use TMatrix::GetMatrixArray().
89
90protected:
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
111 kStatus = BIT(14) // set if matrix object is valid
112 };
113
114public:
115
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
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 ();
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
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.
217#endif // __CLING__
218
219
220template<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
228template<class Element> Bool_t operator== (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
229template<class Element> Element E2Norm (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
230template<class Element1,class Element2>
232template<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
237template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,
238 Int_t verbose,Element maxDevAllow);
239template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,Int_t verbose)
240 { return VerifyMatrixValue(m,val,verbose,Element(0.)); }
241template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val)
242 { return VerifyMatrixValue(m,val,1,Element(0.)); }
243template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2,
244 Int_t verbose,Element maxDevAllow);
246 { return VerifyMatrixIdentity(m1,m2,verbose,Element(0.)); }
248 { return VerifyMatrixIdentity(m1,m2,1,Element(0.)); }
249
250#endif
#define R__EXTERN
Definition: DllImport.h:27
int Int_t
Definition: RtypesCore.h:43
bool Bool_t
Definition: RtypesCore.h:61
double Double_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassDef(name, id)
Definition: Rtypes.h:322
#define BIT(n)
Definition: Rtypes.h:83
char name[80]
Definition: TGX11.cxx:109
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 .
R__EXTERN Int_t gMatrixCheck
Definition: TMatrixTBase.h:81
void Compare(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Compare two matrices and print out the result of the comparison.
Element E2Norm(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Square of the Euclidian norm of the difference between two matrices.
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 .
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.
Bool_t operator==(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Check to see if two matrices are identical.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
TMatrixTBase.
Definition: TMatrixTBase.h:85
virtual Element Sum() const
Compute sum of elements.
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
virtual Int_t * GetColIndexArray()=0
void Invalidate()
Definition: TMatrixTBase.h:145
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
void Draw(Option_t *option="")
Draw this matrix The histogram is named "TMatrixT" by default and no title.
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
Randomize matrix element values.
virtual Int_t * GetRowIndexArray()=0
virtual TMatrixTBase< Element > & Sqr()
Square each element of the matrix.
virtual const Element * GetMatrixArray() const =0
virtual void Determinant(Double_t &d1, Double_t &d2) const
Definition: TMatrixTBase.h:164
Int_t GetNrows() const
Definition: TMatrixTBase.h:124
Bool_t fIsOwner
Definition: TMatrixTBase.h:101
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
void Print(Option_t *name="") const
Print the matrix as a table of elements.
Bool_t operator!=(Element val) const
Are all matrix elements not equal to val?
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *data)=0
Int_t fNrowIndex
Definition: TMatrixTBase.h:96
virtual const Int_t * GetRowIndexArray() const =0
TClass * Class()
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
Element fTol
Definition: TMatrixTBase.h:98
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
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
virtual Double_t Determinant() const
Definition: TMatrixTBase.h:163
virtual const Int_t * GetColIndexArray() const =0
virtual Element E2Norm() const
Square of the Euclidian norm, SUM{ m(i,j)^2 }.
Int_t GetRowUpb() const
Definition: TMatrixTBase.h:123
virtual TMatrixTBase< Element > & Apply(const TElementActionT< Element > &action)
Apply action to each matrix element.
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 TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros=-1)=0
Int_t GetRowLwb() const
Definition: TMatrixTBase.h:122
virtual Element Min() const
return minimum matrix element value
virtual Element * GetMatrixArray()=0
virtual Element Max() const
return maximum vector element value
Int_t GetColLwb() const
Definition: TMatrixTBase.h:125
virtual void GetMatrix2Array(Element *data, Option_t *option="") const
Copy matrix data to array .
Int_t GetNoElements() const
Definition: TMatrixTBase.h:128
Bool_t operator<(Element val) const
Are all matrix elements < val?
Element NormInf() const
Definition: TMatrixTBase.h:177
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.
static Element & NaNValue()
Bool_t IsOwner() const
Definition: TMatrixTBase.h:148
Int_t GetColUpb() const
Definition: TMatrixTBase.h:126
Element Norm1() const
Definition: TMatrixTBase.h:178
static void DoubleLexSort(Int_t n, Int_t *first, Int_t *second, Element *data)
default kTRUE, when Use array kFALSE
virtual TMatrixTBase< Element > & SetSub(Int_t row_lwb, Int_t col_lwb, const TMatrixTBase< Element > &source)=0
Bool_t operator>(Element val) const
Are all matrix elements > val?
virtual Element operator()(Int_t rown, Int_t coln) const =0
virtual TMatrixTBase< Element > & SetRowIndexArray(Int_t *data)=0
virtual TMatrixTBase< Element > & Sqrt()
Take square root of all elements.
void MakeValid()
Definition: TMatrixTBase.h:146
Bool_t IsValid() const
Definition: TMatrixTBase.h:147
Element * GetElements()
Bool_t operator==(Element val) const
Are all matrix elements equal to val?
virtual Element & operator()(Int_t rown, Int_t coln)=0
virtual void Clear(Option_t *option="")=0
Int_t GetNcols() const
Definition: TMatrixTBase.h:127
virtual TMatrixTBase< Element > & Abs()
Take an absolute value of a matrix, i.e. apply Abs() to each element.
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.
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 <= val?
virtual ~TMatrixTBase()
Definition: TMatrixTBase.h:120
Element SetTol(Element tol)
Definition: TMatrixTBase.h:220
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *option="")
Copy array data to matrix .
Bool_t operator>=(Element val) const
Are all matrix elements >= val?
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.
Element GetTol() const
Definition: TMatrixTBase.h:129
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &v, Option_t *option="D")
option:
Mother of all ROOT objects.
Definition: TObject.h:37
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:933
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:187
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
void ResetBit(UInt_t f)
Definition: TObject.h:186
TVectorT.
Definition: TVectorT.h:27
double beta(double x, double y)
Calculates the beta function.
const Int_t n
Definition: legend1.C:16
static constexpr double second
static constexpr double m2
Definition: first.py:1
auto * m
Definition: textangle.C:8