Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of "sum product" as mentioned in Numpy documentation?

Tags:

numpy

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.)

like image 675
JérômeL Avatar asked Oct 11 '18 15:10

JérômeL


People also ask

How do you find the sum in NumPy?

sum() in Python. numpy. sum(arr, axis, dtype, out) : This function returns the sum of array elements over the specified axis.

Does NumPy Have a sum function?

The numpy. sum() function is available in the NumPy package of Python. This function is used to compute the sum of all elements, the sum of each row, and the sum of each column of a given array.

Can I sum a NumPy array?

Python numpy sum() function syntaxThe array elements are used to calculate the sum. If the axis is not provided, the sum of all the elements is returned. If the axis is a tuple of ints, the sum of all the elements in the given axes is returned.

What is NumPy dot product?

numpy.dot(vector_a, vector_b, out = None) returns the dot product of vectors a and b. It can handle 2D arrays but considers them as matrix and will perform matrix multiplication. For N dimensions it is a sum-product over the last axis of a and the second-to-last of b : dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])


1 Answers

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 and b is a 1-D array, it is a sum product over the last axis of a and b.

  • 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'.

like image 57
hpaulj Avatar answered Nov 15 '22 08:11

hpaulj