Home > Mobile >  Difference between Operator * and operator @ for matrix multiplication in Python
Difference between Operator * and operator @ for matrix multiplication in Python

Time:03-18

I want to compute matrix multiplication in the mathematical sense with Python. A matrix with m rows and n columns multiplied by a matrix with n rows and m columns gives a matrix with m rows and m columns. I used the operator * incorrectly at first and got unexpected results. I later learned that the operator @ is what I need. So what is the difference between these two operators for matrix operations in Python?

CodePudding user response:

* is an element wise multiplication and @ is a matrix or dot product.

CodePudding user response:

  • The * operator is equivalent to the multiply function (in terms of array broadcasting).
  • The @ operator is equivalent to the matmul function (the matmul function implements the semantics of the @ operator introduced in Python 3.5 following PEP 465)
  • Related