The Python Oracle

What is the '@=' symbol for in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
00:26 Accepted answer (Score 254)
01:15 Answer 2 (Score 108)
03:34 Answer 3 (Score 9)
03:55 Thank you

--

Full question
https://stackoverflow.com/questions/2738...

Accepted answer links:
[the]: https://docs.python.org/3.5/reference/ex...
[documentation]: https://docs.python.org/3.5/reference/si...
[PEP 465]: http://www.python.org/dev/peps/pep-0465/

Answer 2 links:
[PEP0465]: https://www.python.org/dev/peps/pep-0465/
[arithmetic operators]: http://docs.scipy.org/doc/numpy/referenc...
[ndarrays]: http://docs.scipy.org/doc/numpy/referenc...
[numpy.matrix]: http://docs.scipy.org/doc/numpy/referenc...
[method/function]: http://docs.scipy.org/doc/numpy/referenc...

Answer 3 links:
https://docs.python.org/3/whatsnew/3.5.h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #operators #matrixmultiplication #python35

#avk47



ACCEPTED ANSWER

Score 269


From the documentation:

The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator.

The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__, __rmatmul__ or __imatmul__ similar to how + and += map to __add__, __radd__ or __iadd__.

The operator and the rationale behind it are discussed in detail in PEP 465.




ANSWER 2

Score 124


@= and @ are new operators introduced in Python 3.5 performing matrix multiplication. They are meant to clarify the confusion which existed so far with the operator * which was used either for element-wise multiplication or matrix multiplication depending on the convention employed in that particular library/code. As a result, in the future, the operator * is meant to be used for element-wise multiplication only.

As explained in PEP0465, two operators were introduced:

  • A new binary operator A @ B, used similarly as A * B
  • An in-place version A @= B, used similarly as A *= B

Matrix Multiplication vs Element-wise Multiplication

To quickly highlight the difference, for two matrices:

A = [[1, 2],    B = [[11, 12],
     [3, 4]]         [13, 14]]
  • Element-wise multiplication will yield:

    A * B = [[1 * 11,   2 * 12], 
             [3 * 13,   4 * 14]]
    
  • Matrix multiplication will yield:

    A @ B  =  [[1 * 11 + 2 * 13,   1 * 12 + 2 * 14],
               [3 * 11 + 4 * 13,   3 * 12 + 4 * 14]]
    

Usage in Numpy

So far, Numpy used the following convention:

Introduction of the @ operator makes the code involving matrix multiplications much easier to read. PEP0465 gives us an example:

# Current implementation of matrix multiplications using dot function
S = np.dot((np.dot(H, beta) - r).T,
            np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r))

# Current implementation of matrix multiplications using dot method
S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)

# Using the @ operator instead
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)

Clearly, the last implementation is much easier to read and interpret as an equation.




ANSWER 3

Score 10


@ is the new operator for Matrix Multiplication added in Python3.5

Reference: https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465

Example

C = A @ B



ANSWER 4

Score 0


Numpy 1.25 will support using @= as in place matrix multiplication:

enter image description here