python 自定义 计算向量投影 正交 函数
2018-02-13 02:39
295 查看
# 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(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 scalarMultiply(self,num): new_corrdinate =[num * x for x in self.coordinates] return Vector(new_corrdinate) def minus(self,v): new_corrdinate = [x-y for x,y in zip(self.coordinates, v.coordinates)] return Vector(new_corrdinate) # 将向量归一化 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 projection(self, b): ub = b.normalized() dot_num = self.dot(ub) new_cordinates = ub.scalarMultiply(dot_num) return new_cordinates # 垂直 def vertical(self,b): projection_v = self.projection(b) new_cordinates = self.minus(projection_v) return new_cordinates v1 = Vector([3.039, 1.879]) v2 = Vector([0.825, 2.036]) print v1.projection(v2) v3 = Vector([-9.88, -3.264, -8.159]) v4 = Vector([-2.155, -9.353, -9.473]) print v3.vertical(v4) v5 = Vector([3.009,-6.172,3.692,-2.51]) v6 = Vector([6.404,-9.144,2.759,8.718]) print v5.projection(v6) print v5.vertical(v6) # 输出 # Vector: (1.0823253260174353, 2.671047713662422) # Vector: (-8.350069611957737, 3.3761108674521076, -1.4336957930745413) # Vector: (1.968617063395022, -2.810904813817002, 0.8481284319030084, 2.679950586926578) # Vector: (1.040382936604978, -3.3610951861829976, 2.8438715680969917, -5.189950586926578) # [Finished in 0.2s]优化后 def component_parallel_to(self, basis):
try:
u = basis.normalized()
weight = self.dot(u)
return u.scalarMultiply(weight)
except Exception as e:
if str(e) == self.CANNOT_NORMALIZE_ZERO_VECTOR_MSG:
raise Exception(self.CANNOT_NORMALIZE_ZERO_VECTOR_MSG)
else:
raise e
def component_orthogonal_to(self, basis):
try:
projection = self.component_parallel_to(basis)
return self.minus(projection)
except Exception, e:
if str(e) == self.NO_UNIQUE_ORTHOGONAL_COMPONENT_MSG:
raise Exception(self.NO_UNIQUE_ORTHOGONAL_COMPONENT_MSG)
else:
raise e
相关文章推荐
- Python 导入自定义模块函数,安装NLTK,导入精确计算
- python中 自定义向量 计算向量乘积, 平行四边形面积,三角形面积
- Python写的创建文件夹自定义函数mkdir()
- 一个仅接受两个输入向量的协方差计算函数
- 洗礼灵魂,修炼python(20)--自定义函数(1)—基础概念
- python 自定义排序函数
- python学习之--自定义函数:
- python在引用其他文件夹下自定义函数
- python multiprocessing进程执行自定义的函数,然后又返回结果
- Vector3函数理解-计算两向量之间的角度
- Python 实现类似PHP的strip_tags函数功能,并且可以自定义设置保留标签(一)
- Python中的自定义函数学习笔记
- 洗礼灵魂,修炼python(22)--自定义函数(3)—函数作用域,闭包
- python-Levenshtein几个计算字串相似度的函数解析
- python计算ecdf代的函数
- python基础之函数(自定义函数)
- python科学计算_numpy_函数库
- Python3自定义函数
- Python实现自定义函数的5种常见形式分析
- CCGeometry(几何学。CCPoint两向量夹角、投影向量、以特定轴+角度旋转。CCSize-大小概念。CCRect-成员是前2.函数:是否包含一个ccp,是否和另一rect相交。宏xMake)