Skip to content

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).logabsdet

The rule reports np.log(np.linalg.det(a)) and np.log(np.abs(np.linalg.det(a))).

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.

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.