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"?
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).
dot() will compute the dot product of the inputs. If both inputs are 2-dimensional arrays, then np. dot() will perform matrix multiplication.
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])
np. dot is the dot product of two matrices. Whereas np. multiply does an element-wise multiplication of two matrices.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With