0% found this document useful (0 votes)
24 views4 pages

Ex-8 Arithmatic Complex

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Ex-8 Arithmatic Complex

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EX NO 08

DATE ARITHMETIC COMPLEX CLASS

30/08/24

AIM:
To implement a class "arithmetic complex" to execute the following
functions:
i. To find the conjugation of a complex number.
ii. To find the modulus value (|z|).
iii. To find the phase value in polar format (radius, θ).
iv. Addition and subtraction of complex numbers.

ALGORITHM:

 Define the math library.


 Create a class complex_arithmetic and pass parameters (self, re, im).
 Define the conjugate function to return the conjugate of the complex
number.
 Define the modulus function to return the modulus value by using the
formula modulus=(re2+im2)\text{modulus} = \sqrt{(\text{re}^2 +
\text{im}^2)}modulus=(re2+im2).
 Define the phase function to return the phase in polar form using the
formula θ=atan2(im, re)\theta = \text{atan2(im, re)}θ=atan2(im, re).
 Define functions for addition and subtraction of two complex numbers.
 Test the class by creating complex number objects and applying the
functions.

PROGRAM:

import math

class Complex:
def __init__(self, real, imag):
[Link] = real
[Link] = imag

def conjugate(self):
return complex([Link], -[Link])

def polarform(self):
r = ([Link]**2 + [Link]**2)**0.5
theta = math.atan2([Link] , [Link])
return (r, theta)

def __add__(self, other):


return Complex([Link] + [Link], [Link] + [Link])

def __sub__(self, other):


return Complex([Link] - [Link], [Link] - [Link])

def __mul__(self, other):


return Complex([Link] * [Link] - [Link] * [Link],
[Link] * [Link] + [Link] * [Link])

def __truediv__(self, other):


conjugate = [Link]()
numerator = self * conjugate
denominator = other * conjugate
return Complex([Link] / [Link], [Link] /
[Link])

def __str__(self):
if [Link] >= 0:
return f"{[Link]} + {[Link]}i"
else:
return f"{[Link]} - {-[Link]}i"

C1=Complex(3,6)
C2=Complex(2,-5)
print([Link]())
print([Link]())
print([Link]())
print(C1+C2)
print(C1-C2)
print(C1*C2)
print(C1/C2)
print(C1)
print(C2)

OUTPUT :

[Link]() = (3-6j)
[Link]() = ( 2 + 5i )
[Link]() = (6.708203932499369, 1.1071487177940904)
C1 + C2 = ( 5 + 1i )
C1 - C2 = ( 1 + 11i )
C1 * C2 = ( 36 - 3i )
C1 / C2 = -0.8275862068965517 + 0.9310344827586207i
C1 = ( 3 + 6i )
C2 = ( 2 - 5i )

RESULT:

The arithmetic complex class using python program has been implemented
successfully

You might also like