2×2 Eigenvalues From Trace and Determinant

Linear AlgebraMedium+20 XP+15

2×2 Eigenvalues From Trace and Determinant

An eigenvector is a direction a matrix doesn't rotate — it only stretches it. The eigenvalue is how much.

For a 2×2 matrix the characteristic equation det(A - λI) = 0 expands to a quadratic you can solve directly:

1trace = a + d
2determinant = a*d - b*c
3λ = (trace ± sqrt(trace² - 4*determinant)) / 2

Task: write eigenvalues(matrix) for a 2×2 matrix [[a, b], [c, d]], returning both eigenvalues sorted largest first, rounded to 4 decimal places.

  • All test cases have real eigenvalues — the discriminant is never negative.
  • Repeated eigenvalues are returned twice.

The two invariants are worth remembering: the eigenvalues always sum to the trace and multiply to the determinant. That's a free check on any answer you compute, and it generalises to every size of matrix.

Examples

matrix = [[2.0, 0.0], [0.0, 3.0]]
returns [3.0, 2.0]
matrix = [[4.0, 1.0], [2.0, 3.0]]
returns [5.0, 2.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run