python中的点积

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4093989/
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-18 14:13:01  来源:igfitidea点击:

dot product in python

pythondot-product

提问by user458858

Does this Python code actually find the dot product of two vectors?

这段 Python 代码真的能找到两个向量的点积吗?

import operator

vector1 = (2,3,5)
vector2 = (3,4,6)
dotProduct = reduce( operator.add, map( operator.mul, vector1, vector2))

采纳答案by whatnick

You can also use the numpy implementation of dot productwhich 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.

您还可以使用点积的 numpy 实现,它在本机代码中进行了大型数组优化,使计算速度稍微快一些。更好的是,除非您专门尝试编写点积例程或避免依赖项,否则使用久经考验的广泛使用的库比滚动自己的库要好得多。

回答by John La Rooy

Yes it does. Here is another way

是的,它确实。这是另一种方式

>>> sum(map( operator.mul, vector1, vector2))
48

and another that doesn't use operatorat all

而另一个不使用operator在所有

>>> vector1 = (2,3,5)
>>> vector2 = (3,4,6)
>>> sum(p*q for p,q in zip(vector1, vector2))
48