Previous topic

Linear algebra (numpy.linalg)

Next topic

numpy.vdot

numpy.dot

numpy.dot(a, b, out=None)

Dot product of two arrays.

对于2-D数组,其等效于矩阵乘法,对于1-D数组等效于向量的内积(无共轭复数)。对于N维,它是a的最后一个轴和b的倒数第二个轴的积的和:

dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
Parameters:

a : array_like

First argument.

b : array_like

Second argument.

out : ndarray, optional

Output argument. 如果没有使用,返回必须有确切的类型。特别地,它必须具有正确的类型,必须是C连续的,并且其dtype必须是dot(a,b)将返回的dtype。This is a performance feature. Therefore, if these conditions are not met, an exception is raised, instead of attempting to be flexible.

Returns:

output : ndarray

Returns the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. If out is given, then it is returned.

引发:

ValueError

如果a的最后一个维度大小与b的倒数第二个维度的大小不同。

另见

vdot
Complex-conjugating dot product.
tensordot
Sum products over arbitrary axes.
einsum
Einstein summation convention.
matmul
‘@’ operator as method with out parameter.

Examples

>>> np.dot(3, 4)
12

Neither argument is complex-conjugated:

>>> np.dot([2j, 3j], [2j, 3j])
(-13+0j)

For 2-D arrays it is the matrix product:

>>> a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1],
       [2, 2]])
>>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
>>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
>>> np.dot(a, b)[2,3,2,1,2,2]
499128
>>> sum(a[2,3,2,:] * b[1,2,:,2])
499128