Skip to content

Commit 3433e63

Browse files
committed
Update vector.py
Added dot and cross production, addition and angle between two vectors
1 parent 30001e3 commit 3433e63

File tree

1 file changed

+77
-21
lines changed

1 file changed

+77
-21
lines changed

vector.py

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,97 @@
11
# Class Vector. Stores the data of a 3d vector (Mathematical)
22
# Define suitable attributes and behaviour for the instances.
3-
# Required: scalar and vector product, addition
4-
# Optional: angle between the vectors, distance from origin, Magnitude, ...
3+
# Required: scalar// and vector// product, addition//
4+
# Optional: angle between the vectors //, distance from a point, Magnitude//, ...
55
# ai + bj + ck
66

7-
from math import sqrt
7+
from math import sqrt, acos
88

99

1010
class Vector(object):
11+
# Vector initialization
1112
def __init__(self, /, x=0, y=0, z=0):
12-
self.x = x
13-
self.y = y
14-
self.z = z
15-
self.magnitude = sqrt(x ** 2 + y ** 2 + z ** 2)
13+
self.x = x # Distance along x-axis
14+
self.y = y # Distance along y-axis
15+
self.z = z # Distance along z-axis
16+
self.magnitude = sqrt(x ** 2 + y ** 2 + z ** 2) # Magnitude of vector
17+
18+
# Vector Addition
19+
def add(self, obj):
20+
x = self.x + obj.x
21+
y = self.y + obj.y
22+
z = self.z + obj.z
23+
return Vector(x, y, z)
24+
25+
def __add__(self, obj):
26+
x = self.x + obj.x
27+
y = self.y + obj.y
28+
z = self.z + obj.z
29+
return Vector(x, y, z)
1630

31+
# Scalar Product
1732
def dotProduct(self, obj):
18-
return ()
33+
return self.x*obj.x + self.y*obj.y + self.z*obj.z
1934

20-
# def __mul__(self, obj):
21-
# x = self.y * obj.z - self.z * obj.y
22-
# y = self.z * obj.x - self.x * obj.z
23-
# z = self.x * obj.y - self.y * obj.x
24-
# return Vector(x, y, z)
35+
def __matmul__(self, obj): # Overriding builtin method for @
36+
return self.x*obj.x + self.y*obj.y + self.z*obj.z
2537

26-
# def __matmul__(self, obj):
27-
# return self.x*obj.x + self.y*obj.y + self.z*obj.z
38+
# Vector Product
39+
def crossProduct(self, obj):
40+
x = self.y * obj.z - self.z * obj.y
41+
y = self.z * obj.x - self.x * obj.z
42+
z = self.x * obj.y - self.y * obj.x
43+
return Vector(x, y, z)
2844

45+
def __mul__(self, obj): # Overriding builtin method for *
46+
x = self.y * obj.z - self.z * obj.y
47+
y = self.z * obj.x - self.x * obj.z
48+
z = self.x * obj.y - self.y * obj.x
49+
return Vector(x, y, z)
50+
51+
# String representation of vector
2952
def __repr__(self):
30-
x = f"i" if self.x == 1 else f"-i" if self.x == -1 else f"{self.x}i"
31-
y = f" +j" if self.y == 1 else f" -j" if self.y == -1 else f" {self.y:+}j"
32-
z = f" +k" if self.z == 1 else f" -k" if self.z == -1 else f" {self.z:+}k"
53+
x = (
54+
f"i\u0302" if self.x == 1 else f"-i\u0302" if self.x == -1 else f"{self.x}i\u0302"
55+
)
56+
y = (
57+
f" +j\u0302"
58+
if self.y == 1
59+
else f" -j\u0302"
60+
if self.y == -1
61+
else f" {self.y:+}j\u0302"
62+
)
63+
z = (
64+
f" +k\u0302"
65+
if self.z == 1
66+
else f" -k\u0302"
67+
if self.z == -1
68+
else f" {self.z:+}k\u0302"
69+
)
3370
return x + y + z
3471

72+
# Angle between vectors
73+
def vectorAngle(self, obj):
74+
angle = self @ obj / (self.magnitude * obj.magnitude)
75+
return acos(angle)
3576

77+
print()
3678
aVector = Vector(x=2, y=3, z=-1) # 2i + 3j - k
3779
bVector = Vector(x=1, y=10, z=-3) # i + 10j - 3k
38-
print(aVector)
39-
print(aVector.magnitude)
80+
81+
print(aVector, aVector.magnitude)
82+
print(bVector, bVector.magnitude)
83+
84+
# Dot Product execution
85+
print(f'\n{aVector.dotProduct(bVector)}')
86+
print(aVector @ bVector)
87+
88+
# Cross Product Execution
89+
print(f'\n{aVector.crossProduct(bVector)}')
4090
print(aVector * bVector)
41-
# print(aVector @ bVector)
91+
92+
# Addition
93+
print(f'\n{aVector.add(bVector)}')
94+
print(aVector+bVector)
95+
96+
# Angle between vectors
97+
print(f'\n{aVector.vectorAngle(bVector)} rad')

0 commit comments

Comments
 (0)