Home > other >  What is the difference between the operator acting elementwise vs on the matrix using Numpy?
What is the difference between the operator acting elementwise vs on the matrix using Numpy?

Time:07-03

Numpy docs talks about the difference between the product operator and the matrix operator.

Unlike in many matrix languages, the product operator * operates elementwise in NumPy arrays. The matrix product can be performed using the @ operator (in python >=3.5) or the dot

Question: What is the difference between the operator acting elementwise vs on the matrix?

How would it change the outcome?

CodePudding user response:

Say we've got two matrices:

a = [ p q ]
    [ r s ]

b = [ w x ]
    [ y z ]

Element-wise product means:

a * b = [ p*w  q*x ]
        [ r*y  s*z ]

Matrix product means:

a @ b = [ (p*w) (q*y)  (p*x) (q*z) ]
        [ (r*w) (s*y)  (r*x) (s*z) ]

When literature in math, machine learning etc talks about "matrix multiplication", this matrix product is what is meant. Note that a @ b is not the same as b @ a.

  • Related