Eigenvalues of a 2×2 matrix

Find the eigenvalues of the 2×2 matrix [[a, b], [c, d]].

Input

Two lines: a b and c d, whole numbers.

Output

eigenvalues L1 L2, largest first, each to 3 decimal places. If the eigenvalues are not real, print complex eigenvalues.

Examples

Example 1 · Input
2 1
1 2
Expected output
eigenvalues 3.000 1.000
Example 2 · Input
0 -1
1 0
Expected output
complex eigenvalues

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

The eigenvalues solve λ² − (a + d)λ + (ad − bc) = 0.

Hint 2

If the discriminant (trace² − 4·det) is negative, there are no real eigenvalues.

Solution

Show one correct solution (try it yourself first)
from math import sqrt

a, b = map(int, input().split())
c, d = map(int, input().split())
trace, det = a + d, a * d - b * c
disc = trace * trace - 4 * det
if disc < 0:
    print("complex eigenvalues")
else:
    root = sqrt(disc)
    print(f"eigenvalues {(trace + root) / 2:.3f} {(trace - root) / 2:.3f}")

Lesson: Eigenvalues and eigenvectors →