在Python中真的有一个@运算符来计算点积吗?

Lon*_*ner 7 python operators

这个答案是否正确:https://stackoverflow.com/a/39662710/1175080?

引用那个答案.

在Python 3.5中,dot产品有一个新的运算符,所以你可以写一个= A @ B而不是a = numpy.dot(A,B)

它似乎对我不起作用.

$ python3
Python 3.6.1 (default, Apr  4 2017, 09:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a @ b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for @: 'list' and 'list'
>>>
Run Code Online (Sandbox Code Playgroud)

但是相关的答案已经获得了6个赞成票,所以我必须遗漏一些东西.您能否提供一个完整的示例,说明如何使用@运算符计算点积?

ran*_*mir 11

请参阅Python 3.5中的新内容,部分矩阵mult(PEP 465):

PEP 465添加了@用于矩阵乘法的中缀运算符.目前,没有内置的Python类型执行新的运营商,但是,它可以通过定义来实现__matmul__(),__rmatmul__()以及__imatmul__()定期,反射,就地矩阵乘法.这些方法的语义类似于定义其他中缀算术运算符的方法.

所以,你必须自己实现这些方法.

或者,使用numpy>=1.10已经支持新运算符的用户:

>>> import numpy
>>> x = numpy.ones(3)
>>> m = numpy.eye(3)
>>> x @ m
array([ 1., 1., 1.])
Run Code Online (Sandbox Code Playgroud)