Python program for multiplication and division of complex number
Last Updated :
21 Mar, 2023
Given two complex numbers. The task is to multiply and divide them. Multiplication of complex number: In Python complex numbers can be multiplied using * operator
Examples:
Input: 2+3i, 4+5i
Output: Multiplication is : (-7+22j)
Input: 2+3i, 1+2i
Output: Multiplication is : (-4+7j)
Python3
def mulComplex( z1, z2):
return z1 * z2
z1 = complex ( 2 , 3 )
z2 = complex ( 4 , 5 )
print ( "Multiplication is :" , mulComplex(z1,z2))
|
Output:
Multiplication is : (-7+22j)
Time Complexity: O(1)
Auxiliary Space: O(1)
Division of complex number: In Python, complex numbers can be divided using the / operator.
Examples:
Input: 2+3i, 4+5i
Output: Division is : (0.5609756097560976+0.0487804878048781j)
Input: 2+3i, 1+2i
Output: Division is :(1.6-0.2j)
Python3
def divComplex( z1, z2):
return z1 / z2
z1 = complex ( 2 , 3 )
z2 = complex ( 4 , 5 )
print ( "Division is :" , (divComplex(z1, z2))
|
Output:
Division is : (0.5609756097560976+0.0487804878048781j)
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach:
Define a class ComplexNumber to represent complex numbers with real and imaginary parts.
Implement the __init__ method to initialize the real and imaginary parts of the complex number.
Implement the __repr__ method to provide a string representation of the complex number in the format a + bi.
Implement the __mul__ method to define the multiplication of two complex numbers. This is done using the formula: (a + bi) * (c + di) = (ac – bd) + (ad + bc)i.
Implement the __truediv__ method to define the division of two complex numbers. This is done using the formula: (a + bi) / (c + di) = [(ac + bd) / (c^2 + d^2)] + [(bc – ad) / (c^2 + d^2)]i.
Create two instances of the ComplexNumber class to represent the input complex numbers.
Use the * operator to multiply the two complex numbers and print the result.
Use the / operator to divide the two complex numbers and print the result.
Python3
class ComplexNumber:
def __init__( self , real, imag):
self .real = real
self .imag = imag
def __repr__( self ):
return "{}{:+}i" . format ( self .real, self .imag)
def __mul__( self , other):
real = self .real * other.real - self .imag * other.imag
imag = self .real * other.imag + self .imag * other.real
return ComplexNumber(real, imag)
def __truediv__( self , other):
denom = other.real * * 2 + other.imag * * 2
real = ( self .real * other.real + self .imag * other.imag) / denom
imag = ( self .imag * other.real - self .real * other.imag) / denom
return ComplexNumber(real, imag)
a = ComplexNumber( 2 , 3 )
b = ComplexNumber( 4 , 5 )
print ( "Multiplication is :" , a * b)
a = ComplexNumber( 2 , 3 )
b = ComplexNumber( 1 , 2 )
print ( "Multiplication is :" , a * b)
print ( "Division is :" , a / b)
|
Output
Multiplication is : -7+22i
Multiplication is : -4+7i
Division is : 1.6-0.2i
The time complexity of the __mul__ and __truediv__ methods is O(1), which means that they take a constant amount of time to run, regardless of the input size.
The auxiliary space is also O(1),
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...