-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
114 lines (97 loc) · 4.17 KB
/
vector.py
File metadata and controls
114 lines (97 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vector):
return Vector(self.x + other.x, self.y + other.y)
raise TypeError("Unsupported operand type(s) for +")
def __sub__(self, other):
if isinstance(other, Vector):
return Vector(self.x - other.x, self.y - other.y)
raise TypeError("Unsupported operand type(s) for -")
def __mul__(self, other):
# Support scalar multiplication
if isinstance(other, (int, float)):
return Vector(self.x * other, self.y * other)
# Dot product if multiplying with another vector
elif isinstance(other, Vector):
return self.x * other.x + self.y * other.y
raise TypeError("Unsupported operand type(s) for *")
def __floordiv__(self, other):
if isinstance(other, (int, float)):
return Vector(self.x // other, self.y // other)
raise TypeError("Unsupported operand type(s) for //")
def __truediv__(self, other):
if isinstance(other, (int, float)):
return Vector(self.x / other, self.y / other)
raise TypeError("Unsupported operand type(s) for /")
def __mod__(self, other):
if isinstance(other, (int, float)):
return Vector(self.x % other, self.y % other)
raise TypeError("Unsupported operand type(s) for %")
def __divmod__(self, other):
if isinstance(other, (int, float)):
return (
Vector(self.x // other, self.y // other),
Vector(self.x % other, self.y % other),
)
raise TypeError("Unsupported operand type(s) for divmod()")
def __pow__(self, power):
if isinstance(power, (int, float)):
return Vector(self.x**power, self.y**power)
raise TypeError("Unsupported operand type(s) for **")
def __lshift__(self, other):
if isinstance(other, int):
return Vector(self.x << other, self.y << other)
raise TypeError("Unsupported operand type(s) for <<")
def __rshift__(self, other):
if isinstance(other, int):
return Vector(self.x >> other, self.y >> other)
raise TypeError("Unsupported operand type(s) for >>")
def __and__(self, other):
if isinstance(other, Vector):
return Vector(self.x & other.x, self.y & other.y)
raise TypeError("Unsupported operand type(s) for &")
def __or__(self, other):
if isinstance(other, Vector):
return Vector(self.x | other.x, self.y | other.y)
raise TypeError("Unsupported operand type(s) for |")
def __xor__(self, other):
if isinstance(other, Vector):
return Vector(self.x ^ other.x, self.y ^ other.y)
raise TypeError("Unsupported operand type(s) for ^")
def __repr__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(3, 4)
v2 = Vector(1, 2)
print("v1 + v2 =", v1 + v2) # v1 + v2 = Vector(4, 6)
print("v1 - v2 =", v1 - v2) # v1 - v2 = Vector(2, 2)
print("v1 * 3 =", v1 * 3) # v1 * 3 = Vector(9, 12)
print("v1 * v2 (dot product) =", v1 * v2) # v1 * v2 (dot product) = 11
print("v1 // 2 =", v1 // 2) # v1 // 2 = Vector(1, 2)
print("v1 / 2 =", v1 / 2) # v1 / 2 = Vector(1.5, 2.0)
print("v1 % 2 =", v1 % 2) # v1 % 2 = Vector(1, 0)
print("divmod(v1,2) =", divmod(v1, 2)) # divmod(v1,2) = (Vector(1, 2), Vector(1, 0))
print("v1 ** 2 =", v1**2) # v1 ** 2 = Vector(9, 16)
print("v1 << 1 =", v1 << 1) # v1 << 1 = Vector(6, 8)
print("v1 >> 1 =", v1 >> 1) # v1 >> 1 = Vector(1, 2)
print("v1 & v2 =", v1 & v2) # v1 & v2 = Vector(1, 0)
print("v1 | v2 =", v1 | v2) # v1 | v2 = Vector(3, 6)
print("v1 ^ v2 =", v1 ^ v2) # v1 ^ v2 = Vector(2, 6)
"""
v1 + v2 = Vector(4, 6)
v1 - v2 = Vector(2, 2)
v1 * 3 = Vector(9, 12)
v1 * v2 (dot product) = 11
v1 // 2 = Vector(1, 2)
v1 / 2 = Vector(1.5, 2.0)
v1 % 2 = Vector(1, 0)
divmod(v1,2) = (Vector(1, 2), Vector(1, 0))
v1 ** 2 = Vector(9, 16)
v1 << 1 = Vector(6, 8)
v1 >> 1 = Vector(1, 2)
v1 & v2 = Vector(1, 0)
v1 | v2 = Vector(3, 6)
v1 ^ v2 = Vector(2, 6)
"""