Skip to content

Python Multiplications

Shi Jin edited this page Feb 6, 2018 · 1 revision

a * b = np.multiply(a, b): Element Wise

https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html#numpy.multiply

np.dot(a, b)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html

If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.

a @ b = np.matmul(a, b)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html#numpy.matmul

If both arguments are 2-D they are multiplied like conventional matrices. The matmul function implements the semantics of the @ operator introduced in Python 3.5 following PEP465.

matmul differs from dot in two important ways.

  • Multiplication by scalars is not allowed.
  • Stacks of matrices are broadcast together as if the matrices were elements.