-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
82 lines (61 loc) · 2.07 KB
/
vector.py
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
class Vector:
"""
A helper class. Offers dot product multiplication
as well as standard multiplication.
"""
def __init__(self, v: list):
"""
Constructor for the Vector class.
:param v: the vector values.
"""
self.value = v
def dotProduct(self, other):
"""
Performs a dot product operation
with another vector.
:param other: the other vector.
:return: the resulting vector.
"""
return sum([x * y for x, y in zip(self.value, other.value)])
def __add__(self, other):
"""
Performs a standard vector-vector
addition.
:param other: the other vector.
:return: the resulting vector.
"""
return Vector([x + y for x, y in zip(self.value, other.value)])
def __sub__(self, other):
"""
Allows you to subtract vectors.
:param other: the other vector.
:return: the resulting vector.
"""
return Vector([x - y for x, y in zip(self.value, other.value)])
def __rmul__(self, scalar):
"""
Performs a standard vector-scalar
multiplication. Overrides the
standard multiplication.
:param scalar: the scalar to multiply by.
:return: the resulting vector.
"""
return Vector([x * scalar for x in self.value])
@staticmethod
def isOrthogonal(vectorOne, vectorTwo):
"""
Checks to see if two vectors multiply
to equal zero. If they do, then they're
orthogonal.
:param vectorOne: The first vector.
:param vectorTwo: The second vector.
:return: True if the vectors are orthogonal.
"""
return vectorOne.dotProduct(vectorTwo) == 0
def toString(self):
"""
Allows us to easily view the contents of
the vector.
:return: a string representation of the vector.
"""
return str(self.value)