Issue
In the NumPy v1.15 Reference Guide, the documentation for numpy.dot uses the concept of "sum product".
Namely, we read the following:
- If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
- If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
What is the definition for this "sum product" concept?
(Couldn't find such a definition on Wikipedia, for example.)
Solution
https://en.wikipedia.org/wiki/Matrix_multiplication
That is, the entry c[i,j] of the product is obtained by multiplying
term-by-term the entries of the ith row of A and the jth column of B,
and summing these m products. In other words, c[i,j] is the dot product
of the ith row of A and the jth column of B.
https://en.wikipedia.org/wiki/Dot_product
Algebraically, the dot product is the sum of the products of the
corresponding entries of the two sequences of numbers.
In early math classes did you learn to take the matrix product, by running one finger across the rows of A
and down the columns of B
, mulitplying pairs of numbers and summing them? That motion is part of my intuition of how that product is taken.
For the 1d second argument case, np.dot
and np.matmul
produce the same thing, but describe the action differently:
If
a
is an N-D array andb
is a 1-D array, it is a sum product over the last axis ofa
andb
.If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.
In [103]: np.dot([[1,2],[3,4]], [1,2]) Out[103]: array([ 5, 11]) In [104]: np.matmul([[1,2],[3,4]], [1,2]) Out[104]: array([ 5, 11])
Appending the dimension to B
, does:
In [105]: np.matmul([[1,2],[3,4]], [[1],[2]])
Out[105]:
array([[ 5],
[11]])
This last is a (2,2) with (2,1) => (2,1)
Sometimes it is clearer to express the action in einsum
terms:
In [107]: np.einsum('ij,j->i', [[1,2],[3,4]], [1,2])
Out[107]: array([ 5, 11])
j
, the last axis of both arrays is the one that gets 'summed'.
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.