Week 2: Can "np.dot," "np.matmul" and "@" be used interchangeably for matrix multiplication?

Hello everyone!
From what I’ve realized, in the case of matrix multiplication(where we have 2D matrices), these three give the same output and can be used interchangeably. Although, for 1D matrices, it may not be true.

I even did a small experiment with a 1*3 and a 3*3 matrix and the results for all three cases were the same, so I was wondering if we could use just any of them in our assignments where we are doing matrix multiplication between two 2D matrices.

Also, I read somewhere that @ and np.matmul are better than np.dot and wanted to know whether this is true or not.

Here is my experiment:

import numpy as np

a = np.array([1,2,3]).reshape(3,1)
b = np.array([[4,5,6],[7,8,9],[1,2,3]])

print("\n\na=\n",a)
print("\n\nb=\n",b)

print("\n\nshapes\n",a.shape)
print(b.shape,"\n\n")

print("\n\n a^t=\n",a.T)
print("\n\n b= \n",b)

#using np.dot
ansDot = np.dot(a.T,b)
print("\n ans dot=\n",ansDot)
print("\n shape=",ansDot.shape)


#using np.matmul
ansmat=np.matmul(a.T,b)
print("\n ans matmul=\n",ansmat)
print("\n shape=",ansmat.shape)


#using @
ansAt = a.T @ b
print("\n ans atsign=\n",ansAt)
print("\n shape=",ansAt.shape)


print("\n\n\n----------------------------------------")



#add bias to the 1*3 matrix using broadcasting
b1 =1 
print(ansDot + b1)
print(ansmat + b1)
print(ansAt + b1)

Output:

a=
 [[1]
 [2]
 [3]]


b=
 [[4 5 6]
 [7 8 9]
 [1 2 3]]


shapes
 (3, 1)
(3, 3) 




 a^t=
 [[1 2 3]]


 b= 
 [[4 5 6]
 [7 8 9]
 [1 2 3]]

 ans dot=
 [[21 27 33]]

 shape= (1, 3)

 ans matmul=
 [[21 27 33]]

 shape= (1, 3)

 ans atsign=
 [[21 27 33]]

 shape= (1, 3)



----------------------------------------
[[22 28 34]]
[[22 28 34]]
[[22 28 34]]

It is great that you are actually running the experiments to verify the behavior. Yes, those three operations are equivalent for our purposes here. Prof Ng chooses only to use np.dot. There are some subtle differences between np.dot and np.matmul, but they do not matter for our purposes here, where we are only dealing with 2D arrays (matrices and vectors). I don’t know of any reason why @ or np.matmul would be considered “better”, but maybe that is something I just missed. If you have a reference, please share it and maybe we can all learn from it.

You can read the documentation for “numpy dot” and “numpy matmul” to learn more.

Hey @Nasim_Faridnia,
Welcome to the community. Nasim and @paulinpaloalto Sir, you might wanna take a look at this blog’s “Which should you choose?” section. I guess the only reason why @ is preferred over np.dot() and np.matmul() when they can be interchangeably used is the ease of coding. The former is easier to code as compared to the latter(s), especially when we are multiplying more than 2 matrices simultaneously. I hope this helps.

Cheers,
Elemento

Thank you very much. I found this claim on this stack overflow discussion and wanted to make sure if it’s correct.

Here are my two cents

According to the doc linked above:

  1. matmul differs from dot in two important ways:
  • Multiplication by scalars is not allowed, use * instead.
  • Stacks of matrices are broadcast together as if the matrices were elements, respecting the signature (n,k),(k,m)->(n,m)
  1. The matmul function implements the semantics of the @ operator introduced in Python 3.5 following PEP 465.

Interpret #2 to mean @ is only available for Python 3.5 and greater. You can read more than any sane human could possibly want to know about the creation of @ for Python here PEP 465 – A dedicated infix operator for matrix multiplication | peps.python.org

Cheers