Dunder methods (double underscore) in Python are methods which are commonly used for operator overloading. Some examples of dunder methods are __init__ , __repr__ , __add__ , __str__ etc. These methods are useful to modify the behavior of an object.
For example, when ‘+’ operator is used between two numbers, the result obtained is simply the addition of the two numbers whereas when ‘+’ is used between two strings, the result obtained is the concatenation of the two strings.
Commonly used Vector operations:
Consider two vectors vec1 and vec2 with co-ordinates: vec1 = (x1, y1, z1) and vec2 = (x2, y2, z2).
- Magnitude: Magnitude of vec1 =
.
- Addition: For this operation, we need
__add__method to add two Vector objects.
where co-ordinates of vec3 are
.
- Subtraction: For this operation, we need
__sub__method to subtract two Vector objects.
where co-ordinates of vec3 are
.
- Dot Product: For this operation, we need the
__xor__method as we are using ‘^’ symbol to denote the dot product.
^
where co-ordinates of vec3 are
.
- Cross Product: For this operation, we need the
__mul__method as we are using ‘*’ symbol to denote the cross product.
*
where co-ordinates of vec3 are
.
Finally, we also need a __init__ method to initialize the Vector co-ordinates and the __repr__ method to define the represenation of the Vector object. So when we print our Vector object, the output should be something like this. print(Vector(1, -2, 3)) ==> Output: 1i -2j + 3k
Below is the implementation :
# Python3 program to implement 3-D Vectors. from math import sqrt # Definition of Vector class class Vector: # Initialize 3D Coordinates of the Vector def __init__(self, x, y, z): self.x = x self.y = y self.z = z # Method to calculate magnitude of a Vector def magnitude(self): return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2) # Method to add to Vector def __add__(self, V): return Vector(self.x + V.x, self.y + V.y, self.z + V.z) # Method to subtract 2 Vectors def __sub__(self, V): return Vector(self.x - V.x, self.y - V.y, self.z - V.z) # Method to calculate the dot product of two Vectors def __xor__(self, V): return self.x * V.x + self.y * V.y + self.z * V.z # Method to calculate the cross product of 2 Vectors def __mul__(self, V): return Vector(self.y * V.z - self.z * V.y, self.z * V.x - self.x * V.z, self.x * V.y - self.y * V.x) # Method to define the representation of the Vector def __repr__(self): out = str(self.x) + "i " if self.y >= 0: out += "+ " out += str(self.y) + "j " if self.z >= 0: out += "+ " out += str(self.z) + "k" return out if __name__ == "__main__": vec1 = Vector(1, 2, 2) vec2 = Vector(3, 1, 2) # Magnitude of vector1 print("Magnitude of vector1:", vec1.magnitude()) # String representation of vector print("String represenation of vector1: " + str(vec1)) # Addition of two vectors print("Addition of vector1 and vector2: " + str(vec1 + vec2)) # Subtraction of two vectors print("Subtraction of vector1 and vector2: " + str(vec1 - vec2)) # Dot product of two vectors print("Dot Product of vector1 and vector2: " + str(vec1 ^ vec2)) # Cross product of two vectors print("Cross Product of vector1 and vector2: " + str(vec1 * vec2)) |
Magnitude of vector1: 3.0 String represenation of vector1: 1i + 2j + 2k Addition of vector1 and vector2: 4i + 3j + 4k Subtraction of vector1 and vector2: -2i + 1j + 0k Dot Product of vector1 and vector2: 9 Cross Product of vector1 and vector2: 2i + 4j -5k
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Dunder or magic methods in Python
- Customize your Python class with Magic or Dunder methods
- Compute the outer product of two given vectors using NumPy in Python
- Compute the inner product of vectors for 1-D arrays using NumPy in Python
- Compute the inner product of vectors for 1-D arrays using NumPy in Python
- Minimum distance from a point to the line segment using Vectors
- How to compute the cross product of two given vectors using NumPy?
- Calculate inner, outer, and cross products of matrices and vectors using NumPy
- How to calculate dot product of two vectors in Python?
- Implementing web scraping using lxml in Python
- Python | Implementing Dynamic programming using Dictionary
- Implementing Rich getting Richer phenomenon using Barabasi Albert Model in Python
- Implementing Weather Forecast using Facade Design Pattern in Python
- Implementing Newsletter Subscription using Observer Design Pattern in Python
- Implementing News Parser using Template Method Design Pattern in Python
- Implementing Web Crawler using Abstract Factory Design Pattern in Python
- Array of Vectors in C++ STL
- Orthogonal and Orthonormal Vectors in Linear Algebra
- PyQtGraph – Getting Pixel Vectors of Bar Graph
- Implementing DBSCAN algorithm using Sklearn
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

