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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:44:44  来源:igfitidea点击:

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

pythonpython-3.xoperatorsmatrix-multiplicationpython-3.5

提问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 as A * B
  • An in-place version A @= B, used similarly as A *= 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 使用了以下约定:

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