Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

250. Eigenvectors and eigenspaces

medium
GeneralGeneral
staff

Eigenvectors and eigenspaces show up all over ML (PCA, stability, dynamical systems) and are core linear algebra skills. In this task, you’ll compute the eigenspace dimension(s) for real eigenvalues of a small matrix using NumPy.

An eigenvalue λ\lambda and eigenvector v\mathbf{v} satisfy Av=λvA\mathbf{v} = \lambda\mathbf{v}. The eigenspace corresponding to λ\lambda is the set of all such vectors. The dimension of this space (geometric multiplicity) is given by:

dim(Eλ)=nrank(AλI)\dim(\mathcal{E}_\lambda) = n - \operatorname{rank}(A - \lambda I)

Requirements

Implement the function

python

Rules:

  • Use NumPy to compute eigenvalues, and group eigenvalues that are equal within tol into a single “distinct” eigenvalue.
  • For each distinct real eigenvalue ( \lambda ), compute the eigenspace dimension as the nullity of (A - \lambda I), using: dim(Eλ)=nrank(AλI)\dim(\mathcal{E}_\lambda) = n - \operatorname{rank}(A - \lambda I)
  • Treat an eigenvalue as real if abs(imag_part) <= tol.
  • Return only real eigenvalues (ignore complex ones).
  • Don’t use any symbolic math libraries; stick to NumPy + built-in Python.

Example

python

Output:

python
Input Signature
ArgumentType
Anp.ndarray
tolfloat
Output Signature
Return NameType
valuelist

Constraints

  • Use NumPy only; no symbolic math.

  • Cluster eigenvalues within tol as identical.

  • Rank via SVD thresholded by tol.

Hint 1

Use np.asarray(A) to ensure the input is a NumPy array, then compute eigenvalues with np.linalg.eigvals. Filter to “real” eigenvalues using abs(ev.imag) <= tol.

Hint 2

Floating-point eigenvalues won’t match exactly—sort the real eigenvalues and cluster/group values that differ by at most tol to form distinct eigenvalues.

Hint 3

For each distinct real (\lambda), form B = A - λI and compute its rank numerically (e.g., via SVD singular values > tol). Then eigenspace dimension is n - rank(B).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
staff
senior
entry
Tags
eigenvalues
eigenspace-dimension
numerical-linear-algebra
numpy
23 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit