ROOT  6.06/09
Reference Guide
TMatrixTSym.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_TMatrixTSym
13 #define ROOT_TMatrixTSym
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TMatrixTSym //
18 // //
19 // Implementation of a symmetric matrix in the linear algebra package //
20 // //
21 // Note that in this implementation both matrix element m[i][j] and //
22 // m[j][i] are updated and stored in memory . However, when making the //
23 // object persistent only the upper right triangle is stored . //
24 // //
25 //////////////////////////////////////////////////////////////////////////
26 
27 #ifndef ROOT_TMatrixTBase
28 #include "TMatrixTBase.h"
29 #endif
30 #ifndef ROOT_TMatrixTUtils
31 #include "TMatrixTUtils.h"
32 #endif
33 
34 template<class Element>class TMatrixT;
35 template<class Element>class TMatrixTSymLazy;
36 template<class Element>class TVectorT;
37 
38 template<class Element> class TMatrixTSym : public TMatrixTBase<Element> {
39 
40 protected:
41 
42  Element fDataStack[TMatrixTBase<Element>::kSizeMax]; //! data container
43  Element *fElements; //[fNelems] elements themselves
44 
45  Element *New_m (Int_t size);
46  void Delete_m(Int_t size,Element*&);
47  Int_t Memcpy_m(Element *newp,const Element *oldp,Int_t copySize,
48  Int_t newSize,Int_t oldSize);
49  void Allocate(Int_t nrows,Int_t ncols,Int_t row_lwb = 0,Int_t col_lwb = 0,Int_t init = 0,
50  Int_t /*nr_nonzeros*/ = -1);
51 
52 public:
53 
54  enum {kWorkMax = 100}; // size of work array
57 
58  TMatrixTSym() { fElements = 0; }
59  explicit TMatrixTSym(Int_t nrows);
60  TMatrixTSym(Int_t row_lwb,Int_t row_upb);
61  TMatrixTSym(Int_t nrows,const Element *data,Option_t *option="");
62  TMatrixTSym(Int_t row_lwb,Int_t row_upb,const Element *data,Option_t *option="");
63  TMatrixTSym(const TMatrixTSym<Element> &another);
64  template <class Element2> TMatrixTSym(const TMatrixTSym<Element2> &another)
65  {
66  R__ASSERT(another.IsValid());
67  Allocate(another.GetNrows(),another.GetNcols(),another.GetRowLwb(),another.GetColLwb());
68  *this = another;
69  }
70 
74  TMatrixTSym(const TMatrixTSymLazy<Element> &lazy_constructor);
75 
76  virtual ~TMatrixTSym() { Clear(); }
77 
78  // Elementary constructors
79  void TMult(const TMatrixT <Element> &a);
80  void TMult(const TMatrixTSym<Element> &a);
81  void Mult (const TMatrixTSym<Element> &a) { TMult(a); }
82 
83  void Plus (const TMatrixTSym<Element> &a,const TMatrixTSym<Element> &b);
84  void Minus(const TMatrixTSym<Element> &a,const TMatrixTSym<Element> &b);
85 
86  virtual const Element *GetMatrixArray () const;
87  virtual Element *GetMatrixArray ();
88  virtual const Int_t *GetRowIndexArray() const { return 0; }
89  virtual Int_t *GetRowIndexArray() { return 0; }
90  virtual const Int_t *GetColIndexArray() const { return 0; }
91  virtual Int_t *GetColIndexArray() { return 0; }
92 
93  virtual TMatrixTBase<Element> &SetRowIndexArray(Int_t * /*data*/) { MayNotUse("SetRowIndexArray(Int_t *)"); return *this; }
94  virtual TMatrixTBase<Element> &SetColIndexArray(Int_t * /*data*/) { MayNotUse("SetColIndexArray(Int_t *)"); return *this; }
95 
96  virtual void Clear (Option_t * /*option*/ ="") { if (this->fIsOwner) Delete_m(this->fNelems,fElements);
97  else fElements = 0;
98  this->fNelems = 0; }
99  virtual Bool_t IsSymmetric() const { return kTRUE; }
100 
101  TMatrixTSym <Element> &Use (Int_t row_lwb,Int_t row_upb,Element *data);
102  const TMatrixTSym <Element> &Use (Int_t row_lwb,Int_t row_upb,const Element *data) const
103  { return (const TMatrixTSym<Element>&)
104  ((const_cast<TMatrixTSym<Element> *>(this))->Use(row_lwb,row_upb,const_cast<Element *>(data))); }
105  TMatrixTSym <Element> &Use (Int_t nrows,Element *data);
106  const TMatrixTSym <Element> &Use (Int_t nrows,const Element *data) const;
108  const TMatrixTSym <Element> &Use (const TMatrixTSym<Element> &a) const;
109 
110  TMatrixTSym <Element> &GetSub (Int_t row_lwb,Int_t row_upb,TMatrixTSym<Element> &target,Option_t *option="S") const;
111  virtual TMatrixTBase<Element> &GetSub (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
112  TMatrixTBase<Element> &target,Option_t *option="S") const;
113  TMatrixTSym <Element> GetSub (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Option_t *option="S") const;
114  TMatrixTSym <Element> &SetSub (Int_t row_lwb,const TMatrixTBase<Element> &source);
115  virtual TMatrixTBase<Element> &SetSub (Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source);
116 
117  virtual TMatrixTBase<Element> &SetMatrixArray(const Element *data, Option_t *option="");
118 
119  virtual TMatrixTBase<Element> &Shift (Int_t row_shift,Int_t col_shift);
120  virtual TMatrixTBase<Element> &ResizeTo (Int_t nrows,Int_t ncols,Int_t /*nr_nonzeros*/ =-1);
121  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);
123  return ResizeTo(m.GetRowLwb(),m.GetRowUpb(),m.GetColLwb(),m.GetColUpb()); }
124 
125  virtual Double_t Determinant () const;
126  virtual void Determinant (Double_t &d1,Double_t &d2) const;
127 
131  inline TMatrixTSym<Element> &T () { return this->Transpose(*this); }
132  TMatrixTSym<Element> &Rank1Update (const TVectorT <Element> &v,Element alpha=1.0);
135  Element Similarity (const TVectorT <Element> &v) const;
137 
138  // Either access a_ij as a(i,j)
139  inline Element operator()(Int_t rown,Int_t coln) const;
140  inline Element &operator()(Int_t rown,Int_t coln);
141 
142  // or as a[i][j]
143  inline const TMatrixTRow_const<Element> operator[](Int_t rown) const { return TMatrixTRow_const<Element>(*this,rown); }
144  inline TMatrixTRow <Element> operator[](Int_t rown) { return TMatrixTRow <Element>(*this,rown); }
145 
148  template <class Element2> TMatrixTSym<Element> &operator= (const TMatrixTSym<Element2> &source)
149  {
150  if (!AreCompatible(*this,source)) {
151  Error("operator=(const TMatrixTSym2 &)","matrices not compatible");
152  return *this;
153  }
154 
155  TObject::operator=(source);
156  const Element2 * const ps = source.GetMatrixArray();
157  Element * const pt = this->GetMatrixArray();
158  for (Int_t i = 0; i < this->fNelems; i++)
159  pt[i] = ps[i];
160  this->fTol = source.GetTol();
161  return *this;
162  }
163 
164  TMatrixTSym<Element> &operator= (Element val);
165  TMatrixTSym<Element> &operator-=(Element val);
166  TMatrixTSym<Element> &operator+=(Element val);
167  TMatrixTSym<Element> &operator*=(Element val);
168 
169  TMatrixTSym &operator+=(const TMatrixTSym &source);
170  TMatrixTSym &operator-=(const TMatrixTSym &source);
171 
174 
175  virtual TMatrixTBase<Element> &Randomize (Element alpha,Element beta,Double_t &seed);
176  virtual TMatrixTSym <Element> &RandomizePD(Element alpha,Element beta,Double_t &seed);
177 
178  const TMatrixT<Element> EigenVectors(TVectorT<Element> &eigenValues) const;
179 
180  ClassDef(TMatrixTSym,2) // Template of Symmetric Matrix class
181 };
182 
183 template <class Element> inline const Element *TMatrixTSym<Element>::GetMatrixArray() const { return fElements; }
184 template <class Element> inline Element *TMatrixTSym<Element>::GetMatrixArray() { return fElements; }
185 
186 template <class Element> inline TMatrixTSym<Element> &TMatrixTSym<Element>::Use (Int_t nrows,Element *data) { return Use(0,nrows-1,data); }
187 template <class Element> inline const TMatrixTSym<Element> &TMatrixTSym<Element>::Use (Int_t nrows,const Element *data) const
188  { return Use(0,nrows-1,data); }
190  { return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetMatrixArray()); }
191 template <class Element> inline const TMatrixTSym<Element> &TMatrixTSym<Element>::Use (const TMatrixTSym<Element> &a) const
192  { return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetMatrixArray()); }
193 
194 template <class Element> inline TMatrixTSym<Element> TMatrixTSym<Element>::GetSub (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
195  Option_t *option) const
196  {
198  this->GetSub(row_lwb,row_upb,col_lwb,col_upb,tmp,option);
199  return tmp;
200  }
201 
202 template <class Element> inline Element TMatrixTSym<Element>::operator()(Int_t rown,Int_t coln) const
203 {
204  R__ASSERT(this->IsValid());
205  const Int_t arown = rown-this->fRowLwb;
206  const Int_t acoln = coln-this->fColLwb;
207  if (arown >= this->fNrows || arown < 0) {
208  Error("operator()","Request row(%d) outside matrix range of %d - %d",rown,this->fRowLwb,this->fRowLwb+this->fNrows);
210  }
211  if (acoln >= this->fNcols || acoln < 0) {
212  Error("operator()","Request column(%d) outside matrix range of %d - %d",coln,this->fColLwb,this->fColLwb+this->fNcols);
214  }
215  return (fElements[arown*this->fNcols+acoln]);
216 }
217 
218 template <class Element> inline Element &TMatrixTSym<Element>::operator()(Int_t rown,Int_t coln)
219 {
220  R__ASSERT(this->IsValid());
221  const Int_t arown = rown-this->fRowLwb;
222  const Int_t acoln = coln-this->fColLwb;
223  if (arown >= this->fNrows || arown < 0) {
224  Error("operator()","Request row(%d) outside matrix range of %d - %d",rown,this->fRowLwb,this->fRowLwb+this->fNrows);
226  }
227  if (acoln >= this->fNcols || acoln < 0) {
228  Error("operator()","Request column(%d) outside matrix range of %d - %d",coln,this->fColLwb,this->fColLwb+this->fNcols);
230  }
231  return (fElements[arown*this->fNcols+acoln]);
232 }
233 
234 template <class Element> Bool_t operator== (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
235 template <class Element> TMatrixTSym<Element> operator+ (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
236 template <class Element> TMatrixTSym<Element> operator+ (const TMatrixTSym<Element> &source1, Element val);
237 template <class Element> TMatrixTSym<Element> operator+ ( Element val ,const TMatrixTSym<Element> &source2);
238 template <class Element> TMatrixTSym<Element> operator- (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
239 template <class Element> TMatrixTSym<Element> operator- (const TMatrixTSym<Element> &source1, Element val);
240 template <class Element> TMatrixTSym<Element> operator- ( Element val ,const TMatrixTSym<Element> &source2);
241 template <class Element> TMatrixTSym<Element> operator* (const TMatrixTSym<Element> &source, Element val );
242 template <class Element> TMatrixTSym<Element> operator* ( Element val, const TMatrixTSym<Element> &source );
243 // Preventing warnings with -Weffc++ in GCC since overloading the || and && operators was a design choice.
244 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
245 #pragma GCC diagnostic push
246 #pragma GCC diagnostic ignored "-Weffc++"
247 #endif
248 template <class Element> TMatrixTSym<Element> operator&& (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
249 template <class Element> TMatrixTSym<Element> operator|| (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
250 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
251 #pragma GCC diagnostic pop
252 #endif
253 template <class Element> TMatrixTSym<Element> operator> (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
254 template <class Element> TMatrixTSym<Element> operator>= (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
255 template <class Element> TMatrixTSym<Element> operator<= (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
256 template <class Element> TMatrixTSym<Element> operator< (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
257 
258 template <class Element> TMatrixTSym<Element> &Add (TMatrixTSym<Element> &target, Element scalar,const TMatrixTSym<Element> &source);
259 template <class Element> TMatrixTSym<Element> &ElementMult(TMatrixTSym<Element> &target,const TMatrixTSym<Element> &source);
260 template <class Element> TMatrixTSym<Element> &ElementDiv (TMatrixTSym<Element> &target,const TMatrixTSym<Element> &source);
261 
262 #endif
Int_t GetRowLwb() const
Definition: TMatrixTBase.h:132
const TMatrixTRow_const< Element > operator[](Int_t rown) const
Definition: TMatrixTSym.h:143
TMatrixTSym< Element > & SimilarityT(const TMatrixT< Element > &n)
Calculate B^T * (*this) * B , final matrix will be (ncolsb x ncolsb) It is more efficient than applyi...
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 * GetColIndexArray()
Definition: TMatrixTSym.h:91
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t=-1)
Set size of the matrix to nrows x ncols New dynamic elements are created, the overlapping part of the...
TMatrixTSym< Element > operator>=(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
source1 >= source2
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
TMatrixTSym< Element > & ElementMult(TMatrixTSym< Element > &target, const TMatrixTSym< Element > &source)
Multiply target by the source, element-by-element.
Element fTol
Definition: TMatrixTBase.h:108
void Allocate(Int_t nrows, Int_t ncols, Int_t row_lwb=0, Int_t col_lwb=0, Int_t init=0, Int_t=-1)
Allocate new matrix.
virtual TMatrixTBase< Element > & SetRowIndexArray(Int_t *)
Definition: TMatrixTSym.h:93
TMatrixTSym< Element > & operator=(const TMatrixTSym< Element > &source)
Int_t GetRowUpb() const
Definition: TMatrixTBase.h:133
const char Option_t
Definition: RtypesCore.h:62
void MayNotUse(const char *method) const
Use this method to signal that a method (defined in a base class) may not be called in a derived clas...
Definition: TObject.cxx:971
Element fDataStack[TMatrixTBase< Element >::kSizeMax]
Definition: TMatrixTSym.h:42
const TMatrixT< Element > EigenVectors(TVectorT< Element > &eigenValues) const
Return a matrix containing the eigen-vectors ordered by descending eigen-values.
TMatrixTSym< Element > operator>(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
source1 > source2
void TMult(const TMatrixT< Element > &a)
Create a matrix C such that C = A' * A.
#define R__ASSERT(e)
Definition: TError.h:98
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
virtual void Clear(Option_t *="")
Definition: TMatrixTSym.h:96
TMatrixTSym< Element > & operator*=(Element val)
Multiply every element of the matrix with val.
TMatrixTSym< Element > & Rank1Update(const TVectorT< Element > &v, Element alpha=1.0)
Perform a rank 1 operation on the matrix: A += alpha * v * v^T.
double beta(double x, double y)
Calculates the beta function.
Bool_t IsValid() const
Definition: TMatrixTBase.h:157
void Mult(const TMatrixTSym< Element > &a)
Definition: TMatrixTSym.h:81
Element GetTol() const
Definition: TMatrixTBase.h:139
virtual ~TMatrixTSym()
Definition: TMatrixTSym.h:76
#define ClassDef(name, id)
Definition: Rtypes.h:254
TMatrixTSym< Element > & operator+=(Element val)
Add val to every element of the matrix.
Element operator()(Int_t rown, Int_t coln) const
Definition: TMatrixTSym.h:202
virtual Double_t Determinant() const
TMatrixTSym< Element > & T()
Definition: TMatrixTSym.h:131
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.cxx:102
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 .
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
TMatrixTRow< Element > operator[](Int_t rown)
Definition: TMatrixTSym.h:144
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *option="")
Copy array data to matrix .
void Error(const char *location, const char *msgfmt,...)
void Minus(const TMatrixTSym< Element > &a, const TMatrixTSym< Element > &b)
Symmetric matrix summation. Create a matrix C such that C = A + B.
Int_t GetColUpb() const
Definition: TMatrixTBase.h:136
virtual const Element * GetMatrixArray() const
Definition: TMatrixTSym.h:183
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *)
Definition: TMatrixTSym.h:94
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
Int_t Memcpy_m(Element *newp, const Element *oldp, Int_t copySize, Int_t newSize, Int_t oldSize)
copy copySize doubles from *oldp to *newp .
TMatrixTBase< Element > & Apply(const TElementActionT< Element > &action)
Apply action to each matrix element.
TPaveText * pt
TMatrixTSym< Element > operator||(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
Logical Or.
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8
TMatrixTSym< Element > & Add(TMatrixTSym< Element > &target, Element scalar, const TMatrixTSym< Element > &source)
Modify addition: target += scalar * source.
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
randomize matrix element values but keep matrix symmetric
Int_t GetColLwb() const
Definition: TMatrixTBase.h:135
TMatrixTSym< Element > operator-(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
TMatrixTBase< Element > & ResizeTo(const TMatrixTSym< Element > &m)
Definition: TMatrixTSym.h:122
TMatrixTSym< Element > & Transpose(const TMatrixTSym< Element > &source)
Transpose a matrix.
TMatrixTSym(const TMatrixTSym< Element2 > &another)
Definition: TMatrixTSym.h:64
const TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, const Element *data) const
Definition: TMatrixTSym.h:102
void Plus(const TMatrixTSym< Element > &a, const TMatrixTSym< Element > &b)
Symmetric matrix summation. Create a matrix C such that C = A + B.
static Int_t init()
double Double_t
Definition: RtypesCore.h:55
TMatrixTSym< Element > & operator-=(Element val)
Subtract val from every element of the matrix.
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
virtual TMatrixTSym< Element > & RandomizePD(Element alpha, Element beta, Double_t &seed)
randomize matrix element values but keep matrix symmetric positive definite
TMatrixTSym< Element > & InvertFast(Double_t *det=0)
Invert the matrix and calculate its determinant.
Bool_t fIsOwner
Definition: TMatrixTBase.h:111
TMatrixTSym< Element > & SetSub(Int_t row_lwb, const TMatrixTBase< Element > &source)
Insert matrix source starting at [row_lwb][row_lwb], thereby overwriting the part [row_lwb...
TMatrixTSym< Element > & Similarity(const TMatrixT< Element > &n)
Calculate B * (*this) * B^T , final matrix will be (nrowsb x nrowsb) This is a similarity transform w...
virtual const Int_t * GetRowIndexArray() const
Definition: TMatrixTSym.h:88
void Delete_m(Int_t size, Element *&)
delete data pointer m, if it was assigned on the heap
TMatrixTSym< Element > & Invert(Double_t *det=0)
Invert the matrix and calculate its determinant Notice that the LU decomposition is used instead of B...
virtual const Int_t * GetColIndexArray() const
Definition: TMatrixTSym.h:90
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
Definition: TMatrixTSym.h:99
TMatrixTSym< Element > operator+(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
virtual Int_t * GetRowIndexArray()
Definition: TMatrixTSym.h:89
static Element & NaNValue()
Element * New_m(Int_t size)
return data pointer .
TMatrixTSym< Element > & ElementDiv(TMatrixTSym< Element > &target, const TMatrixTSym< Element > &source)
Multiply target by the source, element-by-element.
TMatrixTSym< Element > operator*(const TMatrixTSym< Element > &source, Element val)
const Bool_t kTRUE
Definition: Rtypes.h:91
Bool_t operator==(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
Check to see if two matrices are identical.
Element * fElements
data container
Definition: TMatrixTSym.h:43
const Int_t n
Definition: legend1.C:16
TMatrixTSym< Element > operator&&(const TMatrixTSym< Element > &source1, const TMatrixTSym< Element > &source2)
Logical AND.
TMatrixTSym< Element > & GetSub(Int_t row_lwb, Int_t row_upb, TMatrixTSym< Element > &target, Option_t *option="S") const
Get submatrix [row_lwb..row_upb][row_lwb..row_upb]; The indexing range of the returned matrix depends...