Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to define class for complex number objects
Complex numbers are mathematical objects with real and imaginary parts, typically written as a + bi. Python allows us to create a custom Complex class to handle complex number operations like addition, subtraction, multiplication, division, and calculating modulus.
Operations Overview
Our Complex class will support the following operations ?
-
Addition:
(a + bi) + (c + di) = (a + c) + (b + d)i -
Subtraction:
(a + bi) - (c + di) = (a - c) + (b - d)i -
Multiplication:
(a + bi) × (c + di) = (ac - bd) + (ad + bc)i -
Division:
(a + bi) ÷ (c + di) = [(ac + bd) + (bc - ad)i] / (c² + d²) -
Modulus:
|a + bi| = ?(a² + b²)
Implementation
Here's a complete implementation of the Complex class with operator overloading ?
from math import sqrt
class Complex:
def __init__(self, real, imag):
self.re = real
self.im = imag
def __add__(self, other):
return Complex(self.re + other.re, self.im + other.im)
def __sub__(self, other):
return Complex(self.re - other.re, self.im - other.im)
def __mul__(self, other):
real_part = self.re * other.re - self.im * other.im
imag_part = self.re * other.im + self.im * other.re
return Complex(real_part, imag_part)
def __truediv__(self, other):
denominator = other.re * other.re + other.im * other.im
real_part = (self.re * other.re + self.im * other.im) / denominator
imag_part = (self.im * other.re - self.re * other.im) / denominator
return Complex(real_part, imag_part)
def mod(self):
return sqrt(self.re * self.re + self.im * self.im)
def __str__(self):
if self.im == 0:
return '%.2f' % self.re
if self.re == 0:
return '%.2fi' % self.im
if self.im < 0:
return '%.2f - %.2fi' % (self.re, -self.im)
else:
return '%.2f + %.2fi' % (self.re, self.im)
# Example usage
comp1 = Complex(2, 3)
comp2 = Complex(5, -2)
print("Addition:", comp1 + comp2)
print("Subtraction:", comp1 - comp2)
print("Multiplication:", comp1 * comp2)
print("Division:", comp1 / comp2)
print("Modulus of comp1:", '%.2f' % comp1.mod())
print("Modulus of comp2:", '%.2f' % comp2.mod())
Addition: 7.00 + 1.00i Subtraction: -3.00 + 5.00i Multiplication: 16.00 + 11.00i Division: 0.14 + 0.66i Modulus of comp1: 3.61 Modulus of comp2: 5.39
Key Features
Operator Overloading: The magic methods __add__, __sub__, __mul__, and __truediv__ allow us to use standard arithmetic operators (+, -, *, /) with our Complex objects.
String Representation: The __str__ method formats the complex number output based on different cases ?
- Pure real numbers:
5.00 - Pure imaginary numbers:
3.00i - Negative imaginary part:
2.00 - 3.00i - Positive imaginary part:
2.00 + 3.00i
Mathematical Verification
Let's verify the multiplication: (2 + 3i) × (5 - 2i) ?
# Manual calculation: (2 + 3i) × (5 - 2i)
# = 2×5 + 2×(-2i) + 3i×5 + 3i×(-2i)
# = 10 - 4i + 15i - 6i²
# = 10 + 11i + 6 (since i² = -1)
# = 16 + 11i
comp1 = Complex(2, 3)
comp2 = Complex(5, -2)
result = comp1 * comp2
print(f"Result: {result}")
print(f"Real part: {result.re}")
print(f"Imaginary part: {result.im}")
Result: 16.00 + 11.00i Real part: 16.0 Imaginary part: 11.0
Conclusion
This Complex class demonstrates operator overloading in Python, allowing natural mathematical operations on complex numbers. The implementation handles all standard complex arithmetic operations and provides proper string formatting for different complex number forms.
