-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmmplex_calculator.py
161 lines (106 loc) · 3.65 KB
/
cmmplex_calculator.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import math
#Code starts here
class complex_numbers(object):
"""The complex number class.
Attributes:
attr1 (x): Real part of complex number.
attr2 (y): Imaginary part of complex number.
"""
def __init__(self, x = 0.00, y = 0.00):
self.real = x
self.imag = y
def __repr__(self):
if self.real == 0.0 and self.imag == 0.0:
return "0.00"
if self.real == 0:
return "%.2fi" % self.imag
if self.imag == 0:
return "%.2f" % self.real
return "%.2f %s %.2fi" % (self.real, "+" if self.imag >= 0 else "-", abs(self.imag))
def __add__(self,other):
"""Overloaded '+' operator.
Args:
param1 (complex_numbers): Other point.
Returns:
complex_numbers: The evaluated complex number.
"""
x = self.real + other.real
y = self.imag + other.imag
return complex_numbers(x,y)
def __sub__(self, other):
"""Overloaded '-' operator.
Args:
param1 (complex_numbers): Other point.
Returns:
complex_numbers: The evaluated complex number.
"""
x = self.real - other.real
y = self.imag - other.imag
return complex_numbers(x,y)
def __mul__(self, other):
"""Overloaded '*' operator.
Args:
param1 (complex_numbers): Other point.
Returns:
complex_numbers: The evaluated complex number.
"""
x = self.real * other.real - self.imag * other.imag
y = self.real * other.imag + self.imag * other.real
return complex_numbers(x,y)
def __truediv__(self, other):
"""Overloaded '/' operator.
Args:
param1 (complex_numbers): Other point.
Returns:
complex_numbers: The evaluated complex number.
"""
if other.real*other.real + other.imag*other.imag != 0:
return complex_numbers((self.real*other.real + self.imag*other.imag) / (other.real*other.real + other.imag*other.imag),
(self.imag*other.real - self.real*other.imag) / (other.real*other.real + other.imag*other.imag))
else:
return "Denominator is zero."
def absolute(self):
"""Returns Absolute value.
Returns:
Integer: The evaluated abs value.
"""
return math.sqrt( self.real * self.real + self.imag * self.imag)
def argument(self):
"""Returns arguments in degree.
Returns:
Float: The evaluated arg in degrees.
"""
return math.degrees(math.atan(self.imag / self.real))
def conjugate(self):
"""Returns conjugate of the complex number.
Returns:
complex_numbers: The conjugate complex number.
"""
x = self.real
y = self.imag * -1
return complex_numbers(x,y)
comp_1 = complex_numbers(3,5)
print('Complex Number 1: ',comp_1)
comp_2 = complex_numbers(4,4)
print('Complex Number 2: ',comp_2)
comp_sum= (comp_1+comp_2)
print('Sum of Complex Number 1 & 2: ', comp_sum)
comp_diff= (comp_1-comp_2)
print('Difference of Complex Number 1 & 2: ',comp_diff)
comp_prod= (comp_1*comp_2)
print('Product of Complex Number 1 & 2: ',comp_prod)
comp_quot= (comp_1/comp_2)
print('Quotient of Complex Number 1 & 2: ',comp_quot)
comp_abs=comp_1.absolute()
print('Absolute value of Complex Number 1: ',comp_abs)
comp_conj= comp_1.conjugate()
print('Conjugate value of Complex Number 1: ',comp_conj)
comp_arg=comp_1.argument()
print('Argument value of Complex Number 1: ',comp_arg)
#Code ends here
# In[ ]: