Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TVectorFfwd.h"
74#include "TVectorDfwd.h"
75
76#include <limits>
77
78template<class Element> class TVectorT;
79template<class Element> class TElementActionT;
80template<class Element> class TElementPosActionT;
81
83
84template<class Element> class TMatrixTBase : public TObject {
85
86private:
87 Element *GetElements(); // This function is now obsolete (and is not implemented) you should use TMatrix::GetMatrixArray().
88
89protected:
90 Int_t fNrows; // number of rows
91 Int_t fNcols; // number of columns
92 Int_t fRowLwb; // lower bound of the row index
93 Int_t fColLwb; // lower bound of the col index
94 Int_t fNelems; // number of elements in matrix
95 Int_t fNrowIndex; // length of row index array (= fNrows+1), which is only used for sparse matrices
96
97 Element fTol; // sqrt(epsilon); epsilon is smallest number number so that 1+epsilon > 1
98 // fTol is used in matrix decomposition (like in inversion)
99
100 Bool_t fIsOwner; //!default kTRUE, when Use array kFALSE
101
102 static void DoubleLexSort (Int_t n,Int_t *first,Int_t *second,Element *data);
103 static void IndexedLexSort(Int_t n,Int_t *first,Int_t swapFirst,
104 Int_t *second,Int_t swapSecond,Int_t *index);
105
106 enum {kSizeMax = 25}; // size data container on stack, see New_m(),Delete_m()
107 enum {kWorkMax = 100}; // size of work array's in several routines
108
110 kStatus = BIT(14) // set if matrix object is valid
111 };
112
113public:
114
116 fNrows(0), fNcols(0), fRowLwb(0), fColLwb(0), fNelems(0), fNrowIndex(0),
117 fTol(0), fIsOwner(kTRUE) { }
118
119 ~TMatrixTBase() override {}
120
121 inline Int_t GetRowLwb () const { return fRowLwb; }
122 inline Int_t GetRowUpb () const { return fNrows+fRowLwb-1; }
123 inline Int_t GetNrows () const { return fNrows; }
124 inline Int_t GetColLwb () const { return fColLwb; }
125 inline Int_t GetColUpb () const { return fNcols+fColLwb-1; }
126 inline Int_t GetNcols () const { return fNcols; }
127 inline Int_t GetNoElements () const { return fNelems; }
128 inline Element GetTol () const { return fTol; }
129
130 virtual const Element *GetMatrixArray () const = 0;
131 virtual Element *GetMatrixArray () = 0;
132 virtual const Int_t *GetRowIndexArray() const = 0;
133 virtual Int_t *GetRowIndexArray() = 0;
134 virtual const Int_t *GetColIndexArray() const = 0;
135 virtual Int_t *GetColIndexArray() = 0;
136
139 virtual TMatrixTBase<Element> &SetMatrixArray (const Element *data,Option_t *option="");
140 inline Element SetTol (Element tol);
141
142 void Clear (Option_t *option="") override = 0;
143
144 inline void Invalidate () { SetBit(kStatus); }
145 inline void MakeValid () { ResetBit(kStatus); }
146 inline Bool_t IsValid () const { return !TestBit(kStatus); }
147 inline Bool_t IsOwner () const { return fIsOwner; }
148 virtual Bool_t IsSymmetric() const;
149
150 virtual TMatrixTBase<Element> &GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
152 virtual TMatrixTBase<Element> &SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source) = 0;
153
154 virtual void GetMatrix2Array(Element *data,Option_t *option="") const;
155 virtual TMatrixTBase<Element> &InsertRow (Int_t row,Int_t col,const Element *v,Int_t n = -1);
156 virtual void ExtractRow (Int_t row,Int_t col, Element *v,Int_t n = -1) const;
157
158 virtual TMatrixTBase<Element> &Shift (Int_t row_shift,Int_t col_shift);
159 virtual TMatrixTBase<Element> &ResizeTo (Int_t nrows,Int_t ncols,Int_t nr_nonzeros=-1) = 0;
160 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;
161
162 virtual Double_t Determinant() const { AbstractMethod("Determinant()"); return 0.; }
163 virtual void Determinant(Double_t &d1,Double_t &d2) const { AbstractMethod("Determinant()"); d1 = 0.; d2 = 0.; }
164
165 virtual TMatrixTBase<Element> &Zero ();
166 virtual TMatrixTBase<Element> &Abs ();
167 virtual TMatrixTBase<Element> &Sqr ();
168 virtual TMatrixTBase<Element> &Sqrt ();
170
172
173 virtual Element RowNorm () const;
174 virtual Element ColNorm () const;
175 virtual Element E2Norm () const;
176 inline Element NormInf () const { return RowNorm(); }
177 inline Element Norm1 () const { return ColNorm(); }
178 virtual Int_t NonZeros () const;
179 virtual Element Sum () const;
180 virtual Element Min () const;
181 virtual Element Max () const;
182
183 void Draw (Option_t *option="") override; // *MENU*
184 void Print(Option_t *name ="") const override; // *MENU*
185
186 virtual Element operator()(Int_t rown,Int_t coln) const = 0;
187 virtual Element &operator()(Int_t rown,Int_t coln) = 0;
188
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 Bool_t operator>=(Element val) const;
195
198
199 virtual TMatrixTBase<Element> &Randomize(Element alpha,Element beta,Double_t &seed);
200
201 // make it public since it can be called by TMatrixTRow
202 static Element & NaNValue();
203
204 ClassDefOverride(TMatrixTBase,5) // Matrix base class (template)
205};
206
207#ifndef __CLING__
208// When building with -fmodules, it instantiates all pending instantiations,
209// instead of delaying them until the end of the translation unit.
210// We 'got away with' probably because the use and the definition of the
211// explicit specialization do not occur in the same TU.
212//
213// In case we are building with -fmodules, we need to forward declare the
214// specialization in order to compile the dictionary G__Matrix.cxx.
216#endif // __CLING__
217
218
219template<class Element> Element TMatrixTBase<Element>::SetTol(Element newTol)
220{
221 const Element oldTol = fTol;
222 if (newTol >= 0.0)
223 fTol = newTol;
224 return oldTol;
225}
226
227inline namespace TMatrixTAutoloadOps {
228
229template<class Element> Bool_t operator== (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
230template<class Element> Element E2Norm (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
231template<class Element1,class Element2>
233template<class Element> void Compare (const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2);
234
235// Service functions (useful in the verification code).
236// They print some detail info if the validation condition fails
237
238template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,
239 Int_t verbose,Element maxDevAllow);
240template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val,Int_t verbose)
241 { return VerifyMatrixValue(m,val,verbose,Element(0.)); }
242template<class Element> Bool_t VerifyMatrixValue (const TMatrixTBase<Element> &m,Element val)
243 { return VerifyMatrixValue(m,val,1,Element(0.)); }
244template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2,
245 Int_t verbose,Element maxDevAllow);
246template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2,Int_t verbose)
247 { return VerifyMatrixIdentity(m1,m2,verbose,Element(0.)); }
248template<class Element> Bool_t VerifyMatrixIdentity(const TMatrixTBase<Element> &m1,const TMatrixTBase<Element> &m2)
249 { return VerifyMatrixIdentity(m1,m2,1,Element(0.)); }
250
251} // inline namespace TMatrixTAutoloadOps
252#endif
#define R__EXTERN
Definition DllImport.h:26
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define BIT(n)
Definition Rtypes.h:90
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
char name[80]
Definition TGX11.cxx:110
R__EXTERN Int_t gMatrixCheck
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
TMatrixTBase.
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()
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
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
Int_t GetNrows() const
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
Bool_t operator!=(Element val) const
Are all matrix elements not equal to val?
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *data)=0
virtual const Int_t * GetRowIndexArray() const =0
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
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
void Print(Option_t *name="") const override
Print the matrix as a table of elements.
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
virtual Double_t Determinant() const
virtual const Int_t * GetColIndexArray() const =0
virtual Element E2Norm() const
Square of the Euclidean norm, SUM{ m(i,j)^2 }.
Int_t GetRowUpb() const
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
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
virtual void GetMatrix2Array(Element *data, Option_t *option="") const
Copy matrix data to array .
Int_t GetNoElements() const
Bool_t operator<(Element val) const
Are all matrix elements < val?
Element NormInf() const
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
Int_t GetColUpb() const
Element Norm1() const
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
~TMatrixTBase() override
Bool_t operator>(Element val) const
Are all matrix elements > val?
static TClass * Class()
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.
Bool_t IsValid() const
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
void Clear(Option_t *option="") override=0
Int_t GetNcols() const
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?
Element SetTol(Element tol)
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
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &v, Option_t *option="D")
option:
Mother of all ROOT objects.
Definition TObject.h:41
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:1035
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:786
void ResetBit(UInt_t f)
Definition TObject.h:198
TVectorT.
Definition TVectorT.h:27
const Int_t n
Definition legend1.C:16
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 .
void Compare(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Compare two matrices and print out the result of the comparison.
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.
Element E2Norm(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Square of the Euclidean 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 .
th1 Draw()
TMarker m
Definition textangle.C:8