Skip to content
DeepForgeby svx
All problems
MediumCalculusca-070

Eigenvalues of 2x2 Hessian

Problem

Compute the eigenvalues of the symmetric matrix H = [[p, q], [q, r]].

They are (p + r ± sqrt((p - r)^2 + 4*q^2)) / 2. Return them sorted in ascending order.

Hint

Symmetric matrices have real eigenvalues; use the discriminant formula and sort.

Test cases

case 1
in: [2,0,2]
expected: [2,2]
case 2
in: [4,1,4]
expected: [3,5]
case 3
in: [1,2,1]
expected: [-1,3]
case 4
in: [-2,0,-8]
expected: [-8,-2]
case 5
in: [3,4,3]
expected: [-1,7]
Starter codePython
def hessian_eigenvalues(p, q, r):
    # Return [smaller, larger]
    # Your code here
    pass

5 test cases · run your code against them in the editor