NumPy's einsum
lets you explicitly choose which axes are contracted with the so-called explicit mode, using ->
:
>>> a = np.arange(9).reshape((3, 3))
>>> np.einsum('ij,ij->j', a, a)
array([45, 66, 93])
einsum
also lets you specify a programmatic interface where the axes are specified using a list of integers rather than a string.
>>> np.einsum(a, [0, 1], a, [0, 1])
204
But it isn't clear if it's possible to combine these two paradigms. I'm trying to write a function that contracts a specified axis of two arrays, where the arrays can have any dimensionality.