Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is the Numpy matrix multiplication operation called "dot"?

Tags:

python

numpy

I am a bit confused about the naming of the Numpy function dot: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.dot.html

It seems like this is what is used in Numpy to perform matrix multiplication. However the "dot product" is something different, it produces a single scalar from two vectors: https://en.wikipedia.org/wiki/Dot_product

Can somebody resolve these two uses of the term "dot"?

like image 648
Stephen Avatar asked Jan 16 '18 00:01

Stephen


People also ask

Why is matrix multiplication a dot product?

Multiplication of two matrices involves dot products between rows of first matrix and columns of the second matrix. The first step is the dot product between the first row of A and the first column of B. The result of this dot product is the element of resulting matrix at position [0,0] (i.e. first row, first column).

Is NumPy dot matrix multiplication?

dot() will compute the dot product of the inputs. If both inputs are 2-dimensional arrays, then np. dot() will perform matrix multiplication.

What does NP dot mean?

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

What is the difference between NumPy dot and multiply?

np. dot is the dot product of two matrices. Whereas np. multiply does an element-wise multiplication of two matrices.


2 Answers

It is common to write multiplication using a centrally positioned dot:

A⋅B

The name almost certainly comes from this notation. There is actually a dedicated codepoint for it named DOT OPERATOR under the "Mathematical Operators" block of unicode: chr(0x22c5). The comments mention this as

...for denotation of multiplication

Now, regarding this comment:

However the "dot product" is something different, it produces a single scalar from two vectors

They are not altogether unrelated! In a 2-d matrix multiplication A⋅B, the element at position (i,j) in the result has come from a dot product of row i by column j.

like image 67
wim Avatar answered Oct 17 '22 13:10

wim


The "dot" or "inner" product has an expanded definition in tensor algebra compared to linear algebra.

For n and m dimensional tensors N and M, N⋅M will have dimension (n + m - 2), as the "inner dimensions" (last dimension of N and first dimension of M) will be summed over after multiplication.

(as an aside, N.dot(M) actually sums over the last dimension of N and the second-last dimension of M, because . . . reasons. The actual tensor algebra "dot product" functionality is relegated to np.tensordot)

For n = m = 1 (i.e. both are 1-d tensors, or "vectors"), the output is a 0-d tensor, or "scalar", and this is equivalent to the "scalar" or "dot" product from linear algebra.

For n = m = 2 (i.e. both are 2-d tensors, or "matrices"), the output is a 2-d tensor, or "matrix", and the operation is eqivalent to "matrix multiplication".

And for n = 2, m = 1, the output is a 1d tensor, or "vector", and this is called (at least by my professors) "mapping."

Note that since the order of dimensions is relevent to the construction of the inner product,

N.dot(M) == M.dot(N)

is not generally True unless n = m = 1 - i.e. only in the case of the scalar product.

like image 33
Daniel F Avatar answered Oct 17 '22 13:10

Daniel F