Skip to content

Commit 43fe8a3

Browse files
committed
Adding solve sub-package+Unary operator(-)+Updating __repr__
1 parent e0de155 commit 43fe8a3

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

cmpx/Complex.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ def __ifloordiv__(self, other):
223223
return Complex(self.re, self.im, self.restore)
224224
except (ZeroDivisionError, ValueError) as err:
225225
self.print_err(err)
226+
# Operator overloading 17: - (Unary operator)
227+
def __neg__(self):
228+
return Complex(-self.re, -self.im, self.restore)
226229
## Helper functions
227230
# Module function to calculate the modulus of a complex number
228231
def mod(self):
@@ -237,11 +240,13 @@ def print_err(self, err):
237240
# Representation function for representing a complex number
238241
def __repr__(self):
239242
if(self.re != 0 and self.im > 0):
240-
output = str(self.re) + ' + ' + str(self.im) + 'j'
243+
output = str(self.re) + ' + ' + str(self.im) + 'j' if(self.im != 1) else str(self.re) + ' + ' + 'j'
241244
if(self.re != 0 and self.im < 0):
242-
output = str(self.re) + ' - ' + str(-self.im) + 'j'
245+
output = str(self.re) + ' - ' + str(-self.im) + 'j' if(self.im != -1) else str(self.re) + ' - ' + 'j'
243246
if(self.re != 0 and self.im == 0):
244247
output = str(self.re)
245-
if(self.re == 0 and self.im != 0):
246-
output = str(self.im) + 'j'
248+
if(self.re == 0 and self.im > 0):
249+
output = str(self.im) + 'j' if(self.im != 1) else 'j'
250+
if(self.re == 0 and self.im < 0):
251+
output = str(self.im) + 'j' if(self.im != -1) else '-j'
247252
return output

cmpx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__author__ = 'Omar Belghaouti'
55
__maintainer__ = 'Omar Belghaouti'
66
__email__ = 'bel_omar18@yahoo.com'
7-
__version__ = '0.7'
7+
__version__ = '0.7.2'
88
__license__ = 'MIT'
99
__all__ = [
1010
'Complex'

cmpx/solve/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
from cmpx.Complex import Complex
3+
from .delta import delta

cmpx/solve/delta.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
from math import sqrt
3+
4+
def delta(a, b, c):
5+
delta = b**2 - 4 * a * c
6+
if(delta < 0):
7+
solution = [Complex(-b/2*a, -sqrt(delta)/2*a),
8+
Complex(-b/2*a, sqrt(delta)/2*a)]
9+
elif(delta == 0):
10+
solution = Complex(-b/2*a)
11+
else:
12+
solution = [Complex((-b - sqrt(delta))/2*a),
13+
Complex((-b + sqrt(delta))/2*a)]
14+
return solution
15+

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup_args = dict(
77
name='cmpx',
8-
version='0.7',
8+
version='0.7.2',
99
description='A package for different operations on complex numbers',
1010
long_description_content_type='text/markdown',
1111
long_description=README,

0 commit comments

Comments
 (0)