numpy/eig-on-symmetric
| Rule | eig-on-symmetric |
|---|---|
| Pack | NumPy |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
eig treats a symmetric matrix as a general one, which was 3.6 times slower and returns complex values. Use np.linalg.eigh.
values, vectors = np.linalg.eig(np.cov(data, rowvar=False))values, vectors = np.linalg.eigh(np.cov(data, rowvar=False))What the rule reports
Section titled “What the rule reports”The rule reports np.linalg.eig and np.linalg.eigvals on a matrix the code made symmetric: x @ x.T, x.T @ x, np.dot(x, x.T), np.dot(x.T, x), a + a.T, np.cov(...) and np.corrcoef(...).
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes np.linalg.eigh(m), np.linalg.eigvalsh(m).
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
$M is symmetric, and eig treats it as a general matrix, which ran 3.6 times slower and returns complex values. Write $NP.linalg.eigh($M).Browse the other rules of the NumPy pack.