Coverage for src/cvx/linalg/decomposition/eigvals.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-03 18:56 +0000

1"""Eigenvalue utilities for general square matrices.""" 

2 

3from __future__ import annotations 

4 

5from typing import cast 

6 

7import numpy as np 

8import numpy.typing as npt 

9 

10from ..core.exceptions import NonSquareMatrixError, NotAMatrixError 

11from ..core.types import Matrix, Vector 

12 

13 

14def eigvals(matrix: Matrix) -> Vector | npt.NDArray[np.complex128]: 

15 """Return the eigenvalues of a square matrix. 

16 

17 This routine supports general (non-symmetric) square matrices and may 

18 return complex eigenvalues. 

19 

20 Args: 

21 matrix: Square input matrix. 

22 

23 Returns: 

24 Eigenvalues of ``matrix`` as returned by ``numpy.linalg.eigvals``. 

25 

26 Raises: 

27 NotAMatrixError: If *matrix* is not two-dimensional. 

28 NonSquareMatrixError: If *matrix* is not square. 

29 """ 

30 if matrix.ndim != 2: 

31 raise NotAMatrixError(matrix.ndim, func="eigvals") 

32 

33 if matrix.shape[0] != matrix.shape[1]: 

34 raise NonSquareMatrixError(matrix.shape[0], matrix.shape[1]) 

35 

36 return cast("Vector | npt.NDArray[np.complex128]", np.linalg.eigvals(matrix))