-
Notifications
You must be signed in to change notification settings - Fork 3
/
complexnumber.py
41 lines (32 loc) · 1.52 KB
/
complexnumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __str__(self):
if self.imaginary < 0:
return f"{self.real} - {abs(self.imaginary)}i"
else:
return f"{self.real} + {self.imaginary}i"
def __add__(self, other):
real_part = self.real + other.real
imaginary_part = self.imaginary + other.imaginary
return ComplexNumber(real_part, imaginary_part)
def __sub__(self, other):
real_part = self.real - other.real
imaginary_part = self.imaginary - other.imaginary
return ComplexNumber(real_part, imaginary_part)
def __mul__(self, other):
real_part = self.real * other.real - self.imaginary * other.imaginary
imaginary_part = self.real * other.imaginary + self.imaginary * other.real
return ComplexNumber(real_part, imaginary_part)
def __truediv__(self, other):
if other.real == 0 and other.imaginary == 0:
raise ValueError("Division by zero is not allowed.")
denominator = other.real ** 2 + other.imaginary ** 2
real_part = (self.real * other.real + self.imaginary * other.imaginary) / denominator
imaginary_part = (self.imaginary * other.real - self.real * other.imaginary) / denominator
return ComplexNumber(real_part, imaginary_part)
def conjugate(self):
return ComplexNumber(self.real, -self.imaginary)
def modulus(self):
return (self.real ** 2 + self.imaginary ** 2) ** 0.5