Guest User

Untitled

a guest
Jun 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import math
  2. from typing import Tuple
  3.  
  4. VectorType = Tuple[float]
  5.  
  6. class Vector:
  7.  
  8. def __init__(self, *point: VectorType):
  9. self._vector = tuple(point)
  10.  
  11. def scale(self, scalar: float) -> VectorType:
  12. self *= scalar
  13. return self
  14.  
  15. @property
  16. def vector(self) -> tuple:
  17. return self._vector
  18.  
  19. @property
  20. def dimension(self) -> int:
  21. return len(self.vector)
  22.  
  23. @property
  24. def magnitude(self) -> float:
  25. return sum(map(lambda x: x**2, self.vector))**0.5
  26.  
  27. @staticmethod
  28. def angle_between(vector1, vector2) -> float:
  29. return math.acos(sum(x * y for x, y in zip(vector1.vector, vector2.vector)) / (vector1.magnitude * vector2.magnitude))
  30.  
  31. def _operator(self, oper, other):
  32. if isinstance(other, self.__class__) and self.dimension == other.dimension:
  33. return other.__class__(*[oper(x, y) for x, y in zip(self.vector, other.vector)])
  34. return NotImplemented
  35.  
  36. def _ioperator(self, oper, other):
  37. if isinstance(other, self.__class__) and self.dimension == other.dimension:
  38. self.vector = tuple(oper(x, y) for x, y in zip(self.vector, other.vector))
  39. else:
  40. return NotImplemented
  41. return self
  42.  
  43. def __repr__(self):
  44. return f'{self.__class__.__name__}{self.vector}'
  45.  
  46. def __bool__(self):
  47. return bool(self.dimension)
  48.  
  49. def __getitem__(self, index):
  50. return self.vector[index]
  51.  
  52. def __iter__(self):
  53. for point in self.vector:
  54. yield point
  55.  
  56. def __add__(self, other):
  57. return self._operator(lambda x, y: x + y, other)
  58.  
  59. def __radd__(self, other):
  60. return self._operator(lambda x, y: y + x, other)
  61.  
  62. def __iadd__(self, other):
  63. return self._ioperator(lambda x, y: y + x, other)
  64.  
  65. def __sub__(self, other):
  66. return self._operator(lambda x, y: x - y, other)
  67.  
  68. def __rsub__(self, other):
  69. return self._operator(lambda x, y: y - x, other)
  70.  
  71. def __isub__(self, other):
  72. return self._ioperator(lambda x, y: y - x, other)
  73.  
  74. def __mul__(self, other):
  75. if isinstance(other, (int, float)):
  76. return self.__class__(*[x * other for x in self.vector])
  77. elif isinstance(other, self.__class__) and self.dimension == other.dimension:
  78. return round(self.magnitude * other.magnitude * math.cos(Vector.angle_between(self, other)), 14)
  79. return NotImplemented
  80.  
  81. def __rmul__(self, other):
  82. if isinstance(other, (int, float)) or isinstance(other, self.__class__) and self.dimension == other.dimension:
  83. return self * other
  84. return NotImplemented
  85.  
  86. def __imul__(self, other):
  87. if isinstance(other, (int, float)):
  88. self.vector = tuple(x * other for x in self.vector)
  89. else:
  90. return NotImplemented
  91. return self
  92.  
  93.  
  94. class Vector2D(Vector):
  95.  
  96. @property
  97. def direction(self) -> float:
  98. return math.atan2(self.vector[1], self.vector[0])
  99.  
  100.  
  101. if __name__ == '__main__':
  102. v = Vector(-1, 2)
  103. v2 = Vector2D(3, 4)
  104. print(v, v2, v*v2)
Add Comment
Please, Sign In to add comment