Repository: sympy/sympy

Issue:
incorrect multiplicities from `eigenvals`
To reproduce:
```
import numpy as np
import sympy as sp

print(sp.__version__)

dd = sd = [0] * 11 + [1]
ds = [2,0,1,0,0,0,1,0,1,0,1,0]
ss = ds.copy()
ss[8] = 2

def rotate(x, i):
    return x[i:] + x[:i]

mat = []
for i in range(12):
    mat.append(rotate(ss,i) + rotate(sd,i))
for i in range(12):
    mat.append(rotate(ds,i) + rotate(dd,i))

print(np.array(mat))

mat = sp.Matrix(mat)
eigenvalues = mat.eigenvals()
print('eigenvalues from eigenvals:')
for i in eigenvalues:
    print(eigenvalues[i], 'x', i)
print('matrix dimensions:', mat.shape)
print('sum of multiplicities:', sum([eigenvalues[i] for i in eigenvalues]))

eigenvectors = mat.eigenvects()
print('eigenvalues from eigenvects (multiplicities appear to be correct):')
for val, mult, _ in eigenvectors:
    print(mult, 'x', val)
```

Output:
```
1.12
[[2 0 1 0 0 0 1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1]
 [0 1 0 0 0 1 0 2 0 1 0 2 0 0 0 0 0 0 0 0 0 0 1 0]
 [1 0 0 0 1 0 2 0 1 0 2 0 0 0 0 0 0 0 0 0 0 1 0 0]
 [0 0 0 1 0 2 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0]
 [0 0 1 0 2 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0]
 [0 1 0 2 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0]
 [1 0 2 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0]
 [0 2 0 1 0 2 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0]
 [2 0 1 0 2 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 1 0 2 0 1 0 0 0 1 0 2 0 0 1 0 0 0 0 0 0 0 0 0]
 [1 0 2 0 1 0 0 0 1 0 2 0 0 1 0 0 0 0 0 0 0 0 0 0]
 [0 2 0 1 0 0 0 1 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 0]
 [2 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1]
 [0 1 0 0 0 1 0 1 0 1 0 2 0 0 0 0 0 0 0 0 0 0 1 0]
 [1 0 0 0 1 0 1 0 1 0 2 0 0 0 0 0 0 0 0 0 0 1 0 0]
 [0 0 0 1 0 1 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0]
 [0 0 1 0 1 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0]
 [0 1 0 1 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0]
 [1 0 1 0 1 0 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0]
 [0 1 0 1 0 2 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0]
 [1 0 1 0 2 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 1 0 2 0 1 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0]
 [1 0 2 0 1 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0]
 [0 2 0 1 0 0 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0]]
eigenvalues from eigenvals:
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 0)
1 x CRootOf(lambda**4 - 5*lambda**2 + 1, 0)
1 x -1
1 x CRootOf(lambda**4 - 5*lambda**2 + 1, 1)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 1)
1 x CRootOf(lambda**2 - 6*lambda - 1, 0)
1 x CRootOf(lambda**2 - 8*lambda + 1, 0)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 2)
1 x CRootOf(lambda**4 - 5*lambda**2 + 1, 2)
1 x 1
1 x CRootOf(lambda**4 - 5*lambda**2 + 1, 3)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 3)
1 x CRootOf(lambda**2 - 6*lambda - 1, 1)
1 x CRootOf(lambda**2 - 8*lambda + 1, 1)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 4)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 5)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 6)
1 x CRootOf(lambda**8 - 10*lambda**6 + 15*lambda**4 - 10*lambda**2 + 1, 7)
matrix dimensions: (24, 24)
sum of multiplicities: 18
eigenvalues from eigenvects (multiplicities appear to be correct):
2 x -1
2 x 1
2 x -sqrt(5/2 - sqrt(21)/2)
2 x sqrt(5/2 - sqrt(21)/2)
1 x 3 - sqrt(10)
1 x 3 + sqrt(10)
1 x 4 - sqrt(15)
1 x sqrt(15) + 4
2 x -sqrt(sqrt(21)/2 + 5/2)
2 x sqrt(sqrt(21)/2 + 5/2)
1 x -cos(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2) - I*sin(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2)
1 x -cos(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2) + I*sin(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2)
1 x cos(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2) - I*sin(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2)
1 x cos(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2) + I*sin(atan(sqrt(-33 + 20*sqrt(3))/(5 - 2*sqrt(3)))/2)
1 x -sqrt(sqrt(3) + 5/2 + sqrt(33 + 20*sqrt(3))/2)
1 x sqrt(sqrt(3) + 5/2 + sqrt(33 + 20*sqrt(3))/2)
1 x -sqrt(-sqrt(33 + 20*sqrt(3))/2 + sqrt(3) + 5/2)
1 x sqrt(-sqrt(33 + 20*sqrt(3))/2 + sqrt(3) + 5/2)
```
The multiplicities from `eigenvals` function only sum up to 18, while the matrix is of dimension 24. The output from `eigenvects` appears to be correct (and it's impressive that it can solve the 8th degree equation symbolically).

Discussion:

Patch:
diff --git a/sympy/matrices/eigen.py b/sympy/matrices/eigen.py
index ea39e83a94..cec37c9e61 100644
--- a/sympy/matrices/eigen.py
+++ b/sympy/matrices/eigen.py
@@ -210,11 +210,8 @@ def _eigenvals_list(
         eigs = roots(charpoly, multiple=True, **flags)
 
         if len(eigs) != block.rows:
-            degree = int(charpoly.degree())
-            f = charpoly.as_expr()
-            x = charpoly.gen
             try:
-                eigs = [CRootOf(f, x, idx) for idx in range(degree)]
+                eigs = charpoly.all_roots(multiple=True)
             except NotImplementedError:
                 if error_when_incomplete:
                     raise MatrixError(eigenvals_error_message)
@@ -254,11 +251,8 @@ def _eigenvals_dict(
         eigs = roots(charpoly, multiple=False, **flags)
 
         if sum(eigs.values()) != block.rows:
-            degree = int(charpoly.degree())
-            f = charpoly.as_expr()
-            x = charpoly.gen
             try:
-                eigs = {CRootOf(f, x, idx): 1 for idx in range(degree)}
+                eigs = dict(charpoly.all_roots(multiple=False))
             except NotImplementedError:
                 if error_when_incomplete:
                     raise MatrixError(eigenvals_error_message)


Test patch:
diff --git a/sympy/matrices/tests/test_eigen.py b/sympy/matrices/tests/test_eigen.py
index 75ec80bcdd..e952d225f7 100644
--- a/sympy/matrices/tests/test_eigen.py
+++ b/sympy/matrices/tests/test_eigen.py
@@ -705,3 +705,21 @@ def test_issue_20752():
     b = symbols('b', nonzero=True)
     m = Matrix([[0, 0, 0], [0, b, 0], [0, 0, b]])
     assert m.is_positive_semidefinite is None
+
+
+def test_issue_25282():
+    dd = sd = [0] * 11 + [1]
+    ds = [2, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0]
+    ss = ds.copy()
+    ss[8] = 2
+
+    def rotate(x, i):
+        return x[i:] + x[:i]
+
+    mat = []
+    for i in range(12):
+        mat.append(rotate(ss, i) + rotate(sd, i))
+    for i in range(12):
+        mat.append(rotate(ds, i) + rotate(dd, i))
+
+    assert sum(Matrix(mat).eigenvals().values()) == 24