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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
@= 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 asA * B - An in-place version
A @= B, used similarly asA *= 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:
-
the
*operator (and arithmetic operators in general) were defined as element-wise operations on ndarrays and as matrix-multiplication on numpy.matrix type. -
method/function
dotwas used for matrix multiplication of ndarrays
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.
Method 3
@ 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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0