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 representation 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
from math import sqrt
class Vector:
def __init__( self , x, y, z):
self .x = x
self .y = y
self .z = z
def magnitude( self ):
return sqrt( self .x * * 2 + self .y * * 2 + self .z * * 2 )
def __add__( self , V):
return Vector( self .x + V.x, self .y + V.y, self .z + V.z)
def __sub__( self , V):
return Vector( self .x - V.x, self .y - V.y, self .z - V.z)
def __xor__( self , V):
return self .x * V.x + self .y * V.y + self .z * V.z
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)
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 )
print ( "Magnitude of vector1:" , vec1.magnitude())
print ( "String representation of vector1: " + str (vec1))
print ( "Addition of vector1 and vector2: " + str (vec1 + vec2))
print ( "Subtraction of vector1 and vector2: " + str (vec1 - vec2))
print ( "Dot Product of vector1 and vector2: " + str (vec1 ^ vec2))
print ( "Cross Product of vector1 and vector2: " + str (vec1 * vec2))
|
Output
Magnitude of vector1: 3.0
String representation 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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!