numpy/log-of-det
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | warning | unsafe, with xpd check --fix --unsafe-fixes |
NumPy 2.0+ |
The determinant can overflow to inf or underflow to 0 before the log is taken. Use np.linalg.slogdet(a).logabsdet.
log_det = np.log(np.linalg.det(cov))log_det = np.linalg.slogdet(cov).logabsdetWhat the rule reports
Section titled “What the rule reports”The rule reports np.log(np.linalg.det(a)) and np.log(np.abs(np.linalg.det(a))).
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.slogdet(a).logabsdet. Needs NumPy 2.0.
For a negative determinant the replacement returns the log of its absolute value, while the original returns NaN.
Diagnostic message
Section titled “Diagnostic message”The determinant overflows to inf or underflows to 0 before the log is taken. Write $NP.linalg.slogdet($A).logabsdet.See all rules in the NumPy pack.