Skip to content

Commit 4ac248d

Browse files
authored
Added vector3
1 parent 7e53e96 commit 4ac248d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Vector:
2+
"""
3+
Constructor
4+
5+
self: a reference to the object we are creating
6+
vals: a list of integers which are the contents of our vector
7+
"""
8+
def __init__(self, vals):
9+
self.vals = vals
10+
# print("Assigned values ", vals, " to vector.")
11+
12+
"""
13+
String Function
14+
15+
Converts the object to a string in readable format for programmers
16+
"""
17+
def __str__(self):
18+
return str(self.vals)
19+
20+
def __pow__(self, power):
21+
return Vector([i**power for i in self.vals])
22+
23+
# Calculates Euclidean norm
24+
def norm(self):
25+
return sum((self**2).vals)**0.5
26+
27+
#__lt__: implements the less than operator (<)
28+
def __lt__(self, other):
29+
return self.norm() < other.norm()
30+
31+
#__gt__: implements the greater than operator (>)
32+
def __gt__(self, other):
33+
return self.norm() > other.norm()
34+
35+
#__le__: implements the less than equal to operator (<=)
36+
def __le__(self, other):
37+
return self.norm() <= other.norm()
38+
39+
#__ge__: implements the greater than equal to operator (>=)
40+
def __ge__(self, other):
41+
return self.norm() >= other.norm()
42+
43+
#__eq__: implements the equals operator (==)
44+
def __eq__(self, other):
45+
return self.norm() == other.norm()
46+
47+
#__ne__:implements the not equals operator (!=)
48+
def __ne__(self, other):
49+
return self.norm() != other.norm()
50+
51+
vec = Vector([2, 3, 2])
52+
vec2 = Vector([3, 4, 5])
53+
print(vec < vec2) # True
54+
print(vec > vec2) # False
55+
56+
print(vec <= vec2) # True
57+
print(vec >= vec2) # False
58+
print(vec <= vec) # True
59+
print(vec >= vec) # True
60+
61+
print(vec == vec2) # False
62+
print(vec == vec) # True
63+
64+
print(vec != vec2) # True
65+
print(vec != vec) # False

0 commit comments

Comments
 (0)