This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomplex_numbers.py
More file actions
50 lines (34 loc) · 1.52 KB
/
Copy pathcomplex_numbers.py
File metadata and controls
50 lines (34 loc) · 1.52 KB
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
40
41
42
43
44
45
46
47
48
49
50
import math
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __eq__(self, other):
return self.real == other.real and self.imaginary == other.imaginary
def __add__(self, other):
real = self.real + other.real
imaginary = self.imaginary + other.imaginary
return ComplexNumber(real, imaginary)
def __mul__(self, other):
real = self.real * other.real - self.imaginary * other.imaginary
imaginary = self.imaginary * other.real + self.real * other.imaginary
return ComplexNumber(real, imaginary)
def __sub__(self, other):
real = self.real - other.real
imaginary = self.imaginary - other.imaginary
return ComplexNumber(real, imaginary)
def __truediv__(self, other):
real = (self.real * other.real + self.imaginary *
other.imaginary) / (abs(other) * abs(other))
imaginary = (self.imaginary * other.real - self.real *
other.imaginary) / (abs(other) * abs(other))
return ComplexNumber(real, imaginary)
def __abs__(self):
square_sum = self.real * self.real + self.imaginary * self.imaginary
return math.sqrt(square_sum)
def conjugate(self):
return ComplexNumber(self.real, -self.imaginary)
def exp(self):
real = math.e ** self.real * math.cos(self.imaginary)
imaginary = math.e ** self.real * math.sin(self.imaginary)
return ComplexNumber(real, imaginary)