Changing the return type of linalg.eig and linalg.eigvals
matti picus via NumPy-Discussion <[email protected]> Wed, 18 Feb 2026 21:01:47 +0200
| Newsgroups | gmane.comp.python.numeric.general |
|---|---|
| Message-ID | <CAO8bCgh1OoW_1bL5brqnbXMYNv9dQQZmH13NxvzA8Km+jks0yA@mail.gmail.com> |
Heads up, we changed the behaviour of linalg.eig and linalg.eigvals to
always return complex ndarrays. If this causes inconvenience that
cannot be mitigated, please open an issue.
>From the release note of a recently merged PR:
* `linalg.eig` and `linalg.eigvals` always return complex arrays.
Previously, the return
values depended on whether the eigenvalues happen to lie on the
real line (which, for
a general, non-symmetric matrix, is not guaranteed).
The user change depends on your usage:
- to retain the previous behavior, do
```
w = eigvals(a)
if w.imag == 0: # this is what NumPy used to do
w = w.real
```
- if your matrix is symmetrix/hermitian, use `eigh` and `eigvalsh`
instead of `eig` and `eigvals`:
these are guaranteed to return real values. (A common case is
covariance matrices, which are
symmetric and positive definite by construction)
Thanks Evgeni for the PR.
Matti
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: [email protected]