我了解可以通过以下方式找到点积:
>>> vector1 = (2,3,5)
>>> vector2 = (3,4,6)
>>> sum(p*q for p,q in zip(vector1, vector2))
48
但我试图找到一个元组的点积说:
>>> vector3 = ((2,2),(3,3),(5,5))
>>> vector4 = ((3,3),(4,4),(6,6))
如何在不使用 numpy.
我不能只使用一些语句sum(p*q for p,q in zip(vector3, vector4))
吗?
因为当我尝试相同时,我收到如下错误:
>>> vector3 = ((2,2),(3,3),(5,5))
>>> vector4 = ((3,3),(4,4),(6,6))
>>> sum(p*q for p,q in zip(vector3, vector4))
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
sum(p*q for p,q in zip(vector1, vector2))
File "<pyshell#5>", line 1, in <genexpr>
sum(p*q for p,q in zip(vector3, vector4))
TypeError: can't multiply sequence by non-int of type 'tuple'
>>>
如何解决这个问题?