2 /*********************************************************************************/
3 /* Symmetric Band Diagonal matrix with half band width W (+1 for diagonal) */
4 /* Only lower triangle is stored in the "profile" format */
7 /* Author: ruben.shahoyan@cern.ch */
9 /*********************************************************************************/
17 #include "AliSymBDMatrix.h"
20 ClassImp(AliSymBDMatrix)
23 //___________________________________________________________
24 AliSymBDMatrix::AliSymBDMatrix()
31 //___________________________________________________________
32 AliSymBDMatrix::AliSymBDMatrix(Int_t size, Int_t w)
33 : AliMatrixSq(),fElems(0)
35 // c-tor for given size
37 fNcols = size; // number of rows
39 if (w>=size) w = size-1;
44 // total number of stored elements
45 fNelems = size*(w+1) - w*(w+1)/2;
47 fElems = new Double_t[fNcols*fRowLwb];
48 memset(fElems,0,fNcols*fRowLwb*sizeof(Double_t));
52 //___________________________________________________________
53 AliSymBDMatrix::AliSymBDMatrix(const AliSymBDMatrix &src)
54 : AliMatrixSq(src),fElems(0)
57 if (src.GetSize()<1) return;
58 fNcols = src.GetSize();
59 fNrows = src.GetBandHWidth();
61 fNelems = src.GetNElemsStored();
62 fElems = new Double_t[fNcols*fRowLwb];
63 memcpy(fElems,src.fElems,fNcols*fRowLwb*sizeof(Double_t));
67 //___________________________________________________________
68 AliSymBDMatrix::~AliSymBDMatrix()
74 //___________________________________________________________
75 AliSymBDMatrix& AliSymBDMatrix::operator=(const AliSymBDMatrix& src)
80 TObject::operator=(src);
81 if (fNcols!=src.fNcols) {
82 // recreate the matrix
83 if (fElems) delete[] fElems;
84 fNcols = src.GetSize();
85 fNrows = src.GetBandHWidth();
86 fNelems = src.GetNElemsStored();
87 fRowLwb = src.fRowLwb;
88 fElems = new Double_t[fNcols*fRowLwb];
90 memcpy(fElems,src.fElems,fNcols*fRowLwb*sizeof(Double_t));
98 //___________________________________________________________
99 void AliSymBDMatrix::Clear(Option_t*)
101 // clear dynamic part
102 if (fElems) {delete[] fElems; fElems = 0;}
104 fNelems = fNcols = fNrows = fRowLwb = 0;
108 //___________________________________________________________
109 Float_t AliSymBDMatrix::GetDensity() const
111 // get fraction of non-zero elements
112 if (!fNelems) return 0;
114 for (int i=fNelems;i--;) if (!IsZero(fElems[i])) nel++;
118 //___________________________________________________________
119 void AliSymBDMatrix::Print(Option_t* option) const
122 printf("Symmetric Band-Diagonal Matrix : Size = %d, half bandwidth = %d\n",
123 GetSize(),GetBandHWidth());
124 TString opt = option; opt.ToLower();
125 if (opt.IsNull()) return;
126 opt = "%"; opt += 1+int(TMath::Log10(double(GetSize()))); opt+="d|";
127 for (Int_t i=0;i<GetSize();i++) {
129 for (Int_t j=TMath::Max(0,i-GetBandHWidth());j<=i;j++) printf("%+.3e|",GetEl(i,j));
134 //___________________________________________________________
135 void AliSymBDMatrix::MultiplyByVec(const Double_t *vecIn,Double_t *vecOut) const
137 // fill vecOut by matrix*vecIn
138 // vector should be of the same size as the matrix
139 if (IsDecomposed()) {
140 for (int i=0;i<GetSize();i++) {
142 int jmax = TMath::Min(GetSize(),i+fRowLwb);
143 for (int j=i+1;j<jmax;j++) sm += vecIn[j]*Query(j,i);
144 vecOut[i] = QueryDiag(i)*(vecIn[i]+sm);
146 for (int i=GetSize();i--;) {
148 int jmin = TMath::Max(0,i - GetBandHWidth());
150 for (int j=jmin;j<jmax;j++) sm += vecOut[j]*Query(i,j);
154 else { // not decomposed
155 for (int i=GetSize();i--;) {
157 int jmin = TMath::Max(0,i - GetBandHWidth());
158 int jmax = TMath::Min(GetSize(),i + fRowLwb);
159 for (int j=jmin;j<jmax;j++) vecOut[i] += vecIn[j]*Query(i,j);
165 //___________________________________________________________
166 void AliSymBDMatrix::Reset()
168 // set all elems to 0
169 if (fElems) memset(fElems,0,fNcols*fRowLwb*sizeof(Double_t));
170 SetDecomposed(kFALSE);
174 //___________________________________________________________
175 void AliSymBDMatrix::AddToRow(Int_t r, Double_t *valc,Int_t *indc,Int_t n)
177 // add list of elements to row r
178 for (int i=0;i<n;i++) (*this)(r,indc[i]) = valc[i];
181 //___________________________________________________________
182 void AliSymBDMatrix::DecomposeLDLT()
184 // decomposition to L Diag L^T
185 if (IsDecomposed()) return;
187 Double_t eps = std::numeric_limits<double>::epsilon()*std::numeric_limits<double>::epsilon();
189 Double_t dtmp,gamma=0.0,xi=0.0;
192 // find max diag and number of non-0 diag.elements
193 for (dtmp=0.0,iDiag=0;iDiag<GetSize();iDiag++) {
194 if ( (dtmp=QueryDiag(iDiag)) <=0.0) break;
195 if (gamma < dtmp) gamma = dtmp;
198 // find max. off-diag element
199 for (int ir=1;ir<iDiag;ir++) {
200 for (int ic=ir-GetBandHWidth();ic<ir;ic++) {
202 dtmp = TMath::Abs(Query(ir,ic));
203 if (xi<dtmp) xi = dtmp;
206 double delta = eps*TMath::Max(1.0,xi+gamma);
208 double sn = GetSize()>1 ? 1.0/TMath::Sqrt( GetSize()*GetSize() - 1.0) : 1.0;
209 double beta = TMath::Sqrt(TMath::Max(eps,TMath::Max(gamma,xi*sn)));
211 for (int kr=1;kr<GetSize();kr++) {
212 int colKmin = TMath::Max(0,kr - GetBandHWidth());
215 for (int jr=colKmin;jr<=kr;jr++) {
216 int colJmin = TMath::Max(0,jr - GetBandHWidth());
219 for (int i=TMath::Max(colKmin,colJmin);i<jr;i++)
220 dtmp += Query(kr,i)*QueryDiag(i)*Query(jr,i);
221 dtmp = (*this)(kr,jr) -= dtmp;
223 theta = TMath::Max(theta, TMath::Abs(dtmp));
226 if (!IsZero(QueryDiag(jr))) (*this)(kr,jr) /= QueryDiag(jr);
227 else (*this)(kr,jr) = 0.0;
232 dtmp = TMath::Max(dtmp, delta);
233 (*this)(kr,jr) = TMath::Max( TMath::Abs(Query(kr,jr)), dtmp);
238 for (int i=0;i<GetSize();i++) {
240 if (!IsZero(dtmp)) DiagElem(i) = 1./dtmp;
246 //___________________________________________________________
247 void AliSymBDMatrix::Solve(Double_t *rhs)
249 // solve matrix equation
251 if (!IsDecomposed()) DecomposeLDLT();
253 for (int kr=0;kr<GetSize();kr++)
254 for (int jr=TMath::Max(0,kr-GetBandHWidth());jr<kr;jr++) rhs[kr] -= Query(kr,jr)*rhs[jr];
256 for (int kr=GetSize();kr--;) rhs[kr] *= QueryDiag(kr);
258 for (int kr=GetSize();kr--;)
259 for (int jr=TMath::Max(0,kr - GetBandHWidth());jr<kr;jr++) rhs[jr] -= Query(kr,jr)*rhs[kr];
263 //___________________________________________________________
264 void AliSymBDMatrix::Solve(const Double_t *rhs,Double_t *sol)
266 // solve matrix equation
267 memcpy(sol,rhs,GetSize()*sizeof(Double_t));