Python Program to Define a Python Class for Complex Numbers
A complex number is a pair of numbers – a real number and an imaginary number – represented as a + bi, where a and b are real numbers and i is the square root of -1. Complex numbers have several applications in scientific fields such as physics, engineering, and mathematics.
In this article, we’ll define a Python class for complex numbers. We’ll create methods to perform arithmetic operations like addition, subtraction, multiplication, and division on complex numbers. We’ll also create methods to represent complex numbers in different formats such as Cartesian form and polar form.
Understanding Complex Numbers in Python
Python’s built-in support for complex numbers is quite robust. Python allows us to define complex numbers using the keyword j
to represent the imaginary part. Here’s an example:
# Define a complex number
x = 3 + 4j
print(x.real) # Output: 3.0
print(x.imag) # Output: 4.0
In the above example, x
is a complex number with real part 3
and imaginary part 4
. We can access the real and imaginary parts of the complex number using the real
and imag
attributes respectively.
Python also allows us to perform arithmetic operations on complex numbers. Here are some examples:
# Add two complex numbers
a = 3 + 4j
b = 1 + 1j
c = a + b
print(c) # Output: (4+5j)
# Multiply two complex numbers
a = 3 + 4j
b = 1 + 1j
c = a * b
print(c) # Output: (-1+7j)
In the above example, we have added two complex numbers using the +
operator and multiplied two complex numbers using the *
operator.
Creating a Python Class for Complex Numbers
While Python’s built-in support for complex numbers is useful, it can be limiting if we want to create a more specialized implementation with custom methods. In that case, we need to create a custom class for complex numbers.
Here’s an example implementation of the ComplexNumber
class:
class ComplexNumber:
def __init__(self, real_part=0, imaginary_part=0):
self.real_part = real_part
self.imaginary_part = imaginary_part
def __str__(self):
return f'{self.real_part} + {self.imaginary_part}i'
def cartesian(self):
return (self.real_part, self.imaginary_part)
def polar(self):
import math
r = math.sqrt(self.real_part**2 + self.imaginary_part**2)
phi = math.atan2(self.imaginary_part, self.real_part)
return (r, phi)
def __add__(self, other):
return ComplexNumber(self.real_part + other.real_part, self.imaginary_part + other.imaginary_part)
def __sub__(self, other):
return ComplexNumber(self.real_part - other.real_part, self.imaginary_part - other.imaginary_part)
def __mul__(self, other):
a = self.real_part
b = self.imaginary_part
c = other.real_part
d = other.imaginary_part
real = a * c - b * d
imag = b * c + a * d
return ComplexNumber(real, imag)
def __truediv__(self, other):
a = self.real_part
b = self.imaginary_part
c = other.real_part
d = other.imaginary_part
real = (a * c + b * d) / (c ** 2 + d ** 2)
imag = (b * c - a * d) / (c ** 2 + d ** 2)
return ComplexNumber(real, imag)
In the above implementation, we have defined a class ComplexNumber
with several methods.
The __init__
method initializes the class with default real and imaginary values of 0.
The __str__
method defines how to represent the complex number as a string.
The cartesian
method returns the real and imaginary parts of the complex number in Cartesian form.
The polar
method returns the magnitude and phase angle of the complex number in polar form.
The __add__
, __subtract__
, __mul__
, and __truediv__
methods define how to perform arithmetic operations on complex numbers.
Using the ComplexNumber Class
Now that we have defined the ComplexNumber
class, let’s see how we can use it to perform various operations on complex numbers.
# Create two complex numbers
a = ComplexNumber(3, 4)
b = ComplexNumber(1, 1)
# Print the real and imaginary parts of the complex numbers
print(a.cartesian()) # Output: (3, 4)
print(b.cartesian()) # Output: (1, 1)
# Print the magnitude and phase angle of the complex numbers
print(a.polar()) # Output: (5.0, 0.93)
print(b.polar()) # Output: (1.41, 0.79)
# Add two complex numbers
c = a + b
print(c) # Output: 4 + 5i
# Subtract two complex numbers
c = a - b
print(c) # Output: 2 + 3i
# Multiply two complex numbers
c = a * b
print(c) # Output: -1 + 7i
# Divide two complex numbers
c = a / b
print(c) # Output: 3.5 + 0.5i
In the above code, we have created two complex numbers a
and b
. We have then used the methods cartesian
and polar
to represent the complex numbers in Cartesian and polar forms respectively.
We have then performed arithmetic operations like addition, subtraction, multiplication, and division on the complex numbers using the methods defined in the ComplexNumber
class.
Conclusion
In this article, we have defined and implemented a custom Python class for complex numbers. We have created methods to represent complex numbers in different formats such as Cartesian form and polar form. We have also created methods to perform arithmetic operations on complex numbers.
Using this implementation, you can create and manipulate complex numbers using Python with ease. The implementation is extendable and customizable, allowing you to specialize your own operations on complex numbers.