Why documentation etc. use @ in place of * for multiplication

Why many documentations and blog posts use @ in place of * (multiplication operator) in python.
Here is an example. They use C@x instead of c*x (also found in the next lines in the page). Is @ used to say it is vector multiplication etc.?

>Solution :

They write it like that because the underlying objects are matrices and/or vectors, not scalars.

The operator @ indicates a matrix multiplication, and hooks into the datamodel __matmul__, which has a different behaviour to * operation i.e. __mul__.

The most common case you will see is with numpy ndarrays, where @ is matrix multiplication and * is element-wise multiplication:

>>> A = np.arange(4).reshape(2,2)
>>> A
array([[0, 1],
       [2, 3]])
>>> A @ A
array([[ 2,  3],
       [ 6, 11]])
>>> A * A
array([[0, 1],
       [4, 9]])

Leave a Reply