|
1 | 1 | # Class Vector. Stores the data of a 3d vector (Mathematical)
|
2 | 2 | # 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//, ... |
5 | 5 | # ai + bj + ck
|
6 | 6 |
|
7 |
| -from math import sqrt |
| 7 | +from math import sqrt, acos |
8 | 8 |
|
9 | 9 |
|
10 | 10 | class Vector(object):
|
| 11 | + # Vector initialization |
11 | 12 | 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) |
16 | 30 |
|
| 31 | + # Scalar Product |
17 | 32 | def dotProduct(self, obj):
|
18 |
| - return () |
| 33 | + return self.x*obj.x + self.y*obj.y + self.z*obj.z |
19 | 34 |
|
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 |
25 | 37 |
|
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) |
28 | 44 |
|
| 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 |
29 | 52 | 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 | + ) |
33 | 70 | return x + y + z
|
34 | 71 |
|
| 72 | + # Angle between vectors |
| 73 | + def vectorAngle(self, obj): |
| 74 | + angle = self @ obj / (self.magnitude * obj.magnitude) |
| 75 | + return acos(angle) |
35 | 76 |
|
| 77 | +print() |
36 | 78 | aVector = Vector(x=2, y=3, z=-1) # 2i + 3j - k
|
37 | 79 | 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)}') |
40 | 90 | 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