Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Compute the eigenvalues of a 2×2 matrix, which describe how the matrix scales vectors along special directions called eigenvectors. You’ll implement a tiny helper that returns both eigenvalues in a consistent order.
The eigenvalues and eigenvectors of a square matrix satisfy:
The eigenvalues are found by solving the characteristic equation:
The eigenvalues of
are the roots of:
Implement the function
Rules:
np.linalg.eig).[lambda, lambda].Output:
| Argument | Type |
|---|---|
| A | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Use quadratic formula; no np.linalg.eig.
Input is 2x2 NumPy array.
Return NumPy array of two floats sorted non-increasing.
Start by unpacking the 2×2 matrix into scalars a, b, c, d, then compute the trace t = a + d and determinant det = a*d - b*c.
Use the characteristic polynomial: eigenvalues are roots of λ^2 - tλ + det = 0. Compute the discriminant Δ = t*t - 4*det, then apply the quadratic formula: (t ± sqrt(Δ)) / 2.
Return a NumPy array of two floats sorted largest to smallest. Don’t call np.linalg.eig; use np.sqrt for the square root.