bundles / numpy 2.4.3 / numpy / linalg / tensordot
_ArrayFunctionDispatcher
numpy.linalg:tensordot
source: /numpy/linalg/_linalg.py :3415
Signature
def tensordot ( x1 , x2 , / , axes = 2 ) Summary
Compute tensor dot product along specified axes.
Extended Summary
Given two tensors, a and b, and an array_like object containing two array_like objects, (a_axes, b_axes), sum the products of a's and b's elements (components) over the axes specified by a_axes and b_axes. The third argument can be a single non-negative integer_like scalar, N; if it is such, then the last N dimensions of a and the first N dimensions of b are summed over.
Parameters
a, b: array_likeTensors to "dot".
axes: int or (2,) array_likeinteger_like If an int N, sum over the last N axes of a and the first N axes of b in order. The sizes of the corresponding axes must match.
(2,) array_like Or, a list of axes to be summed over, first sequence applying to a, second to b. Both elements array_like must be of the same length. Each axis may appear at most once; repeated axes are not allowed. For example,
axes=([1, 1], [0, 0])is invalid.
Returns-------output: ndarrayThe tensor dot product of the input.
Notes
Three common use cases are:
axes = 0tensor productaxes = 1tensor dot productaxes = 2(default) tensor double contraction
When axes is integer_like, the sequence of axes for evaluation will be: from the -Nth axis to the -1th axis in a, and from the 0th axis to (N-1)th axis in b. For example, axes = 2 is the equal to axes = [[-2, -1], [0, 1]]. When N-1 is smaller than 0, or when -N is larger than -1, the element of a and b are defined as the axes.
When there is more than one axis to sum over - and they are not the last (first) axes of a (b) - the argument axes should consist of two sequences of the same length, with the first axis to sum over given first in both sequences, the second axis second, and so forth. The calculation can be referred to numpy.einsum.
For example, if a.shape == (2, 3, 4) and b.shape == (3, 4, 5), then axes=([1, 2], [0, 1]) sums over the (3, 4) dimensions of both arrays and produces an output of shape (2, 5).
Each summation axis corresponds to a distinct contraction index; repeating an axis (for example axes=([1, 1], [0, 0])) is invalid.
The shape of the result consists of the non-contracted axes of the first tensor, followed by the non-contracted axes of the second.
Examples
An example on integer_like:a_0 = np.array([[1, 2], [3, 4]]) b_0 = np.array([[5, 6], [7, 8]]) c_0 = np.tensordot(a_0, b_0, axes=0) c_0.shape✓
c_0
✗a = np.arange(60.).reshape(3,4,5) b = np.arange(24.).reshape(4,3,2) c = np.tensordot(a,b, axes=([1,0],[0,1])) c.shape c✓
d = np.zeros((5,2)) for i in range(5): for j in range(2): for k in range(3): for n in range(4): d[i,j] += a[k,n,i] * b[n,k,j] c == d✓
a = np.array(range(1, 9)).reshape((2, 2, 2)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) A = A.reshape((2, 2))✓
a; A
✗np.tensordot(a, A) # third argument default is 2 for double-contraction
✓np.tensordot(a, A, 1)
✗np.tensordot(a, A, 0) # tensor product (result too long to incl.)
✗np.tensordot(a, A, (0, 1))
✗np.tensordot(a, A, (2, 1))
✗np.tensordot(a, A, ((0, 1), (0, 1)))
✓np.tensordot(a, A, ((2, 1), (1, 0)))
✓See also
Aliases
-
numpy.linalg.tensordot