Python 中的“@=”符号是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27385633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the '@=' symbol for in Python?
提问by Octavia Togami
I know @
is for decorators, but what is @=
for in Python? Is it just reservation for some future idea?
我知道@
是为了装饰器,但是@=
在 Python 中是为了什么?是否只是为一些未来的想法保留?
This is just one of my many questions while reading tokenizer.py
.
这只是我阅读时的众多问题之一tokenizer.py
。
采纳答案by rightfold
From thedocumentation:
The
@
(at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator.
的
@
(在)操作者意图被用于矩阵乘法。没有内置的 Python 类型实现此运算符。
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__
.
该@
运算符是在 Python 3.5 中引入的。@=
正如您所期望的那样,是矩阵乘法,然后是赋值。它们映射到__matmul__
,__rmatmul__
或__imatmul__
类似于 how+
和+=
映射到__add__
,__radd__
或__iadd__
。
The operator and the rationale behind it are discussed in detail in PEP 465.
PEP 465中详细讨论了操作符及其背后的原理。
回答by Andrzej Pronobis
@=
and @
are new operators introduced in Python 3.5performing 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.
@=
和@
是 Python 3.5 中引入的执行矩阵乘法的新运算符。它们旨在澄清迄今为止与*
用于元素乘法或矩阵乘法的运算符存在的混淆,具体取决于该特定库/代码中采用的约定。因此,将来,该运算符*
仅用于元素乘法。
As explained in PEP0465, two operators were introduced:
如PEP0465 中所述,引入了两个运算符:
- A new binary operator
A @ B
, used similarly asA * B
- An in-place version
A @= B
, used similarly asA *= B
- 一个新的二元运算符
A @ B
,用法类似A * B
- 就地版本
A @= B
,使用方式类似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]]
按元素乘法将产生:
A * B = [[1 * 11, 2 * 12], [3 * 13, 4 * 14]]
矩阵乘法将产生:
A @ B = [[1 * 11 + 2 * 13, 1 * 12 + 2 * 14], [3 * 11 + 4 * 13, 3 * 12 + 4 * 14]]
Usage in Numpy
在 Numpy 中的使用
So far, Numpy used the following convention:
到目前为止,Numpy 使用了以下约定:
the
*
operator (and arithmetic operatorsin general) were defined as element-wise operations on ndarraysand as matrix-multiplication on numpy.matrixtype.method/function
dot
was used for matrix multiplication of ndarrays
的
*
操作者(和算术运算符在普通)被定义为在元件为单位的运算ndarrays并作为矩阵乘法numpy.matrix类型。方法/函数
dot
用于 ndarrays 的矩阵乘法
Introduction of the @
operator makes the code involving matrix multiplications much easier to read. PEP0465 gives us an example:
@
运算符的引入使涉及矩阵乘法的代码更易于阅读。PEP0465 给了我们一个例子:
# 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.
显然,最后一个实现更容易阅读和解释为等式。
回答by amehta
@ is the new operator for Matrix Multiplication added in Python3.5
@ 是 Python3.5 中新增的矩阵乘法运算符
Reference: https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465
参考:https: //docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465
Example
例子
C = A @ B