What is the '@=' symbol for in Python?
Asked Answered
M

4

238

I know @ is for decorators, but what is @= for in Python? Is it just reservation for some future idea?

This is just one of my many questions while reading tokenizer.py.

Menashem answered 9/12, 2014 at 17:59 Comment(3)
See cset c553d8f72d65 (GitHub mirror...easier to read) in the CPython repo.Gilman
SymbolHound is a search-engine which can search on punctuation symbols. However searching on @= python doesn't currently return relevant results, because Python 3.5 documentation contains '@' but not an example of '@=' anywhere. I sent SH a message to help improve that. Python doc could improve too.Autotype
Combined with the := walrus operator of Python 3.8, you get what's known as the @:= thorny rose operator. (Or in Japan it's known as the Elvis-walrus operator.)Enravish
L
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.

Levon answered 9/12, 2014 at 18:0 Comment(4)
That explains why it's in the latest version of tokenizer.py but not the 3.4 docs.Menashem
This is covered in 3.5's docs - docs.python.org/3.5/reference/… and docs.python.org/3.5/reference/…Antiworld
Does this have conflict with Python decorators? This is not implemented in Python 2.n, right?Episcopalism
This does not conflict decorators, because decorators may never be preceded by an expression, and binary operators must always be preceded by an expression.Levon
P
123

@= 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.

Pumpernickel answered 3/6, 2015 at 19:44 Comment(3)
Just for clarification: from your first example, we could think that @ has been implemented for list, which is not the case.Aerophone
@ is associated with np.matmul, not np.dot. The two are similar but not the same.Keeling
@A-B-B, perhaps you could provide an example clarifying the nuance and ensuring the answer is then complete?Emarie
R
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
Rosannrosanna answered 10/8, 2017 at 17:25 Comment(0)
M
0

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

enter image description here

Molt answered 5/7, 2023 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.