-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.py
More file actions
237 lines (183 loc) · 5.76 KB
/
Copy pathvector.py
File metadata and controls
237 lines (183 loc) · 5.76 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import math
from typing import Union
class Vector3:
"""
Vector3 represents a 3-dimensional vector.
Standard vector operations are provided: cross product, dot product, norm.
All operators a defined for element-wise and scalar operations.
"""
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def normalize(self) -> "Vector3":
"""
Normalize a vector
Returns
-------
Vector3:
A new vector, which is the input vector normalized.
"""
norm = self.norm()
return Vector3(self.x / norm, self.y / norm, self.z / norm)
def norm(self) -> float:
"""
Vector's norm.
Returns
-------
float:
the norm of the vector (i.e. a scalar)
"""
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
def cross(self, other: "Vector3") -> "Vector3":
"""
Vector cross product.
Parameters
----------
other: Vector3 of scalar
Returns
-------
Vector3:
The cross product of the two vectors.
"""
# aka cross product
nx = self.y * other.z - self.z * other.y
ny = self.z * other.x - self.x * other.z
nz = self.x * other.y - self.y * other.x
return Vector3(nx, ny, nz)
def dot(self, other: "Vector3") -> float:
"""
Vector's dot product.
Parameters
----------
other: Vector3
Returns
-------
float:
the dot product of the two vectors.
"""
# aka dot product, inner product, scalar product
return self.x * other.x + self.y * other.y + self.z * other.z
def __mul__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Multiplication, element-wise and scalar
Parameters
----------
other: Vector3 of scalar
Returns
-------
a Vector3
"""
if isinstance(other, Vector3):
# Element-wise multiplication
return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)
else:
return Vector3(other * self.x, other * self.y, other * self.z)
def __rmul__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Multiplication, element-wise and scalar
Parameters
----------
other: Vector3 of scalar
Returns
-------
a Vector3
"""
if isinstance(other, Vector3):
return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)
else:
return Vector3(other * self.x, other * self.y, other * self.z)
def __add__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Addition, element wise and scalar.
Parameters
----------
other: a Vector3 or a scalar
Returns
-------
a Vector3
Examples
--------
>>> Vector3(1,2,3) + 1
Vector3D(2, 3, 4)
>>> Vector3(1,2,3) + Vector3(-1,0,2)
Vector3D(0, 2, 5)
"""
if isinstance(other, Vector3):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
else:
return Vector3(self.x + other, self.y + other, self.z + other)
def __radd__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Addition, element wise and scalar.
Parameters
----------
other: a Vector3 or a scalar
Returns
-------
a Vector3
Examples
--------
>>> 1+ Vector3(1,2,3)
Vector3D(2, 3, 4)
>>> Vector3(1,2,3) + Vector3(-1,0,2)
Vector3D(0, 2, 5)
"""
if isinstance(other, Vector3):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
else:
return Vector3(self.x + other, self.y + other, self.z + other)
def __sub__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Subtraction, element wise and scalar.
Parameters
----------
other: a Vector3 or a scalar
Returns
-------
a Vector3
"""
if isinstance(other, Vector3):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
else:
return Vector3(self.x - other, self.y - other, self.z - other)
def __rsub__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Subtraction, element wise and scalar.
Parameters
----------
other: a Vector3 or a scalar
Returns
-------
a Vector3
"""
if isinstance(other, Vector3):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
else:
return Vector3(other - self.x, other - self.y, other - self.z)
def __truediv__(self, other: Union["Vector3", float]) -> "Vector3":
"""
Division, element wise and scalar.
Parameters
----------
other: a Vector3 or a scalar
Returns
-------
a Vector3
"""
if isinstance(other, Vector3):
return Vector3(self.x / other.x, self.y / other.y, self.z / other.z)
else:
return Vector3(self.x / other, self.y / other, self.z / other)
def __eq__(self, other) -> bool:
if not isinstance(other, Vector3):
return False
return other.x == self.x and other.y == self.y and self.z == other.z
def __repr__(self):
return f"Vector3({self.x}, {self.y}, {self.z})"
def as_tuple(self):
return self.x, self.y, self.z
def __iter__(self):
yield self.x
yield self.y
yield self.z