Skip to content

numpy/vecdot

Rule vecdot
Pack NumPy
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions NumPy 2.0 and later

Summing squares or products along the last axis builds a temporary array, which was 2.8 times slower. Use np.vecdot.

squared_length = np.sum(points ** 2, axis=-1)
squared_length = np.vecdot(points, points)

The rule reports np.sum(x ** 2, axis=-1) and np.sum(np.square(x), axis=-1). It also reports np.sum(a * b, axis=-1) and np.sum(a.conj() * b, axis=-1) where a and b are names or subscripts. np.vecdot multiplies and sums in one pass without the temporary array, and ran 2 to 3.5 times faster on 1,000,000 values.

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.vecdot(x, x) and np.vecdot(a, b). Floats can differ in the last bits, int8 products can wrap where np.sum counted in int64, two bool arrays give bool, and vecdot conjugates a complex first argument. Needs NumPy 2.0.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This builds the array of squares to sum it along the last axis, which ran 2.8 times slower on 1,000,000 values. Write $NP.vecdot($X, $X).

Browse the other rules of the NumPy pack.