Skip to content

Commit Live PR #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_create_class/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_create_class/__pycache__/build.cpython-36.pyc
Binary file not shown.
96 changes: 93 additions & 3 deletions q01_create_class/build.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,105 @@
# %load q01_create_class/build.py
import pandas as pd
import numpy as np
import math

"write your solution here"
'write your solution here'


class complex_number:
"""The complex number class.
'''The complex number class.

Attributes:
attr1 (x): Real part of complex number.
attr2 (y): Imaginary part of complex number.

"""
'''
def __init__(self, real, imag):
self.real = real
self.imag = imag

def __add__(self, other):
return complex_number(self.real + other.real, self.imag + other.imag)

def __sub__(self, other):
return complex_number(self.real - other.real, self.imag - other.imag)

def __mul__(self, other):

if(type(self) is type(other)):
real_part = (self.real * other.real) - (self.imag * other.imag)
imag_part = (self.imag * other.real) + (self.real * other.imag)
else:
real_part = self.real * other
imag_part = self.imag * other

return complex_number(real_part, imag_part)

def __str__(self):
#print('Test: ', self.imag)
if(self.imag > 0):
if(self.real == 0):
return 'i{0}'.format(self.imag)
else:
return '{0} + i{1}'.format(self.real, self.imag)

elif (self.imag == 0):
if(self.real == 0):
return ' '
else:
return '{0}'.format(self.real)

else:
if(self.real == 0):
return '-i{0}'.format(abs(self.imag))
else:
return '{0} - i{1}'.format(self.real, abs(self.imag))

def conjugate(self):
if(self.imag > 0):
return complex_number(self.real, (self.imag * -1))
else:
return complex_number(self.real, abs(self.imag))

def __truedivWithoutConjug__(self, other):
numerator_real = (self.real * other.real) + (self.imag * other.imag)
numerator_imag = (self.imag * other.real) - (self.real * other.imag)
denominator = (other.real**2) + (other.imag**2)
return (numerator_real/denominator, numerator_imag/denominator)

def __truediv__(self, other):
numerator = self * other.conjugate()
denominator = (other.real ** 2) + (other.imag **2)
#print('Denom is: ', denominator)
return (numerator.real/denominator, numerator.imag/denominator)
#return complex_number(numerator_real/denominator, numerator_imag/denominator)

def abs(self):
return math.sqrt((self.real ** 2) + (self.imag ** 2))

def argument(self):
tan_res = math.atan2(self.imag,self.real)
return (tan_res * (180/math.pi))

a = complex_number(4,4)
print('First complex number is: ', str(a))
b = complex_number(4,-3)
print('Second complex number is: ', str(b))
add_res = a + b
print('Addition of complex numbers is: ', str(add_res))
sub_res = a - b
print('Subtraction of complex numbers is: ', str(sub_res))
mult_res = a * b
print('Multiplication of complex numbers is: ', str(mult_res))
conjug_res = b.conjugate()
print('Conjugation of complex number is: ', str(conjug_res))
div_res = a.__truediv__(b)
print('Division of complex numbers is: ', str(div_res))
abs_res = a.abs()
print('Absolute of complex number is: ', str(abs_res))
arg_res = a.argument()
print('Argument of complex number is: ', str(arg_res))
div_result = a.__truedivWithoutConjug__(b)
print('Division of complex numbers is: ', str(div_result))


Binary file modified q01_create_class/tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.