在函数功能库中添加功能 ==> Vector.py
def norm(self):
'返回向量的模'
return math.sqrt(sum(e**2 for e in self))
def dot(self,another):
'向量点乘,返回结果标量'
assert len(self) == len(another), \
'Error in dot product. Length of vectors must be same'
return sum(a * b for a, b in zip(self,another))
def normalize(self):
'返回向量的单位向量'
if self.norm() < EPSILON:
raise ZeroDivisionError('Normalize error! norm is zero.')
return Vector(self._values) / self.norm()
验证函数功能 ==> main_vector.py
zero2 = Vector.zero(2)
print(zero2)
print('{} + {} = {}'.format(vec, zero2, vec + zero2))
print('norm({}) = {}'.format(vec,vec.norm()))
print('norm({}) = {}'.format(vec,vec2.norm()))
print('norm({}) = {}'.format(vec,zero2.norm()