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

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

概述这个答案是否正确: https://stackoverflow.com/a/39662710/1175080? 引用那个答案. In Python 3.5, there is a new operator for the dot product, so you can write a= A @ B instead of a= numpy.dot(A,B) 它似乎对我不起作用. $python3P 这个答案是否正确: https://stackoverflow.com/a/39662710/1175080?

引用那个答案.

In Python 3.5,there is a new operator for the dot product,so you
can write a= A @ B instead of a= numpy.dot(A,B)

它似乎对我不起作用.

$python3Python 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 darwinType "help","copyright","credits" or "license" for more information.>>> a = [1,2,3]>>> b = [4,5,6]>>> a @ bTraceback (most recent call last):  file "<stdin>",line 1,in <module>TypeError: unsupported operand type(s) for @: 'List' and 'List'>>>

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

解决方法 见 what’s new in Python 3.5,section matrix mult (PEP 465):

PEP 465 adds the @ infix operator for matrix multiplication. Currently,no builtin Python types implement the new operator,however,it can be implemented by defining __matmul__(),__rmatmul__(),and __imatmul__() for regular,reflected,and in-place matrix multiplication. The semantics of these methods is similar to that of methods defining other infix arithmetic operators.

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

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

>>> import numpy>>> x = numpy.ones(3)>>> m = numpy.eye(3)>>> x @ marray([ 1.,1.,1.])
总结

以上是内存溢出为你收集整理的在Python中真的有一个@运算符来计算点积吗?全部内容,希望文章能够帮你解决在Python中真的有一个@运算符来计算点积吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1193996.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存