Python Division Of Complex Numbers Without Using Built In Types And Operators
I have to implement a class called ComplexNumbers which is representing a complex number and I'm not allowed to use the built in types for that. I already have overwritten the oper
Solution 1:
I think this should suffice:
def conjugate(self):
# return a - ib
def __truediv__(self, other):
other_into_conjugate = other * other.conjugate()
new_numerator = self * other.conjugate()
# other_into_conjugate will be a real number
# say, x. If a and b are the new real and imaginary
# parts of the new_numerator, return (a/x) + i(b/x)
__floordiv__ = __truediv__
Solution 2:
Thanks to the tips of @PatrickHaugh I was able to solve the problem. Here is my solution:
def __div__(self, other):
conjugation = ComplexNumber(other.re, -other.im)
denominatorRes = other * conjugation
# denominator has only real part
denominator = denominatorRes.re
nominator = self * conjugation
return ComplexNumber(nominator.re/denominator, nominator.im/denominator)
Calculating the conjugation and than the denominator which has no imaginary part.
Post a Comment for "Python Division Of Complex Numbers Without Using Built In Types And Operators"