Problems
Loading...
1 / 1

Calculate Eigenvalues of a Matrix

Linear Algebra
Medium

Calculate the eigenvalues of a square matrix.

Eigenvalue Definition:

Av=λvAv = \lambda v

Characteristic Equation:

det(AλI)=0\det(A - \lambda I) = 0

Where: A = matrix, λ = eigenvalue, v = eigenvector, I = identity matrix, det = determinant

Function Arguments

  • matrix: list[list[float]] | np.ndarray - Input matrix (should be square)
Loading visualization...

Examples

Input: matrix=[[4, 1], [2, 3]]

Output: [2.0, 5.0] (approximately)

Input: matrix=[[0, -1], [1, 0]]

Output: [-1j, 1j] (pure imaginary)

Input: matrix=[[1, 2, 3], [4, 5]]

Output: None (non-square)

Hint 1

Use np.asarray() to convert input to numpy array. Check matrix dimensions with .shape and .ndim.

Hint 2

Use np.linalg.eigvals() to compute eigenvalues. Use np.lexsort() for consistent sorting by real then imaginary parts.

Hint 3

Handle edge cases: check if matrix is square with matrix.shape[0] == matrix.shape[1]. Return None for invalid inputs.

Requirements

  • Return np.ndarray of eigenvalues (complex dtype if needed)
  • Return None for non-square matrices or invalid input
  • Handle empty matrices gracefully
  • Sort eigenvalues by real part, then imaginary part for consistency
  • Use NumPy's linear algebra functions

Constraints

  • Matrix size ≤ 100×100
  • Numerical precision: relative tolerance ≤ 1e-8
  • Libraries: NumPy only
Try Similar Problems