7 #include "AliSymMatrix.h"
12 ClassImp(AliSymMatrix)
15 AliSymMatrix* AliSymMatrix::fgBuffer = 0;
16 Int_t AliSymMatrix::fgCopyCnt = 0;
17 //___________________________________________________________
18 AliSymMatrix::AliSymMatrix()
19 : fElems(0),fElemsAdd(0)
25 //___________________________________________________________
26 AliSymMatrix::AliSymMatrix(Int_t size)
27 : AliMatrixSq(),fElems(0),fElemsAdd(0)
31 fNrowIndex = fNcols = size;
32 fElems = new Double_t[fNcols*(fNcols+1)/2];
39 //___________________________________________________________
40 AliSymMatrix::AliSymMatrix(const AliSymMatrix &src)
41 : AliMatrixSq(src),fElems(0),fElemsAdd(0)
43 fNrowIndex = fNcols = src.GetSize();
46 int nmainel = fNcols*(fNcols+1)/2;
47 fElems = new Double_t[nmainel];
48 nmainel = src.fNcols*(src.fNcols+1)/2;
49 memcpy(fElems,src.fElems,nmainel*sizeof(Double_t));
50 if (src.fNrows) { // transfer extra rows to main matrix
51 Double_t *pnt = fElems + nmainel;
52 int ncl = src.fNcols + 1;
53 for (int ir=0;ir<src.fNrows;ir++) {
54 memcpy(pnt,src.fElemsAdd[ir],ncl*sizeof(Double_t));
66 //___________________________________________________________
67 AliSymMatrix::~AliSymMatrix()
70 if (--fgCopyCnt < 1 && fgBuffer) {delete fgBuffer; fgBuffer = 0;}
73 //___________________________________________________________
74 AliSymMatrix& AliSymMatrix::operator=(const AliSymMatrix& src)
78 TObject::operator=(src);
79 if (fNcols!=src.fNcols && fNrows!=src.fNrows) {
80 // recreate the matrix
81 if (fElems) delete[] fElems;
82 for (int i=0;i<fNrows;i++) delete[] fElemsAdd[i];
85 fNrowIndex = src.GetSize();
86 fNcols = src.GetSize();
88 fElems = new Double_t[fNcols*(fNcols+1)/2];
89 int nmainel = src.fNcols*(src.fNcols+1);
90 memcpy(fElems,src.fElems,nmainel*sizeof(Double_t));
91 if (src.fNrows) { // transfer extra rows to main matrix
92 Double_t *pnt = fElems + nmainel*sizeof(Double_t);
93 int ncl = src.fNcols + 1;
94 for (int ir=0;ir<src.fNrows;ir++) {
96 memcpy(pnt,src.fElemsAdd[ir],ncl*sizeof(Double_t));
97 pnt += ncl*sizeof(Double_t);
103 memcpy(fElems,src.fElems,fNcols*(fNcols+1)/2*sizeof(Double_t));
104 int ncl = fNcols + 1;
105 for (int ir=0;ir<fNrows;ir++) { // dynamic rows
107 memcpy(fElemsAdd[ir],src.fElemsAdd[ir],ncl*sizeof(Double_t));
115 //___________________________________________________________
116 void AliSymMatrix::Clear(Option_t*)
118 if (fElems) {delete[] fElems; fElems = 0;}
121 for (int i=0;i<fNrows;i++) delete[] fElemsAdd[i];
131 //___________________________________________________________
132 Float_t AliSymMatrix::GetDensity() const
134 // get fraction of non-zero elements
136 for (int i=GetSize();i--;) for (int j=i+1;j--;) if (GetEl(i,j)!=0) nel++;
137 return 2.*nel/( (GetSize()+1)*GetSize() );
140 //___________________________________________________________
141 void AliSymMatrix::Print(Option_t* option) const
143 printf("Symmetric Matrix: Size = %d (%d rows added dynamically)\n",GetSize(),fNrows);
144 TString opt = option; opt.ToLower();
145 if (opt.IsNull()) return;
146 opt = "%"; opt += 1+int(TMath::Log10(double(GetSize()))); opt+="d|";
147 for (Int_t i=0;i<fNrowIndex;i++) {
149 for (Int_t j=0;j<=i;j++) printf("%+.3e|",GetEl(i,j));
154 //___________________________________________________________
155 void AliSymMatrix::MultiplyByVec(Double_t *vecIn,Double_t *vecOut) const
157 // fill vecOut by matrix*vecIn
158 // vector should be of the same size as the matrix
159 for (int i=fNrowIndex;i--;) {
161 for (int j=fNrowIndex;j--;) vecOut[i] += vecIn[j]*GetEl(i,j);
166 //___________________________________________________________
167 AliSymMatrix* AliSymMatrix::DecomposeChol()
169 // Return a matrix with Choleski decomposition
170 // Adopted from Numerical Recipes in C, ch.2-9, http://www.nr.com
171 // consturcts Cholesky decomposition of SYMMETRIC and
172 // POSITIVELY-DEFINED matrix a (a=L*Lt)
173 // Only upper triangle of the matrix has to be filled.
174 // In opposite to function from the book, the matrix is modified:
175 // lower triangle and diagonal are refilled.
177 if (!fgBuffer || fgBuffer->GetSize()!=GetSize()) {
180 fgBuffer = new AliSymMatrix(*this);
183 printf("Failed to allocate memory for Choleski decompostions\n");
187 else (*fgBuffer) = *this;
189 AliSymMatrix& mchol = *fgBuffer;
191 for (int i=0;i<fNrowIndex;i++) {
192 Double_t *rowi = mchol.GetRow(i);
193 for (int j=i;j<fNrowIndex;j++) {
194 Double_t *rowj = mchol.GetRow(j);
195 double sum = rowj[i];
196 for (int k=i-1;k>=0;k--) if (rowi[k]&&rowj[k]) sum -= rowi[k]*rowj[k];
198 if (sum <= 0.0) { // not positive-definite
199 printf("The matrix is not positive definite [%e]\n"
200 "Choleski decomposition is not possible\n",sum);
203 rowi[i] = TMath::Sqrt(sum);
205 } else rowj[i] = sum/rowi[i];
211 //___________________________________________________________
212 Bool_t AliSymMatrix::InvertChol()
214 // Invert matrix using Choleski decomposition
216 AliSymMatrix* mchol = DecomposeChol();
218 printf("Failed to invert the matrix\n");
227 //___________________________________________________________
228 void AliSymMatrix::InvertChol(AliSymMatrix* pmchol)
230 // Invert matrix using Choleski decomposition, provided the Cholseki's L matrix
233 AliSymMatrix& mchol = *pmchol;
235 // Invert decomposed triangular L matrix (Lower triangle is filled)
236 for (int i=0;i<fNrowIndex;i++) {
237 mchol(i,i) = 1.0/mchol(i,i);
238 for (int j=i+1;j<fNrowIndex;j++) {
239 Double_t *rowj = mchol.GetRow(j);
241 for (int k=i;k<j;k++) if (rowj[k]) {
242 double &mki = mchol(k,i); if (mki) sum -= rowj[k]*mki;
244 rowj[i] = sum/rowj[j];
248 // take product of the inverted Choleski L matrix with its transposed
249 for (int i=fNrowIndex;i--;) {
250 for (int j=i+1;j--;) {
252 for (int k=i;k<fNrowIndex;k++) {
253 double &mik = mchol(i,k);
255 double &mjk = mchol(j,k);
256 if (mjk) sum += mik*mjk;
266 //___________________________________________________________
267 Bool_t AliSymMatrix::SolveChol(Double_t *b, Bool_t invert)
269 // Adopted from Numerical Recipes in C, ch.2-9, http://www.nr.com
270 // Solves the set of n linear equations A x = b,
271 // where a is a positive-definite symmetric matrix.
272 // a[1..n][1..n] is the output of the routine CholDecomposw.
273 // Only the lower triangle of a is accessed. b[1..n] is input as the
274 // right-hand side vector. The solution vector is returned in b[1..n].
279 AliSymMatrix *pmchol = DecomposeChol();
281 printf("SolveChol failed\n");
285 AliSymMatrix& mchol = *pmchol;
287 for (i=0;i<fNrowIndex;i++) {
288 Double_t *rowi = mchol.GetRow(i);
289 for (sum=b[i],k=i-1;k>=0;k--) if (rowi[k]&&b[k]) sum -= rowi[k]*b[k];
293 for (i=fNrowIndex-1;i>=0;i--) {
294 for (sum=b[i],k=i+1;k<fNrowIndex;k++) if (b[k]) {
295 double &mki=mchol(k,i); if (mki) sum -= mki*b[k];
300 if (invert) InvertChol(pmchol);
305 //___________________________________________________________
306 Bool_t AliSymMatrix::SolveChol(TVectorD &b, Bool_t invert)
308 return SolveChol((Double_t*)b.GetMatrixArray(),invert);
312 //___________________________________________________________
313 Bool_t AliSymMatrix::SolveChol(Double_t *brhs, Double_t *bsol,Bool_t invert)
315 memcpy(bsol,brhs,GetSize()*sizeof(Double_t));
316 return SolveChol(bsol,invert);
319 //___________________________________________________________
320 Bool_t AliSymMatrix::SolveChol(TVectorD &brhs, TVectorD &bsol,Bool_t invert)
323 return SolveChol(bsol,invert);
326 //___________________________________________________________
327 void AliSymMatrix::AddRows(int nrows)
330 Double_t **pnew = new Double_t*[nrows+fNrows];
331 for (int ir=0;ir<fNrows;ir++) pnew[ir] = fElemsAdd[ir]; // copy old extra rows
332 for (int ir=0;ir<nrows;ir++) {
333 int ncl = GetSize()+1;
334 pnew[fNrows] = new Double_t[ncl];
335 memset(pnew[fNrows],0,ncl*sizeof(Double_t));
344 //___________________________________________________________
345 void AliSymMatrix::Reset()
347 // if additional rows exist, regularize it
350 for (int i=0;i<fNrows;i++) delete[] fElemsAdd[i];
351 delete[] fElemsAdd; fElemsAdd = 0;
353 fElems = new Double_t[fNcols*(fNcols+1)/2];
356 if (fElems) memset(fElems,0,fNcols*(fNcols+1)/2*sizeof(Double_t));
360 //___________________________________________________________
362 void AliSymMatrix::AddToRow(Int_t r, Double_t *valc,Int_t *indc,Int_t n)
364 // for (int i=n;i--;) {
365 // (*this)(indc[i],r) += valc[i];
371 AddRows(r-fNrowIndex+1);
372 row = &((fElemsAdd[r-fNcols])[0]);
374 else row = &fElems[GetIndex(r,0)];
378 if (indc[i]>r) continue;
379 row[indc[i]] += valc[i];
382 if (nadd == n) return;
386 if (indc[i]>r) (*this)(indc[i],r) += valc[i];
392 //___________________________________________________________
393 Double_t* AliSymMatrix::GetRow(Int_t r)
397 AddRows(r-fNrowIndex+1);
398 printf("create %d of %d\n",r, nn);
399 return &((fElemsAdd[r-fNcols])[0]);
401 else return &fElems[GetIndex(r,0)];
405 //___________________________________________________________
406 int AliSymMatrix::SolveSpmInv(double *vecB, Bool_t stabilize)
408 // Solution a la MP1: gaussian eliminations
409 /// Obtain solution of a system of linear equations with symmetric matrix
410 /// and the inverse (using 'singular-value friendly' GAUSS pivot)
416 double eps = 0.00000000000001;
417 int nGlo = GetSize();
418 bool *bUnUsed = new bool[nGlo];
419 double *rowMax,*colMax=0;
420 rowMax = new double[nGlo];
423 colMax = new double[nGlo];
424 for (Int_t i=nGlo; i--;) rowMax[i] = colMax[i] = 0.0;
425 for (Int_t i=nGlo; i--;) for (Int_t j=i+1;j--;) {
426 double vl = TMath::Abs(Query(i,j));
428 if (vl > rowMax[i]) rowMax[i] = vl; // Max elemt of row i
429 if (vl > colMax[j]) colMax[j] = vl; // Max elemt of column j
431 if (vl > rowMax[j]) rowMax[j] = vl; // Max elemt of row j
432 if (vl > colMax[i]) colMax[i] = vl; // Max elemt of column i
435 for (Int_t i=nGlo; i--;) {
436 if (0.0 != rowMax[i]) rowMax[i] = 1./rowMax[i]; // Max elemt of row i
437 if (0.0 != colMax[i]) colMax[i] = 1./colMax[i]; // Max elemt of column i
442 for (Int_t i=nGlo; i--;) bUnUsed[i] = true;
444 if (!fgBuffer || fgBuffer->GetSize()!=GetSize()) {
447 fgBuffer = new AliSymMatrix(*this);
450 printf("Failed to allocate memory for matrix inversion buffer\n");
454 else (*fgBuffer) = *this;
456 if (stabilize) for (int i=0;i<nGlo; i++) { // Small loop for matrix equilibration (gives a better conditioning)
457 for (int j=0;j<=i; j++) {
458 double vl = Query(i,j);
459 if (vl!=0) SetEl(i,j, TMath::Sqrt(rowMax[i])*vl*TMath::Sqrt(colMax[j]) ); // Equilibrate the V matrix
461 for (int j=i+1;j<nGlo;j++) {
462 double vl = Query(j,i);
463 if (vl!=0) fgBuffer->SetEl(j,i,TMath::Sqrt(rowMax[i])*vl*TMath::Sqrt(colMax[j]) ); // Equilibrate the V matrix
467 for (Int_t j=nGlo; j--;) fgBuffer->DiagElem(j) = TMath::Abs(QueryDiag(j)); // save diagonal elem absolute values
469 for (Int_t i=0; i<nGlo; i++) {
473 for (Int_t j=0; j<nGlo; j++) { // First look for the pivot, ie max unused diagonal element
475 if (bUnUsed[j] && (TMath::Abs(vl=QueryDiag(j))>TMath::Max(TMath::Abs(vPivot),eps*fgBuffer->QueryDiag(j)))) {
481 if (iPivot >= 0) { // pivot found
483 bUnUsed[iPivot] = false; // This value is used
485 DiagElem(iPivot) = -vPivot; // Replace pivot by its inverse
487 for (Int_t j=0; j<nGlo; j++) {
488 for (Int_t jj=0; jj<nGlo; jj++) {
489 if (j != iPivot && jj != iPivot) {// Other elements (!!! do them first as you use old matV[k][j]'s !!!)
490 double &r = j>=jj ? (*this)(j,jj) : (*fgBuffer)(jj,j);
491 r -= vPivot* ( j>iPivot ? Query(j,iPivot) : fgBuffer->Query(iPivot,j) )
492 * ( iPivot>jj ? Query(iPivot,jj) : fgBuffer->Query(jj,iPivot));
497 for (Int_t j=0; j<nGlo; j++) if (j != iPivot) { // Pivot row or column elements
498 (*this)(j,iPivot) *= vPivot;
499 (*fgBuffer)(iPivot,j) *= vPivot;
503 else { // No more pivot value (clear those elements)
504 for (Int_t j=0; j<nGlo; j++) {
507 for (Int_t k=0; k<nGlo; k++) {
509 if (j!=k) (*fgBuffer)(j,k) = 0;
513 break; // No more pivots anyway, stop here
517 for (Int_t i=0; i<nGlo; i++) for (Int_t j=0; j<nGlo; j++) {
518 double vl = TMath::Sqrt(colMax[i])*TMath::Sqrt(rowMax[j]); // Correct matrix V
519 if (i>=j) (*this)(i,j) *= vl;
520 else (*fgBuffer)(j,i) *= vl;
523 for (Int_t j=0; j<nGlo; j++) {
525 for (Int_t jj=0; jj<nGlo; jj++) { // Reverse matrix elements
527 if (j>=jj) vl = (*this)(j,jj) = -Query(j,jj);
528 else vl = (*fgBuffer)(j,jj) = -fgBuffer->Query(j,jj);
529 rowMax[j] += vl*vecB[jj];
533 for (Int_t j=0; j<nGlo; j++) {
534 vecB[j] = rowMax[j]; // The final result
539 if (stabilize) delete [] colMax;