Logo ROOT   6.16/01
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#include "TMatrixTBase.h"
16#include "TMatrixTUtils.h"
17
18
19#ifdef CBLAS
20#include <vecLib/vBLAS.h>
21//#include <cblas.h>
22#endif
23
24//////////////////////////////////////////////////////////////////////////
25// //
26// TMatrixTSparse //
27// //
28// Template class of a general sparse matrix in the Harwell-Boeing //
29// format //
30// //
31//////////////////////////////////////////////////////////////////////////
32
33template<class Element> class TMatrixT;
34
35template<class Element> class TMatrixTSparse : public TMatrixTBase<Element> {
36
37protected:
38
39 Int_t *fRowIndex; //[fNrowIndex] row index
40 Int_t *fColIndex; //[fNelems] column index
41 Element *fElements; //[fNelems]
42
43 void Allocate(Int_t nrows,Int_t ncols,Int_t row_lwb = 0,Int_t col_lwb = 0,
44 Int_t init = 0,Int_t nr_nonzeros = 0);
45
46 // Elementary constructors
49 void AMultB (const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0) {
50 const TMatrixTSparse<Element> bsp = b;
52 void AMultB (const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0) {
54
56 void AMultBt(const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0);
57 void AMultBt(const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
58
59 void APlusB (const TMatrixTSparse<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
60 void APlusB (const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0);
61 void APlusB (const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0) { APlusB(b,a,constr); }
62
64 void AMinusB(const TMatrixTSparse<Element> &a,const TMatrixT<Element> &b,Int_t constr=0);
65 void AMinusB(const TMatrixT<Element> &a,const TMatrixTSparse<Element> &b,Int_t constr=0);
66
67public:
68
71
73 TMatrixTSparse(Int_t nrows,Int_t ncols);
74 TMatrixTSparse(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb);
75 TMatrixTSparse(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_nonzeros,
76 Int_t *row, Int_t *col,Element *data);
78 TMatrixTSparse(const TMatrixT<Element> &another);
79
84
85 virtual ~TMatrixTSparse() { Clear(); }
86
87 virtual const Element *GetMatrixArray () const;
88 virtual Element *GetMatrixArray ();
89 virtual const Int_t *GetRowIndexArray() const;
91 virtual const Int_t *GetColIndexArray() const;
93
94 virtual TMatrixTBase<Element> &SetRowIndexArray(Int_t *data) { memmove(fRowIndex,data,(this->fNrows+1)*sizeof(Int_t)); return *this; }
95 virtual TMatrixTBase<Element> &SetColIndexArray(Int_t *data) { memmove(fColIndex,data,this->fNelems*sizeof(Int_t)); return *this; }
96
102 { return SetSparseIndexAB(b,a); }
103
104 virtual void GetMatrix2Array (Element *data,Option_t * /*option*/ ="") const;
105 virtual TMatrixTBase<Element> &SetMatrixArray (const Element *data,Option_t * /*option*/="")
106 { memcpy(fElements,data,this->fNelems*sizeof(Element)); return *this; }
107 virtual TMatrixTBase<Element> &SetMatrixArray (Int_t nr_nonzeros,Int_t *irow,Int_t *icol,Element *data);
108 virtual TMatrixTBase<Element> &InsertRow (Int_t row,Int_t col,const Element *v,Int_t n=-1);
109 virtual void ExtractRow (Int_t row,Int_t col, Element *v,Int_t n=-1) const;
110
111 virtual TMatrixTBase<Element> &ResizeTo(Int_t nrows,Int_t ncols,Int_t nr_nonzeros=-1);
112 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);
113 inline TMatrixTBase<Element> &ResizeTo(const TMatrixTSparse<Element> &m) {return ResizeTo(m.GetRowLwb(),m.GetRowUpb(),m.GetColLwb(),
114 m.GetColUpb(),m.GetNoElements()); }
115
116 virtual void Clear(Option_t * /*option*/ ="") { if (this->fIsOwner) {
117 if (fElements) { delete [] fElements; fElements = 0; }
118 if (fRowIndex) { delete [] fRowIndex; fRowIndex = 0; }
119 if (fColIndex) { delete [] fColIndex; fColIndex = 0; }
120 }
121 this->fNelems = 0;
122 this->fNrowIndex = 0;
123 }
124
125 TMatrixTSparse<Element> &Use (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_nonzeros,
126 Int_t *pRowIndex,Int_t *pColIndex,Element *pData);
127 const TMatrixTSparse<Element> &Use (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t nr_nonzeros,
128 const Int_t *pRowIndex,const Int_t *pColIndex,const Element *pData) const
129 { return (const TMatrixTSparse<Element>&)
130 ((const_cast<TMatrixTSparse<Element> *>(this))->Use(row_lwb,row_upb,col_lwb,col_upb,nr_nonzeros,
131 const_cast<Int_t *>(pRowIndex),
132 const_cast<Int_t *>(pColIndex),
133 const_cast<Element *>(pData))); }
134 TMatrixTSparse<Element> &Use (Int_t nrows,Int_t ncols,Int_t nr_nonzeros,
135 Int_t *pRowIndex,Int_t *pColIndex,Element *pData);
136 const TMatrixTSparse<Element> &Use (Int_t nrows,Int_t ncols,Int_t nr_nonzeros,
137 const Int_t *pRowIndex,const Int_t *pColIndex,const Element *pData) const;
140
141 virtual TMatrixTBase<Element> &GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,
142 TMatrixTBase<Element> &target,Option_t *option="S") const;
143 TMatrixTSparse<Element> GetSub(Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Option_t *option="S") const;
144 virtual TMatrixTBase<Element> &SetSub(Int_t row_lwb,Int_t col_lwb,const TMatrixTBase<Element> &source);
145
146 virtual Bool_t IsSymmetric() const { return (*this == TMatrixTSparse<Element>(kTransposed,*this)); }
148 inline TMatrixTSparse<Element> &T () { return this->Transpose(*this); }
149
150 inline void Mult(const TMatrixTSparse<Element> &a,const TMatrixTSparse<Element> &b) { AMultB(a,b,0); }
151
152 virtual TMatrixTBase<Element> &Zero ();
154
155 virtual Element RowNorm () const;
156 virtual Element ColNorm () const;
157 virtual Int_t NonZeros() const { return this->fNelems; }
158
159 virtual TMatrixTBase<Element> &NormByDiag(const TVectorT<Element> &/*v*/,Option_t * /*option*/)
160 { MayNotUse("NormByDiag"); return *this; }
161
162 // Either access a_ij as a(i,j)
163 Element operator()(Int_t rown,Int_t coln) const;
164 Element &operator()(Int_t rown,Int_t coln);
165
166 // or as a[i][j]
169
172
177
179 if (this == &source) APlusB (tmp,tmp,1);
180 else APlusB (tmp,source,1);
181 return *this; }
183 APlusB(tmp,source,1); return *this; }
185 if (this == &source) AMinusB (tmp,tmp,1);
186 else AMinusB(tmp,source,1);
187 return *this; }
189 AMinusB(tmp,source,1); return *this; }
191 if (this == &source) AMultB (tmp,tmp,1);
192 else AMultB (tmp,source,1);
193 return *this; }
195 AMultB(tmp,source,1);
196 return *this; }
197
198 virtual TMatrixTBase <Element> &Randomize (Element alpha,Element beta,Double_t &seed);
199 virtual TMatrixTSparse<Element> &RandomizePD(Element alpha,Element beta,Double_t &seed);
200
201 ClassDef(TMatrixTSparse,3) // Template of Sparse Matrix class
202};
203
204#ifndef __CINT__
205// When building with -fmodules, it instantiates all pending instantiations,
206// instead of delaying them until the end of the translation unit.
207// We 'got away with' probably because the use and the definition of the
208// explicit specialization do not occur in the same TU.
209//
210// In case we are building with -fmodules, we need to forward declare the
211// specialization in order to compile the dictionary G__Matrix.cxx.
213#endif // __CINT__
214
215template <class Element> inline const Element *TMatrixTSparse<Element>::GetMatrixArray () const { return fElements; }
216template <class Element> inline Element *TMatrixTSparse<Element>::GetMatrixArray () { return fElements; }
217template <class Element> inline const Int_t *TMatrixTSparse<Element>::GetRowIndexArray() const { return fRowIndex; }
218template <class Element> inline Int_t *TMatrixTSparse<Element>::GetRowIndexArray() { return fRowIndex; }
219template <class Element> inline const Int_t *TMatrixTSparse<Element>::GetColIndexArray() const { return fColIndex; }
220template <class Element> inline Int_t *TMatrixTSparse<Element>::GetColIndexArray() { return fColIndex; }
221
222template <class Element>
224 Int_t *pRowIndex,Int_t *pColIndex,Element *pData)
225 { return Use(0,nrows-1,0,ncols-1,nr_nonzeros,pRowIndex,pColIndex,pData); }
226template <class Element>
228 const Int_t *pRowIndex,const Int_t *pColIndex,const Element *pData) const
229 { return Use(0,nrows-1,0,ncols-1,nr_nonzeros,pRowIndex,pColIndex,pData); }
230template <class Element>
232 { R__ASSERT(a.IsValid());
233 return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetColLwb(),a.GetColUpb(),
234 a.GetNoElements(),a.GetRowIndexArray(),
235 a.GetColIndexArray(),a.GetMatrixArray()); }
236template <class Element>
238 { R__ASSERT(a.IsValid());
239 return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetColLwb(),a.GetColUpb(),
240 a.GetNoElements(),a.GetRowIndexArray(),
241 a.GetColIndexArray(),a.GetMatrixArray()); }
242
243template <class Element>
245 Option_t *option) const
246 {
248 this->GetSub(row_lwb,row_upb,col_lwb,col_upb,tmp,option);
249 return tmp;
250 }
251
252template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixTSparse<Element> &source1,const TMatrixTSparse<Element> &source2);
253template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixTSparse<Element> &source1,const TMatrixT<Element> &source2);
254template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixT<Element> &source1,const TMatrixTSparse<Element> &source2);
255template <class Element> TMatrixTSparse<Element> operator+ (const TMatrixTSparse<Element> &source , Element val );
256template <class Element> TMatrixTSparse<Element> operator+ ( Element val ,const TMatrixTSparse<Element> &source );
257template <class Element> TMatrixTSparse<Element> operator- (const TMatrixTSparse<Element> &source1,const TMatrixTSparse<Element> &source2);
258template <class Element> TMatrixTSparse<Element> operator- (const TMatrixTSparse<Element> &source1,const TMatrixT<Element> &source2);
259template <class Element> TMatrixTSparse<Element> operator- (const TMatrixT<Element> &source1,const TMatrixTSparse<Element> &source2);
260template <class Element> TMatrixTSparse<Element> operator- (const TMatrixTSparse<Element> &source , Element val );
261template <class Element> TMatrixTSparse<Element> operator- ( Element val ,const TMatrixTSparse<Element> &source );
262template <class Element> TMatrixTSparse<Element> operator* (const TMatrixTSparse<Element> &source1,const TMatrixTSparse<Element> &source2);
263template <class Element> TMatrixTSparse<Element> operator* (const TMatrixTSparse<Element> &source1,const TMatrixT<Element> &source2);
264template <class Element> TMatrixTSparse<Element> operator* (const TMatrixT<Element> &source1,const TMatrixTSparse<Element> &source2);
265template <class Element> TMatrixTSparse<Element> operator* ( Element val ,const TMatrixTSparse<Element> &source );
266template <class Element> TMatrixTSparse<Element> operator* (const TMatrixTSparse<Element> &source, Element val );
267
268template <class Element> TMatrixTSparse<Element> &Add (TMatrixTSparse<Element> &target, Element scalar,
269 const TMatrixTSparse<Element> &source);
270template <class Element> TMatrixTSparse<Element> &ElementMult(TMatrixTSparse<Element> &target,const TMatrixTSparse<Element> &source);
271template <class Element> TMatrixTSparse<Element> &ElementDiv (TMatrixTSparse<Element> &target,const TMatrixTSparse<Element> &source);
272
273template <class Element> Bool_t AreCompatible(const TMatrixTSparse<Element> &m1,const TMatrixTSparse<Element> &m2,Int_t verbose=0);
274
275#endif
SVector< double, 2 > v
Definition: Dict.h:5
#define b(i)
Definition: RSha256.hxx:100
static Int_t init()
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:324
#define R__ASSERT(e)
Definition: TError.h:96
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)
TMatrixTSparse< Element > operator-(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
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.
TMatrixTSparse< Element > operator+(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
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:75
Linear Algebra Package.
Definition: TMatrixTBase.h:85
Bool_t fIsOwner
Definition: TMatrixTBase.h:101
Int_t fNrowIndex
Definition: TMatrixTBase.h:96
TMatrixTSparse.
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
virtual ~TMatrixTSparse()
TMatrixTSparse< Element > & operator+=(Element val)
Add val to every element of the matrix.
TMatrixTBase< Element > & ResizeTo(const TMatrixTSparse< Element > &m)
TMatrixTSparse< Element > & SetSparseIndex(Int_t nelem_new)
Increase/decrease the number of non-zero elements to nelems_new.
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 > & 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)
Element operator()(Int_t rown, Int_t coln) const
virtual void Clear(Option_t *="")
TMatrixTSparse< Element > & operator+=(const TMatrixTSparse< Element > &source)
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 TMatrixTSparse< Element > &source)
void AMinusB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix subtraction.
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *="")
Copy array data to matrix .
TMatrixTSparse< Element > & operator*=(Element val)
Multiply every element of the matrix with val.
void AMultB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
virtual TMatrixTBase< Element > & SetRowIndexArray(Int_t *data)
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....
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
randomize matrix element values
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
virtual const Int_t * GetRowIndexArray() const
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 Int_t * GetRowIndexArray()
void AMultBt(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix multiplication.
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.
TMatrixTSparse< Element > & Transpose(const TMatrixTSparse< Element > &source)
Transpose a matrix.
TMatrixTSparse< Element > & operator*=(const TMatrixTSparse< Element > &source)
virtual TMatrixTBase< Element > & SetColIndexArray(Int_t *data)
TMatrixTSparse< Element > & operator-=(Element val)
Subtract val from every element of the matrix.
virtual Element * GetMatrixArray()
TMatrixTSparse< Element > & operator+=(const TMatrixT< Element > &source)
virtual const Element * GetMatrixArray() const
TClass * Class()
void APlusB(const TMatrixT< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
TMatrixTSparseRow< Element > operator[](Int_t rown)
virtual Int_t * GetColIndexArray()
virtual const Int_t * GetColIndexArray() const
TMatrixTSparse< Element > & operator*=(const TMatrixT< Element > &source)
const TMatrixTSparse< Element > & Use(const TMatrixTSparse< Element > &a) const
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
Element * fElements
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 Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &, Option_t *)
option:
void AMultB(const TMatrixT< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
const TMatrixTSparseRow_const< Element > operator[](Int_t rown) const
void APlusB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix addition.
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
virtual TMatrixTSparse< Element > & RandomizePD(Element alpha, Element beta, Double_t &seed)
randomize matrix element values but keep matrix symmetric positive definite
TMatrixTSparse< Element > & Use(TMatrixTSparse< Element > &a)
TMatrixTSparse< Element > & operator=(const TMatrixT< Element > &source)
Notice that the sparsity of the matrix is NOT changed : its fRowIndex/fColIndex are used !
TMatrixTSparse< Element > & T()
void AMultB(const TMatrixTSparse< Element > &a, const TMatrixT< Element > &b, Int_t constr=0)
const TMatrixTSparse< Element > & Use(Int_t nrows, Int_t ncols, Int_t nr_nonzeros, const Int_t *pRowIndex, const Int_t *pColIndex, const Element *pData) const
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
TMatrixTSparse< Element > & SetSparseIndexAB(const TMatrixTSparse< Element > &a, const TMatrixT< Element > &b)
TMatrixTSparse< Element > GetSub(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Option_t *option="S") const
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
TMatrixTSparse< Element > & operator-=(const TMatrixT< Element > &source)
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 ...
void Mult(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b)
TMatrixTSparse< Element > & Use(Int_t nrows, Int_t ncols, Int_t nr_nonzeros, Int_t *pRowIndex, Int_t *pColIndex, Element *pData)
TMatrixT.
Definition: TMatrixT.h:39
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:933
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 m2
auto * m
Definition: textangle.C:8
auto * a
Definition: textangle.C:12