1 #include "AliMatrixSparse.h"
2 #include "TStopwatch.h"
4 /**********************************************************************************************/
5 /* Sparse matrix class, used as a global matrix for AliMillePede2 */
7 /* Author: ruben.shahoyan@cern.ch */
9 /**********************************************************************************************/
11 //___________________________________________________________
12 ClassImp(AliMatrixSparse)
14 //___________________________________________________________
15 AliMatrixSparse::AliMatrixSparse(Int_t sz)
16 : AliMatrixSq(),fVecs(0)
20 fVecs = new AliVectorSparse*[sz];
21 for (int i=GetSize();i--;) fVecs[i] = new AliVectorSparse();
24 //___________________________________________________________
25 AliMatrixSparse::AliMatrixSparse(const AliMatrixSparse& src)
26 : AliMatrixSq(src),fVecs(0)
28 fVecs = new AliVectorSparse*[src.GetSize()];
29 for (int i=GetSize();i--;) fVecs[i] = new AliVectorSparse( *src.GetRow(i));
32 //___________________________________________________________
33 AliVectorSparse* AliMatrixSparse::GetRowAdd(Int_t ir)
36 AliVectorSparse** arrv = new AliVectorSparse*[ir+1];
37 for (int i=GetSize();i--;) arrv[i] = fVecs[i];
40 for (int i=GetSize();i<=ir;i++) fVecs[i] = new AliVectorSparse();
42 if (IsSymmetric() && fNcols<fNrows) fNcols = fNrows;
47 //___________________________________________________________
48 AliMatrixSparse& AliMatrixSparse::operator=(const AliMatrixSparse& src)
50 if (*this == src) return *this;
52 fNcols = src.GetNCols();
53 fNrows = src.GetNRows();
54 SetSymmetric(src.IsSymmetric());
55 fVecs = new AliVectorSparse*[fNrows];
56 for (int i=fNrows;i--;) fVecs[i] = new AliVectorSparse( *src.GetRow(i));
60 //___________________________________________________________
61 void AliMatrixSparse::Clear(Option_t*)
63 for (int i=fNrows;i--;) delete GetRow(i);
68 //___________________________________________________________
69 void AliMatrixSparse::Print(Option_t* opt) const
71 printf("Sparse Matrix of size %d x %d %s\n",fNrows,fNcols,IsSymmetric() ? " (Symmetric)":"");
72 for (int i=0;i<fNrows;i++) {
73 AliVectorSparse* row = GetRow(i);
74 if (!row->GetNElems()) continue;
80 //___________________________________________________________
81 void AliMatrixSparse::MultiplyByVec(const Double_t* vecIn, Double_t* vecOut) const
83 // fill vecOut by matrix*vecIn
84 // vector should be of the same size as the matrix
86 memset(vecOut,0,GetSize()*sizeof(Double_t));
88 for (int rw=GetSize();rw--;) { // loop over rows >>>
89 const AliVectorSparse* rowV = GetRow(rw);
90 Int_t nel = rowV->GetNElems();
93 UShort_t* indV = rowV->GetIndices();
94 Double_t* elmV = rowV->GetElems();
97 // treat diagonal term separately. If filled, it should be the last one
98 if (indV[--nel]==rw) vecOut[rw] += vecIn[rw]*elmV[nel];
99 else nel = rowV->GetNElems(); // diag elem was not filled
101 for (int iel=nel;iel--;) { // less element retrieval for symmetric case
103 vecOut[rw] += vecIn[indV[iel]]*elmV[iel];
104 vecOut[indV[iel]] += vecIn[rw]*elmV[iel];
108 else for (int iel=nel;iel--;) if (elmV[iel]) vecOut[rw] += vecIn[indV[iel]]*elmV[iel];
110 } // loop over rows <<<
114 //___________________________________________________________
115 void AliMatrixSparse::SortIndices(Bool_t valuesToo)
119 printf("AliMatrixSparse:sort>>\n");
120 // sort columns in increasing order. Used to fix the matrix after ILUk decompostion
121 for (int i=GetSize();i--;) GetRow(i)->SortIndices(valuesToo);
124 printf("AliMatrixSparse:sort<<\n");
127 //___________________________________________________________
128 void AliMatrixSparse::AddToRow(Int_t r, Double_t *valc,Int_t *indc,Int_t n)
130 // for sym. matrix count how many elems to add have row>=col and assign excplicitly
131 // those which have row<col
133 // range in increasing order of indices
135 for (int j=i;j>=0;j--) {
136 if (indc[j]>indc[i]) { // swap
137 int ti = indc[i]; indc[i] = indc[j]; indc[j] = ti;
138 double tv = valc[i]; valc[i] = valc[j]; valc[j] = tv;
146 if (indc[ni]>r) (*this)(indc[ni],r) += valc[ni];
147 else break; // use the fact that the indices are ranged in increasing order
152 AliVectorSparse* row = GetRowAdd(r);
153 row->Add(valc,indc,ni+1);
156 //___________________________________________________________
157 Float_t AliMatrixSparse::GetDensity() const
159 // get fraction of non-zero elements
161 for (int i=GetSize();i--;) nel += GetRow(i)->GetNElems();
162 int den = IsSymmetric() ? (GetSize()+1)*GetSize()/2 : GetSize()*GetSize();
163 return float(nel)/den;