python 中的点积
这段 Python 代码真的能求出两个向量的点积吗?
import operator
vector1 = (2,3,5)
vector2 = (3,4,6)
dotProduct = reduce( operator.add, map( operator.mul, vector1, vector2))
Does this Python code actually find the dot product of two vectors?
import operator
vector1 = (2,3,5)
vector2 = (3,4,6)
dotProduct = reduce( operator.add, map( operator.mul, vector1, vector2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,确实如此。这是另一种方法
,另一种根本不使用
operator
Yes it does. Here is another way
and another that doesn't use
operator
at all您还可以使用 点积 的 numpy 实现在本机代码中进行大型数组优化,使计算速度稍快一些。更好的是,除非您专门尝试编写点积例程或避免依赖性,否则使用经过测试的广泛使用的库比您自己的库要好得多。
You can also use the numpy implementation of dot product which has large array optimizations in native code to make computations slightly faster. Even better unless you are specifically trying to write a dot product routine or avoid dependencies, using a tried tested widely used library is much better than rolling your own.