Skip to content
DeepForgeby svx
All problems
HardLinear Algebrala-180

Spectral Gap 2x2

Problem

Compute the spectral gap of a 2x2 matrix with real eigenvalues:

gap = |lambda1| - |lambda2|, where lambda1 and lambda2 are the eigenvalues from the characteristic polynomial.

Return None if the discriminant is negative (complex eigenvalues).

Hint

The gap determines how fast power iteration converges.

Test cases

case 1
in: [[[3,0],[0,1]]]
expected: 2
case 2
in: [[[2,1],[1,2]]]
expected: 2
case 3
in: [[[4,1],[2,3]]]
expected: 3
case 4
in: [[[1,1],[-1,1]]]
expected: null
Starter codePython
def spectral_gap_2x2(A):
    # Your code here
    pass

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