# Cv2.eigen function calculates wrong eigenvalues

**URL:** <https://forum.opencv.org/t/cv2-eigen-function-calculates-wrong-eigenvalues/10565>\
**Category:** Python\
**Tags:** core\
**Created:** [October 14, 2022, 6:08pm UTC](https://forum.opencv.org/t/cv2-eigen-function-calculates-wrong-eigenvalues/10565 "2022-10-14T18:08:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![supermaximus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/supermaximus/32/6277_2.png) [@supermaximus](https://forum.opencv.org/u/supermaximus)\
**Post date:** [October 14, 2022, 6:08pm UTC](https://forum.opencv.org/t/cv2-eigen-function-calculates-wrong-eigenvalues/10565/1 "2022-10-14T18:08:01Z")

</div>

Hi.

Below is my python code, which calculates eigenvectors and values.

```auto
import cv2
import numpy as np

#mat=np.array([-6,3,4,5],np.float32).reshape((2,2))
mat = np.array([[-6, 3],
                [4, 5]], np.float32)

ret,eVals,eVecs=cv2.eigen(mat, True)
print("Eigenvectors:\n",eVecs)
print("Eigenvalues:\n", eVals)

```

It prints:  
Eigenvectors: [[0.24708746 0.9689932] [0.9689932 -0.24708746]]  
Eigenvalues: [[5.764982] [-6.764982]]

Eigenvalues are wrong, it should be 6 and -7.  
Matlab also calculates 6 and -7.  
So, my question is, what’s wrong with this function?

Thanks.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 14, 2022, 10:35pm UTC](https://forum.opencv.org/t/cv2-eigen-function-calculates-wrong-eigenvalues/10565/2 "2022-10-14T22:35:11Z")

</div>

> [@supermaximus](#):
>
> ```auto
> mat = np.array([[-6, 3],
> [4, 5]], np.float32)
> 
> ```

> Note  
> Use [cv::eigenNonSymmetric](https://docs.opencv.org/3.4/d2/de8/group __core__ array.html#gaf51987e03cac8d171fbd2b327cf966f6) for calculation of real eigenvalues and eigenvectors of non-symmetric matrix.

```auto
>>> cv2.eigenNonSymmetric(mat)
(array([[6.],
       [-7.]], dtype=float32), array([[-0.24325, -0.97301],
       [-0.94868, 0.31623]], dtype=float32))

```

looks better. don’t ask me why eigen() doesn’t check for symmetry and reject, or why it has this restriction in the first place.

---

<div class="post-metadata">

**Author:** ![supermaximus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/supermaximus/32/6277_2.png) [@supermaximus](https://forum.opencv.org/u/supermaximus)\
**Post date:** [October 15, 2022, 10:00am UTC](https://forum.opencv.org/t/cv2-eigen-function-calculates-wrong-eigenvalues/10565/3 "2022-10-15T10:00:36Z")

</div>

Nice. It works as expected. Thank you.
