Logo ROOT   6.08/07
Reference Guide
TMatrixTSparse.h
Go to the documentation of this file.
1 // @(#)root/matrix:$Id$
2 // Authors: Fons Rademakers, Eddy Offermann Feb 2004
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_TMatrixTSparse
13 #define ROOT_TMatrixTSparse
14 
15 #ifndef ROOT_TMatrixTBase
16 #include "TMatrixTBase.h"
17 #endif
18 #ifndef ROOT_TMatrixTUtils
19 #include "TMatrixTUtils.h"
20 #endif
21 
22 
23 #ifdef CBLAS
24 #include <vecLib/vBLAS.h>
25 //#include <cblas.h>
26 #endif
27 
28 //////////////////////////////////////////////////////////////////////////
29 // //
30 // TMatrixTSparse //
31 // //
32 // Template class of a general sparse matrix in the Harwell-Boeing //
33 // format //
34 // //
35 //////////////////////////////////////////////////////////////////////////
36 
37 template<class Element> class TMatrixT;
38 
39 template<class Element> class TMatrixTSparse : public TMatrixTBase<Element> {
40 
41 protected:
42 
43  Int_t *fRowIndex; //[fNrowIndex] row index
44  Int_t *fColIndex; //[fNelems] column index
45  Element *fElements; //[fNelems]
46 
47  void Allocate(Int_t nrows,Int_t ncols,Int_t row_lwb = 0,Int_t col_lwb = 0,
48  Int_t init = 0,Int_t nr_nonzeros = 0);
49 
50  // Elementary constructors
53  void AMultB (const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0) {
54  const TMatrixTSparse<Element> bsp = b;
55  const TMatrixTSparse<Element> bt(TMatrixTSparse::kTransposed,bsp); AMultBt(a,bt,constr); }
56  void AMultB (const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0) {
58 
59  void AMultBt(const TMatrixTSparse<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
60  void AMultBt(const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0);
61  void AMultBt(const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
62 
63  void APlusB (const TMatrixTSparse<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
64  void APlusB (const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0);
65  void APlusB (const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0) { APlusB(b,a,constr); }
66 
67  void AMinusB(const TMatrixTSparse<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
68  void AMinusB(const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0);
69  void AMinusB(const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
70 
71 public:
72 
75 
76  TMatrixTSparse() { fElements = 0; fRowIndex = 0; fColIndex = 0; }
77  TMatrixTSparse(Int_t nrows,Int_t ncols);
78  TMatrixTSparse(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb);
79  TMatrixTSparse(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_nonzeros,
80  Int_t *row, Int_t *col,Element *data);
82  TMatrixTSparse(const TMatrixT<Element> &another);
83 
88 
89  virtual ~TMatrixTSparse() { Clear(); }
90 
91  virtual const Element *GetMatrixArray () const;
92  virtual Element *GetMatrixArray ();
93  virtual const Int_t *GetRowIndexArray() const;
94  virtual Int_t *GetRowIndexArray();
95  virtual const Int_t *GetColIndexArray() const;
96  virtual Int_t *GetColIndexArray();
97 
98  virtual TMatrixTBase<Element> &SetRowIndexArray(Int_t *data) { memmove(fRowIndex,data,(this->fNrows+1)*sizeof(Int_t)); return *this; }
99  virtual TMatrixTBase<Element> &SetColIndexArray(Int_t *data) { memmove(fColIndex,data,this->fNelems*sizeof(Int_t)); return *this; }
100 
106  { return SetSparseIndexAB(b,a); }
107 
108  virtual void GetMatrix2Array (Element *data,Option_t * /*option*/ ="") const;
109  virtual TMatrixTBase<Element> &SetMatrixArray (const Element *data,Option_t * /*option*/="")
110  { memcpy(fElements,data,this->fNelems*sizeof(Element)); return *this; }
111  virtual TMatrixTBase<Element> &SetMatrixArray (Int_t nr_nonzeros,Int_t *irow,Int_t *icol,Element *data);
112  virtual TMatrixTBase<Element> &InsertRow (Int_t row,Int_t col,const Element *v,Int_t n=-1);
113  virtual void ExtractRow (Int_t row,Int_t col, Element *v,Int_t n=-1) const;
114 
115  virtual TMatrixTBase<Element> &ResizeTo(Int_t nrows,Int_t ncols,Int_t nr_nonzeros=-1);
116  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);
118  m.GetColUpb(),m.GetNoElements()); }
119 
120  virtual void Clear(Option_t * /*option*/ ="") { if (this->fIsOwner) {
121  if (fElements) { delete [] fElements; fElements = 0; }
122  if (fRowIndex) { delete [] fRowIndex; fRowIndex = 0; }
123  if (fColIndex) { delete [] fColIndex; fColIndex = 0; }
124  }
125  this->fNelems = 0;
126  this->fNrowIndex = 0;
127  }
128 
129  TMatrixTSparse<Element> &Use (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_nonzeros,
130  Int_t *pRowIndex,Int_t *pColIndex,Element *pData);
131  const TMatrixTSparse<Element> &Use (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_nonzeros,
132  const Int_t *pRowIndex,const Int_t *pColIndex,const Element *pData) const
133  { return (const TMatrixTSparse<Element>&)
134  ((const_cast<TMatrixTSparse<Element> *>(this))->Use(row_lwb,row_upb,col_lwb,col_upb,nr_nonzeros,
135  const_cast<Int_t *>(pRowIndex),
136  const_cast<Int_t *>(pColIndex),
137  const_cast<Element *>(pData))); }
138  TMatrixTSparse<Element> &Use (Int_t nrows,Int_t ncols,Int_t nr_nonzeros,
139  Int_t *pRowIndex,Int_t *pColIndex,Element *pData);
140  const TMatrixTSparse<Element> &Use (Int_t nrows,Int_t ncols,Int_t nr_nonzeros,
141  const Int_t *pRowIndex,const Int_t *pColIndex,const Element *pData) const;
143  const TMatrixTSparse<Element> &Use (const TMatrixTSparse<Element> &a) const;
144 
145  virtual TMatrixTBase<Element> &GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
146  TMatrixTBase<Element> &target,Option_t *option="S") const;
147  TMatrixTSparse<Element> GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Option_t *option="S") const;
148  virtual TMatrixTBase<Element> &SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source);
149 
150  virtual Bool_t IsSymmetric() const { return (*this == TMatrixTSparse<Element>(kTransposed,*this)); }
152  inline TMatrixTSparse<Element> &T () { return this->Transpose(*this); }
153 
154  inline void Mult(const TMatrixTSparse<Element> &a,const TMatrixTSparse<Element> &b) { AMultB(a,b,0); }
155 
156  virtual TMatrixTBase<Element> &Zero ();
157  virtual TMatrixTBase<Element> &UnitMatrix ();
158 
159  virtual Element RowNorm () const;
160  virtual Element ColNorm () const;
161  virtual Int_t NonZeros() const { return this->fNelems; }
162 
163  virtual TMatrixTBase<Element> &NormByDiag(const TVectorT<Element> &/*v*/,Option_t * /*option*/)
164  { MayNotUse("NormByDiag"); return *this; }
165 
166  // Either access a_ij as a(i,j)
167  Element operator()(Int_t rown,Int_t coln) const;
168  Element &operator()(Int_t rown,Int_t coln);
169 
170  // or as a[i][j]
173 
176 
177  TMatrixTSparse<Element> &operator= (Element val);
178  TMatrixTSparse<Element> &operator-=(Element val);
179  TMatrixTSparse<Element> &operator+=(Element val);
180  TMatrixTSparse<Element> &operator*=(Element val);
181 
183  if (this == &source) APlusB (tmp,tmp,1);
184  else APlusB (tmp,source,1);
185  return *this; }
187  APlusB(tmp,source,1); return *this; }
189  if (this == &source) AMinusB (tmp,tmp,1);
190  else AMinusB(tmp,source,1);
191  return *this; }
193  AMinusB(tmp,source,1); return *this; }
195  if (this == &source) AMultB (tmp,tmp,1);
196  else AMultB (tmp,source,1);
197  return *this; }
199  AMultB(tmp,source,1);
200  return *this; }
201 
202  virtual TMatrixTBase <Element> &Randomize (Element alpha,Element beta,Double_t &seed);
203  virtual TMatrixTSparse<Element> &RandomizePD(Element alpha,Element beta,Double_t &seed);
204 
205  ClassDef(TMatrixTSparse,3) // Template of Sparse Matrix class
206 };
207 
208 #ifndef __CINT__
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 // __CINT__
218 
219 template <class Element> inline const Element *TMatrixTSparse<Element>::GetMatrixArray () const { return fElements; }
220 template <class Element> inline Element *TMatrixTSparse<Element>::GetMatrixArray () { return fElements; }
221 template <class Element> inline const Int_t *TMatrixTSparse<Element>::GetRowIndexArray() const { return fRowIndex; }
222 template <class Element> inline Int_t *TMatrixTSparse<Element>::GetRowIndexArray() { return fRowIndex; }
223 template <class Element> inline const Int_t *TMatrixTSparse<Element>::GetColIndexArray() const { return fColIndex; }
224 template <class Element> inline Int_t *TMatrixTSparse<Element>::GetColIndexArray() { return fColIndex; }
225 
226 template <class Element>
228  Int_t *pRowIndex,Int_t *pColIndex,Element *pData)
229  { return Use(0,nrows-1,0,ncols-1,nr_nonzeros,pRowIndex,pColIndex,pData); }
230 template <class Element>
232  const Int_t *pRowIndex,const Int_t *pColIndex,const Element *pData) const
233  { return Use(0,nrows-1,0,ncols-1,nr_nonzeros,pRowIndex,pColIndex,pData); }
234 template <class Element>
236  { R__ASSERT(a.IsValid());
237  return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetColLwb(),a.GetColUpb(),
239  a.GetColIndexArray(),a.GetMatrixArray()); }
240 template <class Element>
242  { R__ASSERT(a.IsValid());
243  return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetColLwb(),a.GetColUpb(),
245  a.GetColIndexArray(),a.GetMatrixArray()); }
246 
247 template <class Element>
249  Option_t *option) const
250  {
252  this->GetSub(row_lwb,row_upb,col_lwb,col_upb,tmp,option);
253  return tmp;
254  }
255 
256 template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixTSparse<Element> &source1,const TMatrixTSparse<Element> &source2);
257 template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixTSparse<Element> &source1,const TMatrixT<Element> &source2);
258 template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixT<Element> &source1,const TMatrixTSparse<Element> &source2);
259 template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixTSparse<Element> &source , Element val );
260 template <class Element> TMatrixTSparse<Element> operator+ ( Element val ,const TMatrixTSparse<Element> &source );
261 template <class Element> TMatrixTSparse<Element> operator- (const TMatrixTSparse<Element> &source1,const TMatrixTSparse<Element> &source2);
262 template <class Element> TMatrixTSparse<Element> operator- (const TMatrixTSparse<Element> &source1,const TMatrixT<Element> &source2);
263 template <class Element> TMatrixTSparse<Element> operator- (const TMatrixT<Element> &source1,const TMatrixTSparse<Element> &source2);
264 template <class Element> TMatrixTSparse<Element> operator- (const TMatrixTSparse<Element> &source , Element val );
265 template <class Element> TMatrixTSparse<Element> operator- ( Element val ,const TMatrixTSparse<Element> &source );
266 template <class Element> TMatrixTSparse<Element> operator* (const TMatrixTSparse<Element> &source1,const TMatrixTSparse<Element> &source2);
267 template <class Element> TMatrixTSparse<Element> operator* (const TMatrixTSparse<Element> &source1,const TMatrixT<Element> &source2);
268 template <class Element> TMatrixTSparse<Element> operator* (const TMatrixT<Element> &source1,const TMatrixTSparse<Element> &source2);
269 template <class Element> TMatrixTSparse<Element> operator* ( Element val ,const TMatrixTSparse<Element> &source );
270 template <class Element> TMatrixTSparse<Element> operator* (const TMatrixTSparse<Element> &source, Element val );
271 
272 template <class Element> TMatrixTSparse<Element> &Add (TMatrixTSparse<Element> &target, Element scalar,
273  const TMatrixTSparse<Element> &source);
274 template <class Element> TMatrixTSparse<Element> &ElementMult(TMatrixTSparse<Element> &target,const TMatrixTSparse<Element> &source);
275 template <class Element> TMatrixTSparse<Element> &ElementDiv (TMatrixTSparse<Element> &target,const TMatrixTSparse<Element> &source);
276 
277 template <class Element> Bool_t AreCompatible(const TMatrixTSparse<Element> &m1,const TMatrixTSparse<Element> &m2,Int_t verbose=0);
278 
279 #endif
void AMinusB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix subtraction.
virtual const Element * GetMatrixArray() const
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
virtual TMatrixTBase< Element > & InsertRow(Int_t row, Int_t col, const Element *v, Int_t n=-1)
Insert in row rown, n elements of array v at column coln.
virtual void GetMatrix2Array(Element *data, Option_t *="") const
Copy matrix data to array . It is assumed that array is of size >= fNelems.
TMatrixTSparse< Element > & operator*=(const TMatrixT< Element > &source)
Int_t GetRowLwb() const
Definition: TMatrixTBase.h:132
virtual const Int_t * GetRowIndexArray() const
TMatrixTSparse< Element > & Transpose(const TMatrixTSparse< Element > &source)
Transpose a matrix.
const char Option_t
Definition: RtypesCore.h:62
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
Element * fElements
TVectorT.
Definition: TMatrixTBase.h:89
#define R__ASSERT(e)
Definition: TError.h:98
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *="")
Copy array data to matrix .
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
TMatrixTSparse< Element > & operator+=(const TMatrixT< Element > &source)
TMatrixTSparse< Element > & operator-=(const TMatrixTSparse< Element > &source)
const char * Class
Definition: TXMLSetup.cxx:64
Element operator()(Int_t rown, Int_t coln) const
virtual ~TMatrixTSparse()
double beta(double x, double y)
Calculates the beta function.
void AMultB(const TMatrixTSparse< Element > &a, const TMatrixT< Element > &b, Int_t constr=0)
TMatrixT.
Definition: TMatrixDfwd.h:24
Int_t GetNoElements() const
Definition: TMatrixTBase.h:138
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &, Option_t *)
option:
#define ClassDef(name, id)
Definition: Rtypes.h:254
Int_t GetColLwb() const
Definition: TMatrixTBase.h:135
virtual TMatrixTBase< Element > & SetRowIndexArray(Int_t *data)
TMatrixTSparse< Element > & ElementDiv(TMatrixTSparse< Element > &target, const TMatrixTSparse< Element > &source)
Divide target by the source, element-by-element.
TMatrixTSparse< Element > operator-(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
TMatrixTSparseRow< Element > operator[](Int_t rown)
TMatrixTSparse< Element > & operator*=(const TMatrixTSparse< Element > &source)
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:978
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
randomize matrix element values
void AMultBt(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix multiplication.
void APlusB(const TMatrixT< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
virtual TMatrixTSparse< Element > & RandomizePD(Element alpha, Element beta, Double_t &seed)
randomize matrix element values but keep matrix symmetric positive definite
Int_t fNrowIndex
Definition: TMatrixTBase.h:106
TMatrixTSparse< Element > & SetSparseIndexAB(const TMatrixTSparse< Element > &a, const TMatrixT< Element > &b)
const TMatrixTSparseRow_const< Element > operator[](Int_t rown) const
TMatrixTBase< Element > & ResizeTo(const TMatrixTSparse< Element > &m)
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
TMatrixTSparse< Element > & operator-=(Element val)
Subtract val from every element of the matrix.
Bool_t AreCompatible(const TMatrixTSparse< Element > &m1, const TMatrixTSparse< Element > &m2, Int_t verbose=0)
TMatrixTSparse< Element > & Add(TMatrixTSparse< Element > &target, Element scalar, const TMatrixTSparse< Element > &source)
Modify addition: target += scalar * source.
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
SVector< double, 2 > v
Definition: Dict.h:5
TMatrixTSparse.
virtual void Clear(Option_t *="")
TMatrixTSparse< Element > & operator*=(Element val)
Multiply every element of the matrix with val.
TMarker * m
Definition: textangle.C:8
bool verbose
TMatrixTSparse< Element > & SetSparseIndex(Int_t nelem_new)
Increase/decrease the number of non-zero elements to nelems_new.
TMatrixTSparse< Element > & ElementMult(TMatrixTSparse< Element > &target, const TMatrixTSparse< Element > &source)
Multiply target by the source, element-by-element.
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
Int_t GetColUpb() const
Definition: TMatrixTBase.h:136
TMatrixTSparse< Element > & T()
Linear Algebra Package.
virtual const Int_t * GetColIndexArray() const
void Allocate(Int_t nrows, Int_t ncols, Int_t row_lwb=0, Int_t col_lwb=0, Int_t init=0, Int_t nr_nonzeros=0)
Allocate new matrix.
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *data)
static Int_t init()
double Double_t
Definition: RtypesCore.h:55
TMatrixTSparse< Element > & Use(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Int_t nr_nonzeros, Int_t *pRowIndex, Int_t *pColIndex, Element *pData)
TMatrixTSparse< Element > & SetSparseIndexAB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b)
Set the row/column indices to the "sum" of matrices a and b It is checked that enough space has been ...
Bool_t fIsOwner
Definition: TMatrixTBase.h:111
TMatrixTSparse< Element > & operator+=(const TMatrixTSparse< Element > &source)
TMatrixTSparse< Element > operator+(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
TMatrixTSparse< Element > & operator-=(const TMatrixT< Element > &source)
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros=-1)
Set size of the matrix to nrows x ncols with nr_nonzeros non-zero entries if nr_nonzeros > 0 ...
TMatrixTSparse< Element > & operator+=(Element val)
Add val to every element of the matrix.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
Int_t GetRowUpb() const
Definition: TMatrixTBase.h:133
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
TMatrixTSparse< Element > & operator=(const TMatrixT< Element > &source)
Notice that the sparsity of the matrix is NOT changed : its fRowIndex/fColIndex are used ! ...
void AMultB(const TMatrixT< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
void Mult(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b)
const TMatrixTSparse< Element > & Use(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Int_t nr_nonzeros, const Int_t *pRowIndex, const Int_t *pColIndex, const Element *pData) const
void AMultB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
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
Get submatrix [row_lwb..row_upb][col_lwb..col_upb]; The indexing range of the returned matrix depends...
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.
void APlusB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix addition.
const Int_t n
Definition: legend1.C:16
virtual TMatrixTBase< Element > & SetSub(Int_t row_lwb, Int_t col_lwb, const TMatrixTBase< Element > &source)
Insert matrix source starting at [row_lwb][col_lwb], thereby overwriting the part [row_lwb...
Bool_t IsValid() const
Definition: TMatrixTBase.h:157
TMatrixTSparse< Element > operator*(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)