-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
235 lines (205 loc) · 7.71 KB
/
vector.py
File metadata and controls
235 lines (205 loc) · 7.71 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
import math
class Vector(object):
"""
This class gives simple functionalities related to a Vector
params:
coordinates: tuple of vector values
dimension: Stores the number of dimensions of the vector,
equal to the length of coordinates
Example initialization:
v = Vector([1, 2, 3])
v.coordinates => (1, 2, 3)
v.dimension => 3
"""
EMPTY_COORDINATES_MSG = "The Coordinates must be nonempty"
NON_ITERABLE_COORDINATES_MSG = "The Coordinates must be an iterable"
ZERO_VECTOR_ERROR_MSG = "Cannot normalize zero vector"
NO_UNIQUE_PARALLEL_COMPONENT_MSG = "No unique parallel component to zero vector"
NO_UNIQUE_ORTHOGONAL_COMPONENT_MSG = "No unique othogonal component to zero vector"
ANGLE_WITH_ZERO_VECTOR_MG = "Cannot compute an angle with a zero vector."
DIMENSION_MORE_THAN_THREE_MSG = "Cross Product can be done on vectors of dimension 3 or less."
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError(Vector.EMPTY_COORDINATES_MSG)
self.coordinates = tuple(x for x in coordinates)
self.dimension = len(coordinates)
except TypeError:
raise TypeError(Vector.NON_ITERABLE_COORDINATES_MSG)
def __str__(self):
"""
returns vector as a string:
Vector (v1, v2, v3,...)
"""
return "Vector {}".format(self.coordinates)
def __eq__(self, v):
"""
compares 2 vectors for equality;
"""
return self.coordinates == v.coordinates
def __add__(self, v):
"""
Add two vectors element wise. Addition using + operator
"""
if self.dimension != v.dimension:
raise TypeError('Cannot add Vector of dimensions {} and {}'.format(self.dimension, v.dimension))
return Vector([x + y for x, y in zip(self.coordinates, v.coordinates)])
def __sub__(self, v):
"""
Subtract two vectors element wise. Subtraction using - operator
"""
if self.dimension != v.dimension:
raise TypeError('Cannot subtract Vector of dimensions {} and {}'.format(self.dimension, v.dimension))
return Vector([x - y for x, y in zip(self.coordinates, v.coordinates)])
def __mul__(self, n):
"""
Multiply two vectors element wise. Multiplication using vector1 * scalar
or vector1 * vector2 operator.
"""
if isinstance(n, Vector):
return self.dot(n)
else:
return Vector([x * n for x in self.coordinates])
def __rmul__(self, n):
"""
Multiply two vectors element wise. Multiplication using scalar * vector1
or vector2 * vector1 operator.
"""
if isinstance(n, Vector):
return self.dot(n)
else:
return self.__mul__(n)
def magnitude(self):
"""
returns magnitude of a vector
magnitude = sqrt(v1 * v1 + v2 * v2 + v3 * v3 +...)
"""
return math.sqrt(sum([x * x for x in self.coordinates]))
def normalize(self):
"""
Find unit vector in direction of the current vector
"""
magnitude = self.magnitude()
if magnitude == 0:
raise ZeroDivisionError(Vector.ZERO_VECTOR_ERROR_MSG)
return Vector([x / magnitude for x in self.coordinates])
def dot(self, v):
"""
Find dot product of the two vectors.
"""
return sum([x * y for x, y in zip(self.coordinates, v.coordinates)])
def angle_with(self, v, in_degrees=False):
"""
Find angle between 2 vectors in randians as default.
if in_degrees = True, then angle returned is in degrees.
"""
try:
normal_product = round((self.normalize()).dot(v.normalize()), 5)
angle_in_rad = math.acos(normal_product)
if in_degrees:
return angle_in_rad * 180 / math.pi
return angle_in_rad
except Exception as e:
if str(e) == Vector.ZERO_VECTOR_ERROR_MSG:
raise Exception(Vector.ANGLE_WITH_ZERO_VECTOR_MG)
else:
raise e
def project(self, b):
"""
Project current vector on the basis vector b.
Projection = magnitude of current vector * unit vector in direction of b.
"""
try:
return self.dot(b.normalize()) * b.normalize()
except Exception as e:
if str(e) == Vector.ZERO_VECTOR_ERROR_MSG:
raise Exception(Vector.NO_UNIQUE_PARALLEL_COMPONENT_MSG)
else:
raise e
def project_orth(self, b):
"""
Project current vector on a vector perpendicular to the basis vector b.
current vector = projection on b + projection on orthogonal of b
=> projection on orthogonal of b = current vector - projection on b.
"""
try:
parallel_project = self.project(b)
return self - parallel_project
except Exception as e:
if str(e) == Vector.NO_UNIQUE_PARALLEL_COMPONENT_MSG:
raise Exception(Vector.NO_UNIQUE_ORTHOGONAL_COMPONENT_MSG)
else:
raise e
def __make_3d__(self):
"""
returns 3D vector (first 3 dimensions) out of current vector
if dimensions of current vector = 1
Vector (v1, 0, 0)
if dimensions of current vector = 2
Vector (v1, v2, 0)
if dimensions of current vector >= 3
Vector (v1, v2, v3)
"""
if self.dimension > 3:
return Vector([self.coordinates[i] for i in range(3)])
li = []
for i in range(3):
if i > self.dimension:
li.append(0)
else:
li.append(self.coordinates[i])
return Vector(li)
def cross(self, v):
"""
Find cross product of the first 3D of the current vector with the
vector v.
"""
if self.dimension > 3 or v.dimension > 3:
raise ValueError(Vector.DIMENSION_MORE_THAN_THREE_MSG)
else:
print(self)
print(v)
v1 = self.__make_3d__()
v2 = v.__make_3d__()
x_1, y_1, z_1 = v1.coordinates
x_2, y_2, z_2 = v2.coordinates
li = []
li.append(y_1 * z_2 - y_2 * z_1)
li.append(-(x_1 * z_2 - z_1 * x_2))
li.append(x_1 * y_2 - y_1 * x_2)
return Vector(li)
def is_parallel_to(self, v):
"""
Find whether the current vector is parallel to the given vector.
"""
return (self.is_zero() or
v.is_zero() or
self.angle_with(v) == 0 or
self.angle_with(v) == math.pi)
def is_orthogonal_to(self, v, tolerance = 1e-10):
"""
Find whether the current vector is perpendicular to the given vector.
"""
return abs(self.dot(v)) < tolerance
def is_zero(self, tolerance=1e-10):
"""
Find whether the current vector is a zero vector.
"""
return self.magnitude() < tolerance
if __name__ == '__main__':
my_vector = Vector([1, 2, 3])
print(my_vector)
vector1 = Vector([1, 2, 3])
vector2 = Vector([-1, 2, 3])
print('Compare vectors: ')
print(my_vector == vector1)
print(my_vector == vector2)
print()
print(my_vector, '+', vector1, '=')
print(my_vector + vector1)
print()
print(my_vector, '-', vector1, '=')
print(my_vector - vector1)
print('Magnitude of ', my_vector, '=', my_vector.magnitude())
print('Unit vector of ', my_vector, '=', my_vector.normalize())
print('Check for zero vector:', Vector([0, 0, 0]).is_zero())