Skip to content

Commit f5a82b7

Browse files
committed
First commit
First operations implemented
1 parent 5714aab commit f5a82b7

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

cmpx.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Sep 10 17:36:25 2019
4+
5+
@author: Omar Belghaouti
6+
"""
7+
from math import sqrt
8+
# Complex class for complex number manipulations
9+
class Complex():
10+
# Constructor
11+
def __init__(self, re=0, im=0):
12+
self.re = re
13+
self.im = im
14+
# Operator overloading 1 : +
15+
def __add__(self, other):
16+
return Complex(self.re + other.re, self.im + other.im)
17+
# Operator overloading 2 : -
18+
def __sub__(self, other):
19+
return Complex(self.re - other.re, self.im - other.im)
20+
# Operator overloading 3 : *
21+
def __mul__(self, other):
22+
return Complex(self.re * other.re - self.im * other.im, self.re * other.im + self.im * other.re)
23+
# Operator overloading 4 : /
24+
def __truediv__(self, other):
25+
try:
26+
den = other * other.con()
27+
num = self * other.con()
28+
return Complex(num.re / den.re, num.im / den.re)
29+
except ZeroDivisionError as err:
30+
print('Error: {}'.format(err))
31+
# Operator overloading 5 : //
32+
def __floordiv__(self, other):
33+
try:
34+
den = other * other.con()
35+
num = self * other.con()
36+
return Complex(num.re // den.re, num.im // den.re)
37+
except ZeroDivisionError as err:
38+
print('Error: {}'.format(err))
39+
def __gt__(self, other):
40+
return self.mod() > other.mod()
41+
# Operator overloading 7 : >=
42+
def __ge__(self, other):
43+
return self.mod() >= other.mod()
44+
# Operator overloading 8: <
45+
def __lt__(self, other):
46+
return not self >= other
47+
# Operator overloading 9: <=
48+
def __le__(self, other):
49+
return not self > other
50+
# Operator overloading 10: ==
51+
def __eq__(self, other):
52+
return (self.re == other.re) and (self.im == other.im)
53+
# Operator overloading 11: !=
54+
def __ne__(self, other):
55+
return not self == other
56+
# Operator overloading 12: +=
57+
def __iadd__(self, other):
58+
self.re += other.re
59+
self.im += other.im
60+
# Operator overloading 13: -=
61+
def __isub__(self, other):
62+
self.re -= other.re
63+
self.im -= other.im
64+
# Operator overloading 14: *=
65+
def __imul__(self, other):
66+
self.re = self.re * other.re - self.im * other.im
67+
self.im = self.re * other.im + self.im * other.re
68+
# Operator overloading 15: /=
69+
def __idiv__(self, other):
70+
try:
71+
den = other * Complex(other.re, - other.im)
72+
num = self * Complex(other.re, - other.im)
73+
self.re = num.re / den.re
74+
self.im = num.im / den.re
75+
except ZeroDivisionError as err:
76+
print('Error: {}'.format(err))
77+
# Operator overloading 16: //=
78+
def __ifloordiv__(self, other):
79+
try:
80+
den = other * Complex(other.re, - other.im)
81+
num = self * Complex(other.re, - other.im)
82+
self.re = num.re // den.re
83+
self.im = num.im // den.re
84+
except ZeroDivisionError as err:
85+
print('Error: {}'.format(err))
86+
## Helper functions
87+
# Module function to calculate the modulus of a complex number
88+
def mod(self):
89+
return sqrt(self.re**2 + self.im**2)
90+
# Conjugated of a complex number
91+
def con(self):
92+
return Complex(self.re, - self.im)
93+
# String function
94+
def __str__(self):
95+
output = str(self.re) + ' + ' + str(self.im) + 'j' if(self.im != 0) else str(self.re)
96+
output = str(self.re) + ' - ' + str(-self.im) + 'j' if (self.im < 0) else output
97+
return output
98+
# Callable module
99+
def __call__(self):
100+
return self

main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Sep 10 17:37:40 2019
4+
5+
@author: Omar Belghaouti
6+
"""
7+
from cmpx import Complex
8+
9+
def main():
10+
# Instanciation 1
11+
number = Complex() # --> Real = 0, Imaginary = 0
12+
print(number)
13+
# Instanctiation 2
14+
number = Complex(42) # --> Real = 42, Imaginary = 0
15+
print(number)
16+
# Instanciation 3
17+
number = Complex(12, -3.2) # --> Real = 12, Imaginary = -1
18+
print(number)
19+
# Instanciation 4
20+
number = Complex(im=13.2, re=5)
21+
print(number)
22+
23+
# Basic operations
24+
num1 = Complex(re=3, im=-2.5)
25+
num2 = Complex(re=4.2, im=13.2)
26+
# Summation
27+
print('({}) + ({}) = {}'.format(num1, num2, num1 + num2))
28+
# Substraction
29+
print('({}) - ({}) = {}'.format(num1, num2, num1 - num2))
30+
# Multiplication
31+
print('({}) * ({}) = {}'.format(num1, num2, num1 * num2))
32+
# Division
33+
print('({}) / ({}) = {}'.format(num1, num2, num1 / num2))
34+
# Floor division
35+
print('({}) // ({}) = {}'.format(num1, num2, num1 // num2))
36+
# Congugated of a complex number
37+
print('con({}) = {}'.format(num1, num1.con()))
38+
# Module of a complex number
39+
print('mod({}) = {}'.format(num1, num1.mod()))
40+
41+
# Basic comparisons
42+
num1 = Complex(im=3.1, re=-5)
43+
num2 = Complex(im=-1.5, re=0.6)
44+
# Greater than
45+
if(num1 > num2): print('({}) > ({})'.format(num1, num2))
46+
# Greater or equal
47+
if(num1 >= num2): print('({}) >= ({})'.format(num1, num2))
48+
# Less than
49+
if(num1 < num2): print('({}) < ({})'.format(num1, num2))
50+
# Less or equal
51+
if(num1 <= num2): print('({}) <= ({})'.format(num1, num2))
52+
# Equal
53+
if(num1 == num2): print('({}) == ({})'.format(num1, num2))
54+
# Not equal
55+
if(num1 != num2): print('({}) != ({})'.format(num1, num2))
56+
57+
if __name__ == '__main__': main()

0 commit comments

Comments
 (0)