检测判断向量是正交 还是平行
2018-02-12 22:12
162 查看
# coding=utf-8 from math import sqrt, acos, pi class Vector(object): """docstring for Vector""" """根据坐标轴列表输入 创建向量, 并创建该向量所处的空间维度""" CANNOT_NORMALIZE_ZERO_VECTOR_MSG = 'Cannot normalize the zero vector' def __init__(self, coordinates): super(Vector, self).__init__() try: if not coordinates: raise ValueError self.coordinates = tuple( [x for x in coordinates]) self.dimension = len(coordinates) except ValueError: raise ValueError('The coordinates must be nonempty') except TypeError: raise TypeError('The coordinates must be an iterable') # '''能够使python的内置print函数 输出向量坐标轴''' def __str__(self): return 'Vector: {}'.format(self.coordinates) # 计算向量长度 def magnitude(self): coordinates_squared = [x**2 for x in self.coordinates] return sqrt(sum(coordinates_squared)) # 将向量归一化 def normalized(self): try: magnitude = self.magnitude() return Vector([x *1.0/ magnitude for x in self.coordinates]) except ZeroDivisionError: raise Exception('Cannot normalized the zero myVector2') # 点积 def dot(self, v): return sum([round(x*y,3) for x,y in zip(self.coordinates, v.coordinates)]) # 是否平行 def vectorType(self, v): result = "" # print (self.dot(v)) if self.dot(v) ==0: result += "Orthogonal " else: result += "NoOrthogonal " new_cordinates = [y/x for x,y in zip(self.coordinates, v.coordinates)] compair = new_cordinates[0] for i in new_cordinates: if round(compair,3) != round(i,3): result += "NoParaller" return result result += "Paraller" return result v1 = Vector([-7.579, -7.88]) v2 = Vector([22.737, 23.64]) print(v1.vectorType(v2)) v3 = Vector([-2.029, 9.97, 4.172]) v4 = Vector([-9.231, -6.639, -7.245]) print(v3.vectorType(v4)) v5 = Vector([-2.328,-7.284,-1.214]) v6 = Vector([-1.821,1.072,-2.94]) print(v5.vectorType(v6)) v7 = Vector([2.118,4.827]) v8 = Vector([0,0]) print(v7.vectorType(v8)) # 输出结果 # NoOrthogonal Paraller # NoOrthogonal NoParaller # Orthogonal NoParaller # Orthogonal Paraller # [Finished in 0.2s]优化
# 计算向量长度
def magnitude(self):
coordinates_squared = [x**2 for x in self.coordinates]
return sqrt(sum(coordinates_squared))
# 将向量归一化
def normalized(self):
try:
magnitude = self.magnitude()
return Vector([x *1.0/ magnitude for x in self.coordinates])
except ZeroDivisionError:
raise Exception('Cannot normalized the zero myVector2')
# 点积
def dot(self, v):
# print([x*y for x,y in zip(self.coordinates, v.coordinates)])
return sum([round(x*y,3) for x,y in zip(self.coordinates, v.coordinates)])
# 角度弧度
def angle_with(self,v,in_degress=False):
try:
u1 = self.normalized()
u2 = v.normalized()
angle_in_radians = acos(u1.dot(u2))
if in_degress:
degrees_per_radian = 180./pi
return angle_in_radians * degrees_per_radian
else:
return angle_in_radians
except Exception as e:
if str(e) == self.CANNOT_NORMALIZE_ZERO_VECTOR_MSG:
raise Exception('Cannot compute an angle with the zero vector')
else:
raise e
def is_orthogonal_to(self, v, tolerance=1e-10):
return abs(self.dot(v)) < tolerance
def is_paraller_to(self,v):
return (self.is_zero() or v.is_zero() or self.angle_with(v) ==0 or self.angle_with(v) == pi)
def is_zero(self, tolerance = 1e-10):
return self.magnitude()< tolerance
v1 = Vector([-7.579, -7.88])
v2 = Vector([22.737, 23.64])
print 'is paraller:',v1.is_orthogonal_to(v2)
print 'is orthogonal:',v1.is_paraller_to(v2)
v3 = Vector([-2.029, 9.97, 4.172])
v4 = Vector([-9.231, -6.639, -7.245])
print 'is paraller:',v3.is_orthogonal_to(v4)
print 'is orthogonal:',v3.is_paraller_to(v4)
v5 = Vector([-2.328,-7.284,-1.214])
v6 = Vector([-1.821,1.072,-2.94])
print 'is paraller:',v5.is_orthogonal_to(v6)
print 'is orthogonal:',v5.is_paraller_to(v6)
v7 = Vector([2.118,4.827])
v8 = Vector([0,0])
print 'is paraller:',v7.is_orthogonal_to(v8)
print 'is orthogonal:',v7.is_paraller_to(v8)
# 输出结果
# is paraller: False
# is orthogonal: True
# is paraller: False
# is orthogonal: False
# is paraller: True
# is orthogonal: False
# is paraller: True
# is orthogonal: True
# [Finished in 0.1s]
相关文章推荐
- web网站判断是手机访问还是pc电脑访问的检测逻辑
- 向量运用。扇形(或三角形)碰撞检测判断。 lua代码
- 基于MATLAB的线性代数 判断两向量是否正交
- js 设备检测(判断是移动端还是PC端)
- 检测某向量在另一向量的左边还是右边
- 检测Linux VPS是Xen、OpenVZ还是KVM架构的方法 判断VPS是Xen、OpenVZ还是KVM真假的方法
- 判断两个向量是否平行
- js 设备检测(判断是移动端还是PC端)
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)-C. Constellation(贪心+向量平行判断)
- 判断两向量之间夹角是顺时针还是逆时针?
- web网站判断是手机访问还是pc电脑访问的检测逻辑
- 向量叉乘判断两向量之间是顺时针还是逆时针
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)-C. Constellation(贪心+向量平行判断)
- Tolerance-公差,可用来判断两点是否相等,,两向量是否相等、平行、垂直等
- 判断向量平行和垂直的条件
- 判断应用是系统程序还是用户程序?
- [JavaScript]检测(判断)一个链接(URL)是否有效(服务可用)?[整理]
- Android 判断app是否在前台还是在后台运行
- 判断ORACLE启动时使用spfile还是pfile
- Js判断客户端是否为PC还是手持设备