Skip to content

numpy/einsum-optimize

Pack Severity Fix Versions
NumPy warning unsafe, with xpd check --fix --unsafe-fixes All versions

Without optimize=, einsum loops over every index at once, which was 339 times slower for three 100 by 100 matrices. Add optimize=True.

chain = np.einsum("ij,jk,kl->il", a, b, c)
chain = np.einsum("ij,jk,kl->il", a, b, c, optimize=True)

The rule reports np.einsum with three or more 2-D operands and a 2-D result, such as "ij,jk,kl->il", and no optimize=.

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 adds optimize=True.

The contraction order can change the last bits. The rule leaves a scalar result such as "ij,jk,ki->" alone, since optimizing that contraction can be slower.

Without optimize= einsum loops over every index at once, which ran 339 times slower on three 100 by 100 matrices. Add optimize=True.

See all rules in the NumPy pack.