Loading the problem
Find the eigenvalues of a 2x2 matrix by solving the characteristic polynomial
directly, without np.linalg.eig.
For
A = [[a, b],
[c, d]]
the eigenvalues solve det(A - lambda I) = 0, which expands to
lambda^2 - (a + d) * lambda + (a * d - b * c) = 0
The coefficients are the trace and the determinant. Apply the quadratic
formula with discriminant = trace^2 - 4 * det.
Assume the matrix has real eigenvalues, so the discriminant is not negative. Return the two eigenvalues in a list sorted in descending order. A repeated eigenvalue is returned twice.
def eigenvalues_2x2(A):
Input
eigenvalues_2x2(np.array([[2.0, 1.0], [1.0, 2.0]]))
Output
[3.0, 1.0]
Reasoning
The trace is 4 and the determinant is 3, so lambda^2 - 4 lambda + 3 = 0. The discriminant is 16 - 12 = 4, which gives (4 + 2) / 2 = 3 and (4 - 2) / 2 = 1.
Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell