# TMVA GetSQRootMatrix

**URL:** <https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800>\
**Category:** TMVA\
**Created:** [February 2, 2011, 2:47pm UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800 "2011-02-02T14:47:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshgc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@joshgc](https://root-forum.cern.ch/u/joshgc)\
**Post date:** [February 2, 2011, 2:47pm UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800/1 "2011-02-02T14:47:30Z")

</div>

Hi Everyone,

I’m trying to use CutsD and LikelihoodD in the TMVAClassification Macro. When I run root TMVAClassification.C I get this warning many times.

— Tools : error in matrix diagonalization; printed S and B

In fact I get it 216 times, while I only have 206 events in my signal training sample, and 133256 in background. Now normally I wouldn’t be perturbed by a warning, but all my Cut\* optimizations are failing (after training eff\_b = eff\_s for all points). So I’m trying to take apart my log file one warning at a time. Thanks so much for any thoughts!

Josh

---

<div class="post-metadata">

**Author:** ![cferro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@cferro](https://root-forum.cern.ch/u/cferro)\
**Post date:** [June 29, 2011, 6:07pm UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800/2 "2011-06-29T18:07:32Z")

</div>

Dear all,

I have exaclty the same problem, if I try to run my TMVAClassification.C with MLP and MLPBFGS method I get this error:

— Tools : error in matrix diagonalization; printed S and B  
— Tools : error in matrix diagonalization; printed S and B  
— Tools : error in matrix diagonalization; printed S and B  
— Tools : error in matrix diagonalization; printed S and B

a lot of time…

what is the meaning of it? And how it can be solve? Is the issue connected with the number of entries in the tree?

Thanks a lot  
Cristina

---

<div class="post-metadata">

**Author:** ![petrakou](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/d78d45/32.png) [@petrakou](https://root-forum.cern.ch/u/petrakou)\
**Post date:** [August 23, 2012, 7:50pm UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800/3 "2012-08-23T19:50:52Z")

</div>

I would like to ask the same thing. could someone please shed some light on this cryptic message? Is there a way to get more info on it and on what we need to fix?

Thanks,  
Eleni

---

<div class="post-metadata">

**Author:** ![rain](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rain](https://root-forum.cern.ch/u/rain)\
**Post date:** [February 25, 2022, 7:25am UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800/4 "2022-02-25T07:25:07Z")

</div>

It has been ten years.I would like to ask the same thing.  
Thanks,  
zzl

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [February 25, 2022, 11:47pm UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800/5 "2022-02-25T23:47:11Z")

</div>

The code generating this error is in tmva/tmva/src/Tools.cxx:

```auto
 278 ////////////////////////////////////////////////////////////////////////////////
 279 /// square-root of symmetric matrix
 280 /// of course the resulting sqrtMat is also symmetric, but it's easier to
 281 /// treat it as a general matrix
 282 
 283 TMatrixD* TMVA::Tools::GetSQRootMatrix( TMatrixDSym* symMat )
 284 {
 285 Int_t n = symMat->GetNrows();
 286 
 287 // compute eigenvectors
 288 TMatrixDSymEigen* eigen = new TMatrixDSymEigen( *symMat );
 289 
 290 // D = ST C S
 291 TMatrixD* si = new TMatrixD( eigen->GetEigenVectors() );
 292 TMatrixD* s = new TMatrixD( *si ); // copy
 293 si->Transpose( *si ); // invert (= transpose)
 294 
 295 // diagonal matrices
 296 TMatrixD* d = new TMatrixD( n, n);
 297 d->Mult( (*si), (*symMat) ); (*d) *= (*s);
 298 
 299 // sanity check: matrix must be diagonal and positive definit
 300 Int_t i, j;
 301 Double_t epsilon = 1.0e-8;
 302 for (i=0; i<n; i++) {
 303 for (j=0; j<n; j++) {
 304 if ((i != j && TMath::Abs((*d)(i,j))/TMath::Sqrt((*d)(i,i)*(*d)(j,j)) > epsilon) ||
 305 (i == j && (*d)(i,i) < 0)) {
 306 //d->Print();
 307 Log() << kWARNING << "<GetSQRootMatrix> error in matrix diagonalization; printed S and B" << Endl;
 308 }
 309 }
 310 }
 311 
 312 // make exactly diagonal
 313 for (i=0; i<n; i++) for (j=0; j<n; j++) if (j != i) (*d)(i,j) = 0;
 314 
 315 // compute the square-root C' of covariance matrix: C = C'*C'
 316 for (i=0; i<n; i++) (*d)(i,i) = TMath::Sqrt((*d)(i,i));
 317 
 318 TMatrixD* sqrtMat = new TMatrixD( n, n );
 319 sqrtMat->Mult( (*s), (*d) );
 320 (*sqrtMat) *= (*si);
 321 
 322 // invert square-root matrices
 323 sqrtMat->Invert();
 324 
 325 delete eigen;
 326 delete s;
 327 delete si;
 328 delete d;
 329 
 330 return sqrtMat;
 331 }

```

This piece of code is apparently calculating a transformed positive definite matrix whose eigenvalues are the square root of the original matrix. Unfortunately, this is done in a very round-about way with numerical issues.  
The eigen values/vectors are calculated of this symmetric matrix and then matrix multiplications are performed to end up with a diagonal matrix. It is checked that indeed the final matrix is diagonal and its elements positive.

What is actually tested are the numerical limitations of the code that performs the eigen decomposition (TMatrixDSymEigen).

A much more direct way would be to make a Cholesky decomposition with the class TDecompChol. Along the way it also checks that the matrix is positive definite.

@axel , @moneta : The above code should be replaced by these more streamlined lines:

```auto
TMatrixD *GetSQRootMatrix2(TMatrixDSym* symMat)
{

   TDecompChol chol(*symMat);
   if (!chol.Decompose())
   {
      Log() << kWARNING << "<GetSQRootMatrix> matrix not positive definite; printed S and B" << Endl;
   }

   TMatrixDSymEigen eigen = TMatrixDSymEigen(*symMat);
   TMatrixD s = eigen.GetEigenVectors();
   TMatrixDSym d_sqrt(symMat->GetNrows());
   TMatrixDDiag d_diag(d_sqrt);
   d_diag = eigen.GetEigenValues();
   d_sqrt.Sqrt();

   // s d s^T
   d_sqrt.Similarity(s);

   // invert square-root matrices
   d_sqrt.Invert();

   return new TMatrixD(d_sqrt);
}

```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [February 28, 2022, 3:12pm UTC](https://root-forum.cern.ch/t/tmva-getsqrootmatrix/11800/6 "2022-02-28T15:12:48Z")

</div>

Thanks Eddy! This is now [https://github.com/root-project/root/pull/9996](https://github.com/root-project/root/pull/9996)
