# Matrix invert error

I have a TMatrixD covMatrix like this:

``````     |      0    |      1    |      2    |      3    |      4    |      5    |      6    |      7    |      8    |
-----------------------------------------------------------------------------------------------------
0 |      99.69       76.02        42.7       15.49       3.102     -11.61      -35.31      -75.52      -114.5
1 |      76.02       71.42       51.38       21.98       10.54     -8.492      -34.63      -80.02      -108.2
2 |       42.7       51.38       60.21       35.97       17.69     -6.457      -29.58      -74.37      -97.53
3 |      15.49       21.98       35.97        46.7       33.56      3.402      -23.46      -54.44       -79.2
4 |      3.102       10.54       17.69       33.56       49.77      28.94      -2.234      -48.03      -93.32
5 |     -11.61      -8.492      -6.457       3.402       28.94      63.86       31.35      -26.88      -74.11
6 |     -35.31      -34.63      -29.58      -23.46      -2.234      31.35       80.68       56.59      -43.41
7 |     -75.52      -80.02      -74.37      -54.44      -48.03     -26.88       56.59         182       120.7
8 |     -114.5      -108.2      -97.53       -79.2      -93.32     -74.11      -43.41       120.7       489.6
``````

and I want to get its invert matrix by using Invert():

``````TMatrixD covInvert = covMatrix.Invert();
``````

But the result is so strange:

``````      |      0    |      1    |      2    |      3    |      4    |   ...
----------------------------------------------------------------------
0 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
1 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
2 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
3 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
4 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
5 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
6 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
7 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
8 |  1.224e+12   1.224e+12   1.224e+12   1.224e+12   1.224e+12   ...
``````

How can I get the correct invert matrix?

best regards

ROOT Version: v6_18_04d

Check: `covMatrix.Determinant()`

The output of `covMatrix.Determinant()` is 4.820217e-01. Does this affect the invert result?

1 Like
``````{
Double_t m[9][9] =
{ { 99.69, 76.02, 42.7, 15.49, 3.102, -11.61, -35.31, -75.52, -114.5 },
{ 76.02, 71.42, 51.38, 21.98, 10.54, -8.492, -34.63, -80.02, -108.2 },
{ 42.7, 51.38, 60.21, 35.97, 17.69, -6.457, -29.58, -74.37, -97.53 },
{ 15.49, 21.98, 35.97, 46.7, 33.56, 3.402, -23.46, -54.44, -79.2 },
{ 3.102, 10.54, 17.69, 33.56, 49.77, 28.94, -2.234, -48.03, -93.32 },
{ -11.61, -8.492, -6.457, 3.402, 28.94, 63.86, 31.35, -26.88, -74.11 },
{ -35.31, -34.63, -29.58, -23.46, -2.234, 31.35, 80.68, 56.59, -43.41 },
{ -75.52, -80.02, -74.37, -54.44, -48.03, -26.88, 56.59, 182, 120.7 },
{ -114.5, -108.2, -97.53, -79.2, -93.32, -74.11, -43.41, 120.7, 489.6 } };
#if 1 /* 0 or 1 */
TMatrixDSym covMatrix(9, (Double_t*)m, ""); // "" or "F"
covMatrix.Print();
cout << "determinant = " << covMatrix.Determinant() << endl;
TMatrixDSym covInvert(covMatrix); covInvert.Invert(); // uses the LU decomposition
covInvert.Print();
cout << "determinant = " << covInvert.Determinant() << endl;
#else /* 0 or 1 */
TMatrixD covMatrix(9, 9, (Double_t*)m, ""); // "" or "F"
covMatrix.Print();
cout << "determinant = " << covMatrix.Determinant() << endl;
TMatrixD covInvert(covMatrix); covInvert.Invert(); // uses the LU decomposition
covInvert.Print();
cout << "determinant = " << covInvert.Determinant() << endl;
#endif /* 0 or 1 */
}
``````

BTW. You can use different decomposition classes for inverting; see → Matrix Linear Algebra

Thanks a lot for the prompt feedback! I got the right result in your way. But I still wonder why I cannot get the same deternimant and invert results as yours by reading the matrix from a root file instead of defining `Double_t m` .

the print out of your matrix with limited precision is not the same as the one you get from your TFile …

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.