# TMatrixDEigen in python

**URL:** <https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554>\
**Category:** ROOT\
**Created:** [April 23, 2021, 8:56am UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554 "2021-04-23T08:56:26Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 23, 2021, 8:56am UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/1 "2021-04-23T08:56:26Z")

</div>

Hi,

I’m trying to migrate my C++ fitting script into python and encountered a problem doing eigen-analysis on the covariance matrix.  
The following code snippet:

```
fun = ROOT.TF1("func", "expo", 2.5, 9.5)                                                                                                                          
fitres = hist_data.Fit("func", "IRS")

# deal with uncertainties:                                                                                                                                       
C = fitres.GetCovarianceMatrix()
C.Print()
EigenC = ROOT.TMatrixDEigen(C)

```

results in:

FCN=1115.07 FROM MIGRAD STATUS=CONVERGED 54 CALLS 55 TOTAL  
EDM=1.06952e-09 STRATEGY= 1 ERROR MATRIX ACCURATE  
EXT PARAMETER STEP FIRST  
NO. NAME VALUE ERROR SIZE DERIVATIVE  
1 Constant 1.37419e+01 9.78097e-03 5.02296e-05 5.11911e-04  
2 Slope -9.08180e-01 2.58631e-03 1.32823e-05 1.97096e-02

2x2 matrix is as follows

```
 | 0 | 1 |

```

* * *

0 | 9.567e-05 -2.401e-05  
1 | -2.401e-05 6.689e-06

Traceback (most recent call last):  
File “test.py”, line 33, in   
EigenC = ROOT.TMatrixDEigen(C)  
TypeError: none of the 3 overloaded methods succeeded. Full details:  
TMatrixDEigen::TMatrixDEigen() =\>  
takes at most 0 arguments (1 given)  
TMatrixDEigen::TMatrixDEigen(const TMatrixT& a) =\>  
could not convert argument 1  
TMatrixDEigen::TMatrixDEigen(const TMatrixDEigen& another) =\>  
could not convert argument 1

So, I gather the TFitResult object is properly retrieved and I can produce the covariance matrix of the fit. However, TMatrixDEigen constructor does not seem to work with the argument it should accept, to my understanding. The C++ version reads:

…  
TMatrixD C = fitres-\>GetCovarianceMatrix();  
TMatrixDEigen\* EigenC = new TMatrixDEigen(C);  
TMatrixD E = EigenC-\>GetEigenValues();  
TMatrixD V = EigenC-\>GetEigenVectors();  
…

and works for me alright.  
I clearly must have missed something here.

Any hints will be much appreciated.

Many thanks and best wishes,  
pawel bruckman

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 23, 2021, 10:12am UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/2 "2021-04-23T10:12:17Z")

</div>

Hello,

What’s the type of `C` in the snippet? Can you `print(type(C))`?

Also, what ROOT version are you using?

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 23, 2021, 10:33am UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/3 "2021-04-23T10:33:10Z")

</div>

Hi,

Print(type(C) gives:

\<class ‘ROOT.TMatrixTSym’\>

and I’m currently using ROOT 6.16/00

Thanks!  
pawel

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 23, 2021, 11:38am UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/4 "2021-04-23T11:38:56Z")

</div>

Hi, I’m sorry. Perhaps I should have replied to you directly. Anyhow, I replied to your questions in a general post. I’m new to the forum 😉

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 23, 2021, 12:41pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/5 "2021-04-23T12:41:59Z")

</div>

Hi,

First I think it would be worth trying it with a newer version of ROOT (\>= 6.22).

But looking at the code, it seems that the type of the result of `GetCovarianceMatrix` is `TMatrixTSym<double>`, aka `TMatrixDSym`. However, the constructor of `TMatrixDEigen` accepts a `TMatrixT`.

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 23, 2021, 1:08pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/6 "2021-04-23T13:08:45Z")

</div>

Well, this is true. But I thought TMatrixDSym derived from TMatrixT. Otherwise, it wouldn’t work in C++ Root, would it? Is there a way to force an appropriate type conversion? I shall check it with a higher Root version, as you recommend.  
p.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 23, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/7 "2021-04-23T13:27:15Z")

</div>

According to:

[https://root.cern.ch/doc/v622/classTMatrixTBase.html](https://root.cern.ch/doc/v622/classTMatrixTBase.html)

Both `TMatrixTSym` and `TMatrixT` are subclasses of `TMatrixTBase`, but there is no direct inheritance between them. Am I right that `TMatrixDSym` is `TMatrixTSym<double>`?

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 23, 2021, 2:30pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/8 "2021-04-23T14:30:44Z")

</div>

This I do not know for sure, but very likely it is TMatrixTSym. My trouble is that in C++ Root TMatrixDEigen accepts both TMatrixDSym and TMatrixD in the constructor. I just checked it explicitly.

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 23, 2021, 3:07pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/9 "2021-04-23T15:07:27Z")

</div>

I also checked that in PyRoot ROOT.TMatrixDEigen behaves the same no matter if you pass the TMatrixTSym or TMatrixT. The problem seems in interpreting the reference “&”. This is what I get when TMatrixT is passed to the constructor:  
\<class ‘ROOT.TMatrixT’\>  
Traceback (most recent call last):  
File “test.py”, line 40, in   
EigenC = ROOT.TMatrixDEigen(C)  
TypeError: none of the 3 overloaded methods succeeded. Full details:  
TMatrixDEigen::TMatrixDEigen() =\>  
takes at most 0 arguments (1 given)  
TMatrixDEigen::TMatrixDEigen(const TMatrixT& a) =\>  
could not convert argument 1  
TMatrixDEigen::TMatrixDEigen(const TMatrixDEigen& another) =\>  
could not convert argument 1

Maybe there is a simple workaround I’m not aware of?

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 24, 2021, 7:08am UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/10 "2021-04-24T07:08:59Z")

</div>

I temporarily solved my problem by simply converting the TMatrixDSym object into a numpy array. Then I used scipy.linalg.eigh to solve my eigenproblem.  
Still, this is not a satisfactory solution to the original issue, of course ☹  
p.

---

<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:** [April 24, 2021, 3:52pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/11 "2021-04-24T15:52:33Z")

</div>

The missing link

```auto
TMatrixT(const TMatrixTSym <Element> &another);

```

---

<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:** [April 24, 2021, 4:09pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/12 "2021-04-24T16:09:22Z")

</div>

> [@Pawel\_Bruckman\_de\_Re](#):
>
> `EigenC = ROOT.TMatrixDEigen(C)`

Would adding the following line be helpful:

```auto
TMatrixD EigenC = ROOT.TMatrixDEigen(C)
or
EigenC = ROOT.TMatrixDEigen(TMatrixD(C))

```

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 25, 2021, 3:16pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/13 "2021-04-25T15:16:03Z")

</div>

Hi Eddy,  
Bingo! The explicit type conversion does work indeed (just adding the ROOT namesapace):  
EigenC = ROOT.TMatrixDEigen(ROOT.TMatrixD(C))  
The first option throws a syntax error, though.  
Thanks a lot for this very helpful suggestion 🙂  
Best,  
pawel

---

<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:** [April 25, 2021, 6:53pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/14 "2021-04-25T18:53:06Z")

</div>

first option should have been:

```auto
EigenC = TMatrixD(ROOT.TMatrixDEigen(C))

```

---

<div class="post-metadata">

**Author:** ![Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pawel_bruckman_de_re/32/25688_2.png) [@Pawel\_Bruckman\_de\_Re](https://root-forum.cern.ch/u/Pawel_Bruckman_de_Re)\
**Post date:** [April 25, 2021, 7:02pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/15 "2021-04-25T19:02:30Z")

</div>

No, this version still has the same problem with ROOT.TMatrixDEigen(C) which is not accepted.  
So presumably, we are left with the second option. Still fair enough! 🙂  
Thanks again,  
p.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 9, 2021, 7:02pm UTC](https://root-forum.cern.ch/t/tmatrixdeigen-in-python/44554/16 "2021-05-09T19:02:47Z")

</div>

Closed on May 9, 2021, 7:02pm UTC

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