您的位置:首页 > 其它

检测判断向量是正交 还是平行

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: