求点乘方法有:
1. np.sum(a*b)
2. (a*b).sum()
3. np.dot(a, b)
4. a.dot(b)
5. b.dot(a)
求向量长度方法有:
1. np.sqrt((a*a).sum())
2. np.linalg.norm(a)
求两个向量夹角:
根据点乘定义,cosangle = a.dot(b)/(np.linalg.norm(a) * np.linalg.norm(b))
In [25]: a = np.array([1,2])
In [26]: b = np.array([2, 1])
In [27]: dot = 0
In [28]: for e, f in zip(a, b):
...: dot += e*f
...:
In [29]: dot
Out[29]: 4
In [30]: a*b
Out[30]: array([2, 2])
In [31]: np.sum(a*b)
Out[31]: 4
In [32]: (a*b).sum()
Out[32]: 4
In [33]: np.dot(a,b)
Out[33]: 4
In [34]: a.dot(b)
Out[34]: 4
In [35]: b.dot(a)
Out[35]: 4
In [36]: amag = np.sqrt((a*a).sum())
In [37]: amag
Out[37]: 2.23606797749979
In [38]: amag = np.linalg.norm(a)
In [39]: amag
Out[39]: 2.23606797749979
In [40]: cosangle = a.dot(b)/(np.linalg.norm(a) * np.linalg.norm(b))
In [41]: cosangle
Out[41]: 0.7999999999999998
In [42]: angle = np.arccos(cosangle)
In [43]: angle
Out[43]: 0.6435011087932847