Python Division Of Complex Numbers Without Using Built In Types and Operators
Asked Answered
L

2

3

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 operators (__add__, __sub__, __mul__, __abs__, __str_ which allows to perform basic operations. But now I'm stuck with overwriting the __div__ operator.

Allowed to use:

I'm using float to represent the imaginary part of the number and float to represent the rel part.

What I have already tried:

  • I looked up how to perform a division of complex numbers (handwritten)
  • I have done an example calculation
  • Thought about how to implement it programatically without any good result

Explanation of how to divide complex numbers:

http://www.mathwarehouse.com/algebra/complex-number/divide/how-to-divide-complex-numbers.php

My implementation of multiply:

 def __mul__(self, other):
        real = (self.re * other.re - self.im * other.im)
        imag = (self.re * other.im + other.re * self.im)
        return ComplexNumber(real, imag)
Louvar answered 14/12, 2016 at 14:14 Comment(11)
Are you not allowed to use int ?Tamishatamma
@PatrickHaugh I will update the question type of the real part is float the type of the imaginary part is intLouvar
Okay, that's all you need anyways. Where in the division algorithm did you run into trouble?Tamishatamma
Can you show us your mul or add so we can copy your style.Consummation
@DanielLee updated postLouvar
Why is the imaginary part restricted to int?Antenatal
@PatrickHaugh I tried to implement like it is shown in the link of the post. I know how to calculate the numerator and the denominator (step 1: Determine the conjugate of the denominator) but in the end I'm stuck again with a divisionLouvar
@Antenatal your right it should be floatLouvar
The key insight is that the denominator will always be real, after you multiply by the conjugate over itself (I recommend that you prove this to yourself.). Then you can change (x+yi)/z to (x/z) + ((y/z)i)Tamishatamma
@PatrickHaugh Ty, now I got a glue, I need to calculate the denominator first and than I can simply divide to the real and imaginary partLouvar
Yep. Don't forget to also multiply the conjugate of the denominator with the numerator.Tamishatamma
V
4

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__
Vegetarianism answered 14/12, 2016 at 14:20 Comment(0)
L
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.

Louvar answered 14/12, 2016 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.