What is the '@=' symbol for in Python?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping
--
Chapters
00:00 What Is The '@=' Symbol For In Python?
00:19 Accepted Answer Score 269
01:09 Answer 2 Score 124
02:54 Answer 3 Score 10
03:09 Answer 4 Score 0
03:17 Thank you
--
Full question
https://stackoverflow.com/questions/2738...
--
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 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.
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
