tensordot — SciPy v1.15.2 Manual (original) (raw)

scipy.sparse.coo_matrix.

coo_matrix.tensordot(other, axes=2)[source]#

Return the tensordot product with another array along the given axes.

The tensordot differs from dot and matmul in that any axis can be chosen for each of the first and second array and the sum of the products is computed just like for matrix multiplication, only not just for the rows of the first times the columns of the second. It takes the dot product of the collection of vectors along the specified axes. Here we can even take the sum of the products along two or even more axes if desired. So, tensordot is a dot product computation applied to arrays of any dimension >= 1. It is like matmul but over arbitrary axes for each matrix.

Given two tensors, a and b, and the desired axes specified as a 2-tuple/list/array containing two sequences of axis numbers,(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 axes input can be a single non-negative integer, N; if it is, then the last N dimensions of a and the firstN dimensions of b are summed over.

Parameters:

a, barray_like

Tensors to “dot”.

axesint or (2,) array_like

Returns:

outputcoo_array

The tensor dot product of this array with other. It will be dense/sparse if other is dense/sparse.

Examples

import numpy as np import scipy.sparse A = scipy.sparse.coo_array([[[2, 3], [0, 0]], [[0, 1], [0, 5]]]) A.shape (2, 2, 2)

Integer axes N are shorthand for (range(-N, 0), range(0, N)):

A.tensordot(A, axes=1).toarray() array([[[[ 4, 9], [ 0, 15]],

    [[ 0,  0],
     [ 0,  0]]],


   [[[ 0,  1],
     [ 0,  5]],

    [[ 0,  5],
     [ 0, 25]]]])

A.tensordot(A, axes=2).toarray() array([[ 4, 6], [ 0, 25]]) A.tensordot(A, axes=3) array(39)

Using tuple for axes:

a = scipy.sparse.coo_array(np.arange(60).reshape(3,4,5)) b = np.arange(24).reshape(4,3,2) c = a.tensordot(b, axes=([1,0],[0,1])) c.shape (5, 2) c array([[4400, 4730], [4532, 4874], [4664, 5018], [4796, 5162], [4928, 5306]])